-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
PortletWrappingController.java
320 lines (267 loc) · 9.49 KB
/
PortletWrappingController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.portlet.mvc;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.EventPortlet;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceServingPortlet;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.NoHandlerFoundException;
import org.springframework.web.portlet.context.PortletConfigAware;
import org.springframework.web.portlet.context.PortletContextAware;
import org.springframework.web.portlet.util.PortletUtils;
/**
* {@link Controller} implementation that wraps a portlet instance which it manages
* internally. Such a wrapped portlet is not known outside of this controller;
* its entire lifecycle is covered here.
*
* <p>Useful to invoke an existing portlet via Spring's dispatching infrastructure,
* for example to apply Spring
* {@link org.springframework.web.portlet.HandlerInterceptor HandlerInterceptors}
* to its requests.
*
* <p><b>Example:</b>
*
* <pre class="code"><bean id="wrappingController" class="org.springframework.web.portlet.mvc.PortletWrappingController">
* <property name="portletClass">
* <value>org.springframework.web.portlet.sample.HelloWorldPortlet</value>
* </property>
* <property name="portletName">
* <value>hello-world</value>
* </property>
* <property name="initParameters">
* <props>
* <prop key="config">/WEB-INF/hello-world-portlet-config.xml</prop>
* </props>
* </property>
* </bean></pre>
*
* @author Juergen Hoeller
* @author John A. Lewis
* @since 2.0
*/
public class PortletWrappingController extends AbstractController
implements ResourceAwareController, EventAwareController,
BeanNameAware, InitializingBean, DisposableBean, PortletContextAware, PortletConfigAware {
private boolean useSharedPortletConfig = true;
private PortletContext portletContext;
private PortletConfig portletConfig;
private Class<?> portletClass;
private String portletName;
private Map<String, String> initParameters = new LinkedHashMap<String, String>();
private String beanName;
private Portlet portletInstance;
/**
* Set whether to use the shared PortletConfig object passed in
* through {@code setPortletConfig}, if available.
* <p>Default is "true". Turn this setting to "false" to pass in
* a mock PortletConfig object with the bean name as portlet name,
* holding the current PortletContext.
* @see #setPortletConfig
*/
public void setUseSharedPortletConfig(boolean useSharedPortletConfig) {
this.useSharedPortletConfig = useSharedPortletConfig;
}
@Override
public void setPortletContext(PortletContext portletContext) {
this.portletContext = portletContext;
}
@Override
public void setPortletConfig(PortletConfig portletConfig) {
this.portletConfig = portletConfig;
}
/**
* Set the class of the Portlet to wrap.
* Needs to implement {@code javax.portlet.Portlet}.
* @see javax.portlet.Portlet
*/
public void setPortletClass(Class<?> portletClass) {
this.portletClass = portletClass;
}
/**
* Set the name of the Portlet to wrap.
* Default is the bean name of this controller.
*/
public void setPortletName(String portletName) {
this.portletName = portletName;
}
/**
* Specify init parameters for the portlet to wrap,
* as name-value pairs.
*/
public void setInitParameters(Map<String, String> initParameters) {
this.initParameters = initParameters;
}
@Override
public void setBeanName(String name) {
this.beanName = name;
}
@Override
public void afterPropertiesSet() throws Exception {
if (this.portletClass == null) {
throw new IllegalArgumentException("portletClass is required");
}
if (!Portlet.class.isAssignableFrom(this.portletClass)) {
throw new IllegalArgumentException("portletClass [" + this.portletClass.getName() +
"] needs to implement interface [javax.portlet.Portlet]");
}
if (this.portletName == null) {
this.portletName = this.beanName;
}
PortletConfig config = this.portletConfig;
if (config == null || !this.useSharedPortletConfig) {
config = new DelegatingPortletConfig();
}
this.portletInstance = (Portlet) this.portletClass.newInstance();
this.portletInstance.init(config);
}
@Override
protected void handleActionRequestInternal(
ActionRequest request, ActionResponse response) throws Exception {
this.portletInstance.processAction(request, response);
}
@Override
protected ModelAndView handleRenderRequestInternal(
RenderRequest request, RenderResponse response) throws Exception {
this.portletInstance.render(request, response);
return null;
}
@Override
public ModelAndView handleResourceRequest(
ResourceRequest request, ResourceResponse response) throws Exception {
if (!(this.portletInstance instanceof ResourceServingPortlet)) {
throw new NoHandlerFoundException("Cannot handle resource request - target portlet [" +
this.portletInstance.getClass() + " does not implement ResourceServingPortlet");
}
ResourceServingPortlet resourcePortlet = (ResourceServingPortlet) this.portletInstance;
// Delegate to PortletContentGenerator for checking and preparing.
checkAndPrepare(request, response);
// Execute in synchronized block if required.
if (isSynchronizeOnSession()) {
PortletSession session = request.getPortletSession(false);
if (session != null) {
Object mutex = PortletUtils.getSessionMutex(session);
synchronized (mutex) {
resourcePortlet.serveResource(request, response);
return null;
}
}
}
resourcePortlet.serveResource(request, response);
return null;
}
@Override
public void handleEventRequest(
EventRequest request, EventResponse response) throws Exception {
if (!(this.portletInstance instanceof EventPortlet)) {
logger.debug("Ignoring event request for non-event target portlet: " + this.portletInstance.getClass());
return;
}
EventPortlet eventPortlet = (EventPortlet) this.portletInstance;
// Delegate to PortletContentGenerator for checking and preparing.
check(request, response);
// Execute in synchronized block if required.
if (isSynchronizeOnSession()) {
PortletSession session = request.getPortletSession(false);
if (session != null) {
Object mutex = PortletUtils.getSessionMutex(session);
synchronized (mutex) {
eventPortlet.processEvent(request, response);
return;
}
}
}
eventPortlet.processEvent(request, response);
}
@Override
public void destroy() {
this.portletInstance.destroy();
}
/**
* Internal implementation of the PortletConfig interface, to be passed
* to the wrapped portlet.
* <p>Delegates to {@link PortletWrappingController} fields
* and methods to provide init parameters and other environment info.
*/
private class DelegatingPortletConfig implements PortletConfig {
@Override
public String getPortletName() {
return portletName;
}
@Override
public PortletContext getPortletContext() {
return portletContext;
}
@Override
public String getInitParameter(String paramName) {
return initParameters.get(paramName);
}
@Override
public Enumeration<String> getInitParameterNames() {
return Collections.enumeration(initParameters.keySet());
}
@Override
public ResourceBundle getResourceBundle(Locale locale) {
return (portletConfig != null ? portletConfig.getResourceBundle(locale) : null);
}
@Override
public Enumeration<String> getPublicRenderParameterNames() {
return Collections.enumeration(new HashSet<String>());
}
@Override
public String getDefaultNamespace() {
return XMLConstants.NULL_NS_URI;
}
@Override
public Enumeration<QName> getPublishingEventQNames() {
return Collections.enumeration(new HashSet<QName>());
}
@Override
public Enumeration<QName> getProcessingEventQNames() {
return Collections.enumeration(new HashSet<QName>());
}
@Override
public Enumeration<Locale> getSupportedLocales() {
return Collections.enumeration(new HashSet<Locale>());
}
@Override
public Map<String, String[]> getContainerRuntimeOptions() {
return (portletConfig != null ? portletConfig.getContainerRuntimeOptions() : null);
}
}
}