KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > impl > PortletRequestImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.portlet.impl;
10
11 import java.security.Principal JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Vector JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import javax.portlet.PortalContext;
22 import javax.portlet.PortletMode;
23 import javax.portlet.PortletPreferences;
24 import javax.portlet.PortletRequest;
25 import javax.portlet.PortletSession;
26 import javax.portlet.WindowState;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.jboss.portal.common.util.Tools;
32 import org.jboss.portal.portlet.PortletApplication;
33 import org.jboss.portal.portlet.PortletContainer;
34 import org.jboss.portal.portlet.PortletUtils;
35 import org.jboss.portal.server.Component;
36 import org.jboss.portal.server.Instance;
37 import org.jboss.portal.server.InstanceContext;
38 import org.jboss.portal.server.PortalRequest;
39 import org.jboss.portal.server.PortalResponse;
40 import org.jboss.portal.server.Window;
41 import org.jboss.portal.server.WindowContext;
42 import org.jboss.portal.server.util.Properties;
43 import org.jboss.portal.server.plugins.mode.ContentTypes;
44 import org.jboss.portal.server.user.UserContext;
45
46 /**
47  * PortletRequest implemention. The parameter implementation is left
48  * to subclasses that can implement it differently.
49  *
50  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
51  * @version $Revision: 1.5 $
52  */

53 public abstract class PortletRequestImpl implements PortletRequest
54 {
55
56    protected static final Logger log = Logger.getLogger(PortletRequestImpl.class);
57
58    protected UserContext userContext;
59
60    protected Window window;
61    protected WindowContext windowCtx;
62    protected Instance instance;
63    protected InstanceContext instanceCtx;
64    protected Component component;
65
66    protected ContentTypes contentTypes;
67
68    protected PortalRequest req;
69    protected PortalResponse resp;
70
71    protected Map JavaDoc userInfo;
72
73    protected HttpServletRequest JavaDoc dreq;
74
75    protected PortalContext portalContext;
76    protected PortletSessionImpl psession;
77    protected PortletPreferences preferences;
78
79    protected int sessionStatus;
80
81    public PortletRequestImpl(
82          UserContext userContext,
83          PortletPreferences preferences,
84          Window window,
85          WindowContext windowCtx,
86          PortalRequest req,
87          PortalResponse resp,
88          HttpServletRequest JavaDoc dreq)
89    {
90       this.userContext = userContext;
91       this.window = window;
92       this.windowCtx = windowCtx;
93       this.instance = window.getInstance();
94       this.instanceCtx = (InstanceContext)userContext.getContext(instance);
95       this.component = instance.getComponent();
96       this.contentTypes = component.getContentTypes();
97       this.portalContext = new PortalContextImpl(window.getPortal());
98       this.req = req;
99       this.resp = resp;
100       this.dreq = dreq;
101       this.preferences = preferences;
102    }
103
104    // PLT.11.1.3
105

106    public Object JavaDoc getAttribute(String JavaDoc name)
107    {
108       if (name == null)
109       {
110          throw new IllegalArgumentException JavaDoc("name must not be null");
111       }
112       if (PortletRequest.USER_INFO.equals(name))
113       {
114          // todo implement that better
115
// Used to retrieve user information attributes with the getAttribute call.
116
// The user information is returned as a Map object. The portlet must define
117
// the user information attribute it is interested in inside the user-attribute
118
// section of the deployment descriptor. If an attribute is not supported
119
// by the current runtime system it will not show up in the user attribute map.
120
// If the user-attribute is supported by the runtime system, but not defined
121
// for a particular user, then for that user the attribute exists in the returned
122
// map and the attribute has a null value.
123
// If the user-attribute is not defined for the current user it will not show up in the Map.
124
Map JavaDoc infos = userContext.getInformations();
125          return infos != null ? Collections.unmodifiableMap(infos) : null;
126       }
127       else
128       {
129          return req.getAttribute(name);
130       }
131    }
132
133    public Enumeration JavaDoc getAttributeNames()
134    {
135       // todo this must include the USER_INFO stuff
136
// Copy the attribute names to avoid ConcurrentModificationException
137
// one test in the TCK getPortalObjectContext the Enumeration then dispatch the call to a
138
// servlet where it use the Enumeration and it throws a CME if we don't copy
139
ArrayList JavaDoc list = new ArrayList JavaDoc();
140       for (Enumeration JavaDoc e = req.getAttributeNames();e.hasMoreElements();)
141       {
142          list.add(e.nextElement());
143       }
144       return Tools.toEnumeration(list.iterator());
145    }
146
147    public void setAttribute(String JavaDoc name, Object JavaDoc value)
148    {
149       if (name == null)
150       {
151          throw new IllegalArgumentException JavaDoc("name must not be null");
152       }
153       if (!PortletRequest.USER_INFO.equals(name))
154       {
155          if (value != null)
156          {
157             req.setAttribute(name, value);
158          }
159          else
160          {
161             req.removeAttribute(name);
162          }
163       }
164    }
165
166    public void removeAttribute(String JavaDoc name)
167    {
168       if (name == null)
169       {
170          throw new IllegalArgumentException JavaDoc("name must not be null");
171       }
172       if (!PortletRequest.USER_INFO.equals(name))
173       {
174          req.removeAttribute(name);
175       }
176    }
177
178    // PLT.11.1.4
179

180    public String JavaDoc getProperty(String JavaDoc name)
181    {
182       // First try the request properties
183
Properties properties = req.getProperties();
184       String JavaDoc property = properties.getProperty(name);
185       if (property != null)
186       {
187          return property;
188       }
189
190       // Otherwise the request header
191
return req.getHeader(name);
192    }
193
194    public Enumeration JavaDoc getProperties(String JavaDoc name)
195    {
196       // First try the request properties
197
Properties properties = req.getProperties();
198       List JavaDoc list = properties.getProperties(name);
199       if (list.size() > 0)
200       {
201          return Tools.toEnumeration(list.iterator());
202       }
203
204       // Otherwise the request header
205
return req.getHeaders(name);
206    }
207
208    public Enumeration JavaDoc getPropertyNames()
209    {
210       // Merge request properties and headers
211
Properties properties = req.getProperties();
212       Set JavaDoc names = properties.getPropertyNames();
213       Set JavaDoc headers = Tools.toSet(req.getHeaderNames());
214       headers.addAll(names);
215       return Tools.toEnumeration(names.iterator());
216    }
217
218    // PLT.11.1.5
219

220    public String JavaDoc getContextPath()
221    {
222       // Fixme : should use the incoming request ?
223
return instance.getComponent().getApplication().getContextPath();
224    }
225
226    // PLT.11.1.6
227

228    public String JavaDoc getAuthType()
229    {
230       return req.getAuthType();
231    }
232
233    public String JavaDoc getRemoteUser()
234    {
235       return req.getRemoteUser();
236    }
237
238    public Principal JavaDoc getUserPrincipal()
239    {
240       return req.getUserPrincipal();
241    }
242
243    public boolean isUserInRole(String JavaDoc roleName)
244    {
245       // Get the map role name to role link
246
PortletContainer portletContainer = (PortletContainer)instance.getComponent();
247       Map JavaDoc securityRoleRefsMap = portletContainer.getSecurityRoleRefsMap();
248
249       // Process the role link
250
String JavaDoc roleLink = (String JavaDoc)securityRoleRefsMap.get(roleName);
251       if (roleLink == null)
252       {
253          if (securityRoleRefsMap.containsKey(roleName))
254          {
255             // The role name exist without a role link value
256
return req.isUserInRole(roleName);
257          }
258          else
259          {
260             // No role name is defined
261
return false;
262          }
263     }
264       else
265       {
266          // We have the role link value
267
return req.isUserInRole(roleLink);
268       }
269    }
270
271    public boolean isSecure()
272    {
273       return req.isSecure();
274    }
275
276    // PLT.11.1.7
277

278    public String JavaDoc getResponseContentType()
279    {
280       return resp.getContentType();
281    }
282
283    public Enumeration JavaDoc getResponseContentTypes()
284    {
285       Vector JavaDoc v = new Vector JavaDoc();
286       v.add(getResponseContentType());
287       return v.elements();
288    }
289
290    // PLT.11.1.8
291

292    public Locale JavaDoc getLocale()
293    {
294       return req.getLocale();
295    }
296
297    // PLT.11.1.9
298

299    public boolean isPortletModeAllowed(PortletMode portletMode)
300    {
301       if (portletMode == null)
302       {
303          // The spec does not give way to handle that case properly
304
log.warn("Try to test a null portlet mode");
305          return false;
306       }
307       else
308       {
309          String JavaDoc contentType = resp.getContentType();
310          return window.isSupportedMode(
311                contentType,
312                org.jboss.portal.server.plugins.mode.Mode.create(portletMode.toString()));
313       }
314    }
315
316    public PortletMode getPortletMode()
317    {
318       return PortletUtils.decodePortletMode(windowCtx.getMode().toString());
319    }
320
321    // PLT.11.1.10
322

323    public boolean isWindowStateAllowed(WindowState windowState)
324    {
325       return window.isSupportedWindowState(org.jboss.portal.server.plugins.windowstate.WindowState.create(windowState.toString()));
326    }
327
328    public WindowState getWindowState()
329    {
330       return PortletUtils.decodeWindowState(windowCtx.getWindowState().toString());
331    }
332
333    //
334

335    public PortletSession getPortletSession()
336    {
337       return getPortletSession(true);
338    }
339
340    public PortletSession getPortletSession(boolean create)
341    {
342       // We create it if the user ask for it
343
if (!create)
344       {
345          // Or if it does not exist
346
create = psession == null;
347          if (!create)
348          {
349             try
350             {
351                psession.getSession().isNew();
352             }
353             catch (IllegalStateException JavaDoc e)
354             {
355                // Or if it's not valid anymore
356
create = true;
357             }
358          }
359       }
360
361       if (create)
362       {
363          // Create the session with the dispatched request
364
HttpSession JavaDoc hsession = dreq.getSession(create);
365
366          // ID
367
String JavaDoc id = instance.getName() + "." + window.getName();
368
369          // Create portlet session
370
PortletContainer portletContainer = (PortletContainer)instance.getComponent();
371          PortletApplication application = (PortletApplication)portletContainer.getApplication();
372          psession = new PortletSessionImpl(
373                hsession,
374                id,
375                application.getPortletContext());
376       }
377       return psession;
378    }
379
380    public PortalContext getPortalContext()
381    {
382       return portalContext;
383    }
384
385    public String JavaDoc getRequestedSessionId()
386    {
387       return dreq.getRequestedSessionId();
388    }
389
390    public boolean isRequestedSessionIdValid()
391    {
392       return dreq.isRequestedSessionIdValid();
393    }
394
395    public Enumeration JavaDoc getLocales()
396    {
397       return req.getLocales();
398    }
399
400    public String JavaDoc getScheme()
401    {
402       return req.getScheme();
403    }
404
405    public String JavaDoc getServerName()
406    {
407       return req.getServerName();
408    }
409
410    public int getServerPort()
411    {
412       return req.getServerPort();
413    }
414
415    public PortletPreferences getPreferences()
416    {
417       return preferences;
418    }
419 }
420
Popular Tags