Skip to content

Commit 8f8e517

Browse files
jhoellerunknown
authored andcommitted
Portlet mapping predicate compareTo implementations are transitive now
Also removed unused validateHandler code with dead cachedMappings HashMap. Issue: SPR-9874
1 parent 4ff7654 commit 8f8e517

File tree

3 files changed

+87
-70
lines changed

3 files changed

+87
-70
lines changed

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/handler/AbstractHandlerMapping.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22-
2322
import javax.portlet.PortletRequest;
2423

2524
import org.springframework.beans.BeansException;
@@ -42,8 +41,7 @@
4241
* @see #setInterceptors
4342
* @see org.springframework.web.portlet.HandlerInterceptor
4443
*/
45-
public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
46-
implements HandlerMapping, Ordered {
44+
public abstract class AbstractHandlerMapping extends ApplicationObjectSupport implements HandlerMapping, Ordered {
4745

4846
private int order = Integer.MAX_VALUE; // default: same as non-Ordered
4947

@@ -244,7 +242,7 @@ public final HandlerExecutionChain getHandler(PortletRequest request) throws Exc
244242
* <p>For simply adding an interceptor, consider calling <code>super.getHandlerExecutionChain</code>
245243
* and invoking {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
246244
* @param handler the resolved handler instance (never <code>null</code>)
247-
* @param request current HTTP request
245+
* @param request current portlet request
248246
* @return the HandlerExecutionChain (never <code>null</code>)
249247
* @see #getAdaptedInterceptors()
250248
*/

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/mvc/annotation/DefaultAnnotationHandlerMapping.java

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@
9292
*/
9393
public class DefaultAnnotationHandlerMapping extends AbstractMapBasedHandlerMapping<PortletMode> {
9494

95-
private final Map<Class, RequestMapping> cachedMappings = new HashMap<Class, RequestMapping>();
96-
97-
9895
/**
9996
* Calls the <code>registerHandlers</code> method in addition
10097
* to the superclass's initialization.
@@ -118,7 +115,6 @@ protected void detectHandlers() throws BeansException {
118115
RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class);
119116
if (mapping != null) {
120117
// @RequestMapping found at type level
121-
this.cachedMappings.put(handlerType, mapping);
122118
String[] modeKeys = mapping.value();
123119
String[] params = mapping.params();
124120
boolean registerHandlerType = true;
@@ -222,49 +218,6 @@ protected PortletMode getLookupKey(PortletRequest request) throws Exception {
222218
return request.getPortletMode();
223219
}
224220

225-
/**
226-
* Validate the given annotated handler against the current request.
227-
* @see #validateMapping
228-
*/
229-
protected void validateHandler(Object handler, PortletRequest request) throws Exception {
230-
RequestMapping mapping = this.cachedMappings.get(handler.getClass());
231-
if (mapping == null) {
232-
mapping = AnnotationUtils.findAnnotation(handler.getClass(), RequestMapping.class);
233-
}
234-
if (mapping != null) {
235-
validateMapping(mapping, request);
236-
}
237-
}
238-
239-
/**
240-
* Validate the given type-level mapping metadata against the current request,
241-
* checking request method and parameter conditions.
242-
* @param mapping the mapping metadata to validate
243-
* @param request current portlet request
244-
* @throws Exception if validation failed
245-
*/
246-
protected void validateMapping(RequestMapping mapping, PortletRequest request) throws Exception {
247-
RequestMethod[] mappedMethods = mapping.method();
248-
if (!PortletAnnotationMappingUtils.checkRequestMethod(mappedMethods, request)) {
249-
String[] supportedMethods = new String[mappedMethods.length];
250-
for (int i = 0; i < mappedMethods.length; i++) {
251-
supportedMethods[i] = mappedMethods[i].name();
252-
}
253-
if (request instanceof ClientDataRequest) {
254-
throw new PortletRequestMethodNotSupportedException(((ClientDataRequest) request).getMethod(), supportedMethods);
255-
}
256-
else {
257-
throw new PortletRequestMethodNotSupportedException(supportedMethods);
258-
}
259-
}
260-
String[] mappedHeaders = mapping.headers();
261-
if (!PortletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
262-
throw new PortletRequestBindingException("Header conditions \"" +
263-
StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
264-
"\" not met for actual request");
265-
}
266-
}
267-
268221

269222
private interface SpecialRequestTypePredicate {
270223
}
@@ -383,7 +336,10 @@ else if (other instanceof ActionMappingPredicate) {
383336
return compareParams(otherAction);
384337
}
385338
}
386-
return (other instanceof SpecialRequestTypePredicate ? compareParams(other) : -1);
339+
if (other instanceof SpecialRequestTypePredicate) {
340+
return this.getClass().getName().compareTo(other.getClass().getName());
341+
}
342+
return -1;
387343
}
388344
}
389345

@@ -422,7 +378,10 @@ else if (other instanceof RenderMappingPredicate) {
422378
return compareParams(otherRender);
423379
}
424380
}
425-
return (other instanceof SpecialRequestTypePredicate ? compareParams(other) : -1);
381+
if (other instanceof SpecialRequestTypePredicate) {
382+
return this.getClass().getName().compareTo(other.getClass().getName());
383+
}
384+
return -1;
426385
}
427386
}
428387

@@ -451,7 +410,10 @@ public int compareTo(Object other) {
451410
return (hasResourceId ? -1 : 1);
452411
}
453412
}
454-
return (other instanceof SpecialRequestTypePredicate ? 0 : -1);
413+
if (other instanceof SpecialRequestTypePredicate) {
414+
return this.getClass().getName().compareTo(other.getClass().getName());
415+
}
416+
return -1;
455417
}
456418
}
457419

@@ -486,7 +448,10 @@ public int compareTo(Object other) {
486448
return (hasEventName ? -1 : 1);
487449
}
488450
}
489-
return (other instanceof SpecialRequestTypePredicate ? 0 : -1);
451+
if (other instanceof SpecialRequestTypePredicate) {
452+
return this.getClass().getName().compareTo(other.getClass().getName());
453+
}
454+
return -1;
490455
}
491456
}
492457

spring-webmvc-portlet/src/test/java/org/springframework/web/portlet/mvc/annotation/Portlet20AnnotationControllerTests.java

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import javax.portlet.PortletSession;
3434
import javax.portlet.RenderRequest;
3535
import javax.portlet.RenderResponse;
36-
import javax.portlet.ResourceRequest;
3736
import javax.portlet.ResourceResponse;
3837
import javax.portlet.StateAwareResponse;
3938
import javax.portlet.WindowState;
@@ -704,6 +703,7 @@ protected ApplicationContext createPortletApplicationContext(ApplicationContext
704703
// the collection with [Render,Action,Render] predicates
705704
wac.registerSingleton("firstController", FirstController.class);
706705
wac.registerSingleton("secondController", SecondController.class);
706+
wac.registerSingleton("thirdController", ThirdController.class);
707707
wac.registerSingleton("handlerMapping", DefaultAnnotationHandlerMapping.class);
708708
wac.registerSingleton("handlerAdapter", AnnotationMethodHandlerAdapter.class);
709709
wac.setPortletContext(new MockPortletContext());
@@ -714,11 +714,44 @@ protected ApplicationContext createPortletApplicationContext(ApplicationContext
714714
};
715715
portlet.init(new MockPortletConfig());
716716

717-
// Prepare render request with 'page=baz' parameters
717+
// Make sure all 6 annotated methods can be called
718+
718719
MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
719720
MockRenderResponse response = new MockRenderResponse();
720-
request.addParameter("page", "baz");
721+
722+
// renderFirst
723+
portlet.render(request, response);
724+
assertArrayEquals(new String[] { "renderFirst" }, response.getProperties("RESPONSE"));
725+
726+
// renderSecond
727+
request.setWindowState(WindowState.MAXIMIZED);
728+
request.setParameter("report", "second");
729+
portlet.render(request, response);
730+
assertArrayEquals(new String[] { "renderSecond" }, response.getProperties("RESPONSE"));
731+
732+
// renderThirds
733+
request.setWindowState(WindowState.MAXIMIZED);
734+
request.setParameter("report", "third");
721735
portlet.render(request, response);
736+
assertArrayEquals(new String[] { "renderThird" }, response.getProperties("RESPONSE"));
737+
738+
MockResourceRequest resourceRequest;
739+
MockResourceResponse resourceResponse = new MockResourceResponse();
740+
741+
// resourceFirst
742+
resourceRequest = new MockResourceRequest("first");
743+
portlet.serveResource(resourceRequest, resourceResponse);
744+
assertArrayEquals(new String[] { "resourceFirst" }, resourceResponse.getProperties("RESPONSE"));
745+
746+
// resourceSecond
747+
resourceRequest = new MockResourceRequest("second");
748+
portlet.serveResource(resourceRequest, resourceResponse);
749+
assertArrayEquals(new String[] { "resourceSecond" }, resourceResponse.getProperties("RESPONSE"));
750+
751+
// resourceThirds
752+
resourceRequest = new MockResourceRequest("third");
753+
portlet.serveResource(resourceRequest, resourceResponse);
754+
assertArrayEquals(new String[] { "resourceThird" }, resourceResponse.getProperties("RESPONSE"));
722755
}
723756

724757

@@ -1201,28 +1234,49 @@ public void render(String viewName, Map model, PortletRequest request, MimeRespo
12011234
public static class FirstController {
12021235

12031236
@RenderMapping
1204-
public String renderBar() {
1205-
throw new UnsupportedOperationException("Should not be called");
1237+
public String renderFirst(RenderResponse response) {
1238+
response.setProperty("RESPONSE", "renderFirst");
1239+
return "renderFirst";
12061240
}
12071241

1208-
@ActionMapping("xyz")
1209-
public void processXyz() {
1210-
throw new UnsupportedOperationException("Should not be called");
1242+
@ResourceMapping("first")
1243+
public String resourceFirst(ResourceResponse response) {
1244+
response.setProperty("RESPONSE", "resourceFirst");
1245+
return "resourceFirst";
12111246
}
12121247
}
12131248

12141249

12151250
@RequestMapping(value="view")
12161251
public static class SecondController {
12171252

1218-
@ResourceMapping
1219-
public void processResource(ResourceRequest request, ResourceResponse response) {
1220-
throw new UnsupportedOperationException("Should not be called");
1253+
@ResourceMapping("second")
1254+
public String processResource(ResourceResponse response) {
1255+
response.setProperty("RESPONSE", "resourceSecond");
1256+
return "resourceSecond";
1257+
}
1258+
1259+
@RenderMapping(value = "MAXIMIZED", params = "report=second")
1260+
public String renderSecond(RenderResponse response) {
1261+
response.setProperty("RESPONSE", "renderSecond");
1262+
return "renderSecond";
1263+
}
1264+
}
1265+
1266+
1267+
@RequestMapping(value="view")
1268+
public static class ThirdController {
1269+
1270+
@ResourceMapping("third")
1271+
public String processResource(ResourceResponse response) {
1272+
response.setProperty("RESPONSE", "resourceThird");
1273+
return "resourceThird";
12211274
}
12221275

1223-
@RenderMapping(params="page=baz")
1224-
public String renderBaz() {
1225-
return "SUCCESS";
1276+
@RenderMapping(value = "MAXIMIZED", params = "report=third")
1277+
public String renderSecond(RenderResponse response) {
1278+
response.setProperty("RESPONSE", "renderThird");
1279+
return "renderThird";
12261280
}
12271281
}
12281282

0 commit comments

Comments
 (0)