001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic;
050:
051: import javax.portlet.PortletException;
052: import javax.portlet.PortletRequestDispatcher;
053: import javax.portlet.RenderRequest;
054: import javax.portlet.RenderResponse;
055: import javax.servlet.RequestDispatcher;
056: import javax.servlet.ServletContext;
057: import javax.servlet.ServletException;
058: import javax.servlet.ServletInputStream;
059: import javax.servlet.ServletOutputStream;
060: import javax.servlet.http.Cookie;
061: import javax.servlet.http.HttpServletRequest;
062: import javax.servlet.http.HttpServletResponse;
063: import javax.servlet.http.HttpSession;
064: import java.io.BufferedReader;
065: import java.io.IOException;
066: import java.io.OutputStream;
067: import java.io.PrintWriter;
068: import java.io.UnsupportedEncodingException;
069: import java.security.Principal;
070: import java.util.Collections;
071: import java.util.Enumeration;
072: import java.util.HashMap;
073: import java.util.Locale;
074: import java.util.Map;
075: import java.util.logging.Logger;
076:
077: /**
078: * An adapter to a {@link javax.servlet.RequestDispatcher},
079: * based on a HttpServletRequest and HttpServletResponse stored
080: * as request attributes
081: */
082: public class HttpPortletRequestDispatcher implements
083: PortletRequestDispatcher {
084: static protected final Logger log = Logger
085: .getLogger(HttpPortletRequestDispatcher.class.getName());
086:
087: /**
088: * request attribute for the HttpServletRequest
089: */
090: final static public String HTTP_SERVLET_REQUEST = "com.caucho.portal.generic.HttpServletRequest";
091:
092: /**
093: * request attribute for the HttpServletResponse
094: */
095: final static public String HTTP_SERVLET_RESPONSE = "com.caucho.portal.generic.HttpServletResponse";
096:
097: private RequestDispatcher _dispatcher;
098: private Map<String, String[]> _parameterMap;
099:
100: private HttpPortletRequestWrapper _wrappedRequest = new HttpPortletRequestWrapper();
101:
102: private HttpPortletResponseWrapper _wrappedResponse = new HttpPortletResponseWrapper();
103:
104: public HttpPortletRequestDispatcher() {
105: }
106:
107: /**
108: * @return true if the servletContext has a dispatcher for the path
109: */
110: public boolean startWithPath(ServletContext servletContext,
111: String path) {
112: _dispatcher = servletContext.getRequestDispatcher(path);
113:
114: if (_dispatcher == null)
115: return false;
116:
117: int paramIndex = path.indexOf('?');
118:
119: if (paramIndex != -1) {
120: Map<String, String[]> parameterMap = new HashMap<String, String[]>();
121: path = HttpUtil.extractParameters(parameterMap, path,
122: paramIndex);
123: _parameterMap = parameterMap;
124: }
125:
126: return true;
127: }
128:
129: /**
130: * @return true if the servletContext has a dispatcher for the name
131: */
132: public boolean startWithName(ServletContext servletContext,
133: String name) {
134: _dispatcher = servletContext.getNamedDispatcher(name);
135:
136: return _dispatcher != null;
137: }
138:
139: public void finish() {
140: _dispatcher = null;
141: _parameterMap = null;
142: }
143:
144: public void include(RenderRequest renderRequest,
145: RenderResponse renderResponse) throws PortletException,
146: IOException {
147: HttpServletRequest httpRequest = (HttpServletRequest) renderRequest
148: .getAttribute(HTTP_SERVLET_REQUEST);
149:
150: if (httpRequest == null)
151: throw new PortletException(
152: "HttpServletRequest not found in request attribute "
153: + HTTP_SERVLET_REQUEST);
154:
155: HttpServletResponse httpResponse = (HttpServletResponse) renderRequest
156: .getAttribute(HTTP_SERVLET_RESPONSE);
157:
158: if (httpResponse == null)
159: throw new PortletException(
160: "HttpServletResponse not found in request attribute "
161: + HTTP_SERVLET_RESPONSE);
162:
163: _wrappedRequest
164: .start(httpRequest, renderRequest, _parameterMap);
165: _wrappedResponse.start(httpResponse, renderResponse);
166:
167: try {
168: _dispatcher.include(_wrappedRequest, _wrappedResponse);
169: } catch (ServletException ex) {
170: throw new PortletException(ex);
171: } finally {
172: _wrappedResponse.finish();
173: _wrappedRequest.finish();
174: }
175: }
176:
177: /**
178: * This can't extend HttpServletRequestWrapper because of some bizarre
179: * behaviour mandated by the Servlet spec.
180: */
181: static protected class HttpPortletRequestWrapper implements
182: HttpServletRequest {
183: private RenderRequest _renderRequest;
184: private HttpServletRequest _httpRequest;
185: private Map<String, String[]> _parameterMap;
186:
187: private Map<String, String[]> _fullParameterMap;
188:
189: public HttpPortletRequestWrapper() {
190: }
191:
192: public void start(HttpServletRequest httpRequest,
193: RenderRequest renderRequest,
194: Map<String, String[]> parameterMap)
195:
196: {
197: _httpRequest = httpRequest;
198: _renderRequest = renderRequest;
199: _parameterMap = parameterMap;
200: }
201:
202: public void finish() {
203: _renderRequest = null;
204: _httpRequest = null;
205: _parameterMap = null;
206: _fullParameterMap = null;
207: }
208:
209: public String getParameter(String name) {
210: String[] values = getParameterValues(name);
211:
212: return values == null || values.length == 0 ? null
213: : values[0];
214: }
215:
216: public String[] getParameterValues(String name) {
217: if (_fullParameterMap != null)
218: return _fullParameterMap.get(name);
219:
220: String[] values = null;
221:
222: if (_parameterMap != null)
223: values = _parameterMap.get(name);
224:
225: if (values == null)
226: values = _renderRequest.getParameterValues(name);
227:
228: return values;
229: }
230:
231: public Map getParameterMap() {
232: if (_fullParameterMap != null)
233: return _fullParameterMap;
234:
235: if (_parameterMap == null) {
236: _fullParameterMap = _renderRequest.getParameterMap();
237: } else {
238: _fullParameterMap = new HashMap<String, String[]>();
239: _fullParameterMap.putAll(_renderRequest
240: .getParameterMap());
241: _fullParameterMap.putAll(_parameterMap);
242: }
243:
244: return _fullParameterMap;
245: }
246:
247: public Enumeration getParameterNames() {
248: return Collections.enumeration(getParameterMap().keySet());
249: }
250:
251: public String getScheme() {
252: return _renderRequest.getScheme();
253: }
254:
255: public String getServerName() {
256: return _renderRequest.getServerName();
257: }
258:
259: public int getServerPort() {
260: return _renderRequest.getServerPort();
261: }
262:
263: public Object getAttribute(String name) {
264: return _renderRequest.getAttribute(name);
265: }
266:
267: public void setAttribute(String name, Object o) {
268: _renderRequest.setAttribute(name, o);
269: }
270:
271: public Enumeration getAttributeNames() {
272: return _renderRequest.getAttributeNames();
273: }
274:
275: public void removeAttribute(String name) {
276: _renderRequest.removeAttribute(name);
277: }
278:
279: public Locale getLocale() {
280: return _renderRequest.getLocale();
281: }
282:
283: public Enumeration getLocales() {
284: return _renderRequest.getLocales();
285: }
286:
287: public boolean isSecure() {
288: return _renderRequest.isSecure();
289: }
290:
291: public String getAuthType() {
292: return _renderRequest.getAuthType();
293: }
294:
295: public String getRequestedSessionId() {
296: return _renderRequest.getRequestedSessionId();
297: }
298:
299: public boolean isRequestedSessionIdValid() {
300: return _renderRequest.isRequestedSessionIdValid();
301: }
302:
303: public String getRemoteUser() {
304: return _renderRequest.getRemoteUser();
305: }
306:
307: public boolean isUserInRole(String role) {
308: return _renderRequest.isUserInRole(role);
309: }
310:
311: public Principal getUserPrincipal() {
312: return _renderRequest.getUserPrincipal();
313: }
314:
315: public String getHeader(String name) {
316: return _httpRequest.getHeader(name);
317: }
318:
319: public Enumeration getHeaders(String name) {
320: return _httpRequest.getHeaders(name);
321: }
322:
323: public Enumeration getHeaderNames() {
324: return _httpRequest.getHeaderNames();
325: }
326:
327: public int getIntHeader(String name) {
328: return _httpRequest.getIntHeader(name);
329: }
330:
331: public long getDateHeader(String name) {
332: return _httpRequest.getDateHeader(name);
333: }
334:
335: public Cookie[] getCookies() {
336: return _httpRequest.getCookies();
337: }
338:
339: public String getProtocol() {
340: return null;
341: }
342:
343: public String getRealPath(String uri) {
344: return null;
345: }
346:
347: public StringBuffer getRequestURL() {
348: return null;
349: }
350:
351: public String getRemoteAddr() {
352: return null;
353: }
354:
355: public String getRemoteHost() {
356: return null;
357: }
358:
359: public String getCharacterEncoding() {
360: return null;
361: }
362:
363: public void setCharacterEncoding(String encoding)
364: throws UnsupportedEncodingException {
365: }
366:
367: public ServletInputStream getInputStream() throws IOException {
368: return null;
369: }
370:
371: public BufferedReader getReader() throws IOException,
372: IllegalStateException {
373: return null;
374: }
375:
376: public int getContentLength() {
377: return 0;
378: }
379:
380: public String getMethod() {
381: return "GET";
382: }
383:
384: public int getRemotePort() {
385: return _httpRequest.getRemotePort();
386: }
387:
388: public String getLocalAddr() {
389: return _httpRequest.getLocalAddr();
390: }
391:
392: public String getLocalName() {
393: return _httpRequest.getLocalName();
394: }
395:
396: public int getLocalPort() {
397: return _httpRequest.getLocalPort();
398: }
399:
400: public String getContentType() {
401: return _httpRequest.getContentType();
402: }
403:
404: public RequestDispatcher getRequestDispatcher(String uri) {
405: return _httpRequest.getRequestDispatcher(uri);
406: }
407:
408: public String getRequestURI() {
409: return _httpRequest.getRequestURI();
410: }
411:
412: public String getContextPath() {
413: return _httpRequest.getContextPath();
414: }
415:
416: public String getServletPath() {
417: return _httpRequest.getServletPath();
418: }
419:
420: public String getPathInfo() {
421: return _httpRequest.getPathInfo();
422: }
423:
424: public String getPathTranslated() {
425: return _httpRequest.getPathTranslated();
426: }
427:
428: public String getQueryString() {
429: return _httpRequest.getQueryString();
430: }
431:
432: public HttpSession getSession(boolean create) {
433: return _httpRequest.getSession(create);
434: }
435:
436: public HttpSession getSession() {
437: return getSession(true);
438: }
439:
440: public boolean isRequestedSessionIdFromCookie() {
441: return _httpRequest.isRequestedSessionIdFromCookie();
442: }
443:
444: public boolean isRequestedSessionIdFromURL() {
445: return _httpRequest.isRequestedSessionIdFromURL();
446: }
447:
448: /**
449: * @deprecated
450: */
451: public boolean isRequestedSessionIdFromUrl() {
452: return _httpRequest.isRequestedSessionIdFromUrl();
453: }
454: }
455:
456: static protected class HttpPortletResponseWrapper implements
457: HttpServletResponse {
458: private HttpServletResponse _httpResponse;
459: private RenderResponse _renderResponse;
460: private ServletOutputStreamWrapper _servletOutputStream;
461:
462: public HttpPortletResponseWrapper() {
463: }
464:
465: public void start(HttpServletResponse httpResponse,
466: RenderResponse renderResponse)
467:
468: {
469: _httpResponse = httpResponse;
470: _renderResponse = renderResponse;
471: }
472:
473: public void finish() {
474: _servletOutputStream = null;
475: _renderResponse = null;
476: _httpResponse = null;
477: }
478:
479: public void setBufferSize(int size) {
480: _renderResponse.setBufferSize(size);
481: }
482:
483: public int getBufferSize() {
484: return _renderResponse.getBufferSize();
485: }
486:
487: public void flushBuffer() throws IOException {
488: _renderResponse.flushBuffer();
489: }
490:
491: public void resetBuffer() {
492: _renderResponse.resetBuffer();
493: }
494:
495: public void reset() {
496: _renderResponse.reset();
497: }
498:
499: public boolean isCommitted() {
500: return _renderResponse.isCommitted();
501: }
502:
503: public ServletOutputStream getOutputStream() throws IOException {
504: if (_servletOutputStream == null) {
505: OutputStream portletOutputStream = _renderResponse
506: .getPortletOutputStream();
507:
508: _servletOutputStream = new ServletOutputStreamWrapper(
509: portletOutputStream);
510: }
511:
512: return _servletOutputStream;
513: }
514:
515: public String getCharacterEncoding() {
516: return _renderResponse.getCharacterEncoding();
517: }
518:
519: public String getContentType() {
520: return _renderResponse.getContentType();
521: }
522:
523: public void setCharacterEncoding(String enc) {
524: }
525:
526: public PrintWriter getWriter() throws IOException {
527: return _renderResponse.getWriter();
528: }
529:
530: public String encodeURL(String path) {
531: return _renderResponse.encodeURL(path);
532: }
533:
534: public String encodeUrl(String path) {
535: return _renderResponse.encodeURL(path);
536: }
537:
538: public Locale getLocale() {
539: return _renderResponse.getLocale();
540: }
541:
542: public String encodeRedirectURL(String url) {
543: return null;
544: }
545:
546: public String encodeRedirectUrl(String url) {
547: return null;
548: }
549:
550: public void setContentType(String type) {
551: }
552:
553: public void setContentLength(int len) {
554: }
555:
556: public void setLocale(Locale locale) {
557: }
558:
559: public void addCookie(Cookie cookie) {
560: }
561:
562: public void sendError(int sc) throws IOException {
563: }
564:
565: public void sendError(int sc, String msg) throws IOException {
566: }
567:
568: public void sendRedirect(String location) throws IOException {
569: }
570:
571: public void setDateHeader(String name, long date) {
572: }
573:
574: public void addDateHeader(String name, long date) {
575: }
576:
577: public void setHeader(String name, String value) {
578: }
579:
580: public void addHeader(String name, String value) {
581: }
582:
583: public void setIntHeader(String name, int value) {
584: }
585:
586: public void addIntHeader(String name, int value) {
587: }
588:
589: public boolean containsHeader(String name) {
590: return false;
591: }
592:
593: public void setStatus(int sc) {
594: }
595:
596: public void setStatus(int sc, String msg) {
597: }
598: }
599:
600: static final protected class ServletOutputStreamWrapper extends
601: ServletOutputStream {
602: private OutputStream _out;
603:
604: public ServletOutputStreamWrapper(OutputStream out) {
605: _out = out;
606: }
607:
608: public void write(int b) throws IOException {
609: _out.write(b);
610: }
611:
612: public void write(byte b[], int off, int len)
613: throws IOException {
614: _out.write(b, off, len);
615: }
616:
617: public void write(byte b[]) throws IOException {
618: _out.write(b);
619: }
620:
621: public void flush() throws IOException {
622: _out.flush();
623: }
624:
625: public void close() throws IOException {
626: _out.close();
627: }
628: }
629: }
|