1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint;
18
19 import java.io.IOException;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.oxm.Marshaller;
26 import org.springframework.oxm.Unmarshaller;
27 import org.springframework.util.Assert;
28 import org.springframework.ws.WebServiceMessage;
29 import org.springframework.ws.context.MessageContext;
30 import org.springframework.ws.support.MarshallingUtils;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public abstract class AbstractMarshallingPayloadEndpoint implements MessageEndpoint, InitializingBean {
46
47
48 protected final Log logger = LogFactory.getLog(getClass());
49
50 private Marshaller marshaller;
51
52 private Unmarshaller unmarshaller;
53
54
55
56
57
58
59
60
61 protected AbstractMarshallingPayloadEndpoint() {
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 protected AbstractMarshallingPayloadEndpoint(Marshaller marshaller) {
78 Assert.notNull(marshaller, "marshaller must not be null");
79 if (!(marshaller instanceof Unmarshaller)) {
80 throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
81 "interface. Please set an Unmarshaller explicitely by using the " +
82 "AbstractMarshallingPayloadEndpoint(Marshaller, Unmarshaller) constructor.");
83 }
84 else {
85 setMarshaller(marshaller);
86 setUnmarshaller((Unmarshaller) marshaller);
87 }
88 }
89
90
91
92
93
94
95
96 protected AbstractMarshallingPayloadEndpoint(Marshaller marshaller, Unmarshaller unmarshaller) {
97 Assert.notNull(marshaller, "marshaller must not be null");
98 Assert.notNull(unmarshaller, "unmarshaller must not be null");
99 setMarshaller(marshaller);
100 setUnmarshaller(unmarshaller);
101 }
102
103
104 public Marshaller getMarshaller() {
105 return marshaller;
106 }
107
108
109 public final void setMarshaller(Marshaller marshaller) {
110 this.marshaller = marshaller;
111 }
112
113
114 public Unmarshaller getUnmarshaller() {
115 return unmarshaller;
116 }
117
118
119 public final void setUnmarshaller(Unmarshaller unmarshaller) {
120 this.unmarshaller = unmarshaller;
121 }
122
123 public void afterPropertiesSet() throws Exception {
124 afterMarshallerSet();
125 }
126
127 public final void invoke(MessageContext messageContext) throws Exception {
128 WebServiceMessage request = messageContext.getRequest();
129 Object requestObject = unmarshalRequest(request);
130 if (onUnmarshalRequest(messageContext, requestObject)) {
131 Object responseObject = invokeInternal(requestObject);
132 if (responseObject != null) {
133 WebServiceMessage response = messageContext.getResponse();
134 marshalResponse(responseObject, response);
135 onMarshalResponse(messageContext, requestObject, responseObject);
136 }
137 }
138 }
139
140 private Object unmarshalRequest(WebServiceMessage request) throws IOException {
141 Unmarshaller unmarshaller = getUnmarshaller();
142 Assert.notNull(unmarshaller, "No unmarshaller registered. Check configuration of endpoint.");
143 Object requestObject = MarshallingUtils.unmarshal(unmarshaller, request);
144 if (logger.isDebugEnabled()) {
145 logger.debug("Unmarshalled payload request to [" + requestObject + "]");
146 }
147 return requestObject;
148 }
149
150
151
152
153
154
155
156
157
158
159
160 protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception {
161 return true;
162 }
163
164 private void marshalResponse(Object responseObject, WebServiceMessage response) throws IOException {
165 Marshaller marshaller = getMarshaller();
166 Assert.notNull(marshaller, "No marshaller registered. Check configuration of endpoint.");
167 if (logger.isDebugEnabled()) {
168 logger.debug("Marshalling [" + responseObject + "] to response payload");
169 }
170 MarshallingUtils.marshal(marshaller, responseObject, response);
171 }
172
173
174
175
176
177
178
179
180
181
182
183 protected void onMarshalResponse(MessageContext messageContext, Object requestObject, Object responseObject) {
184 }
185
186
187
188
189
190
191
192
193
194 public void afterMarshallerSet() throws Exception {
195 }
196
197
198
199
200
201
202
203
204
205
206 protected abstract Object invokeInternal(Object requestObject) throws Exception;
207 }