Moved adapters to separate package. Added PayloadMethodEndpointAdapter.

This commit is contained in:
Arjen Poutsma
2007-04-03 21:55:08 +00:00
parent 7d32176e17
commit 956fb906d9
6 changed files with 259 additions and 2 deletions

View File

@@ -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
* <code>supportsInternal</code> method of this interface, which must have returned
* <code>true</code>
* @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;
}

View File

@@ -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:

View File

@@ -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:
* <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());
}
}
}

View File

@@ -0,0 +1,5 @@
<html>
<body>
Provides miscellaneous endpoints <code>EndpointAdapter</code> implementations.
</body>
</html>

View File

@@ -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 {

View File

@@ -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("<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;
}
}