KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > PortletRequestImp


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 27, 2003
11  * Time: 8:44:32 PM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
14
15
16 import javax.portlet.PortletRequest ;
17 import javax.portlet.PortletContext ;
18 import javax.portlet.PortalContext ;
19 import javax.portlet.PortletPreferences ;
20 import javax.portlet.PortletMode ;
21 import javax.portlet.WindowState ;
22 import javax.portlet.PortletSession ;
23 import javax.portlet.PortletConfig ;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import java.util.*;
28
29 import org.apache.commons.logging.Log;
30 import org.exoplatform.container.PortalContainer;
31 import org.exoplatform.services.log.LogUtil;
32 import org.exoplatform.services.portletcontainer.ExoPortletRequest;
33 import org.exoplatform.services.portletcontainer.helper.PortletWindowInternal;
34 import org.exoplatform.services.portletcontainer.impl.PortletApplicationProxy;
35 import org.exoplatform.services.portletcontainer.impl.PortletContainerConf;
36 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers.SharedSessionWrapper;
37 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.pool.EmptyRequest;
38 import org.exoplatform.services.portletcontainer.pci.*;
39 import org.exoplatform.services.portletcontainer.pci.model.*;
40
41
42 /**
43  * This implementation acts like a wrapper to the global ServletRequest
44  *
45  * This object should be pooled, therefore two fill and empty methods
46  * are provided.
47  *
48  */

49 public class PortletRequestImp extends HttpServletRequestWrapper JavaDoc
50     implements PortletRequest, ExoPortletRequest, Map {
51
52   private Collection roles_;
53   private PortletSessionImp session_;
54   private Portlet portletDatas_;
55   protected Input input_;
56   private PortletWindowInternal portletWindowInternal_;
57   private PortalContext portalContext_;
58   private PortletContext portletContext_;
59   private List securityContraints_;
60   private List userAttributes_;
61   private List customPortletModes_;
62   protected List customWindowStates_;
63   private Collection supportedContents_;
64   private Log log_;
65
66   /**
67    * This constructor is called by the pool so it gives a null
68    * reference of the httpServletRequest to wrap.
69    *
70    * When the object is retrieve from the pool or released
71    * it should call the fillPortletRequest() and emptyPortletRequest()
72    * methods
73    *
74    * @param httpServletRequest
75    */

76   public PortletRequestImp(HttpServletRequest JavaDoc httpServletRequest) {
77     super(httpServletRequest);
78     this.log_ = LogUtil.getLog("org.exoplatform.services.portletcontainer");
79   }
80
81   public void fillPortletRequest(HttpServletRequest JavaDoc httpServletRequest,
82                                                                     PortalContext portalContext,
83                                                                     PortletContext portletContext,
84                                                                     PortletSessionImp session,
85                                                                     Portlet portletDatas, Input input,
86                                                                     PortletWindowInternal portletWindowInternal,
87                                                                     List securityContraints, List userAttributes,
88                                                                     List customPortletModes,
89                                                                     List customWindowStates, Collection roles,
90                                                                     Collection supportedContents) {
91         super.setRequest(httpServletRequest);
92         this.portalContext_ = portalContext;
93         this.portletContext_ = portletContext;
94         this.session_ = session;
95         this.portletDatas_ = portletDatas;
96         this.input_ = input;
97         this.portletWindowInternal_ = portletWindowInternal;
98         this.securityContraints_ = securityContraints;
99         this.userAttributes_ = userAttributes;
100         this.customPortletModes_ = customPortletModes;
101         this.customWindowStates_ = customWindowStates;
102         this.roles_ = roles;
103         this.supportedContents_ = supportedContents;
104         init();
105     }
106
107   public void emptyPortletRequest() {
108     super.setRequest(new EmptyRequest());
109   }
110
111   public void init() {
112   }
113
114   public Input getInput() {
115     return input_;
116   }
117
118   public boolean isWindowStateAllowed(WindowState windowState) {
119     Enumeration e = portalContext_.getSupportedWindowStates();
120     while (e.hasMoreElements()) {
121       WindowState supportedWindowState = (WindowState) e.nextElement();
122       if (supportedWindowState.equals(windowState))
123         return true;
124     }
125     return false;
126   }
127
128   public boolean isPortletModeAllowed(PortletMode portletMode) {
129     Enumeration e = portalContext_.getSupportedPortletModes();
130     while (e.hasMoreElements()) {
131       PortletMode supportedPortletMode = (PortletMode) e.nextElement();
132       if (supportedPortletMode.toString().toLowerCase().equals(portletMode.toString().toLowerCase()))
133         return true;
134     }
135
136     return false;
137   }
138
139   public PortletConfig getPortletConfig(){
140     PortalContainer manager = PortalContainer.getInstance();
141     String JavaDoc portletAppName = portletWindowInternal_.getWindowID().getPortletApplicationName() ;
142     PortletApplicationProxy proxy =
143       (PortletApplicationProxy) manager.getComponentInstance(portletAppName);
144     return proxy.getPortletConfig(portletWindowInternal_.getWindowID().getPortletName());
145   }
146
147   public PortletMode getPortletMode() {
148     return input_.getPortletMode();
149   }
150
151   public WindowState getWindowState() {
152     return input_.getWindowState();
153   }
154
155   public PortletPreferences getPreferences() {
156     return portletWindowInternal_.getPreferences();
157   }
158
159   public PortletSession getPortletSession() {
160     return getPortletSession(true);
161   }
162
163   public PortletSession getPortletSession(boolean create) {
164     PortletContainerConf conf = (PortletContainerConf)PortalContainer.getInstance().
165         getComponentInstanceOfType(PortletContainerConf.class);
166     boolean isSharedSessionEnable = conf.isSharedSessionEnable();
167     if(create){
168       if(session_.isSessionValid()){
169         return session_;
170       }
171       if(isSharedSessionEnable){
172         ((SharedSessionWrapper)session_.getSession()).init();
173       } else{
174         session_.setSession(((HttpServletRequest JavaDoc)super.getRequest()).getSession());
175       }
176       return session_;
177     }
178     if(isSharedSessionEnable){
179         if(session_.isSessionValid()){
180             return session_;
181         }
182     } else{
183         HttpSession JavaDoc tmpSession = ((HttpServletRequest JavaDoc)super.getRequest()).getSession(false);
184         if(tmpSession == null)
185             return null;
186         //needed for Tomcat 5.0.16 (first stable version)
187
try{
188             tmpSession.getLastAccessedTime();
189         } catch (IllegalStateException JavaDoc e){
190             log_.error("IllegalStateExcetion sent in PortletRequestImp getPortletSession()", e);
191             return null;
192         }
193         return session_;
194     }
195     return null;
196   }
197
198   public boolean isRequestedSessionIdValid() {
199         PortletContainerConf conf =
200       (PortletContainerConf) PortalContainer.getInstance().
201                              getComponentInstanceOfType(PortletContainerConf.class);
202         boolean isSharedSessionEnable = conf.isSharedSessionEnable();
203         if (isSharedSessionEnable) {
204             return this.session_.isSessionValid();
205         }
206         //needed for Tomcat 5.0.16 (first stable version)
207
try {
208             ((HttpServletRequest JavaDoc) super.getRequest()).getSession()
209                     .getLastAccessedTime();
210         } catch (IllegalStateException JavaDoc e) {
211             log_.error("IllegalStateExcetion sent in PortletRequestImp isRequestedSessionIdValid()", e);
212             return false;
213         }
214         return ((HttpServletRequest JavaDoc) super.getRequest()).isRequestedSessionIdValid();
215     }
216
217   public String JavaDoc getProperty(String JavaDoc s) {
218     String JavaDoc header = ((HttpServletRequest JavaDoc)super.getRequest()).getHeader(s);
219     if(header != null)
220       return header;
221     return portalContext_.getProperty(s);
222   }
223
224   public Enumeration getProperties(String JavaDoc s) {
225     Enumeration header = ((HttpServletRequest JavaDoc)super.getRequest()).getHeaders(s);
226     return header;
227   }
228
229   public Enumeration getPropertyNames() {
230     Enumeration headerNames = ((HttpServletRequest JavaDoc)super.getRequest()).getHeaderNames();
231     Enumeration portalPropertyNames = portalContext_.getPropertyNames();
232     Collection global = new ArrayList();
233     while (portalPropertyNames.hasMoreElements()) {
234       String JavaDoc s = (String JavaDoc) portalPropertyNames.nextElement();
235       global.add(s);
236     }
237     while (headerNames.hasMoreElements()) {
238       String JavaDoc s = (String JavaDoc) headerNames.nextElement();
239       global.add(s);
240     }
241     return Collections.enumeration(global);
242   }
243
244   public PortalContext getPortalContext() {
245     return portalContext_;
246   }
247
248   public String JavaDoc getResponseContentType() {
249     List l = portletDatas_.getSupports();
250     String JavaDoc markup = input_.getMarkup() ;
251     String JavaDoc inputPortletMode = input_.getPortletMode().toString() ;
252     for (int i = 0; i < l.size(); i++) {
253       Supports supportsType = (Supports) l.get(i);
254       String JavaDoc mimeType = supportsType.getMimeType();
255       if (mimeType.equals(markup)) {
256         List portletModes = supportsType.getPortletMode();
257         for (int modeIdx = 0; modeIdx < portletModes.size(); modeIdx++) {
258           String JavaDoc portletMode = (String JavaDoc) portletModes.get(modeIdx);
259           if(portletMode.equals(inputPortletMode)) return mimeType;
260         }
261       }
262     }
263     return "text/html";
264   }
265
266   public Enumeration getResponseContentTypes() {
267     List c = new ArrayList();
268     c.add(getResponseContentType());
269     String JavaDoc markup = input_.getMarkup() ;
270     String JavaDoc inputPortletMode = input_.getPortletMode().toString() ;
271     for (Iterator iter = supportedContents_.iterator(); iter.hasNext();) {
272       String JavaDoc supportedContent = (String JavaDoc) iter.next();
273       List l = portletDatas_.getSupports();
274       for (int i = 0; i < l.size(); i++) {
275         Supports supportsType = (Supports) l.get(i);
276         String JavaDoc mimeType = supportsType.getMimeType();
277         if(supportedContent.equals(mimeType) && !supportedContent.equals(markup)){
278           List portletModes = supportsType.getPortletMode();
279           for (Iterator iter2 = portletModes.iterator(); iter2.hasNext();) {
280             String JavaDoc portletMode = (String JavaDoc) iter2.next();
281             if(portletMode.equals(inputPortletMode)){
282               c.add(mimeType);
283               break;
284             }
285           }
286         }
287       }
288     }
289     return Collections.enumeration(c);
290   }
291
292   public boolean isUserInRole(String JavaDoc role){
293     List l = portletDatas_.getSecurityRoleRef();
294     for (Iterator iterator = l.iterator(); iterator.hasNext();) {
295       SecurityRoleRef securityRoleRef = (SecurityRoleRef) iterator.next();
296       if(securityRoleRef.getRoleName().equals(role)){
297         String JavaDoc roleLink = securityRoleRef.getRoleLink();
298         if(roleLink == null || "".equals(roleLink)){
299           if(isRoleDefinedInWebXML(role)) return super.isUserInRole(role);
300           return false;
301         }
302         if(isRoleDefinedInWebXML(roleLink)) return super.isUserInRole(roleLink);
303         return false;
304       }
305     }
306     return false;
307   }
308
309   private boolean isRoleDefinedInWebXML(String JavaDoc role){
310     for (Iterator iter = roles_.iterator(); iter.hasNext();) {
311       String JavaDoc roleDefined = (String JavaDoc) iter.next();
312       if(roleDefined.equals(role))
313         return true;
314     }
315     return false;
316   }
317
318   public Portlet getPortletDatas() {
319     return portletDatas_;
320   }
321
322   public PortletWindowInternal getPortletWindowInternal() {
323     return portletWindowInternal_;
324   }
325
326   public boolean needsSecurityContraints(String JavaDoc portletName) {
327     for (Iterator iterator = securityContraints_.iterator(); iterator.hasNext();) {
328       SecurityConstraint securityConstraint = (SecurityConstraint) iterator.next();
329       List l = securityConstraint.getPortletCollection().getPortletName();
330       for (Iterator iterator2 = l.iterator(); iterator2.hasNext();) {
331         String JavaDoc portletN = (String JavaDoc) iterator2.next();
332         if(portletN.equals(portletName)) return true;
333       }
334     }
335     return false;
336   }
337
338   public String JavaDoc getAuthType(){
339     String JavaDoc type = super.getAuthType();
340     if(HttpServletRequest.BASIC_AUTH.equals(type))
341       return PortletRequest.BASIC_AUTH;
342     else if (HttpServletRequest.DIGEST_AUTH.equals(type))
343       return PortletRequest.DIGEST_AUTH;
344     else if (HttpServletRequest.CLIENT_CERT_AUTH.equals(type))
345       return PortletRequest.CLIENT_CERT_AUTH;
346     else if (HttpServletRequest.FORM_AUTH.equals(type))
347       return PortletRequest.FORM_AUTH;
348     else
349       return type;
350   }
351
352   public Object JavaDoc getAttribute(String JavaDoc name) {
353     if (name == null) {
354       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null") ;
355     }
356     return super.getAttribute(name) ;
357   }
358
359   public void setAttribute(String JavaDoc name, Object JavaDoc value) {
360     if (name == null) {
361       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null") ;
362     }
363     //when the value is null, should have the same effect as removeAttribute (Spec)
364
if(value == null) {
365       super.removeAttribute(name) ;
366     } else {
367       super.setAttribute(name, value) ;
368     }
369   }
370
371   public void removeAttribute(String JavaDoc name) {
372     if (name == null) {
373       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null") ;
374     }
375     super.removeAttribute(name) ;
376   }
377
378   public String JavaDoc getContextPath() {
379     return "/" + this.portletContext_.getPortletContextName() ;
380   }
381
382   // Bridge methods
383

384   public int size() {
385     int n = 0;
386     Enumeration keys = getAttributeNames();
387     while (keys.hasMoreElements()) {
388       //String key = (String) keys.nextElement();
389
n++;
390     }
391     return n;
392   }
393
394   public boolean isEmpty() {
395     return !getAttributeNames().hasMoreElements();
396   }
397
398   public boolean containsKey( Object JavaDoc key ) {
399     return (getAttribute( (String JavaDoc) key ) != null);
400   }
401
402   public boolean containsValue( Object JavaDoc value ) {
403     boolean match = false;
404     Enumeration keys = getAttributeNames();
405     while (keys.hasMoreElements()) {
406       String JavaDoc key = (String JavaDoc) keys.nextElement();
407       Object JavaDoc val = getAttribute( key );
408       if (value.equals( val )) {
409         match = true;
410         break;
411       }
412     }
413     return match;
414   }
415
416   public Object JavaDoc get( Object JavaDoc key ) {
417     return getAttribute( (String JavaDoc) key );
418   }
419
420   public Object JavaDoc put( Object JavaDoc key, Object JavaDoc value ) {
421     Object JavaDoc result = null;
422     if (containsKey( key )) result = getAttribute( (String JavaDoc) key );
423     setAttribute( (String JavaDoc) key, value );
424     return result;
425   }
426
427   public Object JavaDoc remove( Object JavaDoc key ) {
428     Object JavaDoc result = null;
429     if (containsKey( key )) result = getAttribute( (String JavaDoc) key );
430     removeAttribute( (String JavaDoc) key );
431     return result;
432   }
433
434   public void putAll( Map t ) {
435     for (Iterator i = t.keySet().iterator(); i.hasNext();) {
436       String JavaDoc key = (String JavaDoc) i.next();
437       Object JavaDoc value = t.get( key );
438       put( key, value );
439     }
440   }
441
442   public void clear() {
443     throw new UnsupportedOperationException JavaDoc();
444   }
445
446   public Set keySet() {
447     throw new UnsupportedOperationException JavaDoc();
448   }
449
450   public Collection values() {
451     throw new UnsupportedOperationException JavaDoc();
452   }
453
454   public Set entrySet() {
455     throw new UnsupportedOperationException JavaDoc();
456   }
457   
458   public Locale getLocale(){
459     List locales = input_.getLocales();
460     if(locales != null && !locales.isEmpty()){
461       return (Locale) locales.iterator().next();
462     }
463     return super.getLocale();
464   }
465   
466   public Enumeration getLocales(){
467     List locales = input_.getLocales();
468     if(locales != null){
469       return Collections.enumeration(locales);
470     }
471     return super.getLocales();
472     
473   }
474 }
Popular Tags