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      static String hostNameHTTP;
38      static String hostNameHTTPS;
39  
40      static
41      {
42          String hostName      = Config.getParameters().getString ("host.name", "localhost");
43          String hostPortHTTP  = Config.getParameters().getString ("host.port.http", "80");
44          String hostPortHTTPS = Config.getParameters().getString ("host.port.https", "443");
45  
46          StringBuffer hostHTTP = new StringBuffer("http://");
47          hostHTTP.append(hostName);
48          if (!hostPortHTTP.equals("80")) {
49              hostHTTP.append(":");
50              hostHTTP.append(hostPortHTTP);
51          }
52          hostNameHTTP = hostHTTP.toString();
53  
54          StringBuffer hostHTTPS = new StringBuffer("https://");
55          hostHTTPS.append(hostName);
56          if (!hostPortHTTPS.equals("443")) {
57              hostHTTPS.append(":");
58              hostHTTPS.append(hostPortHTTPS);
59          }
60          hostNameHTTPS = hostHTTPS.toString();
61      }
62  
63      /***
64       * Creates and URL pointing to the home of the portal
65       * 
66       * @param request   the servlet request
67       * @return the portal URL
68       */
69      public String getBasePortalURL(HttpServletRequest request)
70      {
71          return getBasePortalURL(PortalEnvironment.getPortalEnvironment(request));
72      }
73  
74      /***
75       * Creates and URL pointing to the home of the portal
76       * 
77       * @param env     the portal environment
78       * @return the portal URL
79       */
80      public String getBasePortalURL(PortalEnvironment env)
81      {
82          StringBuffer result = new StringBuffer(256);
83          /*
84                  result.append(secure?hostNameHTTPS:hostNameHTTP);
85          */
86          result.append(env.getRequest().getContextPath());
87          result.append(env.getRequest().getServletPath());
88  
89          return result.toString();
90      }
91  
92      static public String getBaseURLexcludeContext()
93      {
94          StringBuffer result = new StringBuffer(256);
95  
96          result.append(hostNameHTTP);
97  
98          return result.toString();
99      }
100 
101 
102     private String base;
103     private List startGlobalNavigation = new ArrayList();
104     private List startLocalNavigation = new ArrayList();
105     private HashMap startControlParameter = new HashMap();
106     private HashMap startStateLessControlParameter = new HashMap();
107     private boolean analyzed = false;
108 
109     private PortalEnvironment environment;
110 
111     /***
112      * Creates and URL pointing to the home of the portal
113      * 
114      * @param env     the portal environment
115      */
116     public PortalURL(PortalEnvironment env)
117     {
118         environment = env;
119         this.base = getBasePortalURL(environment);
120     }
121 
122     /***
123      * Creates and URL pointing to the home of the portal
124      * 
125      * @param request     the servlet request
126      */
127     public PortalURL(HttpServletRequest request)
128     {
129         this(PortalEnvironment.getPortalEnvironment(request));
130     }
131 
132     /***
133      * Creates and URL pointing to the given fragment of the portal
134      * 
135      * @param request   the servlet request
136      * @param pointTo the fragment to point to
137      */
138     public PortalURL(HttpServletRequest request, Fragment pointTo)
139     {
140         this(request);
141         pointTo.createURL(this);
142     }
143 
144     /***
145      * Creates and URL pointing to the given fragment of the portal
146      * 
147      * @param env     the portal environment
148      * @param pointTo the fragment to point to
149      */
150     public PortalURL(PortalEnvironment env, Fragment pointTo)
151     {
152         this(env);
153         pointTo.createURL(this);
154     }
155 
156     /***
157      * Adds a navigational information pointing to a portal part, e.g. PageGroups
158      * or Pages
159      * 
160      * @param nav    the string pointing to a portal part
161      */
162     public void addGlobalNavigation(String nav)
163     {
164         startGlobalNavigation.add(nav);
165     }
166 
167     /***
168      * Sets the local navigation. Because the local navigation is always handled
169      * by the Browser, therefore the local navigation cleared.
170      */
171     public void setLocalNavigation()
172     {
173         startLocalNavigation = new ArrayList();
174     }
175 
176     /***
177      * Adds a navigational information pointing to a local portal part inside
178      * of a global portal part, for example, a portlet on a page.
179      * 
180      * @param nav    the string pointing to a local portal part
181      */
182     public void addLocalNavigation(String nav)
183     {
184         startLocalNavigation.add(nav);
185     }
186 
187     /***
188      * Returns true if the given string is part of the global navigation of this URL
189      * 
190      * @param nav    the string to check
191      * @return true, if the string is part of the navigation
192      */
193     public boolean isPartOfGlobalNavigation(String nav)
194     {
195         return startGlobalNavigation.contains(nav);
196     }
197 
198     /***
199      * Returns true if the given string is part of the local navigation of this URL
200      * 
201      * @param nav    the string to check
202      * @return true, if the string is part of the navigation
203      */
204     public boolean isPartOfLocalNavigation(String nav)
205     {
206         return startLocalNavigation.contains(nav);
207     }
208 
209     public String getGlobalNavigationAsString()
210     {
211         StringBuffer result = new StringBuffer(200);
212         Iterator iterator = startGlobalNavigation.iterator();
213         if (iterator.hasNext()) {
214             result.append((String)iterator.next());
215             while (iterator.hasNext()) {
216                 result.append("/");
217                 result.append((String)iterator.next());
218             }
219         }
220         return result.toString();
221     }
222 
223     public String getLocalNavigationAsString()
224     {
225         StringBuffer result = new StringBuffer(30);
226         Iterator iterator = startLocalNavigation.iterator();
227         if (iterator.hasNext()) {
228             result.append((String)iterator.next());
229             while (iterator.hasNext()) {
230                 result.append(".");
231                 result.append((String)iterator.next());
232             }
233         }
234         return result.toString();
235     }
236 
237     public String getControlParameterAsString(PortalControlParameter controlParam)
238     {
239         Map stateFullParams = startControlParameter;
240         Map stateLessParams = null;
241         if (controlParam != null) {
242             stateFullParams = controlParam.getStateFullControlParameter();
243             stateLessParams = controlParam.getStateLessControlParameter();
244         }
245 
246         StringBuffer result = new StringBuffer(100);
247         Iterator iterator = stateFullParams.keySet().iterator();
248         while (iterator.hasNext()) {
249             if (iterator.hasNext()) result.append("/");
250             String name = (String)iterator.next();
251             String value = (String)stateFullParams.get(name);
252             if(value!=null) {
253                 result.append(PortalControlParameter.encodeParameter(name));
254                 result.append("/");
255                 result.append(value);
256             }
257         }
258 
259         return result.toString();
260     }
261 
262     public String getRequestParameterAsString(PortalControlParameter controlParam)
263     {
264         if (controlParam!=null) {
265             Map requestParams = controlParam.getRequestParameter();
266 
267             StringBuffer result = new StringBuffer(100);
268             Iterator iterator = requestParams.keySet().iterator();
269             boolean hasNext = iterator.hasNext();
270             if (hasNext) {
271                 result.append("?");
272             }
273 
274             while (hasNext) {
275                 String name = (String)iterator.next();
276                 Object value = requestParams.get(name);
277                 String[] values = value instanceof String ? new String[] {(String)value} : (String[])value;
278 
279                 result.append(name);
280                 result.append("=");
281                 result.append(values[0]);
282                 for (int i = 1; i < values.length; i++) {
283                     result.append("&");
284                     result.append(name);
285                     result.append("=");
286                     result.append(values[i]);
287                 };
288 
289                 hasNext=iterator.hasNext();
290                 if (hasNext) {
291                     result.append("&");
292                 }
293             }
294 
295             return result.toString();
296         }
297         return "";
298     }
299 
300     public String toString()
301     {
302         return toString(null,null);
303     }
304 
305     public String toString(PortalControlParameter controlParam,Boolean p_secure)
306     {
307 
308         StringBuffer urlBase = new StringBuffer(256);                
309 
310         boolean secure=false;
311         if (p_secure!=null) {
312             secure=p_secure.booleanValue();
313         } else {
314             secure=environment.getRequest().isSecure(); 
315         }
316         urlBase.append(secure?hostNameHTTPS:hostNameHTTP);
317         urlBase.append(base);
318 
319         String url = urlBase.toString();
320         String global = getGlobalNavigationAsString();
321         if (global.length() > 0) {
322             url += "/";
323             url += global;
324         }
325 
326         String control = getControlParameterAsString(controlParam);
327         if (control.length() > 0) {
328             url += control;
329         }
330 
331         String requestParam = getRequestParameterAsString(controlParam);
332         if (requestParam.length() > 0) {
333             url += requestParam;
334         }
335 
336         String local = getLocalNavigationAsString();
337         if (local.length() > 0) {
338             url += "#";
339             url += local;
340         }
341 
342         return environment.getResponse().encodeURL(url);
343     }
344 
345     Map getClonedStateFullControlParameter()
346     {
347         analyzeRequestInformation();
348         return(Map)startControlParameter.clone();
349     }
350 
351     Map getClonedStateLessControlParameter()
352     {
353         analyzeRequestInformation();
354         return(Map)startStateLessControlParameter.clone();
355     }
356 
357     void analyzeControlInformation(PortalControlParameter control)
358     {
359         startControlParameter = (HashMap)control.getStateFullControlParameter();
360         startStateLessControlParameter = (HashMap)control.getStateLessControlParameter();
361     }
362 
363     void analyzeRequestInformation()
364     {
365         if (analyzed) return;
366 
367         startGlobalNavigation = new ArrayList();
368         startLocalNavigation = new ArrayList();
369         startControlParameter = new HashMap();
370         startStateLessControlParameter = new HashMap();
371 
372         // check the complete pathInfo for
373         // * navigational information
374         // * control information
375         
376         if (environment.getRequest().getPathInfo() != null) 
377         {
378             String pathInfo = environment.getRequest().getPathInfo();
379             StringTokenizer tokenizer = new StringTokenizer(pathInfo, "/");
380 
381             int mode = 0; // 0=navigation, 1=control information
382             String name = null;
383             while (tokenizer.hasMoreTokens()) {
384                 String token = tokenizer.nextToken();
385                 if (PortalControlParameter.isControlParameter(token)) {
386                     mode = 1;
387                     name = token;
388                 } else if (mode==0) {
389                     startGlobalNavigation.add(token);
390                 } else if (mode==1) {
391                     if ((PortalControlParameter.isStateFullParameter(name))) {
392                         startControlParameter.put(PortalControlParameter.decodeParameterName(name),
393                                                   PortalControlParameter.decodeParameterValue(name,token));
394                     } else {
395                         startStateLessControlParameter.put(PortalControlParameter.decodeParameterName(name),
396                                                            PortalControlParameter.decodeParameterValue(name,token));
397                     }
398                     mode = 0;
399                 }
400             }
401         }
402         analyzed = true;
403 
404     }
405 
406     public void setRenderParameter(PortletWindow portletWindow,
407                                    String name,
408                                    String[] values)
409     {
410         startControlParameter.put(PortalControlParameter.encodeRenderParamName(portletWindow,name),
411                                   PortalControlParameter.encodeRenderParamValues(values));
412 
413     }
414 
415     public void clearRenderParameters(PortletWindow portletWindow)
416     {
417         String prefix = PortalControlParameter.getRenderParamKey(portletWindow);
418         Iterator keyIterator = startControlParameter.keySet().iterator();
419 
420         while (keyIterator.hasNext()) {
421             String name = (String)keyIterator.next();
422             if (name.startsWith(prefix)) {
423                 keyIterator.remove();
424             }
425         }
426     }
427 
428 }