1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.container.invoker; |
18 |
|
|
19 |
|
import java.io.IOException; |
20 |
|
|
21 |
|
import javax.portlet.ActionRequest; |
22 |
|
import javax.portlet.ActionResponse; |
23 |
|
import javax.portlet.PortletException; |
24 |
|
import javax.portlet.PortletRequest; |
25 |
|
import javax.portlet.PortletResponse; |
26 |
|
import javax.portlet.RenderRequest; |
27 |
|
import javax.portlet.RenderResponse; |
28 |
|
import javax.servlet.RequestDispatcher; |
29 |
|
import javax.servlet.ServletConfig; |
30 |
|
import javax.servlet.ServletContext; |
31 |
|
import javax.servlet.ServletRequest; |
32 |
|
import javax.servlet.ServletResponse; |
33 |
|
import javax.servlet.http.HttpServletRequest; |
34 |
|
|
35 |
|
import org.apache.commons.logging.Log; |
36 |
|
import org.apache.commons.logging.LogFactory; |
37 |
|
import org.apache.jetspeed.PortalReservedParameters; |
38 |
|
import org.apache.jetspeed.container.ContainerConstants; |
39 |
|
import org.apache.jetspeed.container.PortletRequestContext; |
40 |
|
import org.apache.jetspeed.factory.PortletFactory; |
41 |
|
import org.apache.jetspeed.factory.PortletInstance; |
42 |
|
import org.apache.jetspeed.om.common.portlet.MutablePortletApplication; |
43 |
|
import org.apache.jetspeed.request.RequestContext; |
44 |
|
import org.apache.jetspeed.aggregator.CurrentWorkerContext; |
45 |
|
import org.apache.pluto.om.portlet.PortletDefinition; |
46 |
|
import org.apache.pluto.om.servlet.WebApplicationDefinition; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public class ServletPortletInvoker implements JetspeedPortletInvoker |
69 |
|
{ |
70 |
0 |
private final static Log log = LogFactory.getLog(ServletPortletInvoker.class); |
71 |
|
|
72 |
|
protected PortletFactory portletFactory; |
73 |
|
protected ServletContext jetspeedContext; |
74 |
|
protected ServletConfig jetspeedConfig; |
75 |
|
protected PortletDefinition portletDefinition; |
76 |
0 |
protected boolean activated = false; |
77 |
|
protected String servletMappingName; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
protected PortletRequestResponseUnwrapper requestResponseUnwrapper; |
84 |
|
|
85 |
|
public ServletPortletInvoker() |
86 |
|
{ |
87 |
0 |
this(new DefaultPortletRequestResponseUnwrapper()); |
88 |
0 |
} |
89 |
|
|
90 |
|
public ServletPortletInvoker(PortletRequestResponseUnwrapper requestResponseUnwrapper) |
91 |
0 |
{ |
92 |
0 |
this.requestResponseUnwrapper = requestResponseUnwrapper; |
93 |
0 |
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
public void passivate() |
99 |
|
{ |
100 |
0 |
activated = false; |
101 |
0 |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public boolean isActivated() |
107 |
|
{ |
108 |
0 |
return activated; |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
public void activate(PortletFactory portletFactory, PortletDefinition portletDefinition, ServletConfig servletConfig) |
115 |
|
{ |
116 |
0 |
this.portletFactory = portletFactory; |
117 |
0 |
this.jetspeedConfig = servletConfig; |
118 |
0 |
jetspeedContext = servletConfig.getServletContext(); |
119 |
0 |
this.portletDefinition = portletDefinition; |
120 |
0 |
activated = true; |
121 |
0 |
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
public void activate(PortletFactory portletFactory, PortletDefinition portletDefinition, ServletConfig servletConfig, String servletMappingName) |
127 |
|
{ |
128 |
0 |
this.servletMappingName = servletMappingName; |
129 |
0 |
activate(portletFactory, portletDefinition, servletConfig); |
130 |
0 |
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException |
139 |
|
{ |
140 |
0 |
invoke(request, response, ContainerConstants.METHOD_RENDER); |
141 |
0 |
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
public void action(ActionRequest request, ActionResponse response) throws PortletException, IOException |
147 |
|
{ |
148 |
0 |
invoke(request, response, ContainerConstants.METHOD_ACTION); |
149 |
0 |
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public void load(PortletRequest request, RenderResponse response) throws PortletException |
155 |
|
{ |
156 |
|
try |
157 |
|
{ |
158 |
0 |
invoke(request, response, ContainerConstants.METHOD_NOOP); |
159 |
|
} |
160 |
0 |
catch (IOException e) |
161 |
|
{ |
162 |
0 |
log.error("ServletPortletInvokerImpl.load() - Error while dispatching portlet.", e); |
163 |
0 |
throw new PortletException(e); |
164 |
0 |
} |
165 |
0 |
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
protected void invoke(PortletRequest portletRequest, PortletResponse portletResponse, Integer methodID) |
179 |
|
throws PortletException, IOException |
180 |
|
{ |
181 |
|
|
182 |
|
|
183 |
0 |
PortletDefinition portletDefinition = null; |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
0 |
boolean isParallelMode = CurrentWorkerContext.getParallelRenderingMode(); |
188 |
|
|
189 |
0 |
if (isParallelMode) |
190 |
|
{ |
191 |
0 |
portletDefinition = (PortletDefinition) CurrentWorkerContext.getAttribute(PortalReservedParameters.PORTLET_DEFINITION_ATTRIBUTE); |
192 |
|
} |
193 |
|
|
194 |
0 |
if (portletDefinition == null) |
195 |
|
{ |
196 |
0 |
portletDefinition = this.portletDefinition; |
197 |
|
} |
198 |
|
|
199 |
0 |
MutablePortletApplication app = (MutablePortletApplication)portletDefinition.getPortletApplicationDefinition(); |
200 |
|
|
201 |
0 |
WebApplicationDefinition webApplicationDefinition = app.getWebApplicationDefinition(); |
202 |
0 |
if(webApplicationDefinition == null) |
203 |
|
{ |
204 |
0 |
throw new IllegalStateException("Portlet application "+app.getName()+ " has no associated web application."); |
205 |
|
} |
206 |
0 |
String portletApplicationName = webApplicationDefinition.getContextRoot(); |
207 |
|
|
208 |
0 |
ServletContext appContext = jetspeedContext.getContext(portletApplicationName); |
209 |
0 |
if (null == appContext) |
210 |
|
{ |
211 |
0 |
String message = "Failed to find Servlet context for Portlet Application: " + portletApplicationName; |
212 |
0 |
log.error(message); |
213 |
0 |
throw new PortletException(message); |
214 |
|
} |
215 |
0 |
PortletInstance portletInstance = portletFactory.getPortletInstance(appContext, portletDefinition); |
216 |
0 |
RequestDispatcher dispatcher = appContext.getRequestDispatcher(servletMappingName); |
217 |
0 |
if (null == dispatcher) |
218 |
|
{ |
219 |
0 |
String message = |
220 |
|
"Failed to get Request Dispatcher for Portlet Application: " |
221 |
|
+ portletApplicationName |
222 |
|
+ ", servlet: " |
223 |
|
+ servletMappingName; |
224 |
0 |
log.error(message); |
225 |
0 |
throw new PortletException(message); |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
0 |
ServletRequest servletRequest = this.requestResponseUnwrapper.unwrapPortletRequest(portletRequest); |
230 |
0 |
ServletResponse servletResponse = this.requestResponseUnwrapper.unwrapPortletResponse(portletResponse); |
231 |
|
|
232 |
|
try |
233 |
|
{ |
234 |
0 |
RequestContext requestContext = (RequestContext) servletRequest.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE); |
235 |
|
|
236 |
0 |
if (isParallelMode) |
237 |
|
{ |
238 |
0 |
synchronized (servletRequest) |
239 |
|
{ |
240 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET, portletInstance); |
241 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_CONFIG, portletInstance.getConfig()); |
242 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_REQUEST, portletRequest); |
243 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_RESPONSE, portletResponse); |
244 |
0 |
servletRequest.setAttribute(ContainerConstants.METHOD_ID, methodID); |
245 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_NAME, app.getName()+"::"+portletDefinition.getName()); |
246 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTAL_CONTEXT, ((HttpServletRequest) servletRequest).getContextPath()); |
247 |
0 |
} |
248 |
|
} |
249 |
|
else |
250 |
|
{ |
251 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET, portletInstance); |
252 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_CONFIG, portletInstance.getConfig()); |
253 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_REQUEST, portletRequest); |
254 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_RESPONSE, portletResponse); |
255 |
0 |
servletRequest.setAttribute(ContainerConstants.METHOD_ID, methodID); |
256 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTLET_NAME, app.getName()+"::"+portletDefinition.getName()); |
257 |
0 |
servletRequest.setAttribute(ContainerConstants.PORTAL_CONTEXT, requestContext.getRequest().getContextPath()); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
0 |
if (isParallelMode) |
262 |
|
{ |
263 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.PORTLET, portletInstance); |
264 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.PORTLET_CONFIG, portletInstance.getConfig()); |
265 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.PORTLET_REQUEST, portletRequest); |
266 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.PORTLET_RESPONSE, portletResponse); |
267 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.METHOD_ID, methodID); |
268 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.PORTLET_NAME, app.getName()+"::"+portletDefinition.getName()); |
269 |
0 |
CurrentWorkerContext.setAttribute(ContainerConstants.PORTAL_CONTEXT, ((HttpServletRequest) servletRequest).getContextPath()); |
270 |
|
} |
271 |
|
|
272 |
0 |
PortletRequestContext.createContext(portletDefinition, portletInstance, portletRequest, portletResponse); |
273 |
0 |
dispatcher.include(servletRequest, servletResponse); |
274 |
|
|
275 |
0 |
} |
276 |
0 |
catch (Exception e) |
277 |
|
{ |
278 |
0 |
String message = |
279 |
|
"Failed to dispatch.include for Portlet Application: " + portletApplicationName + ", servlet: " + servletMappingName; |
280 |
0 |
log.error(message, e); |
281 |
0 |
throw new PortletException(message, e); |
282 |
|
} |
283 |
|
finally |
284 |
|
{ |
285 |
0 |
PortletRequestContext.clearContext(); |
286 |
|
|
287 |
|
|
288 |
0 |
if (isParallelMode) |
289 |
|
{ |
290 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.PORTLET); |
291 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.PORTLET_CONFIG); |
292 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.PORTLET_REQUEST); |
293 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.PORTLET_RESPONSE); |
294 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.METHOD_ID); |
295 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.PORTLET_NAME); |
296 |
0 |
CurrentWorkerContext.removeAttribute(ContainerConstants.PORTAL_CONTEXT); |
297 |
|
} |
298 |
|
|
299 |
0 |
servletRequest.removeAttribute(ContainerConstants.PORTLET); |
300 |
0 |
servletRequest.removeAttribute(ContainerConstants.PORTLET_CONFIG); |
301 |
0 |
servletRequest.removeAttribute(ContainerConstants.PORTLET_REQUEST); |
302 |
0 |
servletRequest.removeAttribute(ContainerConstants.PORTLET_RESPONSE); |
303 |
0 |
servletRequest.removeAttribute(ContainerConstants.METHOD_ID); |
304 |
0 |
servletRequest.removeAttribute(ContainerConstants.PORTLET_NAME); |
305 |
0 |
servletRequest.removeAttribute(ContainerConstants.PORTAL_CONTEXT); |
306 |
0 |
} |
307 |
|
|
308 |
0 |
} |
309 |
|
|
310 |
|
} |