View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17  
18   */
19   
20  package org.apache.pluto.portalImpl.core;
21  
22  import java.net.URLEncoder;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.StringTokenizer;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.apache.pluto.om.window.PortletWindow;
33  import org.apache.pluto.portalImpl.aggregation.Fragment;
34  import org.apache.pluto.portalImpl.services.config.Config;
35  
36  public class PortalURL {
37  
38      private static final String insecureServlet;
39      private static final String secureServlet;
40      static {
41          insecureServlet = Config.getParameters().getString("servlet.insecure");
42          secureServlet = Config.getParameters().getString("servlet.secure");
43      }
44  
45      /***
46       * Creates and URL pointing to the home of the portal
47       *
48       * @param request   the servlet request
49       * @return the portal URL
50       */
51      public String getBasePortalURL(HttpServletRequest request) {
52          return getBasePortalURL(PortalEnvironment.getPortalEnvironment(request));
53      }
54  
55      /***
56       * Creates and URL pointing to the home of the portal
57       *
58       * @param env     the portal environment
59       * @return the portal URL
60       */
61      public String getBasePortalURL(PortalEnvironment env)
62      {
63          StringBuffer result = new StringBuffer(256);
64          /*
65                  result.append(secure?hostNameHTTPS:hostNameHTTP);
66          */
67          result.append(env.getRequest().getContextPath());
68          result.append(env.getRequest().getServletPath());
69  
70          return result.toString();
71      }
72  
73      private List startGlobalNavigation = new ArrayList();
74      private List startLocalNavigation = new ArrayList();
75      private HashMap encodedStartControlParameter = new HashMap();
76      private HashMap startStateLessControlParameter = new HashMap();
77      private boolean analyzed = false;
78  
79      private PortalEnvironment environment;
80  
81      /***
82       * Creates and URL pointing to the home of the portal
83       *
84       * @param env     the portal environment
85       */
86      public PortalURL(PortalEnvironment env)
87      {
88          environment = env;
89      }
90  
91      /***
92       * Creates and URL pointing to the home of the portal
93       *
94       * @param request     the servlet request
95       */
96      public PortalURL(HttpServletRequest request)
97      {
98          this(PortalEnvironment.getPortalEnvironment(request));
99      }
100 
101     /***
102      * Creates and URL pointing to the given fragment of the portal
103      *
104      * @param request   the servlet request
105      * @param pointTo the fragment to point to
106      */
107     public PortalURL(HttpServletRequest request, Fragment pointTo)
108     {
109         this(request);
110         pointTo.createURL(this);
111     }
112 
113     /***
114      * Creates and URL pointing to the given fragment of the portal
115      *
116      * @param env     the portal environment
117      * @param pointTo the fragment to point to
118      */
119     public PortalURL(PortalEnvironment env, Fragment pointTo)
120     {
121         this(env);
122         pointTo.createURL(this);
123     }
124 
125     /***
126      * Adds a navigational information pointing to a portal part, e.g. PageGroups
127      * or Pages
128      *
129      * @param nav    the string pointing to a portal part
130      */
131     public void addGlobalNavigation(String nav)
132     {
133         startGlobalNavigation.add(nav);
134     }
135 
136     /***
137      * Sets the local navigation. Because the local navigation is always handled
138      * by the Browser, therefore the local navigation cleared.
139      */
140     public void setLocalNavigation()
141     {
142         startLocalNavigation = new ArrayList();
143     }
144 
145     /***
146      * Adds a navigational information pointing to a local portal part inside
147      * of a global portal part, for example, a portlet on a page.
148      *
149      * @param nav    the string pointing to a local portal part
150      */
151     public void addLocalNavigation(String nav)
152     {
153         startLocalNavigation.add(nav);
154     }
155 
156     /***
157      * Returns true if the given string is part of the global navigation of this URL
158      *
159      * @param nav    the string to check
160      * @return true, if the string is part of the navigation
161      */
162     public boolean isPartOfGlobalNavigation(String nav)
163     {
164         return startGlobalNavigation.contains(nav);
165     }
166 
167     /***
168      * Returns true if the given string is part of the local navigation of this URL
169      *
170      * @param nav    the string to check
171      * @return true, if the string is part of the navigation
172      */
173     public boolean isPartOfLocalNavigation(String nav)
174     {
175         return startLocalNavigation.contains(nav);
176     }
177     
178     private String urlEncode(String value) {
179         value = URLEncoder.encode(value);
180         // java.net.URLEncoder encodes space (' ') as a plus sign ('+'),
181         // instead of %20 thus it will not be decoded properly by tomcat when the
182         // request is parsed. Therefore replace all '+' by '%20'.
183         // If there would have been any plus signs in the original string, they would
184         // have been encoded by URLEncoder.encode()
185         // control = control.replace("+", "%20");//only works with JDK 1.5
186         value = value.replaceAll("//+", "%20");
187         return value;
188     }    
189 
190     public String getGlobalNavigationAsString()
191     {
192         StringBuffer result = new StringBuffer(200);
193         Iterator iterator = startGlobalNavigation.iterator();
194         if (iterator.hasNext()) {
195             result.append((String)iterator.next());
196             while (iterator.hasNext()) {
197                 result.append("/");
198                 String st = (String)iterator.next();
199                 result.append(st);
200             }
201         }
202         return result.toString();
203     }
204 
205     public String getLocalNavigationAsString()
206     {
207         StringBuffer result = new StringBuffer(30);
208         Iterator iterator = startLocalNavigation.iterator();
209         if (iterator.hasNext()) {
210             result.append((String)iterator.next());
211             while (iterator.hasNext()) {
212                 result.append(".");
213                 result.append((String)iterator.next());
214             }
215         }
216         return result.toString();
217     }
218 
219     public String getControlParameterAsString(PortalControlParameter controlParam) {
220         Map encodedStateFullParams = encodedStartControlParameter;
221         if (controlParam != null) {
222             encodedStateFullParams = controlParam.getEncodedStateFullControlParameter();
223         }
224 
225         StringBuffer result = new StringBuffer(100);
226         Iterator iterator = encodedStateFullParams.keySet().iterator();
227         while (iterator.hasNext()) {
228             result.append("/");
229             String encodedName = (String)iterator.next();
230             String encodedValue = (String)encodedStateFullParams.get(encodedName);
231             if(encodedValue != null) {
232                 // appends the prefix (currently "_") in front of the encoded parameter name
233                 result.append(PortalControlParameter.encodeParameterName(encodedName));
234                 result.append("/");
235                 result.append(urlEncode(encodedValue));
236             }
237         }
238 
239         return result.toString();
240     }
241 
242     public String getRequestParameterAsString(PortalControlParameter controlParam)
243     {
244         if (controlParam!=null) {
245             Map requestParams = controlParam.getRequestParameter();
246 
247             StringBuffer result = new StringBuffer(100);
248             Iterator iterator = requestParams.keySet().iterator();
249             boolean hasNext = iterator.hasNext();
250             if (hasNext) {
251                 result.append("?");
252             }
253 
254             while (hasNext) {
255                 String name = (String)iterator.next();
256                 Object value = requestParams.get(name);
257                 String[] values = value instanceof String ? new String[] {(String)value} : (String[])value;
258 
259                 result.append(urlEncode(name));
260                 result.append("=");
261                 result.append(urlEncode(values[0]));
262                 for (int i = 1; i < values.length; i++) {
263                     result.append("&");
264                     result.append(urlEncode(name));
265                     result.append("=");
266                     result.append(urlEncode(values[i]));
267                 }
268 
269                 hasNext=iterator.hasNext();
270                 if (hasNext) {
271                     result.append("&");
272                 }
273             }
274 
275             return result.toString();
276         }
277         return "";
278     }
279 
280     public String toString()
281     {
282         return toString(null,null);
283     }
284 
285     public String toString(PortalControlParameter controlParam,Boolean p_secure)
286     {
287 
288         StringBuffer urlBase = new StringBuffer(256);
289 
290         boolean secure=false;
291         if (p_secure!=null) {
292             secure=p_secure.booleanValue();
293         } else {
294             secure=environment.getRequest().isSecure();
295         }
296         urlBase.append(environment.getRequest().getContextPath());
297         urlBase.append(secure ? secureServlet : insecureServlet);
298 
299         String url = urlBase.toString();
300         String global = getGlobalNavigationAsString();
301         if (global.length() > 0) {
302             url += "/";
303             url += global;
304         }
305 
306         String control = getControlParameterAsString(controlParam);
307         if (control.length() > 0) {
308             url += control;
309         }
310 
311         String requestParam = getRequestParameterAsString(controlParam);
312         if (requestParam.length() > 0) {
313             url += requestParam;
314         }
315 
316         String local = getLocalNavigationAsString();
317         if (local.length() > 0) {
318             url += "#";
319             url += local;
320         }
321 
322         return environment.getResponse().encodeURL(url);
323     }
324 
325     Map getClonedEncodedStateFullControlParameter()
326     {
327         analyzeRequestInformation();
328         return(Map)encodedStartControlParameter.clone();
329     }
330 
331     Map getClonedStateLessControlParameter()
332     {
333         analyzeRequestInformation();
334         return(Map)startStateLessControlParameter.clone();
335     }
336 
337     void analyzeControlInformation(PortalControlParameter control)
338     {
339         encodedStartControlParameter = (HashMap)control.getEncodedStateFullControlParameter();
340         startStateLessControlParameter = (HashMap)control.getStateLessControlParameter();
341     }
342 
343     void analyzeRequestInformation() {
344         if (analyzed) return;
345 
346         startGlobalNavigation = new ArrayList();
347         startLocalNavigation = new ArrayList();
348         encodedStartControlParameter = new HashMap();
349         startStateLessControlParameter = new HashMap();
350 
351         // check the complete pathInfo for
352         // * navigational information
353         // * control information
354 
355         if (environment.getRequest().getPathInfo() != null)
356         {
357             String pathInfo = environment.getRequest().getPathInfo();
358             StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
359 
360             int mode = 0; // 0=navigation, 1=control information
361             String encodedName = null;
362             while (tokenizer.hasMoreTokens()) {
363                 String token = tokenizer.nextToken();
364                 if (PortalControlParameter.isControlParameter(token)) {
365                     mode = 1;
366                     encodedName = token;
367                 } else if (mode==0) {
368                     startGlobalNavigation.add(token);
369                 } else if (mode==1) {
370                     if ((PortalControlParameter.isStateFullParameter(encodedName))) {
371                     	// cut the prefix before saving the parameter name
372                     	encodedName = PortalControlParameter.decodeParameterName(encodedName);
373                         encodedStartControlParameter.put(encodedName, token);
374                     } else {
375                         startStateLessControlParameter.put(PortalControlParameter.decodeParameterName(encodedName),
376                                                            PortalControlParameter.decodeParameterValue(encodedName,token));
377                     }
378                     mode = 0;
379                 }
380             }
381         }
382         analyzed = true;
383 
384     }
385 
386     public void setRenderParameter(PortletWindow portletWindow,
387                                    String name,
388                                    String[] values)
389     {
390         encodedStartControlParameter.put(PortalControlParameter.encodeRenderParamName(portletWindow, name),
391                                   PortalControlParameter.encodeRenderParamValues(values));
392 
393     }
394 
395     public void clearRenderParameters(PortletWindow portletWindow) {
396         String prefix = PortalControlParameter.getRenderParamKey(portletWindow);
397         Iterator keyIterator = encodedStartControlParameter.keySet().iterator();
398         while (keyIterator.hasNext()) {
399             String name = (String)keyIterator.next();
400             if (name.startsWith(prefix)) {
401                 keyIterator.remove();
402             }
403         }
404     }
405 
406 }