Fixed SWS-129: SimpleMethodEndpointMapping moved from sandbox.
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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.endpoint.MethodEndpoint;
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*
|
||||
* @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});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* 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.endpoint.MethodEndpoint;
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
/*
|
||||
* 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.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Simple subclass of {@link AbstractMethodEndpointMapping} that maps to all methods that start with a prefix, and end
|
||||
* with a suffix. Endpoint beans are registered using the <code>endpoints</code> property.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class SimpleMethodEndpointMapping extends AbstractMethodEndpointMapping implements InitializingBean {
|
||||
|
||||
public static final String DEFAULT_METHOD_PREFIX = "handle";
|
||||
|
||||
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;
|
||||
|
||||
/** Sets the endpoints */
|
||||
public void setEndpoints(Object[] endpoints) {
|
||||
this.endpoints = endpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 void afterPropertiesSet() throws Exception {
|
||||
Assert.notEmpty(endpoints, "endpoints is required");
|
||||
transformerFactory = TransformerFactory.newInstance();
|
||||
for (int i = 0; i < endpoints.length; i++) {
|
||||
registerMethods(endpoints[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the name of the given method, with the prefix and suffix stripped off. */
|
||||
protected String getLookupKeyForMethod(Method method) {
|
||||
String methodName = method.getName();
|
||||
if (methodName.startsWith(methodPrefix) && methodName.endsWith(methodSuffix)) {
|
||||
return methodName.substring(methodPrefix.length(), methodName.length() - methodSuffix.length());
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected String getLookupKeyForMessage(MessageContext messageContext) throws TransformerException {
|
||||
Element payloadElement = getMessagePayloadElement(messageContext.getRequest());
|
||||
return payloadElement.getLocalName();
|
||||
}
|
||||
|
||||
private Element getMessagePayloadElement(WebServiceMessage message) throws TransformerException {
|
||||
if (message.getPayloadSource() instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) message.getPayloadSource();
|
||||
if (domSource.getNode().getNodeType() == Node.ELEMENT_NODE) {
|
||||
return (Element) domSource.getNode();
|
||||
}
|
||||
}
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(message.getPayloadSource(), domResult);
|
||||
return (Element) domResult.getNode().getFirstChild();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* 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 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 {
|
||||
Method noResponse = getClass().getMethod("supported", new Class[]{MessageContext.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
|
||||
assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
|
||||
}
|
||||
|
||||
public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
|
||||
Method unsupported = getClass()
|
||||
.getMethod("unsupportedMultipleParams", new Class[]{MessageContext.class, MessageContext.class});
|
||||
assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
|
||||
}
|
||||
|
||||
public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
|
||||
Method unsupported = getClass().getMethod("unsupportedWrongParam", new Class[]{String.class});
|
||||
assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
|
||||
}
|
||||
|
||||
public void testInvokeSupported() throws Exception {
|
||||
Method supported = getClass().getMethod("supported", new Class[]{MessageContext.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, supported);
|
||||
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) {
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* 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 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 {
|
||||
Method noResponse = getClass().getMethod("noResponse", new Class[]{Source.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
|
||||
assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
|
||||
}
|
||||
|
||||
public void testSupportedResponse() throws NoSuchMethodException {
|
||||
Method response = getClass().getMethod("response", new Class[]{Source.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
|
||||
assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
|
||||
}
|
||||
|
||||
public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
|
||||
Method unsupported = getClass().getMethod("unsupportedMultipleParams", new Class[]{Source.class, Source.class});
|
||||
assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
|
||||
}
|
||||
|
||||
public void testUnsupportedMethodWrongReturnType() throws NoSuchMethodException {
|
||||
Method unsupported = getClass().getMethod("unsupportedWrongReturnType", new Class[]{Source.class});
|
||||
assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
|
||||
}
|
||||
|
||||
public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
|
||||
Method unsupported = getClass().getMethod("unsupportedWrongParam", new Class[]{String.class});
|
||||
assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
|
||||
}
|
||||
|
||||
public void testNoResponse() throws Exception {
|
||||
Method noResponse = getClass().getMethod("noResponse", new Class[]{Source.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
|
||||
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());
|
||||
Method response = getClass().getMethod("response", new Class[]{Source.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* 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"));
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user