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.xml.xpath.XPathExpression;
33 import org.springframework.xml.xpath.XPathExpressionFactory;
34 import org.springframework.xml.xsd.XsdSchema;
35
36 import org.w3c.dom.Document;
37
38
39
40
41
42
43
44
45
46
47
48
49 public class XsdSchemaHandlerAdapter extends LocationTransformerObjectSupport
50 implements HandlerAdapter, InitializingBean {
51
52
53
54
55 public static final String DEFAULT_SCHEMA_LOCATION_EXPRESSION = "//@schemaLocation";
56
57 private static final String CONTENT_TYPE = "text/xml";
58
59 private Map<String, String> expressionNamespaces = new HashMap<String, String>();
60
61 private String schemaLocationExpression = DEFAULT_SCHEMA_LOCATION_EXPRESSION;
62
63 private XPathExpression schemaLocationXPathExpression;
64
65 private boolean transformSchemaLocations = false;
66
67
68
69
70
71
72 public void setSchemaLocationExpression(String schemaLocationExpression) {
73 this.schemaLocationExpression = schemaLocationExpression;
74 }
75
76
77
78
79
80 public void setTransformSchemaLocations(boolean transformSchemaLocations) {
81 this.transformSchemaLocations = transformSchemaLocations;
82 }
83
84 public long getLastModified(HttpServletRequest request, Object handler) {
85 Source schemaSource = ((XsdSchema) handler).getSource();
86 return LastModifiedHelper.getLastModified(schemaSource);
87 }
88
89 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
90 throws Exception {
91 if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
92 Transformer transformer = createTransformer();
93 Source schemaSource = getSchemaSource((XsdSchema) handler);
94
95 if (transformSchemaLocations) {
96 DOMResult domResult = new DOMResult();
97 transformer.transform(schemaSource, domResult);
98 Document schemaDocument = (Document) domResult.getNode();
99 transformSchemaLocations(schemaDocument, request);
100 schemaSource = new DOMSource(schemaDocument);
101 }
102
103 response.setContentType(CONTENT_TYPE);
104 StreamResult responseResult = new StreamResult(response.getOutputStream());
105 transformer.transform(schemaSource, responseResult);
106 }
107 else {
108 response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
109 }
110 return null;
111 }
112
113 public boolean supports(Object handler) {
114 return handler instanceof XsdSchema;
115 }
116
117 public void afterPropertiesSet() throws Exception {
118 schemaLocationXPathExpression =
119 XPathExpressionFactory.createXPathExpression(schemaLocationExpression, expressionNamespaces);
120 }
121
122
123
124
125
126
127
128
129
130
131
132 protected Source getSchemaSource(XsdSchema schema) throws Exception {
133 return schema.getSource();
134 }
135
136
137
138
139
140
141
142
143
144
145
146 protected void transformSchemaLocations(Document definitionDocument, HttpServletRequest request) throws Exception {
147 transformLocations(schemaLocationXPathExpression, definitionDocument, request);
148 }
149
150
151 }