View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.requestParameterProvider;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.io.IOException;
26  import java.io.ObjectStreamException;
27  import java.io.Serializable;
28  
29  import javax.faces.context.FacesContext;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * @author Thomas Obereder
36   * @version 27.04.2006 22:42:32
37   */
38  public class RequestParameterProviderManager implements Serializable
39  {
40      private static final Log LOG = LogFactory.getLog(RequestParameterProviderManager.class);
41  
42      private static final String PAGE_PARAMETER_SEP = "?";
43      private static final String PARAMETER_SEP = "&";
44      private static final String PARAMETER_PROVIDER_MANAGER_KEY = "org.apache.myfaces.RequestParameterProviderManager";
45  
46      private List providers;
47  
48  
49      private RequestParameterProviderManager()
50      {
51          providers = new ArrayList();
52      }
53  
54      public static RequestParameterProviderManager getInstance()
55      {
56          FacesContext context = FacesContext.getCurrentInstance();
57          if (context == null)
58          {
59              throw new IllegalStateException("no faces context available!");
60          }
61          return getInstance(context);
62      }
63  
64      public static RequestParameterProviderManager getInstance(FacesContext context)
65      {
66          RequestParameterProviderManager manager =
67                  (RequestParameterProviderManager) context.getExternalContext().getSessionMap().get(PARAMETER_PROVIDER_MANAGER_KEY);
68  
69          if (manager == null)
70          {
71              manager = new RequestParameterProviderManager();
72              context.getExternalContext().getSessionMap().put(PARAMETER_PROVIDER_MANAGER_KEY, manager);
73          }
74  
75          return manager;
76      }
77  
78  
79      /**
80       * Register the given provider.
81       * @param provider the provider to register.
82       */
83  
84      public void register(RequestParameterProvider provider)
85      {
86          if(provider == null)
87              LOG.warn("RequestParameterProvider is null -> no registration!");
88          else
89              this.providers.add(provider);
90      }
91  
92  
93      /**
94       * Encode all fields of all providers, and attach the name-value pairs to url.
95       * @param url the URL to which the fields should be attached.
96       * @return the url after attaching all fields.
97       */
98  
99      public String encodeAndAttachParameters(String url)
100     {
101             if (!isFilterCalled())
102             {
103                 throw new IllegalStateException("RequestParameterServletFilter not called. Please configure the filter " + RequestParameterServletFilter.class.getName() + " in your web.xml to cover your faces requests.");
104             }
105 
106         StringBuffer sb = new StringBuffer();
107         if(url == null)
108         {
109             LOG.warn("URL is null -> empty string is returned.");
110             return sb.toString();
111         }
112 
113         int nuofParams = -1;
114         String firstSeparator = url.indexOf(PAGE_PARAMETER_SEP) == -1 ? PAGE_PARAMETER_SEP : PARAMETER_SEP;
115         sb.append(url);
116         for (Iterator it = providers.iterator(); it.hasNext();)
117         {
118             RequestParameterProvider provider = (RequestParameterProvider) it.next();
119             String[] fields = provider.getFields();
120             if (fields == null)
121             {
122                 continue;
123             }
124             for (int i = 0; i < fields.length; i++)
125             {
126                 nuofParams++;
127 
128                 sb.append(nuofParams == 0 ? firstSeparator : PARAMETER_SEP);
129                 sb.append(fields[i]);
130                 sb.append("=");
131                 sb.append(provider.getFieldValue(fields[i]));
132             }
133         }
134         return sb.toString();
135     }
136 
137 
138     /**
139      * Check if there are any providers registered.
140      * @return true, if the list is not null and not empty.
141      */
142     public boolean hasProviders()
143     {
144         return this.providers != null && !this.providers.isEmpty();
145     }
146 
147     public boolean isFilterCalled()
148     {
149         FacesContext context = FacesContext.getCurrentInstance();
150         if (context == null)
151         {
152             throw new IllegalStateException("no faces context available!");
153         }
154 
155         return Boolean.TRUE.equals(context.getExternalContext().getRequestMap().get(RequestParameterServletFilter.REQUEST_PARAM_FILTER_CALLED));
156     }
157 
158     private void writeObject(java.io.ObjectOutputStream out) throws IOException
159     {
160         // the conversation manager is not (yet) serializable, we just implement it
161         // to make it work with distributed sessions
162     }
163 
164     private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
165     {
166         // nothing written, so nothing to read
167     }
168 
169     private Object readResolve() throws ObjectStreamException
170     {
171         // do not return a real object, that way on first request a new manager will be created
172         return null;
173     }
174 }