Fixed SWS-129: SimpleMethodEndpointMapping moved from sandbox.

This commit is contained in:
Arjen Poutsma
2007-05-30 20:56:11 +00:00
parent 3226f21ef3
commit 435b5ebfad
11 changed files with 95 additions and 63 deletions

View File

@@ -18,10 +18,14 @@ package org.springframework.ws.server.endpoint.adapter;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointAdapter;
import org.springframework.ws.server.MessageDispatcher;
import org.springframework.ws.server.endpoint.MessageEndpoint;
import org.springframework.ws.soap.server.SoapMessageDispatcher;
/**
* Adapter to use a <code>MessageEndpoint</code> as the endpoint for a <code>EndpointInvocationChain</code>.
* <p/>
* This adapter is registered by default by the {@link MessageDispatcher} and {@link SoapMessageDispatcher}.
*
* @author Arjen Poutsma
* @see org.springframework.ws.server.EndpointInvocationChain

View File

@@ -0,0 +1,51 @@
/*
* Copyright 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.adapter;
import java.lang.reflect.Method;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.MessageDispatcher;
import org.springframework.ws.server.endpoint.MethodEndpoint;
import org.springframework.ws.soap.server.SoapMessageDispatcher;
/**
* Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature:
* <pre>
* void handleMyMessage(MessageContext request);
* </pre>
* I.e. methods that take a single {@link MessageContext} parameter, and return <code>void</code>. The method can have
* any name, as long as it is mapped by an {@link org.springframework.ws.server.EndpointMapping}.
* <p/>
* This adapter is registered by default by the {@link MessageDispatcher} and {@link SoapMessageDispatcher}.
*
* @author Arjen Poutsma
*/
public class MessageMethodEndpointAdapter extends AbstractMethodEndpointAdapter {
protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
Method method = methodEndpoint.getMethod();
return Void.TYPE.isAssignableFrom(method.getReturnType()) && method.getParameterTypes().length == 1 &&
MessageContext.class.isAssignableFrom(method.getParameterTypes()[0]);
}
protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
methodEndpoint.invoke(new Object[]{messageContext});
}
}

View File

@@ -22,11 +22,15 @@ import javax.xml.transform.Transformer;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointAdapter;
import org.springframework.ws.server.MessageDispatcher;
import org.springframework.ws.server.endpoint.PayloadEndpoint;
import org.springframework.ws.soap.server.SoapMessageDispatcher;
import org.springframework.xml.transform.TransformerObjectSupport;
/**
* Adapter to use a <code>PayloadEndpoint</code> as the endpoint for a <code>EndpointInvocationChain</code>.
* <p/>
* This adapter is registered by default by the {@link MessageDispatcher} and {@link SoapMessageDispatcher}.
*
* @author Arjen Poutsma
* @see org.springframework.ws.server.endpoint.PayloadEndpoint

View File

@@ -0,0 +1,65 @@
/*
* Copyright 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.adapter;
import java.lang.reflect.Method;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.MessageDispatcher;
import org.springframework.ws.server.endpoint.MethodEndpoint;
import org.springframework.ws.soap.server.SoapMessageDispatcher;
/**
* Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature:
* <pre>
* void handleMyMessage(Source request);
* </pre>
* or
* <pre>
* Source handleMyMessage(Source request);
* </pre>
* I.e. methods that take a single {@link Source} parameter, and return either <code>void</code> or a {@link Source}.
* The method can have any name, as long as it is mapped by an {@link org.springframework.ws.server.EndpointMapping}.
* <p/>
* This adapter is registered by default by the {@link MessageDispatcher} and {@link SoapMessageDispatcher}.
*
* @author Arjen Poutsma
*/
public class PayloadMethodEndpointAdapter extends AbstractMethodEndpointAdapter {
protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
Method method = methodEndpoint.getMethod();
return (Void.TYPE.isAssignableFrom(method.getReturnType()) ||
Source.class.isAssignableFrom(method.getReturnType())) && method.getParameterTypes().length == 1 &&
Source.class.isAssignableFrom(method.getParameterTypes()[0]);
}
protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
Source requestSource = messageContext.getRequest().getPayloadSource();
Object result = methodEndpoint.invoke(new Object[]{requestSource});
if (result != null) {
Source responseSource = (Source) result;
WebServiceMessage response = messageContext.getResponse();
Transformer transformer = createTransformer();
transformer.transform(responseSource, response.getPayloadResult());
}
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright 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.mapping;
import java.lang.reflect.Method;
import javax.xml.namespace.QName;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.mapping.support.PayloadRootUtils;
/**
* Simple subclass of {@link AbstractMethodEndpointMapping} that maps from the local name of the request payload to
* methods.Endpoint beans are registered using the <code>endpoints</code> property; the endpoint methods that start with
* <code>methodPrefix</code> and end with <code>methodSuffix</code> will be registered.
* <p/>
* Endpoints typically have the following form:
* <pre>
* public class MyEndpoint{
* <p/>
* public Source handleMyMessage(Source source) {
* ...
* }
* }
* </pre>
* This method will handle any message that has the <code>MyMessage</code> as a payload root local name.
*
* @author Arjen Poutsma
* @see #setEndpoints(Object[])
*/
public class SimpleMethodEndpointMapping extends AbstractMethodEndpointMapping implements InitializingBean {
/** Default method prefix. */
public static final String DEFAULT_METHOD_PREFIX = "handle";
/** Default method suffix. */
public static final String DEFAULT_METHOD_SUFFIX = "";
private Object[] endpoints;
private String methodPrefix = DEFAULT_METHOD_PREFIX;
private String methodSuffix = DEFAULT_METHOD_SUFFIX;
private TransformerFactory transformerFactory;
public Object[] getEndpoints() {
return endpoints;
}
/**
* Sets the endpoints. The endpoint methods that start with <code>methodPrefix</code> and end with
* <code>methodSuffix</code> will be registered.
*/
public void setEndpoints(Object[] endpoints) {
this.endpoints = endpoints;
}
/** Returns the method prefix. */
public String getMethodPrefix() {
return methodPrefix;
}
/**
* Sets the method prefix. All methods with names starting with this string will be registered. Default is
* "<code>handle</code>".
*
* @see #DEFAULT_METHOD_PREFIX
*/
public void setMethodPrefix(String methodPrefix) {
this.methodPrefix = methodPrefix;
}
/** Returns the method suffix. */
public String getMethodSuffix() {
return methodSuffix;
}
/**
* Sets the method suffix. All methods with names ending with this string will be registered. Default is "" (i.e. no
* suffix).
*
* @see #DEFAULT_METHOD_SUFFIX
*/
public void setMethodSuffix(String methodSuffix) {
this.methodSuffix = methodSuffix;
}
public final void afterPropertiesSet() throws Exception {
Assert.notEmpty(getEndpoints(), "'endpoints' is required");
transformerFactory = TransformerFactory.newInstance();
for (int i = 0; i < getEndpoints().length; i++) {
registerMethods(getEndpoints()[i]);
}
}
/** Returns the name of the given method, with the prefix and suffix stripped off. */
protected String getLookupKeyForMethod(Method method) {
String methodName = method.getName();
String prefix = getMethodPrefix();
String suffix = getMethodSuffix();
if (methodName.startsWith(prefix) && methodName.endsWith(suffix)) {
return methodName.substring(prefix.length(), methodName.length() - suffix.length());
}
else {
return null;
}
}
/** Returns the local part of the payload root element of the request. */
protected String getLookupKeyForMessage(MessageContext messageContext) throws TransformerException {
WebServiceMessage request = messageContext.getRequest();
QName rootQName = PayloadRootUtils.getPayloadRootQName(request.getPayloadSource(), transformerFactory);
return rootQName.getLocalPart();
}
}

View File

@@ -3,4 +3,6 @@
# Not meant to be customized by application developers.
org.springframework.ws.server.EndpointAdapter=org.springframework.ws.server.endpoint.adapter.MessageEndpointAdapter,\
org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter
org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter,\
org.springframework.ws.server.endpoint.adapter.MessageMethodEndpointAdapter,\
org.springframework.ws.server.endpoint.adapter.PayloadMethodEndpointAdapter

View File

@@ -3,5 +3,8 @@
# Not meant to be customized by application developers.
org.springframework.ws.server.EndpointAdapter=org.springframework.ws.server.endpoint.adapter.MessageEndpointAdapter,\
org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter
org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter,\
org.springframework.ws.server.endpoint.adapter.MessageMethodEndpointAdapter,\
org.springframework.ws.server.endpoint.adapter.PayloadMethodEndpointAdapter
org.springframework.ws.server.EndpointExceptionResolver=org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver

View File

@@ -0,0 +1,69 @@
/*
* Copyright 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.adapter;
import junit.framework.TestCase;
import org.springframework.ws.MockWebServiceMessageFactory;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MethodEndpoint;
public class MessageMethodEndpointAdapterTest extends TestCase {
private MessageMethodEndpointAdapter adapter;
private boolean supportedInvoked;
private MessageContext messageContext;
protected void setUp() throws Exception {
adapter = new MessageMethodEndpointAdapter();
messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
}
public void testSupported() throws NoSuchMethodException {
MethodEndpoint methodEndpoint = new MethodEndpoint(this, "supported", new Class[]{MessageContext.class});
assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
}
public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, "unsupportedMultipleParams",
new Class[]{MessageContext.class, MessageContext.class})));
}
public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
assertFalse("Method supported",
adapter.supportsInternal(new MethodEndpoint(this, "unsupportedWrongParam", new Class[]{String.class})));
}
public void testInvokeSupported() throws Exception {
MethodEndpoint methodEndpoint = new MethodEndpoint(this, "supported", new Class[]{MessageContext.class});
assertFalse("Method invoked", supportedInvoked);
adapter.invoke(messageContext, methodEndpoint);
assertTrue("Method not invoked", supportedInvoked);
}
public void supported(MessageContext context) {
supportedInvoked = true;
}
public void unsupportedMultipleParams(MessageContext s1, MessageContext s2) {
}
public void unsupportedWrongParam(String request) {
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 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.adapter;
import javax.xml.transform.Source;
import junit.framework.TestCase;
import org.springframework.ws.MockWebServiceMessage;
import org.springframework.ws.MockWebServiceMessageFactory;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MethodEndpoint;
public class PayloadMethodEndpointAdapterTest extends TestCase {
private PayloadMethodEndpointAdapter adapter;
private boolean noResponseInvoked;
private boolean responseInvoked;
private MessageContext messageContext;
protected void setUp() throws Exception {
adapter = new PayloadMethodEndpointAdapter();
messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
}
public void testSupportedNoResponse() throws NoSuchMethodException {
MethodEndpoint methodEndpoint = new MethodEndpoint(this, "noResponse", new Class[]{Source.class});
assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
}
public void testSupportedResponse() throws NoSuchMethodException {
MethodEndpoint methodEndpoint = new MethodEndpoint(this, "response", new Class[]{Source.class});
assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
}
public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
assertFalse("Method supported", adapter.supportsInternal(
new MethodEndpoint(this, "unsupportedMultipleParams", new Class[]{Source.class, Source.class})));
}
public void testUnsupportedMethodWrongReturnType() throws NoSuchMethodException {
assertFalse("Method supported", adapter.supportsInternal(
new MethodEndpoint(this, "unsupportedWrongReturnType", new Class[]{Source.class})));
}
public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
assertFalse("Method supported",
adapter.supportsInternal(new MethodEndpoint(this, "unsupportedWrongParam", new Class[]{String.class})));
}
public void testNoResponse() throws Exception {
MethodEndpoint methodEndpoint = new MethodEndpoint(this, "noResponse", new Class[]{Source.class});
assertFalse("Method invoked", noResponseInvoked);
adapter.invoke(messageContext, methodEndpoint);
assertTrue("Method not invoked", noResponseInvoked);
}
public void testResponse() throws Exception {
WebServiceMessage request = new MockWebServiceMessage("<request/>");
messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
MethodEndpoint methodEndpoint = new MethodEndpoint(this, "response", new Class[]{Source.class});
assertFalse("Method invoked", responseInvoked);
adapter.invoke(messageContext, methodEndpoint);
assertTrue("Method not invoked", responseInvoked);
}
public void noResponse(Source request) {
noResponseInvoked = true;
}
public Source response(Source request) {
responseInvoked = true;
return request;
}
public void unsupportedMultipleParams(Source s1, Source s2) {
}
public Source unsupportedWrongParam(String request) {
return null;
}
public String unsupportedWrongReturnType(Source request) {
return null;
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 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.mapping;
import junit.framework.TestCase;
import org.springframework.ws.MockWebServiceMessage;
import org.springframework.ws.MockWebServiceMessageFactory;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
public class SimpleMethodEndpointMappingTest extends TestCase {
private SimpleMethodEndpointMapping mapping;
protected void setUp() throws Exception {
mapping = new SimpleMethodEndpointMapping();
mapping.setMethodPrefix("prefix");
mapping.setMethodSuffix("Suffix");
MyBean bean = new MyBean();
mapping.setEndpoints(new Object[]{bean});
mapping.afterPropertiesSet();
}
public void testRegistration() throws Exception {
assertNotNull("Endpoint not registered", mapping.lookupEndpoint("MyRequest"));
assertNull("Endpoint registered", mapping.lookupEndpoint("request"));
}
public void testGetLookupKeyForMessageNoNamespace() throws Exception {
MockWebServiceMessage request = new MockWebServiceMessage("<MyRequest/>");
MessageContext messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
String result = mapping.getLookupKeyForMessage(messageContext);
assertEquals("Invalid lookup key", "MyRequest", result);
}
public void testGetLookupKeyForMessageNamespace() throws Exception {
MockWebServiceMessage request =
new MockWebServiceMessage("<MyRequest xmlns='http://springframework.org/spring-ws/' />");
MessageContext messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
String result = mapping.getLookupKeyForMessage(messageContext);
assertEquals("Invalid lookup key", "MyRequest", result);
}
private static class MyBean {
public void prefixMyRequestSuffix() {
}
public void request() {
}
}
}