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.core.impl;
21  
22  import java.io.BufferedReader;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.Vector;
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;
38  import javax.servlet.http.HttpSession;
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 javax.servlet.http.HttpServletRequestWrapper
55  implements PortletRequest, InternalPortletRequest
56  {
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 servletRequest)
79      {
80          super(servletRequest);
81          this.portletWindow = portletWindow;
82  
83          provider = InformationProviderAccess.getDynamicProvider(_getHttpServletRequest());
84      }
85  
86      // javax.portlet.PortletRequest implementation ------------------------------------------------
87      public boolean isWindowStateAllowed(WindowState state)
88      {
89          return provider.isWindowStateAllowed(state);
90      }
91  
92      public boolean isPortletModeAllowed(PortletMode portletMode)
93      {
94          // check if portal supports portlet mode
95          boolean supported = provider.isPortletModeAllowed(portletMode);
96  
97          // check if portlet supports portlet mode as well
98          if (supported)
99          {
100             supported = PortletModeHelper.isPortletModeAllowedByPortlet(portletWindow, portletMode);
101         }
102 
103         return supported;
104     }
105     
106     public PortletMode getPortletMode()
107     {
108         return provider.getPortletMode(portletWindow);
109     }
110     
111     public WindowState getWindowState()
112     {
113         return provider.getWindowState(portletWindow);
114     }
115 
116     // needs to be implemented in each subclass
117     public abstract PortletPreferences getPreferences();
118     
119     public PortletSession getPortletSession()
120     {
121         return getPortletSession(true);
122     }
123     
124     public PortletSession getPortletSession(boolean create)
125     {
126         // check if the session was invalidated
127         javax.servlet.http.HttpSession httpSession = this._getHttpServletRequest().getSession(false);
128 
129         if ((portletSession != null) && (httpSession == null))
130         {
131         	portletSession = null;
132         }
133         else if (httpSession != null)
134         {
135         	create = true;
136         }
137 
138         if (create && portletSession == null)
139         {
140         	httpSession = this._getHttpServletRequest().getSession(create);
141         	if (httpSession != null)
142         	{
143         		portletSession = PortletObjectAccess.getPortletSession(portletWindow, httpSession);
144         	}
145         }
146 
147         return portletSession;
148     }
149     
150     public String getProperty(String name)
151     {
152         if (name == null)
153         {
154             throw new IllegalArgumentException("Attribute name == null");
155         }
156 
157         // get properties from request header
158         String prop = this._getHttpServletRequest().getHeader(name);
159         if (prop == null)
160         {
161             // get properties from PropertyManager
162             Map map = PropertyManager.getRequestProperties(portletWindow, this._getHttpServletRequest());
163             if (map != null)
164             {
165                 String[] properties = (String[]) map.get(name);
166                 if ((properties != null) && (properties.length > 0))
167                 {
168                     prop = properties[0];
169                 }
170             }
171         }
172 
173         return prop;
174     }
175 
176     public Enumeration getProperties(String name)
177     {
178         if (name == null)
179         {
180             throw new IllegalArgumentException("Property name == null");
181         }
182 
183         Set v = new HashSet();
184 
185         // get properties from request header
186         Enumeration props = this._getHttpServletRequest().getHeaders(name);
187         if (props != null)
188         {
189             while (props.hasMoreElements())
190             {
191                 v.add(props.nextElement());
192             }
193         }
194 
195         // get properties from PropertyManager
196         Map map = PropertyManager.getRequestProperties(portletWindow, this._getHttpServletRequest());
197         if (map != null)
198         {
199             String[] properties = (String[]) map.get(name);
200 
201             if (properties != null)
202             {
203                 // add properties to vector
204                 for (int i=0;i<properties.length;i++)
205                 {
206                     v.add(properties[i]);
207                 }
208             }
209         }
210 
211         return new Enumerator(v.iterator());
212     }
213         
214     public Enumeration getPropertyNames()
215     {
216         Set v = new HashSet();           
217 
218         // get properties from PropertyManager
219         Map map = PropertyManager.getRequestProperties(portletWindow, this._getHttpServletRequest());
220         if (map != null)
221         {
222             v.addAll(map.keySet());            
223         }
224 
225         // get properties from request header
226         Enumeration props =  this._getHttpServletRequest().getHeaderNames();
227         if (props != null)
228         {
229             while (props.hasMoreElements())
230             {
231                 v.add(props.nextElement());
232             }
233         }
234 
235         return new Enumerator(v.iterator());
236     }
237 
238     public PortalContext getPortalContext()
239     {
240         return PortletObjectAccess.getPortalContext();
241     }
242 
243     public String getAuthType()
244     {
245         return this._getHttpServletRequest().getAuthType();
246     }
247 
248     public String getContextPath()
249     {
250         return portletWindow.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition().getWebApplicationDefinition().getContextRoot();
251 
252         // we cannot use that because of a bug in tomcat
253         // return this._getHttpServletRequest().getContextPath();
254     }
255 
256     public String getRemoteUser()
257     {
258         return this._getHttpServletRequest().getRemoteUser();
259     }
260 
261     public java.security.Principal getUserPrincipal()
262     {
263         return this._getHttpServletRequest().getUserPrincipal();
264     }
265 
266     /***
267      * Determines whether a user is mapped to the specified
268      * role.  As specified in PLT-20-3, we must reference
269      * the &lt;security-role-ref&gt; mappings within the
270      * deployment descriptor. If no mapping is available,
271      * then, and only then, do we check use the actual role
272      * name specified against the web application deployment
273      * descriptor.
274      *
275      * @param roleName the name of the role
276      * @return true if it is determined the user has the given role.
277      *
278      */
279     public boolean isUserInRole(String roleName)
280     {
281         PortletEntity entity = portletWindow.getPortletEntity();
282         PortletDefinition def = entity.getPortletDefinition();
283         SecurityRoleRefSet set = def.getInitSecurityRoleRefSet();
284         SecurityRoleRef ref = set.get(roleName);
285 
286         String link = null;
287         if ( ref != null && ref.getRoleLink()!=null) {
288             link = ref.getRoleLink();
289         }
290         else {
291             link = roleName;
292         }
293 
294         return this._getHttpServletRequest().isUserInRole(link);
295     }
296 
297     public Object getAttribute(String name)
298     {
299         if (name == null)
300         {
301             throw new IllegalArgumentException("Attribute name == null");
302         }
303 
304         Object attribute = this._getHttpServletRequest().getAttribute(
305                                                                      NamespaceMapperAccess.getNamespaceMapper().encode(portletWindow.getId(),name)
306                                                                      );
307 
308         if (attribute==null && isNameReserved(name))
309         {
310             attribute = this._getHttpServletRequest().getAttribute(name);
311         }
312         return attribute;
313     }
314 
315     public Enumeration getAttributeNames()
316     {
317         Enumeration attributes = this._getHttpServletRequest().getAttributeNames();
318 
319         Vector portletAttributes = new Vector();
320 
321         while (attributes.hasMoreElements())
322         {
323             String attribute = (String)attributes.nextElement();
324 
325             String portletAttribute = NamespaceMapperAccess.getNamespaceMapper().decode(portletWindow.getId(),attribute);
326 
327             if (portletAttribute!=null)
328             { // it is in the portlet's namespace
329                 portletAttributes.add(portletAttribute);
330             }
331         }
332 
333         return portletAttributes.elements();
334     }
335 
336     public String getParameter(String name)
337     {
338         if (name == null)
339         {
340             throw new IllegalArgumentException("Parameter name == null");
341         }
342 
343         bodyAccessed = true;
344 
345         Map parameters = this._getHttpServletRequest().getParameterMap();
346         String[] values = (String[])parameters.get(name);
347         if (values != null)
348         {
349             return values[0];
350         }
351         return null;
352     }
353 
354     public java.util.Enumeration getParameterNames()
355     {
356         bodyAccessed = true;
357 
358         Map parameters = this._getHttpServletRequest().getParameterMap();
359         return Collections.enumeration(parameters.keySet());
360     }
361 
362     public String[] getParameterValues(String name)
363     {
364         if (name == null)
365         {
366             throw new IllegalArgumentException("Parameter name == null");
367         }
368 
369         bodyAccessed = true;
370 
371         String[] values = (String[])this._getHttpServletRequest().getParameterMap().get(name);
372         if (values != null)
373             values = StringUtils.copy(values);
374         return values;
375     }
376 
377     public Map getParameterMap()
378     {
379         bodyAccessed = true;
380         Map result = StringUtils.copyParameters(this._getHttpServletRequest().getParameterMap());
381         return result;
382     }
383 
384     public boolean isSecure()
385     {
386         return this._getHttpServletRequest().isSecure();
387     }
388 
389     public void setAttribute(String name, Object o)
390     {
391         if (name == null)
392         {
393             throw new IllegalArgumentException("Attribute name == null");
394         }
395 
396         if ( o == null)
397         {
398             this.removeAttribute(name);
399         }
400         else if (isNameReserved(name))
401         {
402             // Reserved names go directly in the underlying request
403             _getHttpServletRequest().setAttribute(name, o);
404         }
405         else
406         {
407             this._getHttpServletRequest().setAttribute(NamespaceMapperAccess.getNamespaceMapper().encode(portletWindow.getId(),name), o);
408         }
409     }
410 
411     public void removeAttribute(String name)
412     {
413         if (name == null)
414         {
415             throw new IllegalArgumentException("Attribute name == null");
416         }
417         if (isNameReserved(name))
418         {
419             // Reserved names go directly in the underlying request
420             _getHttpServletRequest().removeAttribute(name);
421         }
422         else
423         {
424 
425             this._getHttpServletRequest().
426               removeAttribute(NamespaceMapperAccess.getNamespaceMapper().encode(portletWindow.getId(), name));
427         }                                                  
428     }
429 
430     public String getRequestedSessionId()
431     {
432         return this._getHttpServletRequest().getRequestedSessionId();
433     }
434 
435     public boolean isRequestedSessionIdValid()
436     {
437         return this._getHttpServletRequest().isRequestedSessionIdValid();
438     }
439 
440     public String getResponseContentType()
441     {
442         // get the default response content type from the container
443         String responseContentType = provider.getResponseContentType();
444 
445         return responseContentType;
446     }
447     
448     public Enumeration getResponseContentTypes()
449     {
450         // get the default response content types from the container
451         Iterator responseContentTypes = provider.getResponseContentTypes();
452 
453         return new Enumerator(responseContentTypes);
454     }
455     
456     public java.util.Locale getLocale()
457     {
458         return this._getHttpServletRequest().getLocale();
459     }
460     
461     public Enumeration getLocales()
462     {
463         return this._getHttpServletRequest().getLocales();
464     }
465 
466     public String getScheme()
467     {
468         return this._getHttpServletRequest().getScheme();
469     }
470 
471     public String getServerName()
472     {
473         return this._getHttpServletRequest().getServerName();
474     }
475 
476     public int getServerPort()
477     {
478         return this._getHttpServletRequest().getServerPort();
479     }
480     // --------------------------------------------------------------------------------------------
481 
482     // org.apache.pluto.core.InternalPortletRequest implementation --------------------------------
483     public void lateInit(javax.servlet.http.HttpServletRequest webModuleServletRequest)
484     {
485         this.setRequest(webModuleServletRequest);
486     }
487 
488     public PortletWindow getInternalPortletWindow()
489     {
490         return portletWindow;
491     }
492 
493     public void setIncluded(boolean included)
494     {
495         this.included = included;
496     }
497 
498     public boolean isIncluded()
499     {
500         return included;
501     }
502     // --------------------------------------------------------------------------------------------
503 
504     // internal methods ---------------------------------------------------------------------------
505     private javax.servlet.http.HttpServletRequest _getHttpServletRequest()
506     {
507         return(javax.servlet.http.HttpServletRequest)super.getRequest();
508     }
509 
510     /***
511      * Is this attribute name a reserved name (by the J2EE spec)?. 
512      * Reserved names begin with "java." or "javax.". 
513      */
514     private boolean isNameReserved(String name)
515     {
516         return name.startsWith("java.") || name.startsWith("javax.");
517     }
518     // --------------------------------------------------------------------------------------------
519 
520     // additional methods
521     // javax.servlet.http.HttpServletRequestWrapper
522     public java.lang.String getCharacterEncoding()
523     {
524         return this._getHttpServletRequest().getCharacterEncoding();
525     }
526 
527     public java.lang.String getContentType()
528     {
529         if (included)
530         {
531             return null;
532         }
533         else
534         {
535             return this._getHttpServletRequest().getContentType();
536         }
537     }
538 
539     public int getContentLength()
540     {
541         if (included)
542         {
543             return 0;
544         }
545         else
546         {
547             return _getHttpServletRequest().getContentLength();
548         }
549     }
550     
551     public BufferedReader getReader() throws java.io.UnsupportedEncodingException,java.io.IOException
552     {
553         if (included)
554         {
555             return null;
556         }
557         else
558         {
559             // the super class will ensure that a IllegalStateException is thrown if getInputStream() was called earlier
560             BufferedReader reader = _getHttpServletRequest().getReader();
561 
562             bodyAccessed = true;
563 
564             return reader;
565         }
566     }
567 
568     public Cookie[] getCookies()
569     {
570         return this._getHttpServletRequest().getCookies();
571     }
572 
573     public long getDateHeader(String name)
574     {
575         return this._getHttpServletRequest().getDateHeader(name);
576     }
577 
578     public String getHeader(String name)
579     {
580         return this._getHttpServletRequest().getHeader(name);
581     }
582 
583     public Enumeration getHeaders(String name)
584     {
585         return this._getHttpServletRequest().getHeaders(name);
586     }
587 
588     public Enumeration getHeaderNames()
589     {
590         return this._getHttpServletRequest().getHeaderNames();
591     }
592 
593     public int getIntHeader(String name)
594     {
595         return this._getHttpServletRequest().getIntHeader(name);
596 
597     }
598 
599     public String getPathInfo()
600     {
601         String attr = (String)super.getAttribute("javax.servlet.include.path_info");
602         return(attr != null) ? attr
603                              : super.getPathInfo(); 
604     }
605 
606     public String getQueryString()
607     {
608         String attr = (String)super.getAttribute("javax.servlet.include.query_string");
609         return(attr != null) ? attr
610                              : super.getQueryString();
611     }
612 
613     public String getPathTranslated()
614     {
615         return null;
616     }
617 
618     public String getRequestURI()
619     {
620         String attr = (String)super.getAttribute("javax.servlet.include.request_uri");
621         return(attr != null) ? attr
622                              : super.getRequestURI();
623     }
624 
625     public StringBuffer getRequestURL()
626     {
627         return null;
628     }
629 
630     public String getServletPath()
631     {
632         String attr = (String)super.getAttribute("javax.servlet.include.servlet_path");
633         return(attr != null) ? attr
634                              : super.getServletPath();
635     }
636 
637     public HttpSession getSession(boolean create)
638     {
639         return this._getHttpServletRequest().getSession(true);
640     }
641 
642     public HttpSession getSession()
643     {
644         return this._getHttpServletRequest().getSession();
645     }
646 
647     public String getMethod()
648     {
649         // TBD
650         return this._getHttpServletRequest().getMethod();
651     }
652 
653     public boolean isRequestedSessionIdFromURL()
654     {
655         // TBD
656         return this._getHttpServletRequest().isRequestedSessionIdFromURL();
657     }
658 
659     public boolean isRequestedSessionIdFromUrl()
660     {
661         return this._getHttpServletRequest().isRequestedSessionIdFromUrl();
662     }
663 
664     public boolean isRequestedSessionIdFromCookie()
665     {
666         return this._getHttpServletRequest().isRequestedSessionIdFromCookie();
667     }
668 
669     public String getProtocol()
670     {
671         return null;
672     }
673 
674     public String getRemoteAddr()
675     {
676         return null;
677     }
678 
679     public String getRemoteHost()
680     {
681         return null;
682     }
683 
684     public String getRealPath(String path)
685     {
686         return null;
687     }
688 
689     public void setCharacterEncoding(String env) throws java.io.UnsupportedEncodingException
690     {
691         if (bodyAccessed)
692         {
693             throw new IllegalStateException("This method must not be called after the HTTP-Body was accessed !");
694         }
695 
696         this._getHttpServletRequest().setCharacterEncoding(env);
697         return; 
698     }
699 
700     public javax.servlet.ServletInputStream getInputStream() throws java.io.IOException
701     {
702         if (included)
703         {
704             return null;
705         }
706         else
707         {
708             // the super class will ensure that a IllegalStateException is thrown if getReader() was called earlier
709             javax.servlet.ServletInputStream stream = _getHttpServletRequest().getInputStream();
710 
711             bodyAccessed = true;
712 
713             return stream;
714         }
715     }
716 
717     public javax.servlet.RequestDispatcher getRequestDispatcher(String path)
718     {
719         return this._getHttpServletRequest().getRequestDispatcher(path);
720     }
721 }