INT-2655: (un)marshal Only via WebServiceTemplate

JIRA: https://jira.spring.io/browse/INT-2655

There might be some use-cases when end-user expect more control over sending/receiving messages via `WebServiceTemplate`.
It would be better to delegate (un)marshalling logic to the `WebServiceTemplate.marshalSendAndReceive()` directly.

Fix Checkstyle violations

Polishing
This commit is contained in:
Artem Bilan
2017-02-16 11:44:31 -05:00
committed by Gary Russell
parent b0094b32b7
commit dda2fea60e
4 changed files with 78 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -66,7 +66,7 @@ public abstract class AbstractWebServiceOutboundGateway extends AbstractReplyPro
private final Map<String, Expression> uriVariableExpressions = new HashMap<String, Expression>();
private volatile StandardEvaluationContext evaluationContext;
private volatile StandardEvaluationContext evaluationContext;
private volatile WebServiceMessageCallback requestCallback;
@@ -140,6 +140,10 @@ public abstract class AbstractWebServiceOutboundGateway extends AbstractReplyPro
}
public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
doSetWebServiceTemplate(webServiceTemplate);
}
protected final void doSetWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
Assert.notNull(webServiceTemplate, "'webServiceTemplate' must not be null");
this.webServiceTemplate = webServiceTemplate;
this.webServiceTemplateExplicitlySet = true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -25,8 +25,8 @@ import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.support.destination.DestinationProvider;
import org.springframework.ws.support.MarshallingUtils;
/**
* An outbound Messaging Gateway for invoking Web Services that also supports
@@ -35,18 +35,18 @@ import org.springframework.ws.support.MarshallingUtils;
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
* @see Marshaller
* @see Unmarshaller
*/
public class MarshallingWebServiceOutboundGateway extends AbstractWebServiceOutboundGateway {
private volatile Marshaller marshaller;
private volatile Unmarshaller unmarshaller;
public MarshallingWebServiceOutboundGateway(DestinationProvider destinationProvider, Marshaller marshaller,
Unmarshaller unmarshaller, WebServiceMessageFactory messageFactory) {
super(destinationProvider, messageFactory);
this.configureMarshallers(marshaller, unmarshaller);
configureMarshallers(marshaller, unmarshaller);
}
public MarshallingWebServiceOutboundGateway(DestinationProvider destinationProvider, Marshaller marshaller,
@@ -66,7 +66,7 @@ public class MarshallingWebServiceOutboundGateway extends AbstractWebServiceOutb
public MarshallingWebServiceOutboundGateway(String uri, Marshaller marshaller, Unmarshaller unmarshaller,
WebServiceMessageFactory messageFactory) {
super(uri, messageFactory);
this.configureMarshallers(marshaller, unmarshaller);
configureMarshallers(marshaller, unmarshaller);
}
public MarshallingWebServiceOutboundGateway(String uri, Marshaller marshaller, Unmarshaller unmarshaller) {
@@ -82,23 +82,32 @@ public class MarshallingWebServiceOutboundGateway extends AbstractWebServiceOutb
this(uri, marshaller, (WebServiceMessageFactory) null);
}
@Override
protected Object doHandle(String uri, Message<?> requestMessage, WebServiceMessageCallback requestCallback) {
Object reply = this.getWebServiceTemplate().sendAndReceive(uri,
new MarshallingRequestMessageCallback(requestCallback, requestMessage),
new MarshallingResponseMessageExtractor());
return reply;
/**
* Construct an instance based on the provided Web Service URI and {@code WebServiceTemplate}.
* @param uri the Web Service URI to use
* @param webServiceTemplate the WebServiceTemplate
* @since 5.0
*/
public MarshallingWebServiceOutboundGateway(String uri, WebServiceTemplate webServiceTemplate) {
super(uri, null);
doSetWebServiceTemplate(webServiceTemplate);
}
@Override
public String getComponentType() {
return "ws:outbound-gateway(marshaling)";
/**
* Construct an instance based on the provided {@code DestinationProvider} and {@code WebServiceTemplate}.
* @param destinationProvider the {@link DestinationProvider} to resolve Web Service URI at runtime
* @param webServiceTemplate the WebServiceTemplate
* @since 5.0
*/
public MarshallingWebServiceOutboundGateway(DestinationProvider destinationProvider,
WebServiceTemplate webServiceTemplate) {
super(destinationProvider, null);
doSetWebServiceTemplate(webServiceTemplate);
}
/**
* Sets the provided Marshaller and Unmarshaller on this gateway's WebServiceTemplate.
* Neither may be null.
*
* @param marshaller The marshaller.
* @param unmarshaller The unmarshaller.
*/
@@ -107,39 +116,39 @@ public class MarshallingWebServiceOutboundGateway extends AbstractWebServiceOutb
if (unmarshaller == null) {
Assert.isInstanceOf(Unmarshaller.class, marshaller,
"Marshaller [" + marshaller + "] does not implement the Unmarshaller interface. " +
"Please set an Unmarshaller explicitly by using one of the constructors that accepts " +
"both Marshaller and Unmarshaller arguments.");
"Please set an Unmarshaller explicitly by using one of the constructors that accepts " +
"both Marshaller and Unmarshaller arguments.");
unmarshaller = (Unmarshaller) marshaller;
}
Assert.notNull(unmarshaller, "unmarshaller must not be null");
this.marshaller = marshaller;
this.unmarshaller = unmarshaller;
getWebServiceTemplate().setMarshaller(marshaller);
getWebServiceTemplate().setUnmarshaller(unmarshaller);
}
private final class MarshallingRequestMessageCallback extends RequestMessageCallback {
@Override
public String getComponentType() {
return "ws:outbound-gateway(marshalling)";
}
MarshallingRequestMessageCallback(WebServiceMessageCallback requestCallback,
Message<?> requestMessage) {
@Override
protected Object doHandle(String uri, Message<?> requestMessage, WebServiceMessageCallback requestCallback) {
return getWebServiceTemplate()
.marshalSendAndReceive(uri, requestMessage.getPayload(),
new PassThroughRequestMessageCallback(requestCallback, requestMessage));
}
private final class PassThroughRequestMessageCallback extends RequestMessageCallback {
PassThroughRequestMessageCallback(WebServiceMessageCallback requestCallback, Message<?> requestMessage) {
super(requestCallback, requestMessage);
}
@Override
public void doWithMessageInternal(WebServiceMessage message, Object payload) throws IOException {
MarshallingUtils.marshal(MarshallingWebServiceOutboundGateway.this.marshaller, payload, message);
}
}
private class MarshallingResponseMessageExtractor extends ResponseMessageExtractor {
MarshallingResponseMessageExtractor() {
super();
}
@Override
public Object doExtractData(WebServiceMessage message) throws IOException {
return MarshallingUtils.unmarshal(MarshallingWebServiceOutboundGateway.this.unmarshaller, message);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -56,7 +56,7 @@ public class SimpleWebServiceOutboundGateway extends AbstractWebServiceOutboundG
public SimpleWebServiceOutboundGateway(DestinationProvider destinationProvider,
SourceExtractor<?> sourceExtractor) {
this(destinationProvider, sourceExtractor, (WebServiceMessageFactory) null);
this(destinationProvider, sourceExtractor, null);
}
public SimpleWebServiceOutboundGateway(DestinationProvider destinationProvider, SourceExtractor<?> sourceExtractor,
@@ -70,7 +70,7 @@ public class SimpleWebServiceOutboundGateway extends AbstractWebServiceOutboundG
}
public SimpleWebServiceOutboundGateway(String uri, SourceExtractor<?> sourceExtractor) {
this(uri, sourceExtractor, (WebServiceMessageFactory) null);
this(uri, sourceExtractor, null);
}
public SimpleWebServiceOutboundGateway(String uri, SourceExtractor<?> sourceExtractor,
@@ -160,7 +160,7 @@ public class SimpleWebServiceOutboundGateway extends AbstractWebServiceOutboundG
return this.result.toString();
}
else if (this.result instanceof DOMResult) {
return ((DOMResult) this.result).getNode();
return ((DOMResult) this.result).getNode();
}
else {
return this.result;

View File

@@ -212,6 +212,7 @@ public class WebServiceOutboundGatewayParserTests {
WebServiceMessageSender messageSender = (WebServiceMessageSender) context.getBean("messageSender");
assertEquals(messageSender, ((WebServiceMessageSender[]) accessor.getPropertyValue("messageSenders"))[0]);
}
@Test
public void simpleGatewayWithCustomMessageSenderList() {
AbstractEndpoint endpoint = this.context.getBean("gatewayWithCustomMessageSenderList", AbstractEndpoint.class);
@@ -283,10 +284,10 @@ public class WebServiceOutboundGatewayParserTests {
"marshallingWebServiceOutboundGatewayParserTests.xml", this.getClass());
AbstractEndpoint endpoint = (AbstractEndpoint) context.getBean("gatewayWithAllInOneMarshaller");
assertEquals(EventDrivenConsumer.class, endpoint.getClass());
MarshallingWebServiceOutboundGateway gateway = (MarshallingWebServiceOutboundGateway) new DirectFieldAccessor(endpoint).getPropertyValue("handler");
Marshaller marshaller = (Marshaller) context.getBean("marshallerAndUnmarshaller");
assertEquals(marshaller, TestUtils.getPropertyValue(gateway, "marshaller", Marshaller.class));
assertEquals(marshaller, TestUtils.getPropertyValue(gateway, "unmarshaller", Unmarshaller.class));
Object gateway = TestUtils.getPropertyValue(endpoint, "handler");
Marshaller marshaller = context.getBean("marshallerAndUnmarshaller", Marshaller.class);
assertSame(marshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.marshaller", Marshaller.class));
assertSame(marshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.unmarshaller", Unmarshaller.class));
context.close();
}
@@ -296,11 +297,11 @@ public class WebServiceOutboundGatewayParserTests {
"marshallingWebServiceOutboundGatewayParserTests.xml", this.getClass());
AbstractEndpoint endpoint = (AbstractEndpoint) context.getBean("gatewayWithSeparateMarshallerAndUnmarshaller");
assertEquals(EventDrivenConsumer.class, endpoint.getClass());
MarshallingWebServiceOutboundGateway gateway = (MarshallingWebServiceOutboundGateway) new DirectFieldAccessor(endpoint).getPropertyValue("handler");
Marshaller marshaller = (Marshaller) context.getBean("marshaller");
Unmarshaller unmarshaller = (Unmarshaller) context.getBean("unmarshaller");
assertEquals(marshaller, TestUtils.getPropertyValue(gateway, "marshaller", Marshaller.class));
assertEquals(unmarshaller, TestUtils.getPropertyValue(gateway, "unmarshaller", Unmarshaller.class));
Object gateway = TestUtils.getPropertyValue(endpoint, "handler");
Marshaller marshaller = context.getBean("marshaller", Marshaller.class);
Unmarshaller unmarshaller = context.getBean("unmarshaller", Unmarshaller.class);
assertSame(marshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.marshaller", Marshaller.class));
assertSame(unmarshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.unmarshaller", Unmarshaller.class));
context.close();
}
@@ -310,7 +311,7 @@ public class WebServiceOutboundGatewayParserTests {
"marshallingWebServiceOutboundGatewayParserTests.xml", this.getClass());
AbstractEndpoint endpoint = (AbstractEndpoint) context.getBean("gatewayWithCustomRequestCallback");
assertEquals(EventDrivenConsumer.class, endpoint.getClass());
Object gateway = new DirectFieldAccessor(endpoint).getPropertyValue("handler");
Object gateway = TestUtils.getPropertyValue(endpoint, "handler");
assertEquals(MarshallingWebServiceOutboundGateway.class, gateway.getClass());
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
WebServiceMessageCallback callback = (WebServiceMessageCallback) context.getBean("requestCallback");
@@ -324,10 +325,10 @@ public class WebServiceOutboundGatewayParserTests {
"marshallingWebServiceOutboundGatewayParserTests.xml", this.getClass());
AbstractEndpoint endpoint = (AbstractEndpoint) context.getBean("gatewayWithAllInOneMarshallerAndMessageFactory");
assertEquals(EventDrivenConsumer.class, endpoint.getClass());
MarshallingWebServiceOutboundGateway gateway = (MarshallingWebServiceOutboundGateway) new DirectFieldAccessor(endpoint).getPropertyValue("handler");
Marshaller marshaller = (Marshaller) context.getBean("marshallerAndUnmarshaller");
assertEquals(marshaller, TestUtils.getPropertyValue(gateway, "marshaller", Marshaller.class));
assertEquals(marshaller, TestUtils.getPropertyValue(gateway, "unmarshaller", Unmarshaller.class));
Object gateway = TestUtils.getPropertyValue(endpoint, "handler");
Marshaller marshaller = context.getBean("marshallerAndUnmarshaller", Marshaller.class);
assertSame(marshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.marshaller", Marshaller.class));
assertSame(marshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.unmarshaller", Unmarshaller.class));
WebServiceMessageFactory messageFactory = (WebServiceMessageFactory) context.getBean("messageFactory");
assertEquals(messageFactory, TestUtils.getPropertyValue(gateway, "webServiceTemplate.messageFactory"));
@@ -340,13 +341,16 @@ public class WebServiceOutboundGatewayParserTests {
"marshallingWebServiceOutboundGatewayParserTests.xml", this.getClass());
AbstractEndpoint endpoint = (AbstractEndpoint) context.getBean("gatewayWithSeparateMarshallerAndUnmarshallerAndMessageFactory");
assertEquals(EventDrivenConsumer.class, endpoint.getClass());
MarshallingWebServiceOutboundGateway gateway = (MarshallingWebServiceOutboundGateway) new DirectFieldAccessor(endpoint).getPropertyValue("handler");
Marshaller marshaller = (Marshaller) context.getBean("marshaller");
Unmarshaller unmarshaller = (Unmarshaller) context.getBean("unmarshaller");
assertEquals(marshaller, TestUtils.getPropertyValue(gateway, "marshaller", Marshaller.class));
assertEquals(unmarshaller, TestUtils.getPropertyValue(gateway, "unmarshaller", Unmarshaller.class));
WebServiceMessageFactory messageFactory = (WebServiceMessageFactory) context.getBean("messageFactory");
Object gateway = TestUtils.getPropertyValue(endpoint, "handler");
Marshaller marshaller = context.getBean("marshaller", Marshaller.class);
Unmarshaller unmarshaller = context.getBean("unmarshaller", Unmarshaller.class);
assertSame(marshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.marshaller", Marshaller.class));
assertSame(unmarshaller, TestUtils.getPropertyValue(gateway, "webServiceTemplate.unmarshaller", Unmarshaller.class));
WebServiceMessageFactory messageFactory = context.getBean("messageFactory", WebServiceMessageFactory.class);
assertEquals(messageFactory, TestUtils.getPropertyValue(gateway, "webServiceTemplate.messageFactory"));
context.close();
}