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.List;
20 import javax.servlet.http.HttpServletRequest;
21
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24 import org.springframework.xml.transform.TransformerObjectSupport;
25 import org.springframework.xml.xpath.XPathExpression;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.w3c.dom.Attr;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32
33
34
35
36
37
38
39
40 public abstract class LocationTransformerObjectSupport extends TransformerObjectSupport {
41
42
43 private final Log logger = LogFactory.getLog(getClass());
44
45
46
47
48
49
50
51 protected void transformLocations(XPathExpression xPathExpression,
52 Document definitionDocument,
53 HttpServletRequest request) {
54 Assert.notNull(xPathExpression, "'xPathExpression' must not be null");
55 Assert.notNull(definitionDocument, "'definitionDocument' must not be null");
56 Assert.notNull(request, "'request' must not be null");
57
58 List<Node> locationNodes = xPathExpression.evaluateAsNodeList(definitionDocument);
59 for (Node locationNode : locationNodes) {
60 if (locationNode instanceof Attr) {
61 Attr location = (Attr) locationNode;
62 if (StringUtils.hasLength(location.getValue())) {
63 String newLocation = transformLocation(location.getValue(), request);
64 if (logger.isDebugEnabled()) {
65 logger.debug("Transforming [" + location.getValue() + "] to [" + newLocation + "]");
66 }
67 location.setValue(newLocation);
68 }
69 }
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 protected String transformLocation(String location, HttpServletRequest request) {
89 StringBuilder url = new StringBuilder(request.getScheme());
90 url.append("://").append(request.getServerName()).append(':').append(request.getServerPort());
91 if (location.startsWith("/")) {
92
93 url.append(request.getContextPath()).append(location);
94 return url.toString();
95 }
96 else {
97 int idx = location.indexOf("://");
98 if (idx != -1) {
99
100 idx = location.indexOf('/', idx + 3);
101 if (idx != -1) {
102 String path = location.substring(idx);
103 url.append(path);
104 return url.toString();
105 }
106 }
107 }
108
109 return location;
110 }
111 }