1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint.adapter;
18
19 import java.io.IOException;
20 import java.lang.reflect.Method;
21
22 import org.springframework.beans.factory.InitializingBean;
23 import org.springframework.oxm.Marshaller;
24 import org.springframework.oxm.Unmarshaller;
25 import org.springframework.util.Assert;
26 import org.springframework.ws.WebServiceMessage;
27 import org.springframework.ws.context.MessageContext;
28 import org.springframework.ws.server.EndpointMapping;
29 import org.springframework.ws.server.endpoint.MethodEndpoint;
30 import org.springframework.ws.support.MarshallingUtils;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class MarshallingMethodEndpointAdapter extends AbstractMethodEndpointAdapter implements InitializingBean {
54
55 private Marshaller marshaller;
56
57 private Unmarshaller unmarshaller;
58
59
60
61
62
63
64
65
66 public MarshallingMethodEndpointAdapter() {
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public MarshallingMethodEndpointAdapter(Marshaller marshaller) {
82 Assert.notNull(marshaller, "marshaller must not be null");
83 if (!(marshaller instanceof Unmarshaller)) {
84 throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
85 "interface. Please set an Unmarshaller explicitely by using the " +
86 "MarshallingMethodEndpointAdapter(Marshaller, Unmarshaller) constructor.");
87 }
88 else {
89 this.setMarshaller(marshaller);
90 this.setUnmarshaller((Unmarshaller) marshaller);
91 }
92 }
93
94
95
96
97
98
99
100 public MarshallingMethodEndpointAdapter(Marshaller marshaller, Unmarshaller unmarshaller) {
101 Assert.notNull(marshaller, "marshaller must not be null");
102 Assert.notNull(unmarshaller, "unmarshaller must not be null");
103 this.setMarshaller(marshaller);
104 this.setUnmarshaller(unmarshaller);
105 }
106
107
108 public Marshaller getMarshaller() {
109 return marshaller;
110 }
111
112
113 public final void setMarshaller(Marshaller marshaller) {
114 this.marshaller = marshaller;
115 }
116
117
118 public Unmarshaller getUnmarshaller() {
119 return unmarshaller;
120 }
121
122
123 public final void setUnmarshaller(Unmarshaller unmarshaller) {
124 this.unmarshaller = unmarshaller;
125 }
126
127 public void afterPropertiesSet() throws Exception {
128 Assert.notNull(getMarshaller(), "marshaller is required");
129 Assert.notNull(getUnmarshaller(), "unmarshaller is required");
130 }
131
132 protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
133 WebServiceMessage request = messageContext.getRequest();
134 Object requestObject = unmarshalRequest(request);
135 Object responseObject = methodEndpoint.invoke(new Object[]{requestObject});
136 if (responseObject != null) {
137 WebServiceMessage response = messageContext.getResponse();
138 marshalResponse(responseObject, response);
139 }
140 }
141
142 private Object unmarshalRequest(WebServiceMessage request) throws IOException {
143 Object requestObject = MarshallingUtils.unmarshal(getUnmarshaller(), request);
144 if (logger.isDebugEnabled()) {
145 logger.debug("Unmarshalled payload request to [" + requestObject + "]");
146 }
147 return requestObject;
148 }
149
150 private void marshalResponse(Object responseObject, WebServiceMessage response) throws IOException {
151 if (logger.isDebugEnabled()) {
152 logger.debug("Marshalling [" + responseObject + "] to response payload");
153 }
154 MarshallingUtils.marshal(getMarshaller(), responseObject, response);
155 }
156
157
158
159
160
161
162
163
164 protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
165 Method method = methodEndpoint.getMethod();
166 return supportsReturnType(method) && supportsParameters(method);
167 }
168
169 private boolean supportsReturnType(Method method) {
170 return (Void.TYPE.equals(method.getReturnType()) || getMarshaller().supports(method.getReturnType()));
171 }
172
173 private boolean supportsParameters(Method method) {
174 if (method.getParameterTypes().length != 1) {
175 return false;
176 }
177 else {
178 return getUnmarshaller().supports(method.getParameterTypes()[0]);
179 }
180 }
181 }