1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.http;
18
19 import java.util.Map;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.springframework.beans.factory.BeanFactoryUtils;
24 import org.springframework.beans.factory.BeanInitializationException;
25 import org.springframework.beans.factory.BeanNameAware;
26 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.web.context.WebApplicationContext;
29 import org.springframework.web.servlet.DispatcherServlet;
30 import org.springframework.web.servlet.FrameworkServlet;
31 import org.springframework.web.util.WebUtils;
32 import org.springframework.ws.WebServiceMessageFactory;
33 import org.springframework.ws.server.EndpointAdapter;
34 import org.springframework.ws.server.EndpointExceptionResolver;
35 import org.springframework.ws.server.EndpointMapping;
36 import org.springframework.ws.server.MessageDispatcher;
37 import org.springframework.ws.support.DefaultStrategiesHelper;
38 import org.springframework.ws.transport.WebServiceMessageReceiver;
39 import org.springframework.ws.wsdl.WsdlDefinition;
40 import org.springframework.xml.xsd.XsdSchema;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class MessageDispatcherServlet extends FrameworkServlet {
66
67
68 public static final String DEFAULT_MESSAGE_FACTORY_BEAN_NAME = "messageFactory";
69
70
71 public static final String DEFAULT_MESSAGE_RECEIVER_BEAN_NAME = "messageReceiver";
72
73
74
75
76
77 public static final String DEFAULT_MESSAGE_RECEIVER_HANDLER_ADAPTER_BEAN_NAME = "messageReceiverHandlerAdapter";
78
79
80 public static final String DEFAULT_WSDL_DEFINITION_HANDLER_ADAPTER_BEAN_NAME = "wsdlDefinitionHandlerAdapter";
81
82
83 public static final String DEFAULT_XSD_SCHEMA_HANDLER_ADAPTER_BEAN_NAME = "xsdSchemaHandlerAdapter";
84
85
86 private static final String WSDL_SUFFIX_NAME = ".wsdl";
87
88
89 private static final String XSD_SUFFIX_NAME = ".xsd";
90
91 private final DefaultStrategiesHelper defaultStrategiesHelper;
92
93 private String messageFactoryBeanName = DEFAULT_MESSAGE_FACTORY_BEAN_NAME;
94
95 private String messageReceiverHandlerAdapterBeanName = DEFAULT_MESSAGE_RECEIVER_HANDLER_ADAPTER_BEAN_NAME;
96
97
98 private WebServiceMessageReceiverHandlerAdapter messageReceiverHandlerAdapter;
99
100 private String wsdlDefinitionHandlerAdapterBeanName = DEFAULT_WSDL_DEFINITION_HANDLER_ADAPTER_BEAN_NAME;
101
102
103 private WsdlDefinitionHandlerAdapter wsdlDefinitionHandlerAdapter;
104
105 private String xsdSchemaHandlerAdapterBeanName = DEFAULT_XSD_SCHEMA_HANDLER_ADAPTER_BEAN_NAME;
106
107
108 private XsdSchemaHandlerAdapter xsdSchemaHandlerAdapter;
109
110 private String messageReceiverBeanName = DEFAULT_MESSAGE_RECEIVER_BEAN_NAME;
111
112
113 private WebServiceMessageReceiver messageReceiver;
114
115
116 private Map<String, WsdlDefinition> wsdlDefinitions;
117
118 private Map<String, XsdSchema> xsdSchemas;
119
120 private boolean transformWsdlLocations = false;
121
122 private boolean transformSchemaLocations = false;
123
124
125
126
127 public MessageDispatcherServlet() {
128 this(null);
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public MessageDispatcherServlet(WebApplicationContext webApplicationContext) {
177 super(webApplicationContext);
178 defaultStrategiesHelper = new DefaultStrategiesHelper(MessageDispatcherServlet.class);
179 }
180
181
182 public String getMessageFactoryBeanName() {
183 return messageFactoryBeanName;
184 }
185
186
187
188
189
190 public void setMessageFactoryBeanName(String messageFactoryBeanName) {
191 this.messageFactoryBeanName = messageFactoryBeanName;
192 }
193
194
195 public String getMessageReceiverBeanName() {
196 return messageReceiverBeanName;
197 }
198
199
200
201
202
203 public void setMessageReceiverBeanName(String messageReceiverBeanName) {
204 this.messageReceiverBeanName = messageReceiverBeanName;
205 }
206
207
208
209
210
211 public boolean isTransformWsdlLocations() {
212 return transformWsdlLocations;
213 }
214
215
216
217
218
219 public void setTransformWsdlLocations(boolean transformWsdlLocations) {
220 this.transformWsdlLocations = transformWsdlLocations;
221 }
222
223
224
225
226
227 public boolean isTransformSchemaLocations() {
228 return transformSchemaLocations;
229 }
230
231
232
233
234
235 public void setTransformSchemaLocations(boolean transformSchemaLocations) {
236 this.transformSchemaLocations = transformSchemaLocations;
237 }
238
239
240 public String getMessageReceiverHandlerAdapterBeanName() {
241 return messageReceiverHandlerAdapterBeanName;
242 }
243
244
245
246
247
248 public void setMessageReceiverHandlerAdapterBeanName(String messageReceiverHandlerAdapterBeanName) {
249 this.messageReceiverHandlerAdapterBeanName = messageReceiverHandlerAdapterBeanName;
250 }
251
252
253 public String getWsdlDefinitionHandlerAdapterBeanName() {
254 return wsdlDefinitionHandlerAdapterBeanName;
255 }
256
257
258
259
260
261 public void setWsdlDefinitionHandlerAdapterBeanName(String wsdlDefinitionHandlerAdapterBeanName) {
262 this.wsdlDefinitionHandlerAdapterBeanName = wsdlDefinitionHandlerAdapterBeanName;
263 }
264
265
266 public String getXsdSchemaHandlerAdapterBeanName() {
267 return xsdSchemaHandlerAdapterBeanName;
268 }
269
270
271
272
273
274 public void setXsdSchemaHandlerAdapterBeanName(String xsdSchemaHandlerAdapterBeanName) {
275 this.xsdSchemaHandlerAdapterBeanName = xsdSchemaHandlerAdapterBeanName;
276 }
277
278
279 @Override
280 protected void doService(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
281 throws Exception {
282 WsdlDefinition definition = getWsdlDefinition(httpServletRequest);
283 if (definition != null) {
284 wsdlDefinitionHandlerAdapter.handle(httpServletRequest, httpServletResponse, definition);
285 return;
286 }
287 XsdSchema schema = getXsdSchema(httpServletRequest);
288 if (schema != null) {
289 xsdSchemaHandlerAdapter.handle(httpServletRequest, httpServletResponse, schema);
290 return;
291 }
292 messageReceiverHandlerAdapter.handle(httpServletRequest, httpServletResponse, messageReceiver);
293 }
294
295
296
297
298 @Override
299 protected void onRefresh(ApplicationContext context) {
300 initStrategies(context);
301 }
302
303 @Override
304 protected long getLastModified(HttpServletRequest httpServletRequest) {
305 WsdlDefinition definition = getWsdlDefinition(httpServletRequest);
306 if (definition != null) {
307 return wsdlDefinitionHandlerAdapter.getLastModified(httpServletRequest, definition);
308 }
309 XsdSchema schema = getXsdSchema(httpServletRequest);
310 if (schema != null) {
311 return xsdSchemaHandlerAdapter.getLastModified(httpServletRequest, schema);
312 }
313 return messageReceiverHandlerAdapter.getLastModified(httpServletRequest, messageReceiver);
314 }
315
316
317 protected WebServiceMessageReceiver getMessageReceiver() {
318 return messageReceiver;
319 }
320
321
322
323
324
325
326
327
328
329
330
331 protected WsdlDefinition getWsdlDefinition(HttpServletRequest request) {
332 if (HttpTransportConstants.METHOD_GET.equals(request.getMethod()) &&
333 request.getRequestURI().endsWith(WSDL_SUFFIX_NAME)) {
334 String fileName = WebUtils.extractFilenameFromUrlPath(request.getRequestURI());
335 return wsdlDefinitions.get(fileName);
336 }
337 else {
338 return null;
339 }
340 }
341
342
343
344
345
346
347
348
349
350
351
352 protected XsdSchema getXsdSchema(HttpServletRequest request) {
353 if (HttpTransportConstants.METHOD_GET.equals(request.getMethod()) &&
354 request.getRequestURI().endsWith(XSD_SUFFIX_NAME)) {
355 String fileName = WebUtils.extractFilenameFromUrlPath(request.getRequestURI());
356 return xsdSchemas.get(fileName);
357 }
358 else {
359 return null;
360 }
361 }
362
363
364
365
366
367 protected void initStrategies(ApplicationContext context) {
368 initMessageReceiverHandlerAdapter(context);
369 initWsdlDefinitionHandlerAdapter(context);
370 initXsdSchemaHandlerAdapter(context);
371 initMessageReceiver(context);
372 initWsdlDefinitions(context);
373 initXsdSchemas(context);
374 }
375
376
377 private void initMessageReceiverHandlerAdapter(ApplicationContext context) {
378 try {
379 try {
380 messageReceiverHandlerAdapter = context.getBean(getMessageReceiverHandlerAdapterBeanName(),
381 WebServiceMessageReceiverHandlerAdapter.class);
382 }
383 catch (NoSuchBeanDefinitionException ignored) {
384 messageReceiverHandlerAdapter = new WebServiceMessageReceiverHandlerAdapter();
385 }
386 initWebServiceMessageFactory(context);
387 messageReceiverHandlerAdapter.afterPropertiesSet();
388 }
389 catch (Exception ex) {
390 throw new BeanInitializationException("Could not initialize WebServiceMessageReceiverHandlerAdapter", ex);
391 }
392 }
393
394 private void initWebServiceMessageFactory(ApplicationContext context) {
395 WebServiceMessageFactory messageFactory;
396 try {
397 messageFactory = context.getBean(getMessageFactoryBeanName(), WebServiceMessageFactory.class);
398 }
399 catch (NoSuchBeanDefinitionException ignored) {
400 messageFactory = defaultStrategiesHelper
401 .getDefaultStrategy(WebServiceMessageFactory.class, context);
402 if (logger.isDebugEnabled()) {
403 logger.debug("No WebServiceMessageFactory found in servlet '" + getServletName() + "': using default");
404 }
405 }
406 messageReceiverHandlerAdapter.setMessageFactory(messageFactory);
407 }
408
409 private void initWsdlDefinitionHandlerAdapter(ApplicationContext context) {
410 try {
411 try {
412 wsdlDefinitionHandlerAdapter =
413 context.getBean(getWsdlDefinitionHandlerAdapterBeanName(), WsdlDefinitionHandlerAdapter.class);
414
415 }
416 catch (NoSuchBeanDefinitionException ignored) {
417 wsdlDefinitionHandlerAdapter = new WsdlDefinitionHandlerAdapter();
418 }
419 wsdlDefinitionHandlerAdapter.setTransformLocations(isTransformWsdlLocations());
420 wsdlDefinitionHandlerAdapter.setTransformSchemaLocations(isTransformSchemaLocations());
421 wsdlDefinitionHandlerAdapter.afterPropertiesSet();
422 }
423 catch (Exception ex) {
424 throw new BeanInitializationException("Could not initialize WsdlDefinitionHandlerAdapter", ex);
425 }
426 }
427
428 private void initXsdSchemaHandlerAdapter(ApplicationContext context) {
429 try {
430 try {
431 xsdSchemaHandlerAdapter = context
432 .getBean(getXsdSchemaHandlerAdapterBeanName(), XsdSchemaHandlerAdapter.class);
433
434 }
435 catch (NoSuchBeanDefinitionException ignored) {
436 xsdSchemaHandlerAdapter = new XsdSchemaHandlerAdapter();
437 }
438 xsdSchemaHandlerAdapter.setTransformSchemaLocations(isTransformSchemaLocations());
439 xsdSchemaHandlerAdapter.afterPropertiesSet();
440 }
441 catch (Exception ex) {
442 throw new BeanInitializationException("Could not initialize XsdSchemaHandlerAdapter", ex);
443 }
444 }
445
446 private void initMessageReceiver(ApplicationContext context) {
447 try {
448 messageReceiver = context.getBean(getMessageReceiverBeanName(), WebServiceMessageReceiver.class);
449 }
450 catch (NoSuchBeanDefinitionException ex) {
451 messageReceiver = defaultStrategiesHelper
452 .getDefaultStrategy(WebServiceMessageReceiver.class, context);
453 if (messageReceiver instanceof BeanNameAware) {
454 ((BeanNameAware) messageReceiver).setBeanName(getServletName());
455 }
456 if (logger.isDebugEnabled()) {
457 logger.debug("No MessageDispatcher found in servlet '" + getServletName() + "': using default");
458 }
459 }
460 }
461
462 private void initWsdlDefinitions(ApplicationContext context) {
463 wsdlDefinitions = BeanFactoryUtils
464 .beansOfTypeIncludingAncestors(context, WsdlDefinition.class, true, false);
465 if (logger.isDebugEnabled()) {
466 for (Map.Entry<String, WsdlDefinition> entry : wsdlDefinitions.entrySet()) {
467 String beanName = entry.getKey();
468 WsdlDefinition definition = entry.getValue();
469 logger.debug("Published [" + definition + "] as " + beanName + WSDL_SUFFIX_NAME);
470 }
471 }
472 }
473
474 private void initXsdSchemas(ApplicationContext context) {
475 xsdSchemas = BeanFactoryUtils
476 .beansOfTypeIncludingAncestors(context, XsdSchema.class, true, false);
477 if (logger.isDebugEnabled()) {
478 for (Map.Entry<String, XsdSchema> entry : xsdSchemas.entrySet()) {
479 String beanName = entry.getKey();
480 XsdSchema schema = entry.getValue();
481 logger.debug("Published [" + schema + "] as " + beanName + XSD_SUFFIX_NAME);
482 }
483 }
484 }
485 }