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