From 956fb906d918490f47cd7153c242c3351b8f0d7a Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 3 Apr 2007 21:55:08 +0000 Subject: [PATCH] Moved adapters to separate package. Added PayloadMethodEndpointAdapter. --- .../AbstractMethodEndpointAdapter.java | 78 ++++++++++++ .../MarshallingMethodEndpointAdapter.java | 3 +- .../adapter/PayloadMethodEndpointAdapter.java | 61 ++++++++++ .../ws/server/endpoint/adapter/package.html | 5 + .../MarshallingMethodEndpointAdapterTest.java | 3 +- .../PayloadMethodEndpointAdapterTest.java | 111 ++++++++++++++++++ 6 files changed, 259 insertions(+), 2 deletions(-) create mode 100644 sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/AbstractMethodEndpointAdapter.java rename sandbox/src/main/java/org/springframework/ws/server/endpoint/{ => adapter}/MarshallingMethodEndpointAdapter.java (97%) create mode 100644 sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapter.java create mode 100644 sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/package.html rename sandbox/src/test/java/org/springframework/ws/server/endpoint/{ => adapter}/MarshallingMethodEndpointAdapterTest.java (98%) create mode 100644 sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapterTest.java diff --git a/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/AbstractMethodEndpointAdapter.java b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/AbstractMethodEndpointAdapter.java new file mode 100644 index 00000000..4e8bc732 --- /dev/null +++ b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/AbstractMethodEndpointAdapter.java @@ -0,0 +1,78 @@ +/* + * 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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.ws.context.MessageContext; +import org.springframework.ws.server.EndpointAdapter; +import org.springframework.ws.server.endpoint.MethodEndpoint; +import org.springframework.xml.transform.TransformerObjectSupport; + +/** + * Abstract base class for {@link EndpointAdapter} implementations that support {@link + * org.springframework.ws.server.endpoint.MethodEndpoint}s. Contains template methods for handling method endpoints. + * + * @author Arjen Poutsma + */ +public abstract class AbstractMethodEndpointAdapter extends TransformerObjectSupport implements EndpointAdapter { + + /** Logger available to subclasses. */ + protected final Log logger = LogFactory.getLog(getClass()); + + /** + * Delegates to {@link #supportsInternal(org.springframework.ws.server.endpoint.MethodEndpoint)}. + * + * @param endpoint endpoint object to check + * @return whether or not this adapter can adapt the given endpoint + */ + public final boolean supports(Object endpoint) { + return endpoint instanceof MethodEndpoint && supportsInternal((MethodEndpoint) endpoint); + } + + /** + * Delegates to {@link #invokeInternal(org.springframework.ws.context.MessageContext,MethodEndpoint)}. + * + * @param messageContext the current message context + * @param endpoint the endpoint to use. This object must have previously been passed to the + * supportsInternal method of this interface, which must have returned + * true + * @throws Exception in case of errors + */ + public final void invoke(MessageContext messageContext, Object endpoint) throws Exception { + invokeInternal(messageContext, (MethodEndpoint) endpoint); + } + + /** + * Given a method endpoint, return whether or not this adapter can support it. + * + * @param methodEndpoint method endpoint to check + * @return whether or not this adapter can adapt the given method + */ + protected abstract boolean supportsInternal(MethodEndpoint methodEndpoint); + + /** + * Use the given method endpoint to handle the request. + * + * @param messageContext the current message context + * @param methodEndpoint the method endpoint to use + * @throws Exception in case of errors + */ + protected abstract void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) + throws Exception; + +} diff --git a/sandbox/src/main/java/org/springframework/ws/server/endpoint/MarshallingMethodEndpointAdapter.java b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapter.java similarity index 97% rename from sandbox/src/main/java/org/springframework/ws/server/endpoint/MarshallingMethodEndpointAdapter.java rename to sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapter.java index 84fb6446..dc6e975e 100644 --- a/sandbox/src/main/java/org/springframework/ws/server/endpoint/MarshallingMethodEndpointAdapter.java +++ b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.server.endpoint; +package org.springframework.ws.server.endpoint.adapter; import java.lang.reflect.Method; @@ -25,6 +25,7 @@ import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.context.MessageContext; import org.springframework.ws.server.EndpointMapping; +import org.springframework.ws.server.endpoint.MethodEndpoint; /** * Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature: diff --git a/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapter.java b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapter.java new file mode 100644 index 00000000..9da5e6d6 --- /dev/null +++ b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapter.java @@ -0,0 +1,61 @@ +/* + * 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: + *
+ * void handleMyMessage(Source request);
+ * 
+ * or + *
+ * Source handleMyMessage(Source request);
+ * 
+ * I.e. methods that take a single {@link Source} parameter, and return either void 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()); + } + } +} diff --git a/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/package.html b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/package.html new file mode 100644 index 00000000..febadcb4 --- /dev/null +++ b/sandbox/src/main/java/org/springframework/ws/server/endpoint/adapter/package.html @@ -0,0 +1,5 @@ + + +Provides miscellaneous endpoints EndpointAdapter implementations. + + diff --git a/sandbox/src/test/java/org/springframework/ws/server/endpoint/MarshallingMethodEndpointAdapterTest.java b/sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapterTest.java similarity index 98% rename from sandbox/src/test/java/org/springframework/ws/server/endpoint/MarshallingMethodEndpointAdapterTest.java rename to sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapterTest.java index abf55ad7..ae151765 100644 --- a/sandbox/src/test/java/org/springframework/ws/server/endpoint/MarshallingMethodEndpointAdapterTest.java +++ b/sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapterTest.java @@ -1,4 +1,4 @@ -package org.springframework.ws.server.endpoint; +package org.springframework.ws.server.endpoint.adapter; import java.lang.reflect.Method; @@ -9,6 +9,7 @@ import org.springframework.oxm.Unmarshaller; 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 MarshallingMethodEndpointAdapterTest extends TestCase { diff --git a/sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapterTest.java b/sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapterTest.java new file mode 100644 index 00000000..e1336f4e --- /dev/null +++ b/sandbox/src/test/java/org/springframework/ws/server/endpoint/adapter/PayloadMethodEndpointAdapterTest.java @@ -0,0 +1,111 @@ +/* + * 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(""); + 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; + } + +} \ No newline at end of file