Skip to content

Commit 45c3e8a

Browse files
committed
SWS-164
1 parent 2911973 commit 45c3e8a

10 files changed

+126
-13
lines changed

core/src/main/java/org/springframework/ws/server/endpoint/AbstractStaxEventPayloadEndpoint.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ private XMLEventFactory getEventFactory() {
7878
}
7979

8080
private XMLEventReader getEventReader(Source source) throws XMLStreamException, TransformerException {
81+
if (source == null) {
82+
return null;
83+
}
8184
XMLEventReader eventReader = null;
8285
if (source instanceof StaxSource) {
8386
StaxSource staxSource = (StaxSource) source;

core/src/main/java/org/springframework/ws/server/endpoint/AbstractStaxStreamPayloadEndpoint.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public final void invoke(MessageContext messageContext) throws Exception {
5454
}
5555

5656
private XMLStreamReader getStreamReader(Source source) throws XMLStreamException, TransformerException {
57+
if (source == null) {
58+
return null;
59+
}
5760
XMLStreamReader streamReader = null;
5861
if (source instanceof StaxSource) {
5962
streamReader = ((StaxSource) source).getXMLStreamReader();

core/src/main/java/org/springframework/ws/support/MarshallingUtils.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import javax.activation.DataHandler;
21+
import javax.xml.transform.Source;
2122

2223
import org.springframework.oxm.Marshaller;
2324
import org.springframework.oxm.Unmarshaller;
@@ -41,20 +42,27 @@ private MarshallingUtils() {
4142

4243
/**
4344
* Unmarshals the payload of the given message using the provided {@link Unmarshaller}.
45+
* <p/>
46+
* If the request message has no payload (i.e. {@link WebServiceMessage#getPayloadSource()} returns
47+
* <code>null</code>), this method will return <code>null</code>.
4448
*
4549
* @param unmarshaller the unmarshaller
4650
* @param message the message of which the payload is to be unmarshalled
4751
* @return the unmarshalled object
4852
* @throws IOException in case of I/O errors
4953
*/
5054
public static Object unmarshal(Unmarshaller unmarshaller, WebServiceMessage message) throws IOException {
51-
if (unmarshaller instanceof MimeUnmarshaller && message instanceof MimeMessage) {
55+
Source payload = message.getPayloadSource();
56+
if (payload == null) {
57+
return null;
58+
}
59+
else if (unmarshaller instanceof MimeUnmarshaller && message instanceof MimeMessage) {
5260
MimeUnmarshaller mimeUnmarshaller = (MimeUnmarshaller) unmarshaller;
5361
MimeMessageContainer container = new MimeMessageContainer((MimeMessage) message);
54-
return mimeUnmarshaller.unmarshal(message.getPayloadSource(), container);
62+
return mimeUnmarshaller.unmarshal(payload, container);
5563
}
5664
else {
57-
return unmarshaller.unmarshal(message.getPayloadSource());
65+
return unmarshaller.unmarshal(payload);
5866
}
5967
}
6068

core/src/test/java/org/springframework/ws/MockWebServiceMessage.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@
4545
*/
4646
public class MockWebServiceMessage implements FaultAwareWebServiceMessage {
4747

48-
private final StringBuffer content;
48+
private StringBuffer content;
4949

5050
private boolean fault = false;
5151

5252
private String faultReason;
5353

5454
public MockWebServiceMessage() {
55-
content = new StringBuffer();
5655
}
5756

5857
public MockWebServiceMessage(Source source) throws TransformerException {
@@ -71,14 +70,17 @@ public MockWebServiceMessage(StringBuffer content) {
7170
}
7271

7372
public MockWebServiceMessage(String content) {
74-
this.content = new StringBuffer(content);
73+
if (content != null) {
74+
this.content = new StringBuffer(content);
75+
}
7576
}
7677

7778
public String getPayloadAsString() {
78-
return content.toString();
79+
return content != null ? content.toString() : null;
7980
}
8081

8182
public void setPayload(InputStreamSource inputStreamSource) throws IOException {
83+
checkContent();
8284
InputStream is = null;
8385
try {
8486
is = inputStreamSource.getInputStream();
@@ -93,16 +95,24 @@ public void setPayload(InputStreamSource inputStreamSource) throws IOException {
9395
}
9496

9597
public void setPayload(String content) {
98+
checkContent();
9699
this.content.replace(0, this.content.length(), content);
97100
}
98101

102+
private void checkContent() {
103+
if (content == null) {
104+
content = new StringBuffer();
105+
}
106+
}
107+
99108
public Result getPayloadResult() {
109+
checkContent();
100110
content.setLength(0);
101111
return new StreamResult(new StringBufferWriter());
102112
}
103113

104114
public Source getPayloadSource() {
105-
return new StringSource(content.toString());
115+
return content != null ? new StringSource(content.toString()) : null;
106116
}
107117

108118
public boolean hasFault() {
@@ -122,13 +132,17 @@ public void setFaultReason(String faultReason) {
122132
}
123133

124134
public void writeTo(OutputStream outputStream) throws IOException {
125-
PrintWriter writer = new PrintWriter(outputStream);
126-
writer.write(content.toString());
135+
if (content != null) {
136+
PrintWriter writer = new PrintWriter(outputStream);
137+
writer.write(content.toString());
138+
}
127139
}
128140

129141
public String toString() {
130142
StringBuffer buffer = new StringBuffer("MockWebServiceMessage {");
131-
buffer.append(content);
143+
if (content != null) {
144+
buffer.append(content);
145+
}
132146
buffer.append('}');
133147
return buffer.toString();
134148
}

core/src/test/java/org/springframework/ws/server/endpoint/AbstractMessageEndpointTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public void testNoResponse() throws Exception {
4242
assertFalse("Response message created", context.hasResponse());
4343
}
4444

45+
public void testNoRequestPayload() throws Exception {
46+
endpoint = createNoRequestPayloadEndpoint();
47+
48+
MessageContext context = new DefaultMessageContext(new MockWebServiceMessage((StringBuffer) null),
49+
new MockWebServiceMessageFactory());
50+
endpoint.invoke(context);
51+
assertFalse("Response message created", context.hasResponse());
52+
}
53+
4554
protected final void testSource(Source requestSource) throws Exception {
4655
MessageContext context =
4756
new DefaultMessageContext(new MockWebServiceMessage(requestSource), new MockWebServiceMessageFactory());
@@ -52,6 +61,8 @@ protected final void testSource(Source requestSource) throws Exception {
5261

5362
protected abstract MessageEndpoint createNoResponseEndpoint();
5463

64+
protected abstract MessageEndpoint createNoRequestPayloadEndpoint();
65+
5566
protected abstract MessageEndpoint createResponseEndpoint();
5667

5768
}

core/src/test/java/org/springframework/ws/server/endpoint/MarshallingPayloadEndpointTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,25 @@ protected Object invokeInternal(Object requestObject) throws Exception {
143143
factoryControl.verify();
144144
}
145145

146+
public void testInvokeNoRequest() throws Exception {
147+
MockWebServiceMessage request = new MockWebServiceMessage((StringBuffer) null);
148+
context = new DefaultMessageContext(request, factoryMock);
149+
AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
150+
151+
protected Object invokeInternal(Object requestObject) throws Exception {
152+
assertNull("No request expected", requestObject);
153+
return null;
154+
}
155+
};
156+
endpoint.setMarshaller(new SimpleMarshaller());
157+
endpoint.setUnmarshaller(new SimpleMarshaller());
158+
endpoint.afterPropertiesSet();
159+
factoryControl.replay();
160+
endpoint.invoke(context);
161+
assertFalse("Response created", context.hasResponse());
162+
factoryControl.verify();
163+
}
164+
146165
public void testInvokeMimeMarshaller() throws Exception {
147166
MockControl unmarshallerControl = MockControl.createControl(MimeUnmarshaller.class);
148167
MimeUnmarshaller unmarshaller = (MimeUnmarshaller) unmarshallerControl.getMock();
@@ -187,7 +206,7 @@ protected Object invokeInternal(Object requestObject) throws Exception {
187206
messageControl.verify();
188207
}
189208

190-
private abstract static class SimpleMarshaller implements Marshaller, Unmarshaller {
209+
private static class SimpleMarshaller implements Marshaller, Unmarshaller {
191210

192211
public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
193212
fail("Not expected");

core/src/test/java/org/springframework/ws/server/endpoint/StaxEventPayloadEndpointTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ protected MessageEndpoint createNoResponseEndpoint() {
3737
protected void invokeInternal(XMLEventReader eventReader,
3838
XMLEventConsumer eventWriter,
3939
XMLEventFactory eventFactory) throws Exception {
40+
assertNotNull("No EventReader passed", eventReader);
41+
}
42+
};
43+
}
44+
45+
protected MessageEndpoint createNoRequestPayloadEndpoint() {
46+
return new AbstractStaxEventPayloadEndpoint() {
47+
48+
protected void invokeInternal(XMLEventReader eventReader,
49+
XMLEventConsumer eventWriter,
50+
XMLEventFactory eventFactory) throws Exception {
51+
assertNull("EventReader passed", eventReader);
4052
}
4153
};
4254
}

core/src/test/java/org/springframework/ws/server/endpoint/StaxStreamPayloadEndpointTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public class StaxStreamPayloadEndpointTest extends AbstractMessageEndpointTestCa
4242
protected MessageEndpoint createNoResponseEndpoint() {
4343
return new AbstractStaxStreamPayloadEndpoint() {
4444
protected void invokeInternal(XMLStreamReader streamReader, XMLStreamWriter streamWriter) throws Exception {
45+
assertNotNull("No StreamReader passed", streamReader);
46+
}
47+
};
48+
}
49+
50+
protected MessageEndpoint createNoRequestPayloadEndpoint() {
51+
return new AbstractStaxStreamPayloadEndpoint() {
52+
protected void invokeInternal(XMLStreamReader streamReader, XMLStreamWriter streamWriter) throws Exception {
53+
assertNull("StreamReader passed", streamReader);
4554
}
4655
};
4756
}

core/src/test/java/org/springframework/ws/server/endpoint/adapter/MarshallingMethodEndpointAdapterTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.easymock.MockControl;
77
import org.springframework.oxm.Marshaller;
88
import org.springframework.oxm.Unmarshaller;
9+
import org.springframework.ws.MockWebServiceMessage;
910
import org.springframework.ws.MockWebServiceMessageFactory;
1011
import org.springframework.ws.context.DefaultMessageContext;
1112
import org.springframework.ws.context.MessageContext;
@@ -38,7 +39,8 @@ protected void setUp() throws Exception {
3839
unmarshallerMock = (Unmarshaller) unmarshallerControl.getMock();
3940
adapter.setUnmarshaller(unmarshallerMock);
4041
adapter.afterPropertiesSet();
41-
messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
42+
MockWebServiceMessage request = new MockWebServiceMessage("<request/>");
43+
messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
4244
}
4345

4446
public void testNoResponse() throws Exception {
@@ -56,6 +58,20 @@ public void testNoResponse() throws Exception {
5658
unmarshallerControl.verify();
5759
}
5860

61+
public void testNoRequestPayload() throws Exception {
62+
MockWebServiceMessage request = new MockWebServiceMessage();
63+
messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
64+
Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class});
65+
MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
66+
marshallerControl.replay();
67+
unmarshallerControl.replay();
68+
assertFalse("Method invoked", noResponseInvoked);
69+
adapter.invoke(messageContext, methodEndpoint);
70+
assertTrue("Method not invoked", noResponseInvoked);
71+
marshallerControl.verify();
72+
unmarshallerControl.verify();
73+
}
74+
5975
public void testResponse() throws Exception {
6076
Method response = getClass().getMethod("response", new Class[]{MyType.class});
6177
MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);

core/src/test/java/org/springframework/ws/support/MarshallingUtilsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ public void testUnmarshalMime() throws Exception {
7676
messageControl.verify();
7777
}
7878

79+
public void testUnmarshalNoPayload() throws Exception {
80+
MockControl unmarshallerControl = MockControl.createControl(MimeUnmarshaller.class);
81+
MimeUnmarshaller unmarshallerMock = (MimeUnmarshaller) unmarshallerControl.getMock();
82+
MockControl messageControl = MockControl.createControl(MimeMessage.class);
83+
MimeMessage messageMock = (MimeMessage) messageControl.getMock();
84+
85+
messageControl.expectAndReturn(messageMock.getPayloadSource(), null);
86+
87+
unmarshallerControl.replay();
88+
messageControl.replay();
89+
90+
Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);
91+
assertNull("Invalid unmarshalled object", result);
92+
93+
unmarshallerControl.verify();
94+
messageControl.verify();
95+
}
96+
7997
public void testMarshal() throws Exception {
8098
MockControl marshallerControl = MockControl.createControl(Marshaller.class);
8199
Marshaller marshallerMock = (Marshaller) marshallerControl.getMock();

0 commit comments

Comments
 (0)