KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > PortletRequestImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 /*
17  *
18  */

19
20 package org.apache.pluto.core.impl;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import javax.portlet.PortalContext;
32 import javax.portlet.PortletMode;
33 import javax.portlet.PortletPreferences;
34 import javax.portlet.PortletRequest;
35 import javax.portlet.PortletSession;
36 import javax.portlet.WindowState;
37 import javax.servlet.http.Cookie JavaDoc;
38 import javax.servlet.http.HttpSession JavaDoc;
39
40 import org.apache.pluto.core.InternalPortletRequest;
41 import org.apache.pluto.factory.PortletObjectAccess;
42 import org.apache.pluto.om.common.SecurityRoleRef;
43 import org.apache.pluto.om.common.SecurityRoleRefSet;
44 import org.apache.pluto.om.entity.PortletEntity;
45 import org.apache.pluto.om.portlet.PortletDefinition;
46 import org.apache.pluto.om.window.PortletWindow;
47 import org.apache.pluto.services.information.DynamicInformationProvider;
48 import org.apache.pluto.services.information.InformationProviderAccess;
49 import org.apache.pluto.services.property.PropertyManager;
50 import org.apache.pluto.util.Enumerator;
51 import org.apache.pluto.util.NamespaceMapperAccess;
52 import org.apache.pluto.util.StringUtils;
53
54 public abstract class PortletRequestImpl extends
55         javax.servlet.http.HttpServletRequestWrapper JavaDoc implements PortletRequest,
56         InternalPortletRequest {
57
58     private PortletWindow portletWindow;
59
60     /**
61      * Holds the portlet session
62      */

63     private PortletSession portletSession;
64
65     private DynamicInformationProvider provider;
66
67     /**
68      * true if the HTTP-Body has been accessed
69      */

70     private boolean bodyAccessed;
71
72     /**
73      * true if we are in an include call
74      */

75     private boolean included;
76
77     public PortletRequestImpl(PortletWindow portletWindow,
78             javax.servlet.http.HttpServletRequest JavaDoc servletRequest) {
79         super(servletRequest);
80         this.portletWindow = portletWindow;
81
82         provider = InformationProviderAccess
83                 .getDynamicProvider(_getHttpServletRequest());
84     }
85
86     public boolean isWindowStateAllowed(WindowState state) {
87         return provider.isWindowStateAllowed(state);
88     }
89
90     public boolean isPortletModeAllowed(PortletMode portletMode) {
91         // check if portal supports portlet mode
92
boolean supported = provider.isPortletModeAllowed(portletMode);
93
94         // check if portlet supports portlet mode as well
95
if (supported) {
96             supported = PortletModeHelper.isPortletModeAllowedByPortlet(
97                     portletWindow, portletMode);
98         }
99
100         return supported;
101     }
102
103     public PortletMode getPortletMode() {
104         return provider.getPortletMode(portletWindow);
105     }
106
107     public WindowState getWindowState() {
108         return provider.getWindowState(portletWindow);
109     }
110
111     // needs to be implemented in each subclass
112
public abstract PortletPreferences getPreferences();
113
114     public PortletSession getPortletSession() {
115         return getPortletSession(true);
116     }
117
118     public PortletSession getPortletSession(boolean create) {
119         // check if the session was invalidated
120
javax.servlet.http.HttpSession JavaDoc httpSession = this
121                 ._getHttpServletRequest().getSession(false);
122
123         if ((portletSession != null) && (httpSession == null)) {
124             portletSession = null;
125         } else if (httpSession != null) {
126             create = true;
127         }
128
129         if (create && portletSession == null) {
130             httpSession = this._getHttpServletRequest().getSession(create);
131             if (httpSession != null) {
132                 portletSession = PortletObjectAccess.getPortletSession(
133                         portletWindow, httpSession);
134             }
135         }
136
137         return portletSession;
138     }
139
140     public String JavaDoc getProperty(String JavaDoc name) {
141         if (name == null) {
142             throw new IllegalArgumentException JavaDoc("Attribute name == null");
143         }
144
145         // get properties from request header
146
String JavaDoc prop = this._getHttpServletRequest().getHeader(name);
147         if (prop == null) {
148             // get properties from PropertyManager
149
Map JavaDoc map = PropertyManager.getRequestProperties(portletWindow, this
150                     ._getHttpServletRequest());
151             if (map != null) {
152                 String JavaDoc[] properties = (String JavaDoc[]) map.get(name);
153                 if ((properties != null) && (properties.length > 0)) {
154                     prop = properties[0];
155                 }
156             }
157         }
158
159         return prop;
160     }
161
162     public Enumeration JavaDoc getProperties(String JavaDoc name) {
163         if (name == null) {
164             throw new IllegalArgumentException JavaDoc("Property name == null");
165         }
166
167         Set JavaDoc v = new HashSet JavaDoc();
168
169         // get properties from request header
170
Enumeration JavaDoc props = this._getHttpServletRequest().getHeaders(name);
171         if (props != null) {
172             while (props.hasMoreElements()) {
173                 v.add(props.nextElement());
174             }
175         }
176
177         // get properties from PropertyManager
178
Map JavaDoc map = PropertyManager.getRequestProperties(portletWindow, this
179                 ._getHttpServletRequest());
180         if (map != null) {
181             String JavaDoc[] properties = (String JavaDoc[]) map.get(name);
182
183             if (properties != null) {
184                 // add properties to vector
185
for (int i = 0; i < properties.length; i++) {
186                     v.add(properties[i]);
187                 }
188             }
189         }
190
191         return new Enumerator(v.iterator());
192     }
193
194     public Enumeration JavaDoc getPropertyNames() {
195         Set JavaDoc v = new HashSet JavaDoc();
196
197         // get properties from PropertyManager
198
Map JavaDoc map = PropertyManager.getRequestProperties(portletWindow, this
199                 ._getHttpServletRequest());
200         if (map != null) {
201             v.addAll(map.keySet());
202         }
203
204         // get properties from request header
205
Enumeration JavaDoc props = this._getHttpServletRequest().getHeaderNames();
206         if (props != null) {
207             while (props.hasMoreElements()) {
208                 v.add(props.nextElement());
209             }
210         }
211
212         return new Enumerator(v.iterator());
213     }
214
215     public PortalContext getPortalContext() {
216         return PortletObjectAccess.getPortalContext();
217     }
218
219     public String JavaDoc getAuthType() {
220         return this._getHttpServletRequest().getAuthType();
221     }
222
223     public String JavaDoc getContextPath() {
224         return portletWindow.getPortletEntity().getPortletDefinition()
225                 .getPortletApplicationDefinition()
226                 .getWebApplicationDefinition().getContextRoot();
227
228         // we cannot use that because of a bug in tomcat
229
// return this._getHttpServletRequest().getContextPath();
230
}
231
232     public String JavaDoc getRemoteUser() {
233         return this._getHttpServletRequest().getRemoteUser();
234     }
235
236     public java.security.Principal JavaDoc getUserPrincipal() {
237         return this._getHttpServletRequest().getUserPrincipal();
238     }
239
240     /**
241      * Determines whether a user is mapped to the specified role. As specified
242      * in PLT-20-3, we must reference the &lt;security-role-ref&gt; mappings
243      * within the deployment descriptor. If no mapping is available, then, and
244      * only then, do we check use the actual role name specified against the web
245      * application deployment descriptor.
246      *
247      * @param roleName
248      * the name of the role
249      * @return true if it is determined the user has the given role.
250      *
251      */

252     public boolean isUserInRole(String JavaDoc roleName) {
253         PortletEntity entity = portletWindow.getPortletEntity();
254         PortletDefinition def = entity.getPortletDefinition();
255         SecurityRoleRefSet set = def.getInitSecurityRoleRefSet();
256         SecurityRoleRef ref = set.get(roleName);
257
258         String JavaDoc link = null;
259         if (ref != null && ref.getRoleLink() != null) {
260             link = ref.getRoleLink();
261         } else {
262             link = roleName;
263         }
264
265         return this._getHttpServletRequest().isUserInRole(link);
266     }
267
268     public Object JavaDoc getAttribute(String JavaDoc name) {
269         if (name == null) {
270             throw new IllegalArgumentException JavaDoc("Attribute name == null");
271         }
272
273         Object JavaDoc attribute = this._getHttpServletRequest().getAttribute(
274                 NamespaceMapperAccess.getNamespaceMapper().encode(
275                         portletWindow.getId(), name));
276
277         if (attribute == null) {
278             attribute = this._getHttpServletRequest().getAttribute(name);
279         }
280         return attribute;
281     }
282
283     public Enumeration JavaDoc getAttributeNames() {
284         Enumeration JavaDoc attributes = this._getHttpServletRequest()
285                 .getAttributeNames();
286
287         Vector JavaDoc portletAttributes = new Vector JavaDoc();
288
289         while (attributes.hasMoreElements()) {
290             String JavaDoc attribute = (String JavaDoc) attributes.nextElement();
291
292             String JavaDoc portletAttribute = NamespaceMapperAccess
293                     .getNamespaceMapper().decode(portletWindow.getId(),
294                             attribute);
295
296             if (portletAttribute != null) { // it is in the portlet's namespace
297
portletAttributes.add(portletAttribute);
298             }
299             else {
300                 portletAttributes.add(attribute);
301             }
302         }
303         return portletAttributes.elements();
304     }
305
306     public String JavaDoc getParameter(String JavaDoc name) {
307         if (name == null) {
308             throw new IllegalArgumentException JavaDoc("Parameter name == null");
309         }
310
311         bodyAccessed = true;
312
313         Map JavaDoc parameters = getParameterMap();
314         String JavaDoc[] values = (String JavaDoc[]) parameters.get(name);
315         if (values != null) {
316             return values[0];
317         }
318         return null;
319     }
320
321     public java.util.Enumeration JavaDoc getParameterNames() {
322         bodyAccessed = true;
323
324         Map JavaDoc parameters = getParameterMap();
325         return Collections.enumeration(parameters.keySet());
326     }
327
328     public String JavaDoc[] getParameterValues(String JavaDoc name) {
329         if (name == null) {
330             throw new IllegalArgumentException JavaDoc("Parameter name == null");
331         }
332
333         bodyAccessed = true;
334
335         String JavaDoc[] values = (String JavaDoc[]) getParameterMap().get(name);
336         if (values != null)
337             values = StringUtils.copy(values);
338         return values;
339     }
340
341     public Map JavaDoc getParameterMap() {
342         bodyAccessed = true;
343         Map JavaDoc result =
344             StringUtils.copyParameters(
345                 this._getHttpServletRequest().getParameterMap()
346             );
347         return result;
348     }
349
350     public boolean isSecure() {
351         return this._getHttpServletRequest().isSecure();
352     }
353
354     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
355         if (name == null) {
356             throw new IllegalArgumentException JavaDoc("Attribute name == null");
357         }
358
359         if (o == null) {
360             this.removeAttribute(name);
361         } else if (isNameReserved(name)) {
362             // Reserved names go directly in the underlying request
363
_getHttpServletRequest().setAttribute(name, o);
364         } else {
365             this._getHttpServletRequest().setAttribute(
366                     NamespaceMapperAccess.getNamespaceMapper().encode(
367                             portletWindow.getId(), name), o);
368         }
369     }
370
371     public void removeAttribute(String JavaDoc name) {
372         if (name == null) {
373             throw new IllegalArgumentException JavaDoc("Attribute name == null");
374         }
375         if (isNameReserved(name)) {
376             // Reserved names go directly in the underlying request
377
_getHttpServletRequest().removeAttribute(name);
378         } else {
379
380             this._getHttpServletRequest().removeAttribute(
381                     NamespaceMapperAccess.getNamespaceMapper().encode(
382                             portletWindow.getId(), name));
383         }
384     }
385
386     public String JavaDoc getRequestedSessionId() {
387         return this._getHttpServletRequest().getRequestedSessionId();
388     }
389
390     public boolean isRequestedSessionIdValid() {
391         return this._getHttpServletRequest().isRequestedSessionIdValid();
392     }
393
394     public String JavaDoc getResponseContentType() {
395         // get the default response content type from the container
396
String JavaDoc responseContentType = provider.getResponseContentType();
397
398         return responseContentType;
399     }
400
401     public Enumeration JavaDoc getResponseContentTypes() {
402         // get the default response content types from the container
403
Iterator JavaDoc responseContentTypes = provider.getResponseContentTypes();
404
405         return new Enumerator(responseContentTypes);
406     }
407
408     public java.util.Locale JavaDoc getLocale() {
409         return this._getHttpServletRequest().getLocale();
410     }
411
412     public Enumeration JavaDoc getLocales() {
413         return this._getHttpServletRequest().getLocales();
414     }
415
416     public String JavaDoc getScheme() {
417         return this._getHttpServletRequest().getScheme();
418     }
419
420     public String JavaDoc getServerName() {
421         return this._getHttpServletRequest().getServerName();
422     }
423
424     public int getServerPort() {
425         return this._getHttpServletRequest().getServerPort();
426     }
427
428     // --------------------------------------------------------------------------------------------
429

430     // org.apache.pluto.core.InternalPortletRequest implementation
431
// --------------------------------
432
public void lateInit(
433             javax.servlet.http.HttpServletRequest JavaDoc webModuleServletRequest) {
434         this.setRequest(webModuleServletRequest);
435     }
436
437     public PortletWindow getInternalPortletWindow() {
438         return portletWindow;
439     }
440
441     public void setIncluded(boolean included) {
442         this.included = included;
443     }
444
445     public boolean isIncluded() {
446         return included;
447     }
448
449     // --------------------------------------------------------------------------------------------
450

451     // internal methods
452
// ---------------------------------------------------------------------------
453
private javax.servlet.http.HttpServletRequest JavaDoc _getHttpServletRequest() {
454         return (javax.servlet.http.HttpServletRequest JavaDoc) super.getRequest();
455     }
456
457     /**
458      * Is this attribute name a reserved name (by the J2EE spec)?. Reserved
459      * names begin with "java." or "javax.".
460      */

461     private boolean isNameReserved(String JavaDoc name) {
462         return name.startsWith("java.") || name.startsWith("javax.");
463     }
464
465     // --------------------------------------------------------------------------------------------
466

467     // additional methods
468
// javax.servlet.http.HttpServletRequestWrapper
469
public java.lang.String JavaDoc getCharacterEncoding() {
470         return this._getHttpServletRequest().getCharacterEncoding();
471     }
472
473     public java.lang.String JavaDoc getContentType() {
474         if (included) {
475             return null;
476         } else {
477             return this._getHttpServletRequest().getContentType();
478         }
479     }
480
481     public int getContentLength() {
482         if (included) {
483             return 0;
484         } else {
485             return _getHttpServletRequest().getContentLength();
486         }
487     }
488
489     public BufferedReader JavaDoc getReader()
490             throws java.io.UnsupportedEncodingException JavaDoc, java.io.IOException JavaDoc {
491         if (included) {
492             return null;
493         } else {
494             // the super class will ensure that a IllegalStateException is
495
// thrown if getInputStream() was called earlier
496
BufferedReader JavaDoc reader = _getHttpServletRequest().getReader();
497
498             bodyAccessed = true;
499
500             return reader;
501         }
502     }
503
504     public Cookie JavaDoc[] getCookies() {
505         return this._getHttpServletRequest().getCookies();
506     }
507
508     public long getDateHeader(String JavaDoc name) {
509         return this._getHttpServletRequest().getDateHeader(name);
510     }
511
512     public String JavaDoc getHeader(String JavaDoc name) {
513         return this._getHttpServletRequest().getHeader(name);
514     }
515
516     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
517         return this._getHttpServletRequest().getHeaders(name);
518     }
519
520     public Enumeration JavaDoc getHeaderNames() {
521         return this._getHttpServletRequest().getHeaderNames();
522     }
523
524     public int getIntHeader(String JavaDoc name) {
525         return this._getHttpServletRequest().getIntHeader(name);
526
527     }
528
529     public String JavaDoc getPathInfo() {
530         String JavaDoc attr = (String JavaDoc) super
531                 .getAttribute("javax.servlet.include.path_info");
532         return (attr != null) ? attr : super.getPathInfo();
533     }
534
535     public String JavaDoc getQueryString() {
536         String JavaDoc attr = (String JavaDoc) super
537                 .getAttribute("javax.servlet.include.query_string");
538         return (attr != null) ? attr : super.getQueryString();
539     }
540
541     public String JavaDoc getPathTranslated() {
542         return null;
543     }
544
545     public String JavaDoc getRequestURI() {
546         String JavaDoc attr = (String JavaDoc) super
547                 .getAttribute("javax.servlet.include.request_uri");
548         return (attr != null) ? attr : super.getRequestURI();
549     }
550
551     public StringBuffer JavaDoc getRequestURL() {
552         return null;
553     }
554
555     public String JavaDoc getServletPath() {
556         String JavaDoc attr = (String JavaDoc) super
557                 .getAttribute("javax.servlet.include.servlet_path");
558         return (attr != null) ? attr : super.getServletPath();
559     }
560
561     public HttpSession JavaDoc getSession(boolean create) {
562         return this._getHttpServletRequest().getSession(true);
563     }
564
565     public HttpSession JavaDoc getSession() {
566         return this._getHttpServletRequest().getSession();
567     }
568
569     public String JavaDoc getMethod() {
570         // TBD
571
return this._getHttpServletRequest().getMethod();
572     }
573
574     public boolean isRequestedSessionIdFromURL() {
575         // TBD
576
return this._getHttpServletRequest().isRequestedSessionIdFromURL();
577     }
578
579     /**
580      * @deprecated use isRequestedSessionIdFromURL instead
581      */

582     public boolean isRequestedSessionIdFromUrl() {
583         return this._getHttpServletRequest().isRequestedSessionIdFromUrl();
584     }
585
586     public boolean isRequestedSessionIdFromCookie() {
587         return this._getHttpServletRequest().isRequestedSessionIdFromCookie();
588     }
589
590     public String JavaDoc getProtocol() {
591         return null;
592     }
593
594     public String JavaDoc getRemoteAddr() {
595         return null;
596     }
597
598     public String JavaDoc getRemoteHost() {
599         return null;
600     }
601
602     public String JavaDoc getRealPath(String JavaDoc path) {
603         return null;
604     }
605
606     public void setCharacterEncoding(String JavaDoc env)
607             throws java.io.UnsupportedEncodingException JavaDoc {
608         if (bodyAccessed) {
609             throw new IllegalStateException JavaDoc(
610                     "This method must not be called after the HTTP-Body was accessed !");
611         }
612
613         this._getHttpServletRequest().setCharacterEncoding(env);
614         return;
615     }
616
617     public javax.servlet.ServletInputStream JavaDoc getInputStream()
618             throws java.io.IOException JavaDoc {
619         if (included) {
620             return null;
621         } else {
622             // the super class will ensure that a IllegalStateException is
623
// thrown if getReader() was called earlier
624
javax.servlet.ServletInputStream JavaDoc stream = _getHttpServletRequest()
625                     .getInputStream();
626
627             bodyAccessed = true;
628
629             return stream;
630         }
631     }
632
633     public javax.servlet.RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
634         return this._getHttpServletRequest().getRequestDispatcher(path);
635     }
636 }
Popular Tags