View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.http;
18  
19  import java.io.File;
20  import java.net.URI;
21  import java.net.URISyntaxException;
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.DOMSource;
27  import javax.xml.transform.stream.StreamResult;
28  
29  import org.w3c.dom.Document;
30  
31  import org.springframework.util.StringUtils;
32  import org.springframework.web.servlet.HandlerAdapter;
33  import org.springframework.web.servlet.ModelAndView;
34  import org.springframework.xml.transform.TransformerObjectSupport;
35  import org.springframework.xml.transform.TraxUtils;
36  import org.springframework.xml.xsd.XsdSchema;
37  
38  /**
39   * Adapter to use the {@link XsdSchema} interface with the generic <code>DispatcherServlet</code>.
40   * <p/>
41   * Reads the source from the mapped {@link XsdSchema} implementation, and writes that as the result to the
42   * <code>HttpServletResponse</code>. Allows for post-processing the schema in subclasses.
43   *
44   * @author Arjen Poutsma
45   * @see XsdSchema
46   * @see #getSchemaSource(XsdSchema)
47   * @since 1.5.3
48   */
49  public class XsdSchemaHandlerAdapter extends TransformerObjectSupport implements HandlerAdapter {
50  
51      private static final String CONTENT_TYPE = "text/xml";
52  
53      public boolean supports(Object handler) {
54          return handler instanceof XsdSchema;
55      }
56  
57      public long getLastModified(HttpServletRequest request, Object handler) {
58          Source schemaSource = ((XsdSchema) handler).getSource();
59          return LastModifiedHelper.getLastModified(schemaSource);
60      }
61  
62      public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
63              throws Exception {
64          if (HttpTransportConstants.METHOD_GET.equals(request.getMethod())) {
65              response.setContentType(CONTENT_TYPE);
66              Transformer transformer = createTransformer();
67              Source schemaSource = getSchemaSource((XsdSchema) handler);
68              StreamResult responseResult = new StreamResult(response.getOutputStream());
69              transformer.transform(schemaSource, responseResult);
70          }
71          else {
72              response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
73          }
74          return null;
75      }
76  
77      /**
78       * Returns the {@link Source} of the given schema. Allows for post-processing and transformation of the schema in
79       * sub-classes.
80       * <p/>
81       * Default implementation simply returns {@link XsdSchema#getSource()}.
82       *
83       * @param schema the schema
84       * @return the source of the given schema
85       * @throws Exception in case of errors
86       */
87      protected Source getSchemaSource(XsdSchema schema) throws Exception {
88          return schema.getSource();
89      }
90  
91  }