KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > core > PortalURL


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 JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
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 JavaDoc insecureServlet;
38     private static final String JavaDoc 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 JavaDoc getBasePortalURL(HttpServletRequest JavaDoc 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 JavaDoc getBasePortalURL(PortalEnvironment env)
62     {
63         StringBuffer JavaDoc result = new StringBuffer JavaDoc(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 JavaDoc startGlobalNavigation = new ArrayList JavaDoc();
74     private List JavaDoc startLocalNavigation = new ArrayList JavaDoc();
75     private HashMap JavaDoc startControlParameter = new HashMap JavaDoc();
76     private HashMap JavaDoc startStateLessControlParameter = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc nav)
174     {
175         return startLocalNavigation.contains(nav);
176     }
177
178     public String JavaDoc getGlobalNavigationAsString()
179     {
180         StringBuffer JavaDoc result = new StringBuffer JavaDoc(200);
181         Iterator JavaDoc iterator = startGlobalNavigation.iterator();
182         if (iterator.hasNext()) {
183             result.append((String JavaDoc)iterator.next());
184             while (iterator.hasNext()) {
185                 result.append("/");
186                 String JavaDoc st = (String JavaDoc)iterator.next();
187                 result.append(st);
188             }
189         }
190         return result.toString();
191     }
192
193     public String JavaDoc getLocalNavigationAsString()
194     {
195         StringBuffer JavaDoc result = new StringBuffer JavaDoc(30);
196         Iterator JavaDoc iterator = startLocalNavigation.iterator();
197         if (iterator.hasNext()) {
198             result.append((String JavaDoc)iterator.next());
199             while (iterator.hasNext()) {
200                 result.append(".");
201                 result.append((String JavaDoc)iterator.next());
202             }
203         }
204         return result.toString();
205     }
206
207     public String JavaDoc getControlParameterAsString(PortalControlParameter controlParam)
208     {
209         Map JavaDoc stateFullParams = startControlParameter;
210         if (controlParam != null) {
211             stateFullParams = controlParam.getStateFullControlParameter();
212         }
213
214         StringBuffer JavaDoc result = new StringBuffer JavaDoc(100);
215         Iterator JavaDoc iterator = stateFullParams.keySet().iterator();
216         while (iterator.hasNext()) {
217             if (iterator.hasNext()) result.append("/");
218             String JavaDoc name = (String JavaDoc)iterator.next();
219             String JavaDoc value = (String JavaDoc)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 JavaDoc getRequestParameterAsString(PortalControlParameter controlParam)
231     {
232         if (controlParam!=null) {
233             Map JavaDoc requestParams = controlParam.getRequestParameter();
234
235             StringBuffer JavaDoc result = new StringBuffer JavaDoc(100);
236             Iterator JavaDoc iterator = requestParams.keySet().iterator();
237             boolean hasNext = iterator.hasNext();
238             if (hasNext) {
239                 result.append("?");
240             }
241
242             while (hasNext) {
243                 String JavaDoc name = (String JavaDoc)iterator.next();
244                 Object JavaDoc value = requestParams.get(name);
245                 String JavaDoc[] values = value instanceof String JavaDoc ? new String JavaDoc[] {(String JavaDoc)value} : (String JavaDoc[])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 JavaDoc toString()
269     {
270         return toString(null,null);
271     }
272
273     public String JavaDoc toString(PortalControlParameter controlParam,Boolean JavaDoc p_secure)
274     {
275
276         StringBuffer JavaDoc urlBase = new StringBuffer JavaDoc(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 JavaDoc url = urlBase.toString();
288         String JavaDoc global = getGlobalNavigationAsString();
289         if (global.length() > 0) {
290             url += "/";
291             url += global;
292         }
293
294
295         String JavaDoc control = getControlParameterAsString(controlParam);
296         if (control.length() > 0) {
297             url += control;
298         }
299
300         String JavaDoc requestParam = getRequestParameterAsString(controlParam);
301         if (requestParam.length() > 0) {
302             url += requestParam;
303         }
304
305         String JavaDoc local = getLocalNavigationAsString();
306         if (local.length() > 0) {
307             url += "#";
308             url += local;
309         }
310
311         return environment.getResponse().encodeURL(url);
312     }
313
314     Map JavaDoc getClonedStateFullControlParameter()
315     {
316         analyzeRequestInformation();
317         return(Map JavaDoc)startControlParameter.clone();
318     }
319
320     Map JavaDoc getClonedStateLessControlParameter()
321     {
322         analyzeRequestInformation();
323         return(Map JavaDoc)startStateLessControlParameter.clone();
324     }
325
326     void analyzeControlInformation(PortalControlParameter control)
327     {
328         startControlParameter = (HashMap JavaDoc)control.getStateFullControlParameter();
329         startStateLessControlParameter = (HashMap JavaDoc)control.getStateLessControlParameter();
330     }
331
332     void analyzeRequestInformation()
333     {
334         if (analyzed) return;
335
336         startGlobalNavigation = new ArrayList JavaDoc();
337         startLocalNavigation = new ArrayList JavaDoc();
338         startControlParameter = new HashMap JavaDoc();
339         startStateLessControlParameter = new HashMap JavaDoc();
340
341         // check the complete pathInfo for
342
// * navigational information
343
// * control information
344

345         if (environment.getRequest().getPathInfo() != null)
346         {
347             String JavaDoc pathInfo = environment.getRequest().getPathInfo();
348             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(pathInfo, "/");
349
350             int mode = 0; // 0=navigation, 1=control information
351
String JavaDoc name = null;
352             while (tokenizer.hasMoreTokens()) {
353                 String JavaDoc 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 JavaDoc name,
377                                    String JavaDoc[] 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 JavaDoc prefix = PortalControlParameter.getRenderParamKey(portletWindow);
387         Iterator JavaDoc keyIterator = startControlParameter.keySet().iterator();
388
389         while (keyIterator.hasNext()) {
390             String JavaDoc name = (String JavaDoc)keyIterator.next();
391             if (name.startsWith(prefix)) {
392                 keyIterator.remove();
393             }
394         }
395     }
396
397 }
398
Popular Tags