001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portal.container;
021:
022: import java.io.Serializable;
023: import java.security.Principal;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.Locale;
027: import java.util.Map;
028:
029: import javax.portlet.PortalContext;
030: import javax.portlet.PortletContext;
031: import javax.portlet.PortletMode;
032: import javax.portlet.PortletModeException;
033: import javax.portlet.PortletPreferences;
034: import javax.portlet.PortletRequest;
035: import javax.portlet.PortletSecurityException;
036: import javax.portlet.PortletSession;
037: import javax.portlet.WindowState;
038: import javax.portlet.WindowStateException;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpSession;
041:
042: import com.nabhinc.util.ArrayEnumeration;
043: import com.nabhinc.util.IteratorEnumerator;
044:
045: /**
046: *
047: *
048: * @author Padmanabh Dabke
049: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
050: */
051: public class PortletRequestImpl implements PortletRequest, Serializable {
052: private static final long serialVersionUID = 8084547545540887933L;
053:
054: public static final String PORTLET_SESSION_ATTRIB = "com.nabhinc.portal.portlet_session";
055:
056: public static final HashMap EMPTY_PARAM_MAP = new HashMap();
057:
058: /**
059: * Portlet Context
060: */
061: protected PortletContext priPortletContext = null;
062:
063: /**
064: * Portal Context
065: */
066: protected PortalContext priPortalContext = null;
067:
068: protected String priContextPath = null;
069:
070: protected String[] priMimeTypes = null;
071: /**
072: * Locale for this request.
073: */
074: protected Locale[] priLocales = null;
075:
076: protected PortalInterface priPortalInterface = null;
077:
078: /**
079: * Portlet mode
080: */
081: protected PortletMode priPortletMode = PortletMode.VIEW;
082:
083: /**
084: * Window state
085: */
086: protected WindowState priWindowState = WindowState.NORMAL;
087:
088: /**
089: * Parameter map
090: */
091: protected Map<String, String[]> priParams = new HashMap<String, String[]>();
092:
093: /**
094: * Original servlet request
095: */
096: protected HttpServletRequest priServletRequest = null;
097:
098: /**
099: *
100: * @param req
101: * @param prefs
102: * @param pWindowID
103: * @param roleMapping
104: * @param portletContext
105: * @param portalContext
106: * @param l
107: * @param accessChecker
108: */
109: public PortletRequestImpl(HttpServletRequest req,
110: PortletContext portletContext, PortalContext portalContext,
111: Locale[] l, String mimeType) {
112: priServletRequest = req;
113: priPortletContext = portletContext;
114: priPortalContext = portalContext;
115: priLocales = l;
116: priMimeTypes = new String[1];
117: priMimeTypes[0] = mimeType;
118: priContextPath = req.getContextPath();
119: }
120:
121: public void setHttpServletRequest(HttpServletRequest req) {
122: priServletRequest = req;
123: // priContextPath = req.getContextPath();
124: }
125:
126: /* (non-Javadoc)
127: * @see javax.portlet.PortletRequest#isWindowStateAllowed(javax.portlet.WindowState)
128: */
129: public boolean isWindowStateAllowed(WindowState state) {
130: try {
131: // priPortalInterface.checkWindowState(state, priMimeTypes[0]);
132: return true;
133: } catch (Exception ex) {
134: return false;
135: }
136: }
137:
138: /* (non-Javadoc)
139: * @see javax.portlet.PortletRequest#isPortletModeAllowed(javax.portlet.PortletMode)
140: */
141: public boolean isPortletModeAllowed(PortletMode mode) {
142: try {
143: priPortalInterface.checkPortletMode(mode, priMimeTypes[0],
144: priServletRequest);
145: return true;
146: } catch (Exception ex) {
147: return false;
148: }
149: }
150:
151: /* (non-Javadoc)
152: * @see javax.portlet.PortletRequest#getPortletMode()
153: */
154: public PortletMode getPortletMode() {
155: return priPortletMode;
156: }
157:
158: public void setPortletMode(PortletMode newMode)
159: throws PortletModeException {
160: try {
161: priPortalInterface.checkPortletMode(newMode,
162: priMimeTypes[0], priServletRequest);
163: priPortletMode = newMode;
164: } catch (PortletSecurityException ex) {
165: throw new PortletModeException("Not permissioned.", newMode);
166: }
167: //priPortalInterface.setPortletMode(newMode);
168: }
169:
170: /* (non-Javadoc)
171: * @see javax.portlet.PortletRequest#getWindowState()
172: */
173: public WindowState getWindowState() {
174: return priWindowState;
175: }
176:
177: public void setWindowState(WindowState newWindowState)
178: throws WindowStateException {
179: // priPortalInterface.checkWindowState(newWindowState, priMimeTypes[0]);
180: priWindowState = newWindowState;
181: //priPortalInterface.setWindowState(newWindowState);
182: }
183:
184: /* (non-Javadoc)
185: * @see javax.portlet.PortletRequest#getPreferences()
186: */
187: public PortletPreferences getPreferences() {
188: return priPortalInterface
189: .getPreferences(this .priServletRequest);
190: }
191:
192: /* (non-Javadoc)
193: * @see javax.portlet.PortletRequest#getPortletSession()
194: */
195: public PortletSession getPortletSession() {
196: // return priPortletSession;
197: HttpSession session = priServletRequest.getSession();
198: if (session == null) {
199: return null;
200: } else {
201: PortletSessionImpl pSession = (PortletSessionImpl) session
202: .getAttribute(PORTLET_SESSION_ATTRIB);
203: String windowId = priPortalInterface.getPortletWindowId();
204: if (pSession == null) {
205: pSession = new PortletSessionImpl(windowId, session,
206: priPortletContext);
207: session.setAttribute(PORTLET_SESSION_ATTRIB, pSession);
208: } else {
209: pSession.setPortletWindowID(windowId);
210: pSession.setServletSession(session);
211: }
212: return pSession;
213: }
214: }
215:
216: /* (non-Javadoc)
217: * @see javax.portlet.PortletRequest#getPortletSession(boolean)
218: */
219: public PortletSession getPortletSession(boolean create) {
220:
221: HttpSession session = priServletRequest.getSession(create);
222: if (session == null) {
223: return null;
224: } else {
225: PortletSessionImpl pSession = (PortletSessionImpl) session
226: .getAttribute(PORTLET_SESSION_ATTRIB);
227: String windowId = priPortalInterface.getPortletWindowId();
228: if (pSession == null) {
229: pSession = new PortletSessionImpl(windowId, session,
230: priPortletContext);
231: session.setAttribute(PORTLET_SESSION_ATTRIB, pSession);
232: } else {
233: pSession.setPortletWindowID(windowId);
234: pSession.setServletSession(session);
235: }
236: return pSession;
237: }
238: }
239:
240: /* (non-Javadoc)
241: * @see javax.portlet.PortletRequest#getProperty(java.lang.String)
242: */
243: public String getProperty(String name) {
244: return priServletRequest.getHeader(name);
245: }
246:
247: /* (non-Javadoc)
248: * @see javax.portlet.PortletRequest#getProperties(java.lang.String)
249: */
250: public Enumeration getProperties(String name) {
251: return priServletRequest.getHeaders(name);
252: }
253:
254: /* (non-Javadoc)
255: * @see javax.portlet.PortletRequest#getPropertyNames()
256: */
257: public Enumeration getPropertyNames() {
258: return priServletRequest.getHeaderNames();
259: }
260:
261: /* (non-Javadoc)
262: * @see javax.portlet.PortletRequest#getPortalContext()
263: */
264: public PortalContext getPortalContext() {
265: return priPortalContext;
266: }
267:
268: /* (non-Javadoc)
269: * @see javax.portlet.PortletRequest#getAuthType()
270: */
271: public String getAuthType() {
272: return priServletRequest.getAuthType();
273: }
274:
275: /* (non-Javadoc)
276: * @see javax.portlet.PortletRequest#getContextPath()
277: */
278: public String getContextPath() {
279: // return priServletRequest.getContextPath();
280: return priContextPath;
281: }
282:
283: /* (non-Javadoc)
284: * @see javax.portlet.PortletRequest#getRemoteUser()
285: */
286: public String getRemoteUser() {
287: return priServletRequest.getRemoteUser();
288: }
289:
290: /* (non-Javadoc)
291: * @see javax.portlet.PortletRequest#getUserPrincipal()
292: */
293: public Principal getUserPrincipal() {
294: return priServletRequest.getUserPrincipal();
295: }
296:
297: /* (non-Javadoc)
298: * @see javax.portlet.PortletRequest#isUserInRole(java.lang.String)
299: */
300: public boolean isUserInRole(String role) {
301: // First check if there is a mapping between portlet roles
302: // and web application roles
303: String servletRole = priPortalInterface.getServletRole(role);
304: if (servletRole == null)
305: servletRole = role;
306: return priServletRequest.isUserInRole(servletRole);
307: }
308:
309: /* (non-Javadoc)
310: * @see javax.portlet.PortletRequest#getAttribute(java.lang.String)
311: */
312: public Object getAttribute(String name) {
313: return priServletRequest.getAttribute(name);
314: }
315:
316: /* (non-Javadoc)
317: * @see javax.portlet.PortletRequest#getAttributeNames()
318: */
319: public Enumeration getAttributeNames() {
320: return priServletRequest.getAttributeNames();
321: }
322:
323: /* (non-Javadoc)
324: * @see javax.portlet.PortletRequest#getParameter(java.lang.String)
325: */
326: public String getParameter(String name) {
327: String[] paramValues = (String[]) priParams.get(name);
328: if (paramValues == null)
329: return null;
330: else
331: return paramValues[0];
332: }
333:
334: /* (non-Javadoc)
335: * @see javax.portlet.PortletRequest#getParameterNames()
336: */
337: public Enumeration getParameterNames() {
338:
339: return new IteratorEnumerator(priParams.keySet().iterator());
340: }
341:
342: /* (non-Javadoc)
343: * @see javax.portlet.PortletRequest#getParameterValues(java.lang.String)
344: */
345: public String[] getParameterValues(String name) {
346: return (String[]) priParams.get(name);
347: }
348:
349: public Map getParameterMap() {
350: return priParams;
351: }
352:
353: /* (non-Javadoc)
354: * @see javax.portlet.PortletRequest#isSecure()
355: */
356: public boolean isSecure() {
357: return priServletRequest.isSecure();
358: }
359:
360: /* (non-Javadoc)
361: * @see javax.portlet.PortletRequest#setAttribute(java.lang.String, java.lang.Object)
362: */
363: public void setAttribute(String name, Object o) {
364: priServletRequest.setAttribute(name, o);
365:
366: }
367:
368: /* (non-Javadoc)
369: * @see javax.portlet.PortletRequest#removeAttribute(java.lang.String)
370: */
371: public void removeAttribute(String name) {
372: priServletRequest.removeAttribute(name);
373:
374: }
375:
376: /* (non-Javadoc)
377: * @see javax.portlet.PortletRequest#getRequestedSessionId()
378: */
379: public String getRequestedSessionId() {
380: return priServletRequest.getRequestedSessionId();
381: }
382:
383: /* (non-Javadoc)
384: * @see javax.portlet.PortletRequest#isRequestedSessionIdValid()
385: */
386: public boolean isRequestedSessionIdValid() {
387: return priServletRequest.isRequestedSessionIdValid();
388: }
389:
390: /* (non-Javadoc)
391: * @see javax.portlet.PortletRequest#getResponseContentType()
392: */
393: public String getResponseContentType() {
394: return priMimeTypes[0];
395: }
396:
397: /* (non-Javadoc)
398: * @see javax.portlet.PortletRequest#getResponseContentTypes()
399: */
400: public Enumeration getResponseContentTypes() {
401: return new ArrayEnumeration(priMimeTypes);
402: }
403:
404: /* (non-Javadoc)
405: * @see javax.portlet.PortletRequest#getLocale()
406: */
407: public Locale getLocale() {
408: return priLocales[0];
409: }
410:
411: /* (non-Javadoc)
412: * @see javax.portlet.PortletRequest#getLocales()
413: */
414: public Enumeration getLocales() {
415: // return priServletRequest.getLocales();
416: return new ArrayEnumeration(priLocales);
417: }
418:
419: /* (non-Javadoc)
420: * @see javax.portlet.PortletRequest#getScheme()
421: */
422: public String getScheme() {
423: return priServletRequest.getScheme();
424: }
425:
426: /* (non-Javadoc)
427: * @see javax.portlet.PortletRequest#getServerName()
428: */
429: public String getServerName() {
430: return priServletRequest.getServerName();
431: }
432:
433: /* (non-Javadoc)
434: * @see javax.portlet.PortletRequest#getServerPort()
435: */
436: public int getServerPort() {
437: return priServletRequest.getServerPort();
438: }
439:
440: public HttpServletRequest getHttpServletRequest() {
441: return priServletRequest;
442: }
443:
444: @SuppressWarnings("unchecked")
445: public void setParameter(String key, String[] value) {
446: priParams.put(key, value);
447: }
448:
449: protected void removeParameter(String key) {
450: priParams.remove(key);
451: }
452:
453: @SuppressWarnings("unchecked")
454: public void setParameter(String key, String value) {
455: priParams.put(key, new String[] { value });
456: }
457:
458: @SuppressWarnings("unchecked")
459: public void setParameters(Map paramMap) {
460: priParams = paramMap;
461:
462: }
463:
464: public void setPortletContext(PortletContextImpl pContext) {
465: priPortletContext = pContext;
466: }
467:
468: public void setContextPath(String contextPath) {
469: priContextPath = contextPath;
470: }
471:
472: public void setLocales(Locale[] locales) {
473: priLocales = locales;
474: }
475:
476: protected String getServletRole(String portletRole) {
477: return (String) priPortalInterface.getServletRole(portletRole);
478: }
479:
480: public PortalInterface getPortalInterface() {
481: return this .priPortalInterface;
482: }
483:
484: public void setPortalInterface(PortalInterface pi) {
485: this.priPortalInterface = pi;
486: }
487:
488: }
|