Skip to content

Commit 596b824

Browse files
committed
SWS-672 - Allow MarshallingPayloadMethodProcessor to have a null marshaller or unmarshaller
1 parent b271489 commit 596b824

File tree

2 files changed

+108
-13
lines changed

2 files changed

+108
-13
lines changed

core/src/main/java/org/springframework/ws/server/endpoint/adapter/method/MarshallingPayloadMethodProcessor.java

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.ws.server.endpoint.adapter.method;
1818

19-
import org.springframework.beans.factory.InitializingBean;
2019
import org.springframework.core.MethodParameter;
2120
import org.springframework.oxm.GenericMarshaller;
2221
import org.springframework.oxm.GenericUnmarshaller;
@@ -34,53 +33,88 @@
3433
* @author Arjen Poutsma
3534
* @since 2.0
3635
*/
37-
public class MarshallingPayloadMethodProcessor extends AbstractPayloadMethodProcessor implements InitializingBean {
36+
public class MarshallingPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
3837

3938
private Marshaller marshaller;
4039

4140
private Unmarshaller unmarshaller;
4241

42+
/**
43+
* Creates a new {@code MarshallingPayloadMethodProcessor}. The {@link Marshaller} and {@link Unmarshaller} must be
44+
* injected using properties.
45+
*
46+
* @see #setMarshaller(Marshaller)
47+
* @see #setUnmarshaller(Unmarshaller)
48+
*/
4349
public MarshallingPayloadMethodProcessor() {
4450
}
4551

52+
/**
53+
* Creates a new {@code MarshallingPayloadMethodProcessor} with the given marshaller. If the given {@link
54+
* Marshaller} also implements the {@link Unmarshaller} interface, it is used for both marshalling and
55+
* unmarshalling. Otherwise, an exception is thrown.
56+
* <p/>
57+
* Note that all {@link Marshaller} implementations in Spring also implement the {@link Unmarshaller} interface, so
58+
* that you can safely use this constructor.
59+
*
60+
* @param marshaller object used as marshaller and unmarshaller
61+
* @throws IllegalArgumentException when {@code marshaller} does not implement the {@link Unmarshaller} interface
62+
*/
4663
public MarshallingPayloadMethodProcessor(Marshaller marshaller) {
4764
Assert.notNull(marshaller, "marshaller must not be null");
4865
Assert.isInstanceOf(Unmarshaller.class, marshaller);
4966
setMarshaller(marshaller);
5067
setUnmarshaller((Unmarshaller) marshaller);
5168
}
5269

70+
/**
71+
* Creates a new {@code MarshallingPayloadMethodProcessor} with the given marshaller and unmarshaller.
72+
*
73+
* @param marshaller the marshaller to use
74+
* @param unmarshaller the unmarshaller to use
75+
*/
5376
public MarshallingPayloadMethodProcessor(Marshaller marshaller, Unmarshaller unmarshaller) {
5477
Assert.notNull(marshaller, "marshaller must not be null");
5578
Assert.notNull(unmarshaller, "unmarshaller must not be null");
5679
setMarshaller(marshaller);
5780
setUnmarshaller(unmarshaller);
5881
}
5982

83+
/**
84+
* Returns the marshaller used for transforming objects into XML.
85+
*/
6086
public Marshaller getMarshaller() {
6187
return marshaller;
6288
}
6389

90+
/**
91+
* Sets the marshaller used for transforming objects into XML.
92+
*/
6493
public void setMarshaller(Marshaller marshaller) {
6594
this.marshaller = marshaller;
6695
}
6796

97+
/**
98+
* Returns the unmarshaller used for transforming XML into objects.
99+
*/
68100
public Unmarshaller getUnmarshaller() {
69101
return unmarshaller;
70102
}
71103

104+
/**
105+
* Sets the unmarshaller used for transforming XML into objects.
106+
*/
72107
public void setUnmarshaller(Unmarshaller unmarshaller) {
73108
this.unmarshaller = unmarshaller;
74109
}
75110

76-
public void afterPropertiesSet() throws Exception {
77-
Assert.notNull(marshaller, "marshaller is required");
78-
Assert.notNull(unmarshaller, "unmarshaller is required");
79-
}
80-
81111
@Override
82112
protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
83-
if (unmarshaller instanceof GenericUnmarshaller) {
113+
Unmarshaller unmarshaller = getUnmarshaller();
114+
if (unmarshaller == null) {
115+
return false;
116+
}
117+
else if (unmarshaller instanceof GenericUnmarshaller) {
84118
return ((GenericUnmarshaller) unmarshaller).supports(parameter.getGenericParameterType());
85119
}
86120
else {
@@ -89,8 +123,11 @@ protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
89123
}
90124

91125
public Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws Exception {
126+
Unmarshaller unmarshaller = getUnmarshaller();
127+
Assert.state(unmarshaller != null, "unmarshaller must not be null");
128+
92129
WebServiceMessage request = messageContext.getRequest();
93-
Object argument = MarshallingUtils.unmarshal(getUnmarshaller(), request);
130+
Object argument = MarshallingUtils.unmarshal(unmarshaller, request);
94131
if (logger.isDebugEnabled()) {
95132
logger.debug("Unmarshalled payload request to [" + argument + "]");
96133
}
@@ -99,7 +136,11 @@ public Object resolveArgument(MessageContext messageContext, MethodParameter par
99136

100137
@Override
101138
protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
102-
if (marshaller instanceof GenericMarshaller) {
139+
Marshaller marshaller = getMarshaller();
140+
if (marshaller == null) {
141+
return false;
142+
}
143+
else if (marshaller instanceof GenericMarshaller) {
103144
GenericMarshaller genericMarshaller = (GenericMarshaller) marshaller;
104145
return genericMarshaller.supports(returnType.getGenericParameterType());
105146
}
@@ -110,11 +151,14 @@ protected boolean supportsResponsePayloadReturnType(MethodParameter returnType)
110151

111152
public void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
112153
throws Exception {
154+
Marshaller marshaller = getMarshaller();
155+
Assert.state(marshaller != null, "marshaller must not be null");
156+
113157
if (logger.isDebugEnabled()) {
114158
logger.debug("Marshalling [" + returnValue + "] to response payload");
115159
}
116160
WebServiceMessage response = messageContext.getResponse();
117-
MarshallingUtils.marshal(getMarshaller(), returnValue, response);
161+
MarshallingUtils.marshal(marshaller, returnValue, response);
118162
}
119163

120164
}

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -76,6 +76,19 @@ public void supportsParameterUnsupported() {
7676
verify(marshaller, unmarshaller);
7777
}
7878

79+
@Test
80+
public void supportsParameterNoUnmarshallerSupported() {
81+
processor = new MarshallingPayloadMethodProcessor();
82+
processor.setMarshaller(marshaller);
83+
84+
replay(marshaller, unmarshaller);
85+
86+
assertFalse("processor supports parameter with no unmarshaller set",
87+
processor.supportsParameter(supportedParameter));
88+
89+
verify(marshaller, unmarshaller);
90+
}
91+
7992
@Test
8093
public void supportsReturnTypeSupported() {
8194
expect(marshaller.supports(isA(Type.class))).andReturn(true);
@@ -98,6 +111,20 @@ public void supportsReturnTypeUnsupported() {
98111
verify(marshaller, unmarshaller);
99112
}
100113

114+
@Test
115+
public void supportsReturnTypeNoMarshaller() {
116+
processor = new MarshallingPayloadMethodProcessor();
117+
processor.setUnmarshaller(unmarshaller);
118+
119+
replay(marshaller, unmarshaller);
120+
121+
assertFalse("processor supports return type with no marshaller set",
122+
processor.supportsReturnType(supportedReturnType));
123+
124+
verify(marshaller, unmarshaller);
125+
}
126+
127+
101128
@Test
102129
public void resolveArgument() throws Exception {
103130
MyObject expected = new MyObject();
@@ -113,6 +140,17 @@ public void resolveArgument() throws Exception {
113140
verify(marshaller, unmarshaller);
114141
}
115142

143+
@Test(expected = IllegalStateException.class)
144+
public void resolveArgumentNoUnmarshaller() throws Exception {
145+
processor = new MarshallingPayloadMethodProcessor();
146+
processor.setMarshaller(marshaller);
147+
148+
replay(marshaller, unmarshaller);
149+
MessageContext messageContext = createMockMessageContext();
150+
151+
processor.resolveArgument(messageContext, supportedParameter);
152+
}
153+
116154
@Test
117155
public void handleReturnValue() throws Exception {
118156
MyObject returnValue = new MyObject();
@@ -127,6 +165,19 @@ public void handleReturnValue() throws Exception {
127165
verify(marshaller, unmarshaller);
128166
}
129167

168+
@Test(expected = IllegalStateException.class)
169+
public void handleReturnValueNoMarshaller() throws Exception {
170+
processor = new MarshallingPayloadMethodProcessor();
171+
processor.setUnmarshaller(unmarshaller);
172+
173+
MyObject returnValue = new MyObject();
174+
175+
replay(marshaller, unmarshaller);
176+
MessageContext messageContext = createMockMessageContext();
177+
178+
processor.handleReturnValue(messageContext, supportedReturnType, returnValue);
179+
}
180+
130181
@ResponsePayload
131182
public MyObject method(@RequestPayload MyObject object) {
132183
return object;

0 commit comments

Comments
 (0)