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.Iterator;
20 import java.util.List;
21 import java.util.Properties;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.dom.DOMResult;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamResult;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.w3c.dom.Attr;
33 import org.w3c.dom.Document;
34
35 import org.springframework.beans.factory.InitializingBean;
36 import org.springframework.util.StringUtils;
37 import org.springframework.web.servlet.HandlerAdapter;
38 import org.springframework.web.servlet.ModelAndView;
39 import org.springframework.ws.wsdl.WsdlDefinition;
40 import org.springframework.xml.transform.TransformerObjectSupport;
41 import org.springframework.xml.xpath.XPathExpression;
42 import org.springframework.xml.xpath.XPathExpressionFactory;
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
70
71
72
73
74
75 public class WsdlDefinitionHandlerAdapter extends TransformerObjectSupport implements HandlerAdapter, InitializingBean {
76
77
78 public static final String DEFAULT_LOCATION_EXPRESSION = "//@location";
79
80 private static final String CONTENT_TYPE = "text/xml";
81
82 private static final Log logger = LogFactory.getLog(WsdlDefinitionHandlerAdapter.class);
83
84 private Properties expressionNamespaces = new Properties();
85
86 private String locationExpression = DEFAULT_LOCATION_EXPRESSION;
87
88 private XPathExpression locationXPathExpression;
89
90 private boolean transformLocations = false;
91
92
93
94
95
96
97
98
99 public void setLocationExpression(String locationExpression) {
100 this.locationExpression = locationExpression;
101 }
102
103
104
105
106
107 public void setTransformLocations(boolean transformLocations) {
108 this.transformLocations = transformLocations;
109 }
110
111 public long getLastModified(HttpServletRequest request, Object handler) {
112 Source definitionSource = ((WsdlDefinition) handler).getSource();
113 return LastModifiedHelper.getLastModified(definitionSource);
114 }
115
116 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
117 throws Exception {
118 if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
119 response.setContentType(CONTENT_TYPE);
120 Transformer transformer = createTransformer();
121 WsdlDefinition definition = (WsdlDefinition) handler;
122 Source definitionSource = definition.getSource();
123 if (transformLocations) {
124 DOMResult domResult = new DOMResult();
125 transformer.transform(definitionSource, domResult);
126 Document definitionDocument = (Document) domResult.getNode();
127 transformLocations(definitionDocument, request);
128 definitionSource = new DOMSource(definitionDocument);
129 }
130 StreamResult responseResult = new StreamResult(response.getOutputStream());
131 transformer.transform(definitionSource, responseResult);
132 }
133 else {
134 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
135 }
136 return null;
137 }
138
139 public boolean supports(Object handler) {
140 return handler instanceof WsdlDefinition;
141 }
142
143 public void afterPropertiesSet() throws Exception {
144 locationXPathExpression =
145 XPathExpressionFactory.createXPathExpression(locationExpression, expressionNamespaces);
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 protected String transformLocation(String location, HttpServletRequest request) {
164 StringBuffer url = new StringBuffer(request.getScheme());
165 url.append("://").append(request.getServerName()).append(':').append(request.getServerPort());
166 if (location.startsWith("/")) {
167
168 url.append(request.getContextPath()).append(location);
169 return url.toString();
170 }
171 else {
172 int idx = location.indexOf("://");
173 if (idx != -1) {
174
175 idx = location.indexOf('/', idx + 3);
176 if (idx != -1) {
177 String path = location.substring(idx);
178 url.append(path);
179 return url.toString();
180 }
181 }
182 }
183
184 return location;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 protected void transformLocations(Document definitionDocument, HttpServletRequest request) throws Exception {
199 List locationNodes = locationXPathExpression.evaluateAsNodeList(definitionDocument);
200 for (Iterator iterator = locationNodes.iterator(); iterator.hasNext();) {
201 Attr location = (Attr) iterator.next();
202 if (location != null && StringUtils.hasLength(location.getValue())) {
203 String newLocation = transformLocation(location.getValue(), request);
204 if (logger.isDebugEnabled()) {
205 logger.debug("Transforming [" + location.getValue() + "] to [" + newLocation + "]");
206 }
207 location.setValue(newLocation);
208 }
209 }
210 }
211 }