Introduced @PayloadRoots annotation

Introduced the @PayloadRoots annotation, a composite of @PayloadRoot.
which allows users to map to multiple payload root element names.

Issue: SWS-864
This commit is contained in:
Arjen Poutsma
2014-02-25 12:41:04 +01:00
parent 05e6a960d9
commit 35a8831528
5 changed files with 126 additions and 26 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2007 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.ws.server.endpoint.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks an endpoint method as containing multiple {@link PayloadRoot PayloadRoots}.
*
* @author Arjen Poutsma
* @see org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping
* @since 2.1.5
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PayloadRoots {
PayloadRoot[] value();
}

View File

@@ -19,8 +19,10 @@ package org.springframework.ws.server.endpoint.mapping;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -119,10 +121,10 @@ public abstract class AbstractMethodEndpointMapping<T> extends AbstractEndpointM
ReflectionUtils.doWithMethods(endpointClass, new ReflectionUtils.MethodCallback() {
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
T key = getLookupKeyForMethod(method);
if (key != null) {
registerEndpoint(key, new MethodEndpoint(endpoint, method));
}
List<T> keys = getLookupKeysForMethod(method);
for (T key : keys) {
registerEndpoint(key, new MethodEndpoint(endpoint, method));
}
}
});
}
@@ -133,6 +135,7 @@ public abstract class AbstractMethodEndpointMapping<T> extends AbstractEndpointM
* using {@link #registerEndpoint(Object, MethodEndpoint)}.
*
* @see #getLookupKeyForMethod(Method)
* @see #getLookupKeysForMethod(Method)
*/
protected void registerMethods(String beanName) {
Assert.hasText(beanName, "'beanName' must not be empty");
@@ -141,13 +144,15 @@ public abstract class AbstractMethodEndpointMapping<T> extends AbstractEndpointM
Set<Method> methods = findEndpointMethods(endpointType, new ReflectionUtils.MethodFilter() {
public boolean matches(Method method) {
return getLookupKeyForMethod(method) != null;
return !getLookupKeysForMethod(method).isEmpty();
}
});
for (Method method : methods) {
T key = getLookupKeyForMethod(method);
registerEndpoint(key, new MethodEndpoint(beanName, getApplicationContext(), method));
List<T> keys = getLookupKeysForMethod(method);
for (T key : keys) {
registerEndpoint(key, new MethodEndpoint(beanName, getApplicationContext(), method));
}
}
}
@@ -179,16 +184,30 @@ public abstract class AbstractMethodEndpointMapping<T> extends AbstractEndpointM
}
/**
* Returns the the endpoint keys for the given method. Returns <code>null</code> if the method is not to be
* Returns the the endpoint key for the given method. Returns <code>null</code> if the method is not to be
* registered, which is the default.
*
* @param method the method
* @return a registration key, or <code>null</code> if the method is not to be registered
* @see #getLookupKeysForMethod(Method)
*/
protected T getLookupKeyForMethod(Method method) {
return null;
}
/**
* Returns the the endpoint keys for the given method. Should return an empty array if the method is not to be
* registered. The default delegates to {@link #getLookupKeysForMethod(Method)}.
*
* @param method the method
* @return a list of registration keys
* @since 2.1.5
*/
protected List<T> getLookupKeysForMethod(Method method) {
T key = getLookupKeyForMethod(method);
return key != null ? Collections.singletonList(key) : Collections.<T>emptyList();
}
/**
* Return the class or interface to use for method reflection.
* <p/>

View File

@@ -17,6 +17,8 @@
package org.springframework.ws.server.endpoint.mapping;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.transform.TransformerFactory;
@@ -25,6 +27,7 @@ import org.springframework.util.StringUtils;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointMapping;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.PayloadRoots;
import org.springframework.ws.server.endpoint.support.PayloadRootUtils;
/**
@@ -60,21 +63,33 @@ public class PayloadRootAnnotationMethodEndpointMapping extends AbstractAnnotati
}
@Override
protected QName getLookupKeyForMethod(Method method) {
PayloadRoot annotation = AnnotationUtils.findAnnotation(method, PayloadRoot.class);
if (annotation != null) {
QName qname;
if (StringUtils.hasLength(annotation.localPart()) && StringUtils.hasLength(annotation.namespace())) {
qname = new QName(annotation.namespace(), annotation.localPart());
}
else {
qname = new QName(annotation.localPart());
}
return qname;
}
else {
return null;
protected List<QName> getLookupKeysForMethod(Method method) {
List<QName> result = new ArrayList<QName>();
PayloadRoots payloadRoots = AnnotationUtils.findAnnotation(method, PayloadRoots.class);
if (payloadRoots != null) {
for (PayloadRoot payloadRoot : payloadRoots.value()) {
result.add(getQNameFromAnnotation(payloadRoot));
}
}
else {
PayloadRoot payloadRoot = AnnotationUtils.findAnnotation(method, PayloadRoot.class);
if (payloadRoot != null) {
result.add(getQNameFromAnnotation(payloadRoot));
}
}
return result;
}
private QName getQNameFromAnnotation(PayloadRoot payloadRoot) {
if (StringUtils.hasLength(payloadRoot.localPart()) && StringUtils.hasLength(
payloadRoot.namespace())) {
return new QName(payloadRoot.namespace(), payloadRoot.localPart());
}
else {
return new QName(payloadRoot.localPart());
}
}
}

View File

@@ -36,6 +36,7 @@ import org.springframework.ws.server.endpoint.MethodEndpoint;
import org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.PayloadRoots;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
@@ -58,15 +59,32 @@ public class PayloadRootAnnotationMethodEndpointMappingTest {
private ApplicationContext applicationContext;
@Test
public void registration() throws NoSuchMethodException {
public void registrationSingle() throws NoSuchMethodException {
MethodEndpoint endpoint = mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Request"));
assertNotNull("MethodEndpoint not registered", endpoint);
Method doIt = MyEndpoint.class.getMethod("doIt", Source.class);
MethodEndpoint expected = new MethodEndpoint("endpoint", applicationContext, doIt);
assertEquals("Invalid endpoint registered", expected, endpoint);
}
@Test
public void registrationMultiple() throws NoSuchMethodException {
Method doItMultiple = MyEndpoint.class.getMethod("doItMultiple", Source.class);
MethodEndpoint expected = new MethodEndpoint("endpoint", applicationContext, doItMultiple);
MethodEndpoint endpoint = mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Request1"));
assertNotNull("MethodEndpoint not registered", endpoint);
assertEquals("Invalid endpoint registered", expected, endpoint);
endpoint = mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Request2"));
assertNotNull("MethodEndpoint not registered", endpoint);
assertEquals("Invalid endpoint registered", expected, endpoint);
}
@Test
public void registrationInvalid() {
assertNull("Invalid endpoint registered",
mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Request2")));
mapping.lookupEndpoint(new QName("http://springframework.org/spring-ws", "Invalid")));
}
@Test
@@ -112,11 +130,20 @@ public class PayloadRootAnnotationMethodEndpointMappingTest {
logger.info("In doIt()");
}
@PayloadRoots({
@PayloadRoot(localPart = "Request1", namespace = "http://springframework.org/spring-ws"),
@PayloadRoot(localPart = "Request2", namespace = "http://springframework.org/spring-ws")
})
public void doItMultiple(@RequestPayload Source payload) {
doItInvoked = true;
logger.info("In doIt()");
}
}
static class OtherBean {
@PayloadRoot(localPart = "Request2", namespace = "http://springframework.org/spring-ws")
@PayloadRoot(localPart = "Invalid", namespace = "http://springframework.org/spring-ws")
public void doIt() {
}

View File

@@ -15,6 +15,6 @@
<bean id="logAspect" class="org.springframework.ws.server.endpoint.mapping.LogAspect"/>
<sws:annotation-driven/>
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping"/>
</beans>