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.HashMap;
20 import java.util.Map;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.xml.transform.Source;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.dom.DOMResult;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.stream.StreamResult;
28
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.web.servlet.HandlerAdapter;
31 import org.springframework.web.servlet.ModelAndView;
32 import org.springframework.ws.wsdl.WsdlDefinition;
33 import org.springframework.xml.xpath.XPathExpression;
34 import org.springframework.xml.xpath.XPathExpressionFactory;
35
36 import org.w3c.dom.Document;
37
38
39
40
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
66
67
68
69 public class WsdlDefinitionHandlerAdapter extends LocationTransformerObjectSupport implements HandlerAdapter, InitializingBean {
70
71
72 public static final String DEFAULT_LOCATION_EXPRESSION = "//@location";
73
74
75 public static final String DEFAULT_SCHEMA_LOCATION_EXPRESSION = "//@schemaLocation";
76
77 private static final String CONTENT_TYPE = "text/xml";
78
79 private Map<String, String> expressionNamespaces = new HashMap<String, String>();
80
81 private String locationExpression = DEFAULT_LOCATION_EXPRESSION;
82
83 private String schemaLocationExpression = DEFAULT_SCHEMA_LOCATION_EXPRESSION;
84
85 private XPathExpression locationXPathExpression;
86
87 private XPathExpression schemaLocationXPathExpression;
88
89 private boolean transformLocations = false;
90
91 private boolean transformSchemaLocations = false;
92
93
94
95
96
97
98 public void setLocationExpression(String locationExpression) {
99 this.locationExpression = locationExpression;
100 }
101
102
103
104
105
106
107 public void setSchemaLocationExpression(String schemaLocationExpression) {
108 this.schemaLocationExpression = schemaLocationExpression;
109 }
110
111
112
113
114
115 public void setTransformLocations(boolean transformLocations) {
116 this.transformLocations = transformLocations;
117 }
118
119
120
121
122
123 public void setTransformSchemaLocations(boolean transformSchemaLocations) {
124 this.transformSchemaLocations = transformSchemaLocations;
125 }
126
127 public long getLastModified(HttpServletRequest request, Object handler) {
128 Source definitionSource = ((WsdlDefinition) handler).getSource();
129 return LastModifiedHelper.getLastModified(definitionSource);
130 }
131
132 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
133 throws Exception {
134 if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
135 WsdlDefinition definition = (WsdlDefinition) handler;
136
137 Transformer transformer = createTransformer();
138 Source definitionSource = definition.getSource();
139
140 if (transformLocations || transformSchemaLocations) {
141 DOMResult domResult = new DOMResult();
142 transformer.transform(definitionSource, domResult);
143 Document definitionDocument = (Document) domResult.getNode();
144 if (transformLocations) {
145 transformLocations(definitionDocument, request);
146 }
147 if (transformSchemaLocations) {
148 transformSchemaLocations(definitionDocument, request);
149 }
150 definitionSource = new DOMSource(definitionDocument);
151 }
152
153 response.setContentType(CONTENT_TYPE);
154 StreamResult responseResult = new StreamResult(response.getOutputStream());
155 transformer.transform(definitionSource, responseResult);
156 }
157 else {
158 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
159 }
160 return null;
161 }
162
163 public boolean supports(Object handler) {
164 return handler instanceof WsdlDefinition;
165 }
166
167 public void afterPropertiesSet() throws Exception {
168 locationXPathExpression =
169 XPathExpressionFactory.createXPathExpression(locationExpression, expressionNamespaces);
170 schemaLocationXPathExpression =
171 XPathExpressionFactory.createXPathExpression(schemaLocationExpression, expressionNamespaces);
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185 protected void transformLocations(Document definitionDocument, HttpServletRequest request) throws Exception {
186 transformLocations(locationXPathExpression, definitionDocument, request);
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200 protected void transformSchemaLocations(Document definitionDocument, HttpServletRequest request) throws Exception {
201 transformLocations(schemaLocationXPathExpression, definitionDocument, request);
202 }
203
204 }