SWS-351 - Arbitrary parameter injection for @Endpoints

This commit is contained in:
Arjen Poutsma
2010-05-03 10:23:49 +00:00
parent a9cc3333d1
commit f57b0b2e7a
13 changed files with 98 additions and 62 deletions

View File

@@ -24,8 +24,6 @@ import javax.xml.transform.stream.StreamResult;
import org.springframework.core.MethodParameter;
import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.xml.transform.TransformerObjectSupport;
@@ -46,7 +44,7 @@ public abstract class AbstractPayloadMethodProcessor extends TransformerObjectSu
* {@inheritDoc}
* <p/>
* This implementation gets checks if the given parameter is annotated with {@link RequestPayload}, and invokes
* {@link #supportsRequestPayloadParameter(MethodParameter)} afterwards.
* {@link #supportsRequestPayloadParameter(org.springframework.core.MethodParameter)} afterwards.
*/
public final boolean supportsParameter(MethodParameter parameter) {
Assert.isTrue(parameter.getParameterIndex() >= 0, "Parameter index larger smaller than 0");
@@ -67,35 +65,13 @@ public abstract class AbstractPayloadMethodProcessor extends TransformerObjectSu
*/
protected abstract boolean supportsRequestPayloadParameter(MethodParameter parameter);
public final Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws Exception {
Source requestPayload = getRequestPayload(messageContext);
return requestPayload != null ? resolveRequestPayloadArgument(parameter, requestPayload) : null;
}
/** Returns the request payload as {@code Source}. */
protected Source getRequestPayload(MessageContext messageContext) {
WebServiceMessage request = messageContext.getRequest();
return request != null ? request.getPayloadSource() : null;
}
/**
* Resolves the given parameter, annotated with {@link RequestPayload}, into a method argument.
*
* @param parameter the parameter to resolve to an argument
* @param requestPayload the request payload
* @return the resolved argument. May be {@code null}.
* @throws Exception in case of errors
*/
protected abstract Object resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload)
throws Exception;
// MethodReturnValueHandler
/**
* {@inheritDoc}
* <p/>
* This implementation gets checks if the method of the given return type is annotated with {@link ResponsePayload},
* and invokes {@link #supportsResponsePayloadReturnType(MethodParameter)} afterwards.
* and invokes {@link #supportsResponsePayloadReturnType(org.springframework.core.MethodParameter)} afterwards.
*/
public final boolean supportsReturnType(MethodParameter returnType) {
Assert.isTrue(returnType.getParameterIndex() == -1, "Parameter index is not -1");
@@ -116,38 +92,16 @@ public abstract class AbstractPayloadMethodProcessor extends TransformerObjectSu
*/
protected abstract boolean supportsResponsePayloadReturnType(MethodParameter returnType);
public final void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
throws Exception {
if (returnValue != null) {
Source responsePayload = createResponsePayload(returnType, returnValue);
if (responsePayload != null) {
WebServiceMessage response = messageContext.getResponse();
transform(responsePayload, response.getPayloadResult());
}
}
}
/**
* Creates a response payload for the given return value.
*
* @param returnType the return type to handle
* @param returnValue the return value to handle
* @return the response payload
* @throws Exception in case of errors
*/
protected abstract Source createResponsePayload(MethodParameter returnType, Object returnValue) throws Exception;
/**
* Converts the given source to a byte array input stream.
*
* @param source the source to convert
* @return the input stream
* @throws TransformerException in case of transformation errors
* @throws javax.xml.transform.TransformerException in case of transformation errors
*/
protected ByteArrayInputStream convertToByteArrayInputStream(Source source) throws TransformerException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transform(source, new StreamResult(bos));
return new ByteArrayInputStream(bos.toByteArray());
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2005-2010 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.method;
import javax.xml.transform.Source;
import org.springframework.core.MethodParameter;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
/**
* Abstract base class for {@link MethodArgumentResolver} and {@link MethodReturnValueHandler} implementations based on
* {@link Source}s.
*
* @author Arjen Poutsma
* @since 2.0
*/
public abstract class AbstractPayloadSourceMethodProcessor extends AbstractPayloadMethodProcessor {
// MethodArgumentResolver
public final Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws Exception {
Source requestPayload = getRequestPayload(messageContext);
return requestPayload != null ? resolveRequestPayloadArgument(parameter, requestPayload) : null;
}
/** Returns the request payload as {@code Source}. */
private Source getRequestPayload(MessageContext messageContext) {
WebServiceMessage request = messageContext.getRequest();
return request != null ? request.getPayloadSource() : null;
}
/**
* Resolves the given parameter, annotated with {@link RequestPayload}, into a method argument.
*
* @param parameter the parameter to resolve to an argument
* @param requestPayload the request payload
* @return the resolved argument. May be {@code null}.
* @throws Exception in case of errors
*/
protected abstract Object resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload)
throws Exception;
// MethodReturnValueHandler
public final void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
throws Exception {
if (returnValue != null) {
Source responsePayload = createResponsePayload(returnType, returnValue);
if (responsePayload != null) {
WebServiceMessage response = messageContext.getResponse();
transform(responsePayload, response.getPayloadResult());
}
}
}
/**
* Creates a response payload for the given return value.
*
* @param returnType the return type to handle
* @param returnValue the return value to handle
* @return the response payload
* @throws Exception in case of errors
*/
protected abstract Source createResponsePayload(MethodParameter returnType, Object returnValue) throws Exception;
}

View File

@@ -35,7 +35,7 @@ import org.dom4j.io.DocumentSource;
* @author Arjen Poutsma
* @since 2.0
*/
public class Dom4jPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
public class Dom4jPayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
@Override
protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {

View File

@@ -33,7 +33,7 @@ import org.w3c.dom.Node;
* @author Arjen Poutsma
* @since 2.0
*/
public class DomPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
public class DomPayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
// MethodArgumentResolver

View File

@@ -35,7 +35,7 @@ import org.w3c.dom.Node;
* @author Arjen Poutsma
* @since 2.0
*/
public class JDomPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
public class JDomPayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
@Override
protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {

View File

@@ -36,7 +36,7 @@ import org.xml.sax.InputSource;
* @author Arjen Poutsma
* @since 2.0
*/
public class SourcePayloadMethodProcessor extends AbstractPayloadMethodProcessor {
public class SourcePayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
// MethodArgumentResolver

View File

@@ -41,7 +41,7 @@ import org.w3c.dom.DOMImplementation;
* @author Arjen Poutsma
* @since 2.0
*/
public class XomPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
public class XomPayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
private DocumentBuilderFactory documentBuilderFactory = createDocumentBuilderFactory();

View File

@@ -31,7 +31,7 @@ import static org.junit.Assert.assertTrue;
public abstract class AbstractPayloadMethodProcessorTest extends AbstractMethodArgumentResolverTest {
private AbstractPayloadMethodProcessor processor;
private AbstractPayloadSourceMethodProcessor processor;
private MethodParameter[] supportedParameters;
@@ -44,7 +44,7 @@ public abstract class AbstractPayloadMethodProcessorTest extends AbstractMethodA
supportedReturnTypes = createSupportedReturnTypes();
}
protected abstract AbstractPayloadMethodProcessor createProcessor();
protected abstract AbstractPayloadSourceMethodProcessor createProcessor();
protected abstract MethodParameter[] createSupportedParameters() throws NoSuchMethodException;

View File

@@ -30,7 +30,7 @@ import static org.junit.Assert.assertTrue;
public class Dom4jPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
protected AbstractPayloadSourceMethodProcessor createProcessor() {
return new Dom4jPayloadMethodProcessor();
}

View File

@@ -33,7 +33,7 @@ import static org.junit.Assert.assertTrue;
public class DomPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
protected AbstractPayloadSourceMethodProcessor createProcessor() {
return new DomPayloadMethodProcessor();
}

View File

@@ -28,7 +28,7 @@ import static org.junit.Assert.assertTrue;
public class JDomPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
protected AbstractPayloadSourceMethodProcessor createProcessor() {
return new JDomPayloadMethodProcessor();
}

View File

@@ -30,7 +30,7 @@ import org.springframework.xml.transform.StringSource;
public class SourcePayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
protected AbstractPayloadSourceMethodProcessor createProcessor() {
return new SourcePayloadMethodProcessor();
}

View File

@@ -28,7 +28,7 @@ import static org.junit.Assert.assertTrue;
public class XomPayloadMethodProcessorTest extends AbstractPayloadMethodProcessorTest {
@Override
protected AbstractPayloadMethodProcessor createProcessor() {
protected AbstractPayloadSourceMethodProcessor createProcessor() {
return new XomPayloadMethodProcessor();
}