001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.portlet;
018:
019: import org.apache.cocoon.environment.Cookie;
020: import org.apache.cocoon.environment.Request;
021: import org.apache.cocoon.environment.Session;
022: import org.apache.cocoon.portlet.multipart.MultipartActionRequest;
023:
024: import org.apache.avalon.framework.CascadingRuntimeException;
025:
026: import javax.portlet.PortalContext;
027: import javax.portlet.PortletMode;
028: import javax.portlet.PortletPreferences;
029: import javax.portlet.WindowState;
030:
031: import java.util.Collections;
032: import java.util.Enumeration;
033: import java.util.HashMap;
034: import java.util.Locale;
035: import java.util.Map;
036: import java.util.NoSuchElementException;
037: import java.util.Vector;
038:
039: /**
040: * Implements the {@link Request} interface for the JSR-168 (Portlet) environment.
041: *
042: * @author <a href="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
043: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
044: * @version $Id: PortletRequest.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public abstract class PortletRequest implements Request {
047:
048: /** Portlet request does not has servletPath, so it will be passed via constructor */
049: private String servletPath;
050:
051: /** Portlet request does not has pathInfo, so it will be passed via constructor */
052: private String pathInfo;
053:
054: /** The real PortletRequest object */
055: private final javax.portlet.PortletRequest request;
056:
057: /** The HttpEnvironment object */
058: private final PortletEnvironment environment;
059:
060: /** The character encoding of parameters */
061: private String form_encoding;
062:
063: /** The default form encoding of the servlet container */
064: private String container_encoding;
065:
066: /** The current session */
067: private PortletSession session;
068:
069: private Cookie[] wrappedCookies;
070: private Map wrappedCookieMap;
071: protected String portletRequestURI;
072:
073: /**
074: * Creates a PortletRequest based on a real PortletRequest object
075: */
076: protected PortletRequest(String servletPath, String pathInfo,
077: javax.portlet.PortletRequest request,
078: PortletEnvironment environment) {
079: super ();
080: this .servletPath = servletPath;
081: this .pathInfo = pathInfo;
082: this .request = request;
083: this .environment = environment;
084: }
085:
086: /* (non-Javadoc)
087: * @see org.apache.cocoon.environment.Request#get(java.lang.String)
088: */
089: public Object get(String name) {
090: // if the request has been wrapped then access its method
091: if (request instanceof MultipartActionRequest) {
092: return ((MultipartActionRequest) request).get(name);
093: } else {
094: String[] values = request.getParameterValues(name);
095: if (values == null) {
096: return null;
097: }
098: if (values.length == 1) {
099: return values[0];
100: }
101: if (values.length > 1) {
102: Vector vect = new Vector(values.length);
103: for (int i = 0; i < values.length; i++) {
104: vect.add(values[i]);
105: }
106: return vect;
107: }
108: return null;
109: }
110: }
111:
112: /* The Request interface methods */
113:
114: public String getAuthType() {
115: return this .request.getAuthType();
116: }
117:
118: private synchronized void wrapCookies() {
119: this .wrappedCookieMap = new HashMap();
120: PortletPreferences cookies = this .request.getPreferences();
121: if (cookies != null) {
122: this .wrappedCookies = new Cookie[cookies.getMap().size()];
123: int i = 0;
124: for (Enumeration e = cookies.getNames(); e
125: .hasMoreElements(); i++) {
126: String name = (String) e.nextElement();
127: PortletCookie cookie = new PortletCookie(name, cookies
128: .getValue(name, null));
129: this .wrappedCookies[i] = cookie;
130: this .wrappedCookieMap.put(cookie.getName(), cookie);
131: }
132: }
133: this .wrappedCookieMap = Collections
134: .unmodifiableMap(this .wrappedCookieMap);
135: }
136:
137: public Cookie[] getCookies() {
138: if (this .wrappedCookieMap == null) {
139: wrapCookies();
140: }
141: return this .wrappedCookies;
142: }
143:
144: public Map getCookieMap() {
145: if (this .wrappedCookieMap == null) {
146: wrapCookies();
147: }
148: return this .wrappedCookieMap;
149: }
150:
151: public long getDateHeader(String name) {
152: return Long.parseLong(this .request.getProperty(name));
153: }
154:
155: public String getHeader(String name) {
156: if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) {
157: return this .request.getPortletMode().toString();
158: } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) {
159: return this .request.getWindowState().toString();
160: } else {
161: return this .request.getProperty(name);
162: }
163: }
164:
165: public Enumeration getHeaders(String name) {
166: return this .request.getProperties(name);
167: }
168:
169: public Enumeration getHeaderNames() {
170: final Enumeration names = this .request.getPropertyNames();
171: // return this.request.getPropertyNames();
172: return new Enumeration() {
173: int position;
174:
175: public boolean hasMoreElements() {
176: return names.hasMoreElements() || position < 2;
177: }
178:
179: public Object nextElement() throws NoSuchElementException {
180: if (names.hasMoreElements()) {
181: return names.nextElement();
182: }
183:
184: if (position == 0) {
185: position++;
186: return PortletEnvironment.HEADER_PORTLET_MODE;
187: } else if (position == 1) {
188: position++;
189: return PortletEnvironment.HEADER_WINDOW_STATE;
190: } else {
191: throw new NoSuchElementException();
192: }
193: }
194: };
195: }
196:
197: /**
198: * Concrete request object will implement this
199: */
200: public abstract String getMethod();
201:
202: public String getPathInfo() {
203: return this .pathInfo;
204: }
205:
206: public String getPathTranslated() {
207: // TODO: getPathTranslated
208: return null;
209: }
210:
211: public String getContextPath() {
212: return this .request.getContextPath();
213: }
214:
215: public String getQueryString() {
216: // TODO: getQueryString
217: return "";
218: }
219:
220: public String getRemoteUser() {
221: return this .request.getRemoteUser();
222: }
223:
224: public boolean isUserInRole(String role) {
225: return this .request.isUserInRole(role);
226: }
227:
228: public java.security.Principal getUserPrincipal() {
229: return this .request.getUserPrincipal();
230: }
231:
232: public String getRequestedSessionId() {
233: return this .request.getRequestedSessionId();
234: }
235:
236: public String getRequestURI() {
237: if (this .portletRequestURI == null) {
238: final StringBuffer buffer = new StringBuffer();
239: buffer.append(this .request.getContextPath());
240:
241: if (getServletPath() != null) {
242: if (buffer.charAt(buffer.length() - 1) != '/') {
243: buffer.append('/');
244: }
245: buffer.append(getServletPath());
246: }
247:
248: if (getPathInfo() != null) {
249: if (buffer.charAt(buffer.length() - 1) != '/') {
250: buffer.append('/');
251: }
252: buffer.append(getPathInfo());
253: }
254:
255: this .portletRequestURI = buffer.toString();
256: }
257: return this .portletRequestURI;
258: }
259:
260: /* (non-Javadoc)
261: * @see org.apache.cocoon.environment.Request#getSitemapURI()
262: */
263: public String getSitemapURI() {
264: return this .environment.getURI();
265: }
266:
267: public String getSitemapURIPrefix() {
268: return this .environment.getURIPrefix();
269: }
270:
271: public String getServletPath() {
272: return this .servletPath;
273: }
274:
275: public Session getSession(boolean create) {
276: javax.portlet.PortletSession serverSession = this .request
277: .getPortletSession(create);
278: if (null != serverSession) {
279: if (null != this .session) {
280: if (this .session.session != serverSession) {
281: // update wrapper
282: this .session.session = serverSession;
283: }
284: } else {
285: // new wrapper
286: this .session = new PortletSession(serverSession,
287: this .environment.getDefaultSessionScope());
288: }
289: } else {
290: // invalidate
291: this .session = null;
292: }
293: return this .session;
294: }
295:
296: public Session getSession() {
297: return this .getSession(true);
298: }
299:
300: public boolean isRequestedSessionIdValid() {
301: return this .request.isRequestedSessionIdValid();
302: }
303:
304: /**
305: * Portlet does not know how portal manages session.
306: * This method returns false always.
307: */
308: public boolean isRequestedSessionIdFromCookie() {
309: return false;
310: }
311:
312: /**
313: * Portlet does not know how portal manages session.
314: * This method returns true always.
315: */
316: public boolean isRequestedSessionIdFromURL() {
317: return true;
318: }
319:
320: /* The ServletRequest interface methods */
321:
322: public Object getAttribute(String name) {
323: return this .request.getAttribute(name);
324: }
325:
326: public Enumeration getAttributeNames() {
327: return this .request.getAttributeNames();
328: }
329:
330: public String getCharacterEncoding() {
331: return this .form_encoding;
332: }
333:
334: public void setCharacterEncoding(String form_encoding)
335: throws java.io.UnsupportedEncodingException {
336: this .form_encoding = form_encoding;
337: }
338:
339: /**
340: * Sets the default encoding of the servlet container.
341: */
342: public void setContainerEncoding(String container_encoding) {
343: this .container_encoding = container_encoding;
344: }
345:
346: public int getContentLength() {
347: // TODO getContentLength
348: // return this.request.getContentLength();
349: return -1;
350: }
351:
352: public String getContentType() {
353: // TODO getContentType
354: // return this.request.getContentType();
355: return null;
356: }
357:
358: public String getParameter(String name) {
359: String value = this .request.getParameter(name);
360: if (this .form_encoding == null || value == null) {
361: return value;
362: }
363: return decode(value);
364: }
365:
366: private String decode(String str) {
367: if (str == null) {
368: return null;
369: }
370:
371: try {
372: if (this .container_encoding == null) {
373: this .container_encoding = "ISO-8859-1";
374: }
375: byte[] bytes = str.getBytes(this .container_encoding);
376: return new String(bytes, form_encoding);
377: } catch (java.io.UnsupportedEncodingException uee) {
378: throw new CascadingRuntimeException(
379: "Unsupported Encoding Exception", uee);
380: }
381: }
382:
383: public Enumeration getParameterNames() {
384: return this .request.getParameterNames();
385: }
386:
387: public String[] getParameterValues(String name) {
388: String[] values = this .request.getParameterValues(name);
389: if (values == null) {
390: return null;
391: } else if (this .form_encoding == null) {
392: return values;
393: }
394: String[] decoded_values = new String[values.length];
395: for (int i = 0; i < values.length; ++i) {
396: decoded_values[i] = decode(values[i]);
397: }
398: return decoded_values;
399: }
400:
401: public String getProtocol() {
402: return "JSR168";
403: }
404:
405: public String getScheme() {
406: return this .request.getScheme();
407: }
408:
409: public String getServerName() {
410: return this .request.getServerName();
411: }
412:
413: public int getServerPort() {
414: return this .request.getServerPort();
415: }
416:
417: public String getRemoteAddr() {
418: // TODO getRemoteAddr
419: // return this.request.getRemoteAddr();
420: return null;
421: }
422:
423: public String getRemoteHost() {
424: // TODO getRemoteHost
425: // return this.request.getRemoteHost();
426: return null;
427: }
428:
429: public void setAttribute(String name, Object o) {
430: this .request.setAttribute(name, o);
431: }
432:
433: public void removeAttribute(String name) {
434: this .request.removeAttribute(name);
435: }
436:
437: public Locale getLocale() {
438: return this .request.getLocale();
439: }
440:
441: public Enumeration getLocales() {
442: return this .request.getLocales();
443: }
444:
445: public boolean isSecure() {
446: return this .request.isSecure();
447: }
448:
449: /* The PortletRequest interface methods */
450:
451: /**
452: * Returns underlying portlet API request object
453: * @return portlet requesst object
454: */
455: public javax.portlet.PortletRequest getPortletRequest() {
456: return request;
457: }
458:
459: public Map getParameterMap() {
460: return this .request.getParameterMap();
461: }
462:
463: public PortalContext getPortalContext() {
464: return this .request.getPortalContext();
465: }
466:
467: public PortletMode getPortletMode() {
468: return this .request.getPortletMode();
469: }
470:
471: public javax.portlet.PortletSession getPortletSession() {
472: return this .request.getPortletSession();
473: }
474:
475: public javax.portlet.PortletSession getPortletSession(boolean create) {
476: return this .request.getPortletSession(create);
477: }
478:
479: public PortletPreferences getPreferences() {
480: return this .request.getPreferences();
481: }
482:
483: public Enumeration getProperties(String name) {
484: return this .request.getProperties(name);
485: }
486:
487: public String getProperty(String name) {
488: return this .request.getProperty(name);
489: }
490:
491: public Enumeration getPropertyNames() {
492: return this .request.getPropertyNames();
493: }
494:
495: public String getResponseContentType() {
496: return this .request.getResponseContentType();
497: }
498:
499: public Enumeration getResponseContentTypes() {
500: return this .request.getResponseContentTypes();
501: }
502:
503: public WindowState getWindowState() {
504: return this .request.getWindowState();
505: }
506:
507: public boolean isPortletModeAllowed(PortletMode mode) {
508: return this .request.isPortletModeAllowed(mode);
509: }
510:
511: public boolean isWindowStateAllowed(WindowState state) {
512: return this.request.isWindowStateAllowed(state);
513: }
514: }
|