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.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.StringTokenizer;
28  
29  import javax.servlet.http.HttpServletRequest;
30  
31  import org.apache.pluto.om.window.PortletWindow;
32  import org.apache.pluto.portalImpl.aggregation.Fragment;
33  import org.apache.pluto.portalImpl.services.config.Config;
34  
35  public class PortalURL {
36  
37      private static final String insecureServlet;
38      private static final String secureServlet;
39      static {
40          insecureServlet = Config.getParameters().getString("servlet.insecure");
41          secureServlet = Config.getParameters().getString("servlet.secure");
42      }
43  
44      /***
45       * Creates and URL pointing to the home of the portal
46       * 
47       * @param request   the servlet request
48       * @return the portal URL
49       */
50      public String getBasePortalURL(HttpServletRequest request)
51      {
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 startControlParameter = 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     public String getGlobalNavigationAsString()
179     {
180         StringBuffer result = new StringBuffer(200);
181         Iterator iterator = startGlobalNavigation.iterator();
182         if (iterator.hasNext()) {
183             result.append((String)iterator.next());
184             while (iterator.hasNext()) {
185                 result.append("/");
186                 String st = (String)iterator.next();
187                 result.append(st);
188             }
189         }
190         return result.toString();
191     }
192 
193     public String getLocalNavigationAsString()
194     {
195         StringBuffer result = new StringBuffer(30);
196         Iterator iterator = startLocalNavigation.iterator();
197         if (iterator.hasNext()) {
198             result.append((String)iterator.next());
199             while (iterator.hasNext()) {
200                 result.append(".");
201                 result.append((String)iterator.next());
202             }
203         }
204         return result.toString();
205     }
206 
207     public String getControlParameterAsString(PortalControlParameter controlParam)
208     {
209         Map stateFullParams = startControlParameter;
210         if (controlParam != null) {
211             stateFullParams = controlParam.getStateFullControlParameter();
212         }
213 
214         StringBuffer result = new StringBuffer(100);
215         Iterator iterator = stateFullParams.keySet().iterator();
216         while (iterator.hasNext()) {
217             if (iterator.hasNext()) result.append("/");
218             String name = (String)iterator.next();
219             String value = (String)stateFullParams.get(name);
220             if(value!=null) {
221                 result.append(PortalControlParameter.encodeParameter(name));
222                 result.append("/");
223                 result.append(value);
224             }
225         }
226 
227         return result.toString();
228     }
229 
230     public String getRequestParameterAsString(PortalControlParameter controlParam)
231     {
232         if (controlParam!=null) {
233             Map requestParams = controlParam.getRequestParameter();
234 
235             StringBuffer result = new StringBuffer(100);
236             Iterator iterator = requestParams.keySet().iterator();
237             boolean hasNext = iterator.hasNext();
238             if (hasNext) {
239                 result.append("?");
240             }
241 
242             while (hasNext) {
243                 String name = (String)iterator.next();
244                 Object value = requestParams.get(name);
245                 String[] values = value instanceof String ? new String[] {(String)value} : (String[])value;
246 
247                 result.append(name);
248                 result.append("=");
249                 result.append(values[0]);
250                 for (int i = 1; i < values.length; i++) {
251                     result.append("&");
252                     result.append(name);
253                     result.append("=");
254                     result.append(values[i]);
255                 }
256 
257                 hasNext=iterator.hasNext();
258                 if (hasNext) {
259                     result.append("&");
260                 }
261             }
262 
263             return result.toString();
264         }
265         return "";
266     }
267 
268     public String toString()
269     {
270         return toString(null,null);
271     }
272 
273     public String toString(PortalControlParameter controlParam,Boolean p_secure)
274     {
275 
276         StringBuffer urlBase = new StringBuffer(256);                
277 
278         boolean secure=false;
279         if (p_secure!=null) {
280             secure=p_secure.booleanValue();
281         } else {
282             secure=environment.getRequest().isSecure(); 
283         }
284         urlBase.append(environment.getRequest().getContextPath());
285         urlBase.append(secure ? secureServlet : insecureServlet);
286 
287         String url = urlBase.toString();
288         String global = getGlobalNavigationAsString();
289         if (global.length() > 0) {
290             url += "/";
291             url += global;
292         }
293 
294 
295         String control = getControlParameterAsString(controlParam);
296         if (control.length() > 0) {
297             url += control;
298         }
299 
300         String requestParam = getRequestParameterAsString(controlParam);
301         if (requestParam.length() > 0) {
302             url += requestParam;
303         }
304 
305         String local = getLocalNavigationAsString();
306         if (local.length() > 0) {
307             url += "#";
308             url += local;
309         }
310 
311         return environment.getResponse().encodeURL(url);
312     }
313 
314     Map getClonedStateFullControlParameter()
315     {
316         analyzeRequestInformation();
317         return(Map)startControlParameter.clone();
318     }
319 
320     Map getClonedStateLessControlParameter()
321     {
322         analyzeRequestInformation();
323         return(Map)startStateLessControlParameter.clone();
324     }
325 
326     void analyzeControlInformation(PortalControlParameter control)
327     {
328         startControlParameter = (HashMap)control.getStateFullControlParameter();
329         startStateLessControlParameter = (HashMap)control.getStateLessControlParameter();
330     }
331 
332     void analyzeRequestInformation()
333     {
334         if (analyzed) return;
335 
336         startGlobalNavigation = new ArrayList();
337         startLocalNavigation = new ArrayList();
338         startControlParameter = new HashMap();
339         startStateLessControlParameter = new HashMap();
340 
341         // check the complete pathInfo for
342         // * navigational information
343         // * control information
344         
345         if (environment.getRequest().getPathInfo() != null) 
346         {
347             String pathInfo = environment.getRequest().getPathInfo();
348             StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
349 
350             int mode = 0; // 0=navigation, 1=control information
351             String name = null;
352             while (tokenizer.hasMoreTokens()) {
353                 String token = tokenizer.nextToken();
354                 if (PortalControlParameter.isControlParameter(token)) {
355                     mode = 1;
356                     name = token;
357                 } else if (mode==0) {
358                     startGlobalNavigation.add(token);
359                 } else if (mode==1) {
360                     if ((PortalControlParameter.isStateFullParameter(name))) {
361                         startControlParameter.put(PortalControlParameter.decodeParameterName(name),
362                                                   PortalControlParameter.decodeParameterValue(name,token));
363                     } else {
364                         startStateLessControlParameter.put(PortalControlParameter.decodeParameterName(name),
365                                                            PortalControlParameter.decodeParameterValue(name,token));
366                     }
367                     mode = 0;
368                 }
369             }
370         }
371         analyzed = true;
372 
373     }
374 
375     public void setRenderParameter(PortletWindow portletWindow,
376                                    String name,
377                                    String[] values)
378     {
379         startControlParameter.put(PortalControlParameter.encodeRenderParamName(portletWindow,name),
380                                   PortalControlParameter.encodeRenderParamValues(values));
381 
382     }
383 
384     public void clearRenderParameters(PortletWindow portletWindow)
385     {
386         String prefix = PortalControlParameter.getRenderParamKey(portletWindow);
387         Iterator keyIterator = startControlParameter.keySet().iterator();
388 
389         while (keyIterator.hasNext()) {
390             String name = (String)keyIterator.next();
391             if (name.startsWith(prefix)) {
392                 keyIterator.remove();
393             }
394         }
395     }
396 
397 }