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      {
53          return getBasePortalURL(PortalEnvironment.getPortalEnvironment(request));
54      }
55  
56      /***
57       * Creates and URL pointing to the home of the portal
58       *
59       * @param env     the portal environment
60       * @return the portal URL
61       */
62      public String getBasePortalURL(PortalEnvironment env)
63      {
64          StringBuffer result = new StringBuffer(256);
65          /*
66                  result.append(secure?hostNameHTTPS:hostNameHTTP);
67          */
68          result.append(env.getRequest().getContextPath());
69          result.append(env.getRequest().getServletPath());
70  
71          return result.toString();
72      }
73  
74      private List startGlobalNavigation = new ArrayList();
75      private List startLocalNavigation = new ArrayList();
76      private HashMap startControlParameter = new HashMap();
77      private HashMap startStateLessControlParameter = new HashMap();
78      private boolean analyzed = false;
79  
80      private PortalEnvironment environment;
81  
82      /***
83       * Creates and URL pointing to the home of the portal
84       *
85       * @param env     the portal environment
86       */
87      public PortalURL(PortalEnvironment env)
88      {
89          environment = env;
90      }
91  
92      /***
93       * Creates and URL pointing to the home of the portal
94       *
95       * @param request     the servlet request
96       */
97      public PortalURL(HttpServletRequest request)
98      {
99          this(PortalEnvironment.getPortalEnvironment(request));
100     }
101 
102     /***
103      * Creates and URL pointing to the given fragment of the portal
104      *
105      * @param request   the servlet request
106      * @param pointTo the fragment to point to
107      */
108     public PortalURL(HttpServletRequest request, Fragment pointTo)
109     {
110         this(request);
111         pointTo.createURL(this);
112     }
113 
114     /***
115      * Creates and URL pointing to the given fragment of the portal
116      *
117      * @param env     the portal environment
118      * @param pointTo the fragment to point to
119      */
120     public PortalURL(PortalEnvironment env, Fragment pointTo)
121     {
122         this(env);
123         pointTo.createURL(this);
124     }
125 
126     /***
127      * Adds a navigational information pointing to a portal part, e.g. PageGroups
128      * or Pages
129      *
130      * @param nav    the string pointing to a portal part
131      */
132     public void addGlobalNavigation(String nav)
133     {
134         startGlobalNavigation.add(nav);
135     }
136 
137     /***
138      * Sets the local navigation. Because the local navigation is always handled
139      * by the Browser, therefore the local navigation cleared.
140      */
141     public void setLocalNavigation()
142     {
143         startLocalNavigation = new ArrayList();
144     }
145 
146     /***
147      * Adds a navigational information pointing to a local portal part inside
148      * of a global portal part, for example, a portlet on a page.
149      *
150      * @param nav    the string pointing to a local portal part
151      */
152     public void addLocalNavigation(String nav)
153     {
154         startLocalNavigation.add(nav);
155     }
156 
157     /***
158      * Returns true if the given string is part of the global navigation of this URL
159      *
160      * @param nav    the string to check
161      * @return true, if the string is part of the navigation
162      */
163     public boolean isPartOfGlobalNavigation(String nav)
164     {
165         return startGlobalNavigation.contains(nav);
166     }
167 
168     /***
169      * Returns true if the given string is part of the local navigation of this URL
170      *
171      * @param nav    the string to check
172      * @return true, if the string is part of the navigation
173      */
174     public boolean isPartOfLocalNavigation(String nav)
175     {
176         return startLocalNavigation.contains(nav);
177     }
178 
179     public String getGlobalNavigationAsString()
180     {
181         StringBuffer result = new StringBuffer(200);
182         Iterator iterator = startGlobalNavigation.iterator();
183         if (iterator.hasNext()) {
184             result.append((String)iterator.next());
185             while (iterator.hasNext()) {
186                 result.append("/");
187                 String st = (String)iterator.next();
188                 result.append(st);
189             }
190         }
191         return result.toString();
192     }
193 
194     public String getLocalNavigationAsString()
195     {
196         StringBuffer result = new StringBuffer(30);
197         Iterator iterator = startLocalNavigation.iterator();
198         if (iterator.hasNext()) {
199             result.append((String)iterator.next());
200             while (iterator.hasNext()) {
201                 result.append(".");
202                 result.append((String)iterator.next());
203             }
204         }
205         return result.toString();
206     }
207 
208     public String getControlParameterAsString(PortalControlParameter controlParam)
209     {
210         Map stateFullParams = startControlParameter;
211         if (controlParam != null) {
212             stateFullParams = controlParam.getStateFullControlParameter();
213         }
214 
215         StringBuffer result = new StringBuffer(100);
216         Iterator iterator = stateFullParams.keySet().iterator();
217         while (iterator.hasNext()) {
218             if (iterator.hasNext()) result.append("/");
219             String name = (String)iterator.next();
220             String value = (String)stateFullParams.get(name);
221             if(value!=null) {
222                 result.append(PortalControlParameter.encodeParameter(name));
223                 result.append("/");
224                 result.append(value);
225             }
226         }
227 
228         return result.toString();
229     }
230 
231     public String getRequestParameterAsString(PortalControlParameter controlParam)
232     {
233         if (controlParam!=null) {
234             Map requestParams = controlParam.getRequestParameter();
235 
236             StringBuffer result = new StringBuffer(100);
237             Iterator iterator = requestParams.keySet().iterator();
238             boolean hasNext = iterator.hasNext();
239             if (hasNext) {
240                 result.append("?");
241             }
242 
243             while (hasNext) {
244                 String name = (String)iterator.next();
245                 Object value = requestParams.get(name);
246                 String[] values = value instanceof String ? new String[] {(String)value} : (String[])value;
247 
248                 result.append(name);
249                 result.append("=");
250                 result.append(values[0]);
251                 for (int i = 1; i < values.length; i++) {
252                     result.append("&");
253                     result.append(name);
254                     result.append("=");
255                     result.append(values[i]);
256                 }
257 
258                 hasNext=iterator.hasNext();
259                 if (hasNext) {
260                     result.append("&");
261                 }
262             }
263 
264             return result.toString();
265         }
266         return "";
267     }
268 
269     public String toString()
270     {
271         return toString(null,null);
272     }
273 
274     public String toString(PortalControlParameter controlParam,Boolean p_secure)
275     {
276 
277         StringBuffer urlBase = new StringBuffer(256);
278 
279         boolean secure=false;
280         if (p_secure!=null) {
281             secure=p_secure.booleanValue();
282         } else {
283             secure=environment.getRequest().isSecure();
284         }
285         urlBase.append(environment.getRequest().getContextPath());
286         urlBase.append(secure ? secureServlet : insecureServlet);
287 
288         String url = urlBase.toString();
289         String global = getGlobalNavigationAsString();
290         if (global.length() > 0) {
291             url += "/";
292             url += global;
293         }
294 
295 
296         String control = getControlParameterAsString(controlParam);
297         if (control.length() > 0) {
298            	// parameter string should be x-www-form-urlencoded here! See jira 119
299       		// parameter string will be decoded by the tomcat container
300       		control = URLEncoder.encode(control);
301       		// java.net.URLEncoder encodes space (' ') as a plus sign ('+'),
302       		// instead of %20 thus it will not be decoded properly by tomcat when the
303       		// request is parsed. Therefore replace all '+' by '%20'.
304       		// If there would have been any plus signs in the original string, they would
305       		// have been encoded by URLEncoder.encode()
306 //      		control = control.replace("+", "%20");//only works with JDK 1.5
307       		control = control.replaceAll("//+", "%20");
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 getClonedStateFullControlParameter()
326     {
327         analyzeRequestInformation();
328         return(Map)startControlParameter.clone();
329     }
330 
331     Map getClonedStateLessControlParameter()
332     {
333         analyzeRequestInformation();
334         return(Map)startStateLessControlParameter.clone();
335     }
336 
337     void analyzeControlInformation(PortalControlParameter control)
338     {
339         startControlParameter = (HashMap)control.getStateFullControlParameter();
340         startStateLessControlParameter = (HashMap)control.getStateLessControlParameter();
341     }
342 
343     void analyzeRequestInformation()
344     {
345         if (analyzed) return;
346 
347         startGlobalNavigation = new ArrayList();
348         startLocalNavigation = new ArrayList();
349         startControlParameter = new HashMap();
350         startStateLessControlParameter = new HashMap();
351 
352         // check the complete pathInfo for
353         // * navigational information
354         // * control information
355 
356         if (environment.getRequest().getPathInfo() != null)
357         {
358             String pathInfo = environment.getRequest().getPathInfo();
359             StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
360 
361             int mode = 0; // 0=navigation, 1=control information
362             String name = null;
363             while (tokenizer.hasMoreTokens()) {
364                 String token = tokenizer.nextToken();
365                 if (PortalControlParameter.isControlParameter(token)) {
366                     mode = 1;
367                     name = token;
368                 } else if (mode==0) {
369                     startGlobalNavigation.add(token);
370                 } else if (mode==1) {
371                     if ((PortalControlParameter.isStateFullParameter(name))) {
372                         startControlParameter.put(PortalControlParameter.decodeParameterName(name),
373                                                   PortalControlParameter.decodeParameterValue(name,token));
374                     } else {
375                         startStateLessControlParameter.put(PortalControlParameter.decodeParameterName(name),
376                                                            PortalControlParameter.decodeParameterValue(name,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         startControlParameter.put(PortalControlParameter.encodeRenderParamName(portletWindow,name),
391                                   PortalControlParameter.encodeRenderParamValues(values));
392 
393     }
394 
395     public void clearRenderParameters(PortletWindow portletWindow)
396     {
397         String prefix = PortalControlParameter.getRenderParamKey(portletWindow);
398         Iterator keyIterator = startControlParameter.keySet().iterator();
399 
400         while (keyIterator.hasNext()) {
401             String name = (String)keyIterator.next();
402             if (name.startsWith(prefix)) {
403                 keyIterator.remove();
404             }
405         }
406     }
407 
408 }