1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.addressing.server;
18
19 import java.net.URI;
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import javax.xml.transform.TransformerException;
23
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.core.JdkVersion;
26 import org.springframework.util.Assert;
27 import org.springframework.ws.context.MessageContext;
28 import org.springframework.ws.server.EndpointInterceptor;
29 import org.springframework.ws.server.EndpointInvocationChain;
30 import org.springframework.ws.server.EndpointMapping;
31 import org.springframework.ws.soap.SoapHeader;
32 import org.springframework.ws.soap.SoapHeaderElement;
33 import org.springframework.ws.soap.SoapMessage;
34 import org.springframework.ws.soap.addressing.core.MessageAddressingProperties;
35 import org.springframework.ws.soap.addressing.messageid.MessageIdStrategy;
36 import org.springframework.ws.soap.addressing.messageid.RandomGuidMessageIdStrategy;
37 import org.springframework.ws.soap.addressing.messageid.UuidMessageIdStrategy;
38 import org.springframework.ws.soap.addressing.version.Addressing10;
39 import org.springframework.ws.soap.addressing.version.Addressing200408;
40 import org.springframework.ws.soap.addressing.version.AddressingVersion;
41 import org.springframework.ws.soap.server.SoapEndpointInvocationChain;
42 import org.springframework.ws.soap.server.SoapEndpointMapping;
43 import org.springframework.ws.transport.WebServiceMessageSender;
44 import org.springframework.xml.transform.TransformerObjectSupport;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public abstract class AbstractAddressingEndpointMapping extends TransformerObjectSupport
69 implements SoapEndpointMapping, InitializingBean {
70
71 private String[] actorsOrRoles;
72
73 private boolean isUltimateReceiver = true;
74
75 private MessageIdStrategy messageIdStrategy;
76
77 private WebServiceMessageSender[] messageSenders = new WebServiceMessageSender[0];
78
79 private AddressingVersion[] versions;
80
81 private EndpointInterceptor[] preInterceptors = new EndpointInterceptor[0];
82
83 private EndpointInterceptor[] postInterceptors = new EndpointInterceptor[0];
84
85
86 protected AbstractAddressingEndpointMapping() {
87 initDefaultStrategies();
88 }
89
90
91
92
93
94
95
96 protected void initDefaultStrategies() {
97 this.versions = new AddressingVersion[]{new Addressing200408(), new Addressing10()};
98 if (JdkVersion.isAtLeastJava15()) {
99 messageIdStrategy = new UuidMessageIdStrategy();
100 }
101 else {
102 messageIdStrategy = new RandomGuidMessageIdStrategy();
103 }
104 }
105
106 public final void setActorOrRole(String actorOrRole) {
107 Assert.notNull(actorOrRole, "actorOrRole must not be null");
108 actorsOrRoles = new String[]{actorOrRole};
109 }
110
111 public final void setActorsOrRoles(String[] actorsOrRoles) {
112 Assert.notEmpty(actorsOrRoles, "actorsOrRoles must not be empty");
113 this.actorsOrRoles = actorsOrRoles;
114 }
115
116 public final void setUltimateReceiver(boolean ultimateReceiver) {
117 this.isUltimateReceiver = ultimateReceiver;
118 }
119
120
121
122
123
124 public final void setPreInterceptors(EndpointInterceptor[] preInterceptors) {
125 Assert.notNull(preInterceptors, "'preInterceptors' must not be null");
126 this.preInterceptors = preInterceptors;
127 }
128
129
130
131
132
133 public final void setPostInterceptors(EndpointInterceptor[] postInterceptors) {
134 Assert.notNull(postInterceptors, "'postInterceptors' must not be null");
135 this.postInterceptors = postInterceptors;
136 }
137
138
139
140
141
142
143
144 public final void setMessageIdStrategy(MessageIdStrategy messageIdStrategy) {
145 Assert.notNull(messageIdStrategy, "'messageIdStrategy' must not be null");
146 this.messageIdStrategy = messageIdStrategy;
147 }
148
149 public final void setMessageSenders(WebServiceMessageSender[] messageSenders) {
150 Assert.notNull(messageSenders, "'messageSenders' must not be null");
151 this.messageSenders = messageSenders;
152 }
153
154
155
156
157
158
159
160
161 public final void setVersions(AddressingVersion[] versions) {
162 this.versions = versions;
163 }
164
165 public void afterPropertiesSet() throws Exception {
166 if (logger.isInfoEnabled()) {
167 logger.info("Supporting " + Arrays.asList(versions));
168 }
169 }
170
171 public final EndpointInvocationChain getEndpoint(MessageContext messageContext) throws TransformerException {
172 Assert.isInstanceOf(SoapMessage.class, messageContext.getRequest());
173 SoapMessage request = (SoapMessage) messageContext.getRequest();
174 for (int i = 0; i < versions.length; i++) {
175 if (supports(versions[i], request)) {
176 if (logger.isDebugEnabled()) {
177 logger.debug("Request [" + request + "] uses [" + versions[i] + "]");
178 }
179 MessageAddressingProperties requestMap = versions[i].getMessageAddressingProperties(request);
180 if (requestMap == null) {
181 return null;
182 }
183 Object endpoint = getEndpointInternal(requestMap);
184 if (endpoint == null) {
185 return null;
186 }
187 return getEndpointInvocationChain(endpoint, versions[i], requestMap);
188 }
189 }
190 return null;
191 }
192
193
194
195
196
197 private EndpointInvocationChain getEndpointInvocationChain(Object endpoint,
198 AddressingVersion version,
199 MessageAddressingProperties requestMap) {
200 URI responseAction = getResponseAction(endpoint, requestMap);
201 URI faultAction = getFaultAction(endpoint, requestMap);
202 EndpointInterceptor[] interceptors =
203 new EndpointInterceptor[preInterceptors.length + postInterceptors.length + 1];
204 System.arraycopy(preInterceptors, 0, interceptors, 0, preInterceptors.length);
205 AddressingEndpointInterceptor interceptor = new AddressingEndpointInterceptor(version, messageIdStrategy,
206 messageSenders, responseAction, faultAction);
207 interceptors[preInterceptors.length] = interceptor;
208 System.arraycopy(postInterceptors, 0, interceptors, preInterceptors.length + 1, postInterceptors.length);
209 return new SoapEndpointInvocationChain(endpoint, interceptors, actorsOrRoles, isUltimateReceiver);
210 }
211
212 private boolean supports(AddressingVersion version, SoapMessage request) {
213 SoapHeader header = request.getSoapHeader();
214 if (header != null) {
215 for (Iterator iterator = header.examineAllHeaderElements(); iterator.hasNext();) {
216 SoapHeaderElement headerElement = (SoapHeaderElement) iterator.next();
217 if (version.understands(headerElement)) {
218 return true;
219 }
220 }
221 }
222 return false;
223 }
224
225
226
227
228
229
230
231
232 protected abstract Object getEndpointInternal(MessageAddressingProperties map);
233
234
235
236
237
238
239
240
241
242 protected abstract URI getResponseAction(Object endpoint, MessageAddressingProperties requestMap);
243
244
245
246
247
248
249
250
251
252 protected abstract URI getFaultAction(Object endpoint, MessageAddressingProperties requestMap);
253
254 }