View Javadoc

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