KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > portal > PortalControlURL


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 package org.infoglue.deliver.portal;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.portlet.PortletMode;
30 import javax.portlet.WindowState;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.pluto.om.window.PortletWindow;
37
38 /**
39  * Represents an portal URL including control parameters.
40  *
41  * @author jand
42  * @author robert
43  */

44 public class PortalControlURL {
45     private static final Log log = LogFactory.getLog(PortalControlURL.class);
46
47     public static final String JavaDoc IG = "_ig_";
48     public static final String JavaDoc RENDER = "_rp_";
49     public static final String JavaDoc ACTION = "_ac";
50     public static final String JavaDoc PID = "_pid";
51     public static final String JavaDoc MULTI_VALUE = "__";
52
53     private String JavaDoc contextPath;
54     private String JavaDoc realPath;
55     private String JavaDoc webWorkAction;
56     private Map JavaDoc queryParams; // ?<str>
57
private Map JavaDoc pathParams;
58     private String JavaDoc local; // #<str>
59

60     /**
61      * Constructor
62      *
63      * @param req current request
64      */

65     public PortalControlURL(HttpServletRequest JavaDoc req) {
66         // TODO ok to unwrap request? It seems it must be done
67
while (req instanceof HttpServletRequestWrapper JavaDoc) {
68             req = (HttpServletRequest JavaDoc) ((HttpServletRequestWrapper JavaDoc) req).getRequest();
69         }
70         this.contextPath = req.getContextPath();
71
72         String JavaDoc path = req.getRequestURI();
73         if (contextPath != null && path.length() > 0) {
74             path = path.substring(contextPath.length(), path.length());
75         }
76
77         if (path.endsWith(".action")) {
78             int last = path.lastIndexOf("/");
79             this.webWorkAction = path.substring(last + 1);
80             path = path.substring(0, last);
81         }
82
83         this.realPath = PathParser.getRealPath(path);
84         this.queryParams = PathParser.copyParameters(req.getParameterMap());
85         this.pathParams = PathParser.parsePathParameters("_", path, true);
86         log.debug("realPath: " + realPath);
87         log.debug("wwAction: " + webWorkAction);
88     }
89
90     /**
91      * Copy-constructor
92      *
93      * @param url copy this portal control URL
94      */

95     public PortalControlURL(PortalControlURL url) {
96         this.contextPath = url.contextPath;
97         this.realPath = url.realPath;
98         this.webWorkAction = url.webWorkAction;
99         this.queryParams = new HashMap JavaDoc(url.queryParams.size());
100         this.queryParams.putAll(url.queryParams);
101         this.pathParams = new HashMap JavaDoc(url.pathParams.size());
102         this.pathParams.putAll(url.pathParams);
103     }
104
105     public PortletMode getPortletMode(PortletWindow window) {
106         // TODO fixme
107
return PortletMode.VIEW;
108     }
109     public PortletMode getPreviousPortletMode(PortletWindow portletWindow) {
110         // TODO fixme
111
return PortletMode.VIEW;
112     }
113
114     public WindowState getWindowState(PortletWindow portletWindow) {
115         // TODO fixme
116
return WindowState.NORMAL;
117     }
118
119     public WindowState getPreviousWindowState(PortletWindow portletWindow) {
120         // TODO fixme
121
return WindowState.NORMAL;
122     }
123
124     public void setPortletMode(PortletWindow window, PortletMode mode) {
125         // TODO implement
126
}
127
128     public void setPortletWindowState(PortletWindow window, WindowState state) {
129         // TODO implement
130
}
131
132     public void setPortletId(PortletWindow window) {
133         setPathParameter(PID, new String JavaDoc[] { window.getId().toString()});
134     }
135
136     // general param
137

138     public void setPathParameter(String JavaDoc nsName, String JavaDoc[] values) {
139         if (values == null || values.length == 0) {
140             pathParams.remove(nsName);
141         } else {
142             pathParams.put(nsName, values);
143         }
144     }
145
146     // --- ACTION param
147

148     /**
149      * Get the window-id of current request.
150      *
151      * @return window-id or null if no action.
152      */

153     public String JavaDoc getActionWindowID() {
154         String JavaDoc[] values = (String JavaDoc[]) pathParams.get(ACTION);
155         if (values != null && values.length > 0) {
156             return values[0];
157         }
158         return null;
159     }
160
161     public void setActionParameter(PortletWindow window) {
162         setPathParameter(ACTION, new String JavaDoc[] { window.getId().toString()});
163     }
164
165     public void clearActionParameter() {
166         pathParams.remove(ACTION);
167         pathParams.remove(PID);
168     }
169
170     
171     // --- RENDER params
172

173     /**
174      * Get all render parameters of a window.
175      * Note: the resulting Map will not contain namespaced parameters.
176      *
177      * @param window Window
178      */

179     public Map JavaDoc getRenderParameterMap(PortletWindow window) {
180         Map JavaDoc result = new HashMap JavaDoc();
181         int nsSize = RENDER.length() + window.getId().toString().length() + 1;
182         for (Iterator JavaDoc iter = pathParams.keySet().iterator(); iter.hasNext();) {
183             String JavaDoc name = (String JavaDoc) iter.next();
184             if (name.startsWith(RENDER + window.getId())) {
185                 String JavaDoc paramName = name.substring(nsSize);
186                 result.put(paramName, pathParams.get(name));
187             }
188         }
189         return result;
190     }
191
192     /**
193      * Set render parameter of window.
194      *
195      * @param window Window
196      * @param name Parameter-name (not namespaced)
197      * @param values Parameter-values
198      */

199     public void setRenderParameter(PortletWindow window, String JavaDoc name, String JavaDoc[] values) {
200         String JavaDoc nsName = RENDER + window.getId() + "_" + name;
201         setPathParameter(nsName, values);
202     }
203
204     /**
205      * Clear render parameters of window.
206      *
207      * @param window Window
208      */

209     public void clearRenderParameters(PortletWindow window) {
210         for (Iterator JavaDoc iter = pathParams.keySet().iterator(); iter.hasNext();) {
211             String JavaDoc name = (String JavaDoc) iter.next();
212             if (name.startsWith(RENDER + window.getId())) {
213                 //pathParams.remove(name);
214
iter.remove();
215             }
216         }
217     }
218
219     // QUERY params
220

221     /**
222      * Get all query (action) parameters of a window.
223      * Note: the resulting Map will _not_ contain namespaced parameters.
224      *
225      * @param window Window
226      */

227     public Map JavaDoc getQueryParameterMap(PortletWindow window) {
228         return queryParams;
229     }
230
231     /**
232      * Get all query (action) parameters of a window.
233      * Note: the resulting Map will contain namespaced parameters.
234      *
235      * @param window Window
236      */

237     public Map JavaDoc getQueryParameterMap() {
238         return queryParams;
239     }
240
241     /**
242      * Set query (action) parameter of window.
243      * Note: current implementation does not namespace parameters
244      *
245      * @param window Window
246      * @param name Parameter-name (not namespaced)
247      * @param values Parameter-values
248      */

249     public void setQueryParameter(PortletWindow window, String JavaDoc name, String JavaDoc[] values) {
250         String JavaDoc nsName = name; // Prepared for namespaced params
251
if (values == null || values.length == 0) {
252             queryParams.remove(nsName);
253         } else {
254             queryParams.put(nsName, values);
255         }
256     }
257
258     public void clearQueryParameters() {
259         queryParams.clear();
260     }
261
262     /**
263      * Generates an portal URL.
264      * Format: 'context/realPath/control-params/infoglueServletPath?query-params#local'
265      * where 'control-params': _type_pid_name/[value | __value1/__value2]/...
266      * separated by '/'.
267      */

268     public String JavaDoc toString() {
269         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
270
271         // Append context-path
272
buf.append(contextPath);
273
274         // Append real path (path besides control params, think "niceuri")
275
if (realPath != null) {
276             buf.append(realPath);
277         }
278
279         // Append "control-parameters"
280
if (!pathParams.isEmpty()) {
281             buf.append("/");
282             for (Iterator JavaDoc iter = pathParams.keySet().iterator(); iter.hasNext();) {
283                 String JavaDoc name = (String JavaDoc) iter.next();
284                 String JavaDoc[] values = (String JavaDoc[]) pathParams.get(name);
285                 buf.append(name);
286                 buf.append("/");
287                 buf.append(PathParser.encodeValues(values));
288
289                 if (iter.hasNext()) {
290                     buf.append("/");
291                 }
292             }
293         }
294
295         // Append servlet-path (infoglue-action)
296
if (webWorkAction != null) {
297             buf.append("/");
298             buf.append(webWorkAction);
299         }
300
301         // Append query-parameters
302
if (!queryParams.isEmpty()) {
303             buf.append("?");
304             for (Iterator JavaDoc iter = queryParams.keySet().iterator(); iter.hasNext();) {
305                 // Iterating over names
306
String JavaDoc name = (String JavaDoc) iter.next();
307                 String JavaDoc[] values = (String JavaDoc[]) queryParams.get(name);
308                 for (int i = 0; i < values.length; i++) {
309                     // Iterating over values
310
buf.append(name);
311                     buf.append("=");
312                     buf.append(values[i]);
313                     if (i < values.length - 1) {
314                         buf.append("&");
315                     }
316                 }
317                 if (iter.hasNext()) {
318                     buf.append("&");
319                 }
320             }
321         }
322
323         // Append local navigation (reference)
324
if (local != null && local.length() > 0) {
325             buf.append("#");
326             buf.append(local);
327         }
328         if (log.isDebugEnabled()) {
329             log.debug("Generated URL: " + buf.toString());
330         }
331         return buf.toString();
332     }
333
334     /**
335      * @return Returns the targeted.
336      */

337     public boolean isTargeted() {
338         return pathParams.containsKey(ACTION) || pathParams.containsKey(PID);
339         //return targeted;
340
}
341 }
342
Popular Tags