1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 510753 2007-02-23 01:28:50Z 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 = (StrutsPortletRenderContext)request.getAttribute(StrutsPortlet.RENDER_CONTEXT);
132
133 if (context != null)
134 {
135 if (log.isDebugEnabled())
136 {
137 log.debug("render context path: " + context.getPath());
138 }
139 if (context.getActionForm() != null) {
140 String attribute = mapping.getAttribute();
141 if (attribute != null) {
142 if (log.isDebugEnabled())
143 {
144 log.debug("Putting form " + context.getActionForm().getClass().getName() +
145 " into request as " + attribute + " for mapping " + mapping.getName());
146 }
147 request.setAttribute(mapping.getAttribute(), context
148 .getActionForm());
149 }
150 else if (log.isWarnEnabled())
151 {
152 log.warn("Attribute is null for form " + context.getActionForm().getClass().getName() +
153 ", won't put it into request for mapping " + mapping.getName());
154 }
155 }
156 if (context.isRequestCancelled())
157 request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
158 if (context.getMessages() != null)
159 request.setAttribute(Globals.MESSAGE_KEY, context
160 .getMessages());
161 if (context.getErrors() != null)
162 request
163 .setAttribute(Globals.ERROR_KEY, context
164 .getErrors());
165 RequestDispatcher dispatcher = null;
166 if (context.getDispatchNamed())
167 dispatcher = getServletContext().getNamedDispatcher(
168 context.getPath());
169 else
170 dispatcher = getServletContext().getRequestDispatcher(
171 context.getPath());
172 dispatcher.include(request, response);
173 return true;
174 }
175 }
176 return false;
177 }
178
179 public static boolean isPortletRequest(ServletRequest request)
180 {
181 return request.getAttribute("javax.portlet.request") != null;
182 }
183 }