View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.struts;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.RequestDispatcher;
21  import javax.servlet.ServletException;
22  import javax.servlet.ServletRequest;
23  import javax.servlet.ServletResponse;
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.struts.Globals;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionMessages;
31  import org.apache.struts.config.ActionConfig;
32  
33  /***
34   * PortletServletRequestDispatcher
35   * 
36   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
37   * @version $Id: PortletServletRequestDispatcher.java 510753 2007-02-23 01:28:50Z ate $
38   */
39  public class PortletServletRequestDispatcher implements RequestDispatcher
40  {
41      private static final Log log = LogFactory.getLog(PortletServletRequestDispatcher.class);
42      private RequestDispatcher dispatcher;
43      private String path;
44      private boolean named;
45      
46      public PortletServletRequestDispatcher(RequestDispatcher dispatcher,
47              String path, boolean named)
48      {
49          this.dispatcher = dispatcher;
50          this.path = path;
51          this.named = named;
52      }
53      
54      private void invoke(ServletRequest request, ServletResponse response,
55              boolean include) throws ServletException, IOException
56      {
57          String request_type = (String) request
58                  .getAttribute(StrutsPortlet.REQUEST_TYPE);
59          if (request_type != null
60                  && request_type.equals(StrutsPortlet.ACTION_REQUEST))
61          {
62              if (log.isDebugEnabled())
63              {
64                  log.debug("saving " + (named ? "named " : " ")
65                          + "dispatch to :" + path + ", from "
66                          + request_type + " "
67                          + StrutsPortletURL.getPageURL(request));
68              }
69              HttpServletRequest req = (HttpServletRequest) request;
70              StrutsPortletRenderContext context = new StrutsPortletRenderContext();
71              context.setPath(path);
72              context.setDispatchNamed(named);
73              ActionConfig actionConfig = (ActionConfig) request
74                      .getAttribute(Globals.MAPPING_KEY);
75              if (actionConfig != null)
76              {
77                  if (actionConfig.getAttribute() != null
78                          && actionConfig.getScope().equals("request"))
79                  {
80                      ActionForm actionForm = (ActionForm) request
81                              .getAttribute(actionConfig.getAttribute());
82                      context.setActionForm(actionForm);
83                      Boolean requestCancelled = (Boolean) request
84                              .getAttribute(Globals.CANCEL_KEY);
85                      if (requestCancelled != null
86                              && requestCancelled.booleanValue())
87                          context.setRequestCancelled(true);
88                  }
89              }
90              context.setMessages((ActionMessages) request
91                      .getAttribute(Globals.MESSAGE_KEY));
92              context.setErrors((ActionMessages) request
93                      .getAttribute(Globals.ERROR_KEY));
94              if (context.getErrors() != null)
95              {
96                  String originURL = StrutsPortletURL.getOriginURL(request);
97                  if (originURL != null)
98                  {
99                      request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL,
100                             originURL);
101                 }
102             }
103             // save context in the request, StrutsPortlet will store it in the Portlet Session later on
104             req.setAttribute(StrutsPortlet.RENDER_CONTEXT, context);
105         } 
106         else
107         {
108             if (log.isDebugEnabled())
109             {
110                 log.debug("invoking " + (named ? "named " : " ")
111                         + " dispatch to :" + path + ", from "
112                         + request_type + " "
113                         + StrutsPortletURL.getPageURL(request));
114             }
115             dispatcher.include(request, response);
116         }
117     }
118 
119     public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
120         if ( PortletServlet.isPortletRequest(request) )
121         {
122             invoke(request, response, false);
123         }
124         else
125         {
126             dispatcher.forward(request,response);
127         }
128     }
129 
130     public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
131         if ( PortletServlet.isPortletRequest(request) )
132         {
133             invoke(request, response, true);
134         }
135         else
136         {
137             dispatcher.include(request,response);
138         }
139     }
140 
141     public String toString() {
142         return dispatcher.toString();
143     }
144 }