KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > portlet > PortletRequest


1 /*
2  * Copyright 1999-2005 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 package org.apache.cocoon.environment.portlet;
17
18 import org.apache.cocoon.environment.Cookie;
19 import org.apache.cocoon.environment.Request;
20 import org.apache.cocoon.environment.Session;
21 import org.apache.cocoon.portlet.multipart.MultipartActionRequest;
22
23 import org.apache.avalon.framework.CascadingRuntimeException;
24
25 import javax.portlet.PortalContext;
26 import javax.portlet.PortletMode;
27 import javax.portlet.PortletPreferences;
28 import javax.portlet.WindowState;
29
30 import java.util.Collections JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 /**
39  * Implements the {@link Request} interface for the JSR-168 (Portlet) environment.
40  *
41  * @author <a HREF="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
42  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
43  * @version $Id: PortletRequest.java 202262 2005-06-28 18:02:55Z vgritsenko $
44  */

45 public abstract class PortletRequest implements Request {
46
47     /** Portlet request does not has servletPath, so it will be passed via constructor */
48     private String JavaDoc servletPath;
49
50     /** Portlet request does not has pathInfo, so it will be passed via constructor */
51     private String JavaDoc pathInfo;
52
53     /** The real PortletRequest object */
54     private final javax.portlet.PortletRequest request;
55
56     /** The HttpEnvironment object */
57     private final PortletEnvironment environment;
58
59     /** The character encoding of parameters */
60     private String JavaDoc form_encoding;
61
62     /** The default form encoding of the servlet container */
63     private String JavaDoc container_encoding;
64
65     /** The current session */
66     private PortletSession session;
67
68     private Cookie[] wrappedCookies;
69     private Map JavaDoc wrappedCookieMap;
70     protected String JavaDoc portletRequestURI;
71
72
73     /**
74      * Creates a PortletRequest based on a real PortletRequest object
75      */

76     protected PortletRequest(String JavaDoc servletPath,
77                              String JavaDoc pathInfo,
78                              javax.portlet.PortletRequest request,
79                              PortletEnvironment environment) {
80         super();
81         this.servletPath = servletPath;
82         this.pathInfo = pathInfo;
83         this.request = request;
84         this.environment = environment;
85     }
86
87     /* (non-Javadoc)
88      * @see org.apache.cocoon.environment.Request#get(java.lang.String)
89      */

90     public Object JavaDoc get(String JavaDoc name) {
91         // if the request has been wrapped then access its method
92
if (request instanceof MultipartActionRequest) {
93             return ((MultipartActionRequest) request).get(name);
94         } else {
95             String JavaDoc[] values = request.getParameterValues(name);
96             if (values == null) {
97                 return null;
98             }
99             if (values.length == 1) {
100                 return values[0];
101             }
102             if (values.length > 1) {
103                 Vector JavaDoc vect = new Vector JavaDoc(values.length);
104                 for (int i = 0; i < values.length; i++) {
105                     vect.add(values[i]);
106                 }
107                 return vect;
108             }
109             return null;
110         }
111     }
112
113     /* The Request interface methods */
114
115     public String JavaDoc getAuthType() {
116         return this.request.getAuthType();
117     }
118
119     private synchronized void wrapCookies() {
120         this.wrappedCookieMap = new HashMap JavaDoc();
121         PortletPreferences cookies = this.request.getPreferences();
122         if (cookies != null) {
123             this.wrappedCookies = new Cookie[cookies.getMap().size()];
124             int i = 0;
125             for (Enumeration JavaDoc e = cookies.getNames(); e.hasMoreElements(); i++) {
126                 String JavaDoc name = (String JavaDoc) e.nextElement();
127                 PortletCookie cookie = new PortletCookie(name, cookies.getValue(name, null));
128                 this.wrappedCookies[i] = cookie;
129                 this.wrappedCookieMap.put(cookie.getName(), cookie);
130             }
131         }
132         this.wrappedCookieMap = Collections.unmodifiableMap(this.wrappedCookieMap);
133     }
134
135     public Cookie[] getCookies() {
136         if (this.wrappedCookieMap == null) {
137             wrapCookies();
138         }
139         return this.wrappedCookies;
140     }
141
142     public Map JavaDoc getCookieMap() {
143         if (this.wrappedCookieMap == null) {
144             wrapCookies();
145         }
146         return this.wrappedCookieMap;
147     }
148
149     public long getDateHeader(String JavaDoc name) {
150         return Long.parseLong(this.request.getProperty(name));
151     }
152
153     public String JavaDoc getHeader(String JavaDoc name) {
154         if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) {
155             return this.request.getPortletMode().toString();
156         } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) {
157             return this.request.getWindowState().toString();
158         } else {
159             return this.request.getProperty(name);
160         }
161     }
162
163     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
164         return this.request.getProperties(name);
165     }
166
167     public Enumeration JavaDoc getHeaderNames() {
168         final Enumeration JavaDoc names = this.request.getPropertyNames();
169         // return this.request.getPropertyNames();
170
return new Enumeration JavaDoc() {
171             int position;
172
173             public boolean hasMoreElements() {
174                 return names.hasMoreElements() || position < 2;
175             }
176
177             public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
178                 if (names.hasMoreElements()) {
179                     return names.nextElement();
180                 }
181
182                 if (position == 0) {
183                     position++;
184                     return PortletEnvironment.HEADER_PORTLET_MODE;
185                 } else if (position == 1) {
186                     position++;
187                     return PortletEnvironment.HEADER_WINDOW_STATE;
188                 } else {
189                     throw new NoSuchElementException JavaDoc();
190                 }
191             }
192         };
193     }
194
195     /**
196      * Concrete request object will implement this
197      */

198     public abstract String JavaDoc getMethod();
199
200     public String JavaDoc getPathInfo() {
201         return this.pathInfo;
202     }
203
204     public String JavaDoc getPathTranslated() {
205         // TODO: getPathTranslated
206
return null;
207     }
208
209     public String JavaDoc getContextPath() {
210         return this.request.getContextPath();
211     }
212
213     public String JavaDoc getQueryString() {
214         // TODO: getQueryString
215
return "";
216     }
217
218     public String JavaDoc getRemoteUser() {
219         return this.request.getRemoteUser();
220     }
221
222     public boolean isUserInRole(String JavaDoc role) {
223         return this.request.isUserInRole(role);
224     }
225
226     public java.security.Principal JavaDoc getUserPrincipal() {
227         return this.request.getUserPrincipal();
228     }
229
230     public String JavaDoc getRequestedSessionId() {
231         return this.request.getRequestedSessionId();
232     }
233
234     public String JavaDoc getRequestURI() {
235         if (this.portletRequestURI == null) {
236             final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
237             buffer.append(this.request.getContextPath());
238
239             if (getServletPath() != null) {
240                 if (buffer.charAt(buffer.length()-1) != '/') {
241                     buffer.append('/');
242                 }
243                 buffer.append(getServletPath());
244             }
245
246             if (getPathInfo() != null) {
247                 if (buffer.charAt(buffer.length()-1) != '/') {
248                     buffer.append('/');
249                 }
250                 buffer.append(getPathInfo());
251             }
252
253             this.portletRequestURI = buffer.toString();
254         }
255         return this.portletRequestURI;
256     }
257
258     /* (non-Javadoc)
259      * @see org.apache.cocoon.environment.Request#getSitemapURI()
260      */

261     public String JavaDoc getSitemapURI() {
262         return this.environment.getURI();
263     }
264
265     public String JavaDoc getSitemapURIPrefix() {
266         return this.environment.getURIPrefix();
267     }
268
269     public String JavaDoc getServletPath() {
270         return this.servletPath;
271     }
272
273     public Session getSession(boolean create) {
274         javax.portlet.PortletSession serverSession = this.request.getPortletSession(create);
275         if (null != serverSession) {
276             if (null != this.session) {
277                 if (this.session.session != serverSession) {
278                     // update wrapper
279
this.session.session = serverSession;
280                 }
281             } else {
282                 // new wrapper
283
this.session = new PortletSession(serverSession,
284                                                   this.environment.getDefaultSessionScope());
285             }
286         } else {
287             // invalidate
288
this.session = null;
289         }
290         return this.session;
291     }
292
293     public Session getSession() {
294         return this.getSession(true);
295     }
296
297     public boolean isRequestedSessionIdValid() {
298         return this.request.isRequestedSessionIdValid();
299     }
300
301     /**
302      * Portlet does not know how portal manages session.
303      * This method returns false always.
304      */

305     public boolean isRequestedSessionIdFromCookie() {
306         return false;
307     }
308
309     /**
310      * Portlet does not know how portal manages session.
311      * This method returns true always.
312      */

313     public boolean isRequestedSessionIdFromURL() {
314         return true;
315     }
316
317     /* The ServletRequest interface methods */
318
319     public Object JavaDoc getAttribute(String JavaDoc name) {
320         return this.request.getAttribute(name);
321     }
322
323     public Enumeration JavaDoc getAttributeNames() {
324         return this.request.getAttributeNames();
325     }
326
327     public String JavaDoc getCharacterEncoding() {
328         return this.form_encoding;
329     }
330
331     public void setCharacterEncoding(String JavaDoc form_encoding) throws java.io.UnsupportedEncodingException JavaDoc {
332         this.form_encoding = form_encoding;
333     }
334
335     /**
336      * Sets the default encoding of the servlet container.
337      */

338     public void setContainerEncoding(String JavaDoc container_encoding) {
339         this.container_encoding = container_encoding;
340     }
341
342     public int getContentLength() {
343         // TODO getContentLength
344
// return this.request.getContentLength();
345
return -1;
346     }
347
348     public String JavaDoc getContentType() {
349         // TODO getContentType
350
// return this.request.getContentType();
351
return null;
352     }
353
354     public String JavaDoc getParameter(String JavaDoc name) {
355         String JavaDoc value = this.request.getParameter(name);
356         if (this.form_encoding == null || value == null) {
357             return value;
358         }
359         return decode(value);
360     }
361
362     private String JavaDoc decode(String JavaDoc str) {
363         if (str == null) {
364             return null;
365         }
366
367         try {
368             if (this.container_encoding == null) {
369                 this.container_encoding = "ISO-8859-1";
370             }
371             byte[] bytes = str.getBytes(this.container_encoding);
372             return new String JavaDoc(bytes, form_encoding);
373         } catch (java.io.UnsupportedEncodingException JavaDoc uee) {
374             throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
375         }
376     }
377
378     public Enumeration JavaDoc getParameterNames() {
379         return this.request.getParameterNames();
380     }
381
382     public String JavaDoc[] getParameterValues(String JavaDoc name) {
383         String JavaDoc[] values = this.request.getParameterValues(name);
384         if (values == null) {
385             return null;
386         } else if (this.form_encoding == null) {
387             return values;
388         }
389         String JavaDoc[] decoded_values = new String JavaDoc[values.length];
390         for (int i = 0; i < values.length; ++i) {
391             decoded_values[i] = decode(values[i]);
392         }
393         return decoded_values;
394     }
395
396     public String JavaDoc getProtocol() {
397         return "JSR168";
398     }
399
400     public String JavaDoc getScheme() {
401         return this.request.getScheme();
402     }
403
404     public String JavaDoc getServerName() {
405         return this.request.getServerName();
406     }
407
408     public int getServerPort() {
409         return this.request.getServerPort();
410     }
411
412     public String JavaDoc getRemoteAddr() {
413         // TODO getRemoteAddr
414
// return this.request.getRemoteAddr();
415
return null;
416     }
417
418     public String JavaDoc getRemoteHost() {
419         // TODO getRemoteHost
420
// return this.request.getRemoteHost();
421
return null;
422     }
423
424     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
425         this.request.setAttribute(name, o);
426     }
427
428     public void removeAttribute(String JavaDoc name) {
429         this.request.removeAttribute(name);
430     }
431
432     public Locale JavaDoc getLocale() {
433         return this.request.getLocale();
434     }
435
436     public Enumeration JavaDoc getLocales() {
437         return this.request.getLocales();
438     }
439
440     public boolean isSecure() {
441         return this.request.isSecure();
442     }
443
444
445     /* The PortletRequest interface methods */
446
447     /**
448      * Returns underlying portlet API request object
449      * @return portlet requesst object
450      */

451     public javax.portlet.PortletRequest getPortletRequest() {
452         return request;
453     }
454
455     public Map JavaDoc getParameterMap() {
456         return this.request.getParameterMap();
457     }
458
459     public PortalContext getPortalContext() {
460         return this.request.getPortalContext();
461     }
462
463     public PortletMode getPortletMode() {
464         return this.request.getPortletMode();
465     }
466
467     public javax.portlet.PortletSession getPortletSession() {
468         return this.request.getPortletSession();
469     }
470
471     public javax.portlet.PortletSession getPortletSession(boolean create) {
472         return this.request.getPortletSession(create);
473     }
474
475     public PortletPreferences getPreferences() {
476         return this.request.getPreferences();
477     }
478
479     public Enumeration JavaDoc getProperties(String JavaDoc name) {
480         return this.request.getProperties(name);
481     }
482
483     public String JavaDoc getProperty(String JavaDoc name) {
484         return this.request.getProperty(name);
485     }
486
487     public Enumeration JavaDoc getPropertyNames() {
488         return this.request.getPropertyNames();
489     }
490
491     public String JavaDoc getResponseContentType() {
492         return this.request.getResponseContentType();
493     }
494
495     public Enumeration JavaDoc getResponseContentTypes() {
496         return this.request.getResponseContentTypes();
497     }
498
499     public WindowState getWindowState() {
500         return this.request.getWindowState();
501     }
502
503     public boolean isPortletModeAllowed(PortletMode mode) {
504         return this.request.isPortletModeAllowed(mode);
505     }
506
507     public boolean isWindowStateAllowed(WindowState state) {
508         return this.request.isWindowStateAllowed(state);
509     }
510 }
511
Popular Tags