KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > HttpPortletRequestDispatcher


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49
50 package com.caucho.portal.generic;
51
52 import javax.portlet.PortletException;
53 import javax.portlet.PortletRequestDispatcher;
54 import javax.portlet.RenderRequest;
55 import javax.portlet.RenderResponse;
56 import javax.servlet.RequestDispatcher JavaDoc;
57 import javax.servlet.ServletContext JavaDoc;
58 import javax.servlet.ServletException JavaDoc;
59 import javax.servlet.ServletInputStream JavaDoc;
60 import javax.servlet.ServletOutputStream JavaDoc;
61 import javax.servlet.http.Cookie JavaDoc;
62 import javax.servlet.http.HttpServletRequest JavaDoc;
63 import javax.servlet.http.HttpServletResponse JavaDoc;
64 import javax.servlet.http.HttpSession JavaDoc;
65 import java.io.BufferedReader JavaDoc;
66 import java.io.IOException JavaDoc;
67 import java.io.OutputStream JavaDoc;
68 import java.io.PrintWriter JavaDoc;
69 import java.io.UnsupportedEncodingException JavaDoc;
70 import java.security.Principal JavaDoc;
71 import java.util.Collections JavaDoc;
72 import java.util.Enumeration JavaDoc;
73 import java.util.HashMap JavaDoc;
74 import java.util.Locale JavaDoc;
75 import java.util.Map JavaDoc;
76 import java.util.logging.Logger JavaDoc;
77
78 /**
79  * An adapter to a {@link javax.servlet.RequestDispatcher},
80  * based on a HttpServletRequest and HttpServletResponse stored
81  * as request attributes
82  */

83 public class HttpPortletRequestDispatcher
84   implements PortletRequestDispatcher
85 {
86   static protected final Logger JavaDoc log =
87     Logger.getLogger(HttpPortletRequestDispatcher.class.getName());
88
89   /**
90    * request attribute for the HttpServletRequest
91    */

92   final static public String JavaDoc HTTP_SERVLET_REQUEST
93     = "com.caucho.portal.generic.HttpServletRequest";
94
95   /**
96    * request attribute for the HttpServletResponse
97    */

98   final static public String JavaDoc HTTP_SERVLET_RESPONSE
99     = "com.caucho.portal.generic.HttpServletResponse";
100
101
102   private RequestDispatcher _dispatcher;
103   private Map JavaDoc<String JavaDoc, String JavaDoc[]> _parameterMap;
104
105   private HttpPortletRequestWrapper _wrappedRequest
106     = new HttpPortletRequestWrapper();
107
108   private HttpPortletResponseWrapper _wrappedResponse
109     = new HttpPortletResponseWrapper();
110
111   public HttpPortletRequestDispatcher()
112   {
113   }
114
115   /**
116    * @return true if the servletContext has a dispatcher for the path
117    */

118   public boolean startWithPath(ServletContext JavaDoc servletContext, String JavaDoc path)
119   {
120     _dispatcher = servletContext.getRequestDispatcher(path);
121
122     if (_dispatcher == null)
123       return false;
124
125     int paramIndex = path.indexOf('?');
126
127     if (paramIndex != -1) {
128       Map JavaDoc<String JavaDoc, String JavaDoc[]> parameterMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
129       path = HttpUtil.extractParameters(parameterMap, path, paramIndex);
130       _parameterMap = parameterMap;
131     }
132
133
134     return true;
135   }
136
137   /**
138    * @return true if the servletContext has a dispatcher for the name
139    */

140   public boolean startWithName(ServletContext JavaDoc servletContext, String JavaDoc name)
141   {
142     _dispatcher = servletContext.getNamedDispatcher(name);
143
144     return _dispatcher != null;
145   }
146
147   public void finish()
148   {
149     _dispatcher = null;
150     _parameterMap = null;
151   }
152
153   public void include(RenderRequest renderRequest,
154                       RenderResponse renderResponse)
155     throws PortletException, IOException JavaDoc
156   {
157     HttpServletRequest JavaDoc httpRequest =
158       (HttpServletRequest JavaDoc) renderRequest.getAttribute(HTTP_SERVLET_REQUEST);
159
160     if (httpRequest == null)
161       throw new PortletException(
162           "HttpServletRequest not found in request attribute "
163           + HTTP_SERVLET_REQUEST);
164
165     HttpServletResponse JavaDoc httpResponse =
166       (HttpServletResponse JavaDoc) renderRequest.getAttribute(HTTP_SERVLET_RESPONSE);
167
168     if (httpResponse == null)
169       throw new PortletException(
170           "HttpServletResponse not found in request attribute "
171           + HTTP_SERVLET_RESPONSE);
172
173     _wrappedRequest.start(httpRequest, renderRequest, _parameterMap);
174     _wrappedResponse.start(httpResponse, renderResponse);
175
176     try {
177       _dispatcher.include(_wrappedRequest, _wrappedResponse);
178     }
179     catch (ServletException JavaDoc ex) {
180       throw new PortletException(ex);
181     }
182     finally {
183       _wrappedResponse.finish();
184       _wrappedRequest.finish();
185     }
186   }
187
188   /**
189    * This can't extend HttpServletRequestWrapper because of some bizarre
190    * behaviour mandated by the Servlet spec.
191    */

192   static protected class HttpPortletRequestWrapper
193       implements HttpServletRequest JavaDoc
194   {
195     private RenderRequest _renderRequest;
196     private HttpServletRequest JavaDoc _httpRequest;
197     private Map JavaDoc<String JavaDoc, String JavaDoc[]> _parameterMap;
198
199     private Map JavaDoc<String JavaDoc, String JavaDoc[]> _fullParameterMap;
200
201     public HttpPortletRequestWrapper()
202     {
203     }
204
205     public void start(HttpServletRequest JavaDoc httpRequest,
206                       RenderRequest renderRequest,
207                       Map JavaDoc<String JavaDoc, String JavaDoc[]> parameterMap)
208
209     {
210       _httpRequest = httpRequest;
211       _renderRequest = renderRequest;
212       _parameterMap = parameterMap;
213     }
214
215     public void finish()
216     {
217       _renderRequest = null;
218       _httpRequest = null;
219       _parameterMap = null;
220       _fullParameterMap = null;
221     }
222
223     public String JavaDoc getParameter(String JavaDoc name)
224     {
225       String JavaDoc[] values = getParameterValues(name);
226
227       return values == null || values.length == 0 ? null : values[0];
228     }
229
230     public String JavaDoc []getParameterValues(String JavaDoc name)
231     {
232       if (_fullParameterMap != null)
233         return _fullParameterMap.get(name);
234
235       String JavaDoc[] values = null;
236
237       if (_parameterMap != null)
238         values = _parameterMap.get(name);
239
240       if (values == null)
241         values = _renderRequest.getParameterValues(name);
242       
243       return values;
244     }
245
246     public Map JavaDoc getParameterMap()
247     {
248       if (_fullParameterMap != null)
249         return _fullParameterMap;
250
251       if (_parameterMap == null) {
252         _fullParameterMap = _renderRequest.getParameterMap();
253       }
254       else {
255         _fullParameterMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
256         _fullParameterMap.putAll(_renderRequest.getParameterMap());
257         _fullParameterMap.putAll(_parameterMap);
258       }
259
260       return _fullParameterMap;
261     }
262
263     public Enumeration JavaDoc getParameterNames()
264     {
265       return Collections.enumeration(getParameterMap().keySet());
266     }
267
268     public String JavaDoc getScheme()
269     {
270       return _renderRequest.getScheme();
271     }
272     public String JavaDoc getServerName()
273     {
274       return _renderRequest.getServerName();
275     }
276     public int getServerPort()
277     {
278       return _renderRequest.getServerPort();
279     }
280     public Object JavaDoc getAttribute(String JavaDoc name)
281     {
282       return _renderRequest.getAttribute(name);
283     }
284     public void setAttribute(String JavaDoc name, Object JavaDoc o)
285     {
286       _renderRequest.setAttribute(name, o);
287     }
288     public Enumeration JavaDoc getAttributeNames()
289     {
290       return _renderRequest.getAttributeNames();
291     }
292     public void removeAttribute(String JavaDoc name)
293     {
294       _renderRequest.removeAttribute(name);
295     }
296     public Locale JavaDoc getLocale()
297     {
298       return _renderRequest.getLocale();
299     }
300     public Enumeration JavaDoc getLocales()
301     {
302       return _renderRequest.getLocales();
303     }
304     public boolean isSecure()
305     {
306       return _renderRequest.isSecure();
307     }
308     public String JavaDoc getAuthType()
309     {
310       return _renderRequest.getAuthType();
311     }
312     public String JavaDoc getRequestedSessionId()
313     {
314       return _renderRequest.getRequestedSessionId();
315     }
316     public boolean isRequestedSessionIdValid()
317     {
318       return _renderRequest.isRequestedSessionIdValid();
319     }
320     public String JavaDoc getRemoteUser()
321     {
322       return _renderRequest.getRemoteUser();
323     }
324     public boolean isUserInRole(String JavaDoc role)
325     {
326       return _renderRequest.isUserInRole(role);
327     }
328     public Principal JavaDoc getUserPrincipal()
329     {
330       return _renderRequest.getUserPrincipal();
331     }
332
333     public String JavaDoc getHeader(String JavaDoc name)
334     {
335       return _httpRequest.getHeader(name);
336     }
337     public Enumeration JavaDoc getHeaders(String JavaDoc name)
338     {
339       return _httpRequest.getHeaders(name);
340     }
341     public Enumeration JavaDoc getHeaderNames()
342     {
343       return _httpRequest.getHeaderNames();
344     }
345     public int getIntHeader(String JavaDoc name)
346     {
347       return _httpRequest.getIntHeader(name);
348     }
349     public long getDateHeader(String JavaDoc name)
350     {
351       return _httpRequest.getDateHeader(name);
352     }
353     public Cookie JavaDoc []getCookies()
354     {
355       return _httpRequest.getCookies();
356     }
357
358     public String JavaDoc getProtocol()
359     {
360       return null;
361     }
362     public String JavaDoc getRealPath(String JavaDoc uri)
363     {
364       return null;
365     }
366     public StringBuffer JavaDoc getRequestURL()
367     {
368       return null;
369     }
370     public String JavaDoc getRemoteAddr()
371     {
372       return null;
373     }
374     public String JavaDoc getRemoteHost()
375     {
376       return null;
377     }
378     public String JavaDoc getCharacterEncoding()
379     {
380       return null;
381     }
382     public void setCharacterEncoding(String JavaDoc encoding)
383       throws UnsupportedEncodingException JavaDoc
384     {
385     }
386     public ServletInputStream JavaDoc getInputStream()
387       throws IOException JavaDoc
388     {
389       return null;
390     }
391     public BufferedReader JavaDoc getReader()
392       throws IOException JavaDoc, IllegalStateException JavaDoc
393     {
394       return null;
395     }
396     public int getContentLength()
397     {
398       return 0;
399     }
400
401     public String JavaDoc getMethod()
402     {
403       return "GET";
404     }
405
406     public int getRemotePort()
407     {
408       return _httpRequest.getRemotePort();
409     }
410     public String JavaDoc getLocalAddr()
411     {
412       return _httpRequest.getLocalAddr();
413     }
414     public String JavaDoc getLocalName()
415     {
416       return _httpRequest.getLocalName();
417     }
418     public int getLocalPort()
419     {
420       return _httpRequest.getLocalPort();
421     }
422     public String JavaDoc getContentType()
423     {
424       return _httpRequest.getContentType();
425     }
426     public RequestDispatcher getRequestDispatcher(String JavaDoc uri)
427     {
428       return _httpRequest.getRequestDispatcher(uri);
429     }
430     public String JavaDoc getRequestURI()
431     {
432       return _httpRequest.getRequestURI();
433     }
434     public String JavaDoc getContextPath()
435     {
436       return _httpRequest.getContextPath();
437     }
438     public String JavaDoc getServletPath()
439     {
440       return _httpRequest.getServletPath();
441     }
442     public String JavaDoc getPathInfo()
443     {
444       return _httpRequest.getPathInfo();
445     }
446     public String JavaDoc getPathTranslated()
447     {
448       return _httpRequest.getPathTranslated();
449     }
450     public String JavaDoc getQueryString()
451     {
452       return _httpRequest.getQueryString();
453     }
454     public HttpSession JavaDoc getSession(boolean create)
455     {
456       return _httpRequest.getSession(create);
457     }
458     public HttpSession JavaDoc getSession()
459     {
460       return getSession(true);
461     }
462     public boolean isRequestedSessionIdFromCookie()
463     {
464       return _httpRequest.isRequestedSessionIdFromCookie();
465     }
466     public boolean isRequestedSessionIdFromURL()
467     {
468       return _httpRequest.isRequestedSessionIdFromURL();
469     }
470     /**
471      * @deprecated
472      */

473     public boolean isRequestedSessionIdFromUrl()
474     {
475       return _httpRequest.isRequestedSessionIdFromUrl();
476     }
477   }
478
479   static protected class HttpPortletResponseWrapper
480       implements HttpServletResponse JavaDoc
481   {
482     private HttpServletResponse JavaDoc _httpResponse;
483     private RenderResponse _renderResponse;
484     private ServletOutputStreamWrapper _servletOutputStream;
485
486     public HttpPortletResponseWrapper()
487     {
488     }
489
490     public void start(HttpServletResponse JavaDoc httpResponse,
491                       RenderResponse renderResponse)
492
493     {
494       _httpResponse = httpResponse;
495       _renderResponse = renderResponse;
496     }
497
498     public void finish()
499     {
500       _servletOutputStream = null;
501       _renderResponse = null;
502       _httpResponse = null;
503     }
504
505     public void setBufferSize(int size)
506     {
507       _renderResponse.setBufferSize(size);
508     }
509
510     public int getBufferSize()
511     {
512       return _renderResponse.getBufferSize();
513     }
514
515     public void flushBuffer()
516       throws IOException JavaDoc
517     {
518       _renderResponse.flushBuffer();
519     }
520
521     public void resetBuffer()
522     {
523       _renderResponse.resetBuffer();
524     }
525
526     public void reset()
527     {
528       _renderResponse.reset();
529     }
530
531     public boolean isCommitted()
532     {
533       return _renderResponse.isCommitted();
534     }
535
536     public ServletOutputStream JavaDoc getOutputStream()
537       throws IOException JavaDoc
538     {
539       if (_servletOutputStream == null) {
540         OutputStream JavaDoc portletOutputStream
541           = _renderResponse.getPortletOutputStream();
542
543         _servletOutputStream
544           = new ServletOutputStreamWrapper(portletOutputStream);
545       }
546
547       return _servletOutputStream;
548     }
549
550     public String JavaDoc getCharacterEncoding()
551     {
552       return _renderResponse.getCharacterEncoding();
553     }
554
555     public String JavaDoc getContentType()
556     {
557       return _renderResponse.getContentType();
558     }
559     public void setCharacterEncoding(String JavaDoc enc)
560     {
561     }
562
563     public PrintWriter JavaDoc getWriter()
564       throws IOException JavaDoc
565     {
566       return _renderResponse.getWriter();
567     }
568
569     public String JavaDoc encodeURL(String JavaDoc path)
570     {
571       return _renderResponse.encodeURL(path);
572     }
573
574     public String JavaDoc encodeUrl(String JavaDoc path)
575     {
576       return _renderResponse.encodeURL(path);
577     }
578
579     public Locale JavaDoc getLocale()
580     {
581       return _renderResponse.getLocale();
582     }
583
584     public String JavaDoc encodeRedirectURL(String JavaDoc url)
585     {
586       return null;
587     }
588     public String JavaDoc encodeRedirectUrl(String JavaDoc url)
589     {
590       return null;
591     }
592     public void setContentType(String JavaDoc type)
593     {
594     }
595     public void setContentLength(int len)
596     {
597     }
598     public void setLocale(Locale JavaDoc locale)
599     {
600     }
601     public void addCookie(Cookie JavaDoc cookie)
602     {
603     }
604     public void sendError(int sc)
605       throws IOException JavaDoc
606     {
607     }
608     public void sendError(int sc, String JavaDoc msg)
609       throws IOException JavaDoc
610     {
611     }
612     public void sendRedirect(String JavaDoc location)
613       throws IOException JavaDoc
614     {
615     }
616     public void setDateHeader(String JavaDoc name, long date)
617     {
618     }
619     public void addDateHeader(String JavaDoc name, long date)
620     {
621     }
622     public void setHeader(String JavaDoc name, String JavaDoc value)
623     {
624     }
625     public void addHeader(String JavaDoc name, String JavaDoc value)
626     {
627     }
628     public void setIntHeader(String JavaDoc name, int value)
629     {
630     }
631     public void addIntHeader(String JavaDoc name, int value)
632     {
633     }
634     public boolean containsHeader(String JavaDoc name)
635     {
636       return false;
637     }
638     public void setStatus(int sc)
639     {
640     }
641     public void setStatus(int sc, String JavaDoc msg)
642     {
643     }
644   }
645
646   static final protected class ServletOutputStreamWrapper
647       extends ServletOutputStream JavaDoc
648   {
649     private OutputStream JavaDoc _out;
650
651     public ServletOutputStreamWrapper(OutputStream JavaDoc out)
652     {
653       _out = out;
654     }
655
656     public void write(int b)
657       throws IOException JavaDoc
658     {
659       _out.write(b);
660     }
661
662     public void write(byte b[], int off, int len)
663       throws IOException JavaDoc
664     {
665       _out.write(b, off, len);
666     }
667
668     public void write(byte b[])
669       throws IOException JavaDoc
670     {
671       _out.write(b);
672     }
673
674     public void flush()
675       throws IOException JavaDoc
676     {
677       _out.flush();
678     }
679
680     public void close()
681       throws IOException JavaDoc
682     {
683       _out.close();
684     }
685   }
686 }
687
688
689
Popular Tags