View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.dispatcher;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.RequestDispatcher;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.portlet.PortletRequestDispatcher;
26  import javax.portlet.RenderResponse;
27  import javax.portlet.RenderRequest;
28  import javax.portlet.PortletException;
29  
30  import org.apache.jetspeed.container.PortletDispatcherIncludeAware;
31  import org.apache.pluto.core.impl.RenderRequestImpl;
32  import org.apache.pluto.core.impl.RenderResponseImpl;
33  
34  /***
35   * Implements the Portlet API Request Dispatcher to dispatch to portlets
36   *
37   * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
38   * @version $Id: JetspeedRequestDispatcher.java 516448 2007-03-09 16:25:47Z ate $
39   */
40  public class JetspeedRequestDispatcher implements PortletRequestDispatcher
41  {
42      private RequestDispatcher requestDispatcher;
43  
44      public JetspeedRequestDispatcher(RequestDispatcher requestDispatcher)
45      {
46      	if(requestDispatcher == null)
47      	{
48      		throw new IllegalArgumentException("RequestDispatcher cannot be null for JetspeedRequestDispatcher.");
49      	}
50          this.requestDispatcher = requestDispatcher;
51      }
52  
53      // portlet-only implementation
54  
55      public void include(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException
56      {
57          HttpServletRequest servletRequest = null;
58          HttpServletResponse servletResponse = null;
59          try
60          {
61              servletRequest = (HttpServletRequest) ((RenderRequestImpl) request).getRequest();
62              servletResponse = (HttpServletResponse) ((RenderResponseImpl) response).getResponse();
63              
64              if ( servletRequest instanceof PortletDispatcherIncludeAware )
65              {
66                  ((PortletDispatcherIncludeAware)servletRequest).setPortletDispatcherIncluded(true);
67              }
68              if ( servletResponse instanceof PortletDispatcherIncludeAware )
69              {
70                  ((PortletDispatcherIncludeAware)servletResponse).setPortletDispatcherIncluded(true);
71              }
72              
73              this.requestDispatcher.include(servletRequest, servletResponse);
74  
75          }
76          catch (RuntimeException re)
77          {
78              // PLT.16.3.4 cxlii: 
79              // RuntimeExceptions must be propagated back
80              throw re;
81          }
82          catch (IOException ioe)
83          {
84              // PLT.16.3.4 cxlii: 
85              // IOExceptions must be propagated back
86              throw ioe;
87          }
88          catch (Exception e)
89          {
90              // PLT.16.3.4 cxliii: 
91              // All other exceptions, including ServletExceptions must be wrapped in a PortletException 
92              // with the root cause set to the original exception before propagated back
93  
94              Throwable rootCause = null;
95              if ( e instanceof ServletException)
96              {
97                  rootCause = ((ServletException)e).getRootCause();
98              }
99              else
100             {
101                 rootCause = e.getCause();
102             }
103             throw new PortletException(rootCause != null ? rootCause : e);
104         }
105         finally
106         {
107             if ( servletRequest != null && servletRequest instanceof PortletDispatcherIncludeAware )
108             {
109                 ((PortletDispatcherIncludeAware)servletRequest).setPortletDispatcherIncluded(false);
110             }
111             if ( servletResponse != null && servletResponse instanceof PortletDispatcherIncludeAware )
112             {
113                 ((PortletDispatcherIncludeAware)servletResponse).setPortletDispatcherIncluded(false);
114             }
115             
116         }
117     }
118 }