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.ServletConfig;
    23  import javax.servlet.ServletContext;
    24  import javax.servlet.ServletException;
    25  import javax.servlet.ServletRequest;
    26  import javax.servlet.http.HttpServletRequest;
    27  import javax.servlet.http.HttpServletResponse;
    28
    29  import org.apache.commons.logging.Log;
    30  import org.apache.commons.logging.LogFactory;
    31  import org.apache.struts.Globals;
    32  import org.apache.struts.action.ActionMapping;
    33  import org.apache.struts.action.ActionServlet;
    34  import org.apache.struts.config.ModuleConfig;
    35  import org.apache.struts.config.PlugInConfig;
    36  import org.apache.struts.tiles.TilesPlugin;
    37  import org.apache.struts.util.RequestUtils;
    38
    39  /***
    40   * PortletServlet
    41   *
    42   * @author <a href="mailto:ate@douma.nu">Ate Douma </a>
    43   * @version $Id: PortletServlet.java 546262 2007-06-11 20:34:49Z ate $
    44   */
    45  public class PortletServlet extends ActionServlet
    46  {
    47      private static final Log log = LogFactory.getLog(PortletServlet.class);
    48
    49      public PortletServlet()
    50      {
    51          super();
    52      }
    53
    54      public void init(ServletConfig config) throws ServletException
    55      {
    56          super.init(new PortletServletConfigImpl(config));
    57      }
    58
    59      public ServletContext getServletContext()
    60      {
    61          return getServletConfig().getServletContext();
    62      }
    63
    64      protected void initModulePlugIns(ModuleConfig config)
    65      throws ServletException
    66      {
    67          boolean needTilesProcessor = false;
    68          PlugInConfig plugInConfigs[] = config.findPlugInConfigs();
    69          for ( int i = 0; !needTilesProcessor && i < plugInConfigs.length; i++ )
    70          {
    71              Class pluginClass = null;
    72              try
    73              {
    74                  pluginClass = RequestUtils.applicationClass(plugInConfigs[i].getClassName());
    75              }
    76              catch (ClassNotFoundException ex)
    77              {
    78                  log.fatal("Can't load Plugin class: bad class name '"+ plugInConfigs[i].getClassName()+ "'.");
    79                  throw new ServletException(ex);
    80              }
    81
    82              if (TilesPlugin.class.isAssignableFrom(pluginClass))
    83              {
    84                  needTilesProcessor = true;
    85              }
    86          }
    87
    88          String processorClassName = config.getControllerConfig().getProcessorClass();
    89          Class processorClass = null;
    90          try
    91          {
    92              processorClass = RequestUtils.applicationClass(processorClassName);
    93          }
    94          catch (ClassNotFoundException ex)
    95          {
    96              log.fatal("Can't load Plugin class: bad class name '"+ processorClass +"'.");
    97              throw new ServletException(ex);
    98          }
    99
    100         String newProcessorClassName = null;
    101
    102         if (needTilesProcessor && !PortletTilesRequestProcessor.class.isAssignableFrom(processorClass))
    103         {
    104             newProcessorClassName = PortletTilesRequestProcessor.class.getName();
    105         }
    106         else if (!needTilesProcessor && !PortletRequestProcessor.class.isAssignableFrom(processorClass))
    107         {
    108             newProcessorClassName = PortletRequestProcessor.class.getName();
    109         }
    110
    111         if (newProcessorClassName != null )
    112         {
    113             log.warn( "Replacing RequestProcessor " +
    114                     processorClassName +
    115                     " with " +
    116                     newProcessorClassName +
    117                     " for module: " +
    118                     (config.getPrefix().equals("") ? "default" : config.getPrefix()) + ".");
    119
    120             config.getControllerConfig().setProcessorClass(newProcessorClassName);
    121         }
    122         super.initModulePlugIns(config);
    123     }
    124
    125     public boolean performActionRenderRequest(HttpServletRequest request,
    126             HttpServletResponse response, ActionMapping mapping)
    127             throws IOException, ServletException
    128     {
    129         if (!request.getAttribute(StrutsPortlet.REQUEST_TYPE).equals(
    130                 StrutsPortlet.ACTION_REQUEST))
    131         {
    132             StrutsPortletRenderContext context = (StrutsPortletRenderContext)request.getAttribute(StrutsPortlet.RENDER_CONTEXT);
    133
    134             if (context != null)
    135             {
    136                 // only use the RENDER_CONTEXT once: if the target path is an Struts Action which might result in a subsequent
    137                 // dispatch, the normal Struts processing should happen, otherwise a infinite loop might occurr.
    138                 request.removeAttribute(StrutsPortlet.RENDER_CONTEXT);
    139
    140                 if (log.isDebugEnabled())
    141                 {
    142                     log.debug("render context path: " + context.getPath());
    143                 }
    144                 if (context.getActionForm() != null) {
    145                 	String attribute = mapping.getAttribute();
    146                 	if (attribute != null) {
    147                 	    if (log.isDebugEnabled())
    148                 	    {
    149                 	        log.debug("Putting form " + context.getActionForm().getClass().getName() +
    150                 	                " into request as " + attribute + " for mapping " + mapping.getName());
    151                 	    }
    152                     	request.setAttribute(mapping.getAttribute(), context
    153                                 .getActionForm());
    154                 	}
    155                 	else if (log.isWarnEnabled())
    156                 	{
    157                 	    log.warn("Attribute is null for form " + context.getActionForm().getClass().getName() +
    158                 	            ", won't put it into request for mapping " + mapping.getName());
    159                 	}
    160                 }
    161                 if (context.isRequestCancelled())
    162                     request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
    163                 if (context.getMessages() != null)
    164                     request.setAttribute(Globals.MESSAGE_KEY, context
    165                             .getMessages());
    166                 if (context.getErrors() != null)
    167                     request
    168                             .setAttribute(Globals.ERROR_KEY, context
    169                                     .getErrors());
    170                 RequestDispatcher dispatcher = null;
    171                 if (context.getDispatchNamed())
    172                     dispatcher = getServletContext().getNamedDispatcher(
    173                             context.getPath());
    174                 else
    175                     dispatcher = getServletContext().getRequestDispatcher(
    176                             context.getPath());
    177                 dispatcher.include(request, response);
    178                 return true;
    179             }
    180         }
    181         return false;
    182     }
    183
    184     public static boolean isPortletRequest(ServletRequest request)
    185     {
    186         return request.getAttribute("javax.portlet.request") != null;
    187     }
    188 }