1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.client.support.destination;
18
19 import java.io.IOException;
20 import java.net.URI;
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerException;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMResult;
27
28 import org.w3c.dom.Document;
29
30 import org.springframework.core.io.Resource;
31 import org.springframework.util.Assert;
32 import org.springframework.ws.client.WebServiceIOException;
33 import org.springframework.ws.client.WebServiceTransformerException;
34 import org.springframework.xml.transform.ResourceSource;
35 import org.springframework.xml.xpath.XPathExpression;
36 import org.springframework.xml.xpath.XPathExpressionFactory;
37
38
39
40
41
42
43
44
45
46
47
48
49 public class Wsdl11DestinationProvider extends AbstractCachingDestinationProvider {
50
51
52 public static final String DEFAULT_WSDL_LOCATION_EXPRESSION =
53 "/wsdl:definitions/wsdl:service/wsdl:port/soap:address/@location";
54
55 private static TransformerFactory transformerFactory = TransformerFactory.newInstance();
56
57 private Map<String, String> expressionNamespaces = new HashMap<String, String>();
58
59 private XPathExpression locationXPathExpression;
60
61 private Resource wsdlResource;
62
63 public Wsdl11DestinationProvider() {
64 expressionNamespaces.put("wsdl", "http://schemas.xmlsoap.org/wsdl/");
65 expressionNamespaces.put("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
66 expressionNamespaces.put("soap12", "http://schemas.xmlsoap.org/wsdl/soap12/");
67
68 locationXPathExpression = XPathExpressionFactory
69 .createXPathExpression(DEFAULT_WSDL_LOCATION_EXPRESSION, expressionNamespaces);
70 }
71
72
73 public void setWsdl(Resource wsdlResource) {
74 Assert.notNull(wsdlResource, "'wsdl' must not be null");
75 Assert.isTrue(wsdlResource.exists(), wsdlResource + " does not exist");
76 this.wsdlResource = wsdlResource;
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90 public void setLocationExpression(String expression) {
91 Assert.hasText(expression, "'expression' must not be empty");
92 locationXPathExpression = XPathExpressionFactory
93 .createXPathExpression(expression, expressionNamespaces);
94 }
95
96 @Override
97 protected URI lookupDestination() {
98 try {
99 DOMResult result = new DOMResult();
100 Transformer transformer = transformerFactory.newTransformer();
101 transformer.transform(new ResourceSource(wsdlResource), result);
102 Document definitionDocument = (Document) result.getNode();
103 String location = locationXPathExpression.evaluateAsString(definitionDocument);
104 if (logger.isDebugEnabled()) {
105 logger.debug("Found location [" + location + "] in " + wsdlResource);
106 }
107 return location != null ? URI.create(location) : null;
108 }
109 catch (IOException ex) {
110 throw new WebServiceIOException("Error extracting location from WSDL [" + wsdlResource + "]", ex);
111 }
112 catch (TransformerException ex) {
113 throw new WebServiceTransformerException("Error extracting location from WSDL [" + wsdlResource + "]", ex);
114 }
115 }
116
117 }