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.portals.bridges.struts;
    18
    19  import java.io.IOException;
    20
    21  import javax.servlet.RequestDispatcher;
    22  import javax.servlet.ServletException;
    23  import javax.servlet.ServletRequest;
    24  import javax.servlet.ServletResponse;
    25  import javax.servlet.http.HttpServletRequest;
    26
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.struts.Globals;
    30  import org.apache.struts.action.ActionForm;
    31  import org.apache.struts.action.ActionMessages;
    32  import org.apache.struts.config.ActionConfig;
    33
    34  /***
    35   * PortletServletRequestDispatcher
    36   *
    37   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
    38   * @version $Id: PortletServletRequestDispatcher.java 517068 2007-03-12 01:44:37Z ate $
    39   */
    40  public class PortletServletRequestDispatcher implements RequestDispatcher
    41  {
    42      private static final Log log = LogFactory.getLog(PortletServletRequestDispatcher.class);
    43      private RequestDispatcher dispatcher;
    44      private String path;
    45      private boolean named;
    46
    47      public PortletServletRequestDispatcher(RequestDispatcher dispatcher,
    48              String path, boolean named)
    49      {
    50          this.dispatcher = dispatcher;
    51          this.path = path;
    52          this.named = named;
    53      }
    54
    55      private void invoke(ServletRequest request, ServletResponse response,
    56              boolean include) throws ServletException, IOException
    57      {
    58          String request_type = (String) request
    59                  .getAttribute(StrutsPortlet.REQUEST_TYPE);
    60          if (request_type != null
    61                  && request_type.equals(StrutsPortlet.ACTION_REQUEST))
    62          {
    63              if (log.isDebugEnabled())
    64              {
    65                  log.debug("saving " + (named ? "named " : " ")
    66                          + "dispatch to :" + path + ", from "
    67                          + request_type + " "
    68                          + StrutsPortletURL.getPageURL(request));
    69              }
    70              HttpServletRequest req = (HttpServletRequest) request;
    71              StrutsPortletRenderContext context = new StrutsPortletRenderContext();
    72              context.setPath(path);
    73              context.setDispatchNamed(named);
    74              ActionConfig actionConfig = (ActionConfig) request
    75                      .getAttribute(Globals.MAPPING_KEY);
    76              if (actionConfig != null)
    77              {
    78                  if (actionConfig.getAttribute() != null
    79                          && actionConfig.getScope().equals("request"))
    80                  {
    81                      ActionForm actionForm = (ActionForm) request
    82                              .getAttribute(actionConfig.getAttribute());
    83                      context.setActionForm(actionForm);
    84                      Boolean requestCancelled = (Boolean) request
    85                              .getAttribute(Globals.CANCEL_KEY);
    86                      if (requestCancelled != null
    87                              && requestCancelled.booleanValue())
    88                          context.setRequestCancelled(true);
    89                  }
    90              }
    91              context.setMessages((ActionMessages) request
    92                      .getAttribute(Globals.MESSAGE_KEY));
    93              context.setErrors((ActionMessages) request
    94                      .getAttribute(Globals.ERROR_KEY));
    95              if (context.getErrors() != null)
    96              {
    97                  String originURL = StrutsPortletURL.getOriginURL(request);
    98                  if (originURL != null)
    99                  {
    100                     request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL,
    101                             originURL);
    102                 }
    103             }
    104             // save context in the request, StrutsPortlet will store it in the Portlet Session later on
    105             req.setAttribute(StrutsPortlet.RENDER_CONTEXT, context);
    106         }
    107         else
    108         {
    109             if (log.isDebugEnabled())
    110             {
    111                 log.debug("invoking " + (named ? "named " : " ")
    112                         + " dispatch to :" + path + ", from "
    113                         + request_type + " "
    114                         + StrutsPortletURL.getPageURL(request));
    115             }
    116             dispatcher.include(request, response);
    117         }
    118     }
    119
    120     public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    121         if ( PortletServlet.isPortletRequest(request) )
    122         {
    123             invoke(request, response, false);
    124         }
    125         else
    126         {
    127             dispatcher.forward(request,response);
    128         }
    129     }
    130
    131     public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    132         if ( PortletServlet.isPortletRequest(request) )
    133         {
    134             invoke(request, response, true);
    135         }
    136         else
    137         {
    138             dispatcher.include(request,response);
    139         }
    140     }
    141
    142     public String toString() {
    143         return dispatcher.toString();
    144     }
    145 }