View Javadoc

1   /*
2    * Copyright 2005-2010 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.server.endpoint;
18  
19  import javax.xml.parsers.DocumentBuilder;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  import javax.xml.parsers.ParserConfigurationException;
22  import javax.xml.transform.Source;
23  import javax.xml.transform.TransformerException;
24  import javax.xml.transform.dom.DOMResult;
25  import javax.xml.transform.dom.DOMSource;
26  
27  import org.springframework.xml.transform.TransformerObjectSupport;
28  
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  
33  /**
34   * Abstract base class for endpoints that handle the message payload as DOM elements.
35   * <p/>
36   * Offers the message payload as a DOM <code>Element</code>, and allows subclasses to create a response by returning an
37   * <code>Element</code>.
38   * <p/>
39   * An <code>AbstractDomPayloadEndpoint</code> only accept <em>one</em> payload element. Multiple payload elements are
40   * not in accordance with WS-I.
41   *
42   * @author Arjen Poutsma
43   * @author Alef Arendsen
44   * @see #invokeInternal(org.w3c.dom.Element,org.w3c.dom.Document)
45   * @since 1.0.0
46   * @deprecated as of Spring Web Services 2.0, in favor of annotated endpoints
47   */
48  @Deprecated
49  public abstract class AbstractDomPayloadEndpoint extends TransformerObjectSupport implements PayloadEndpoint {
50  
51      private DocumentBuilderFactory documentBuilderFactory;
52  
53      private boolean validating = false;
54  
55      private boolean namespaceAware = true;
56  
57      private boolean alwaysTransform = false;
58  
59      /** Set whether or not the XML parser should be XML namespace aware. Default is <code>true</code>. */
60      public void setNamespaceAware(boolean namespaceAware) {
61          this.namespaceAware = namespaceAware;
62      }
63  
64      /** Set if the XML parser should validate the document. Default is <code>false</code>. */
65      public void setValidating(boolean validating) {
66          this.validating = validating;
67      }
68  
69      /**
70       * Set if the request {@link Source} should always be transformed into a new {@link DOMResult}.
71       * <p/>
72       * Default is {@code false}, which is faster.
73       */
74      public void setAlwaysTransform(boolean alwaysTransform) {
75          this.alwaysTransform = alwaysTransform;
76      }
77  
78      public final Source invoke(Source request) throws Exception {
79          if (documentBuilderFactory == null) {
80              documentBuilderFactory = createDocumentBuilderFactory();
81          }
82          DocumentBuilder documentBuilder = createDocumentBuilder(documentBuilderFactory);
83          Element requestElement = getDocumentElement(request, documentBuilder);
84          Document responseDocument = documentBuilder.newDocument();
85          Element responseElement = invokeInternal(requestElement, responseDocument);
86          return responseElement != null ? new DOMSource(responseElement) : null;
87      }
88  
89      /**
90       * Create a <code>DocumentBuilder</code> that this endpoint will use for parsing XML documents. Can be overridden in
91       * subclasses, adding further initialization of the builder.
92       *
93       * @param factory the <code>DocumentBuilderFactory</code> that the DocumentBuilder should be created with
94       * @return the <code>DocumentBuilder</code>
95       * @throws ParserConfigurationException if thrown by JAXP methods
96       */
97      protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory)
98              throws ParserConfigurationException {
99          return factory.newDocumentBuilder();
100     }
101 
102     /**
103      * Create a <code>DocumentBuilderFactory</code> that this endpoint will use for constructing XML documents. Can be
104      * overridden in subclasses, adding further initialization of the factory. The resulting
105      * <code>DocumentBuilderFactory</code> is cached, so this method will only be called once.
106      *
107      * @return the DocumentBuilderFactory
108      * @throws ParserConfigurationException if thrown by JAXP methods
109      */
110     protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
111         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
112         factory.setValidating(validating);
113         factory.setNamespaceAware(namespaceAware);
114         return factory;
115     }
116 
117     /**
118      * Returns the payload element of the given source.
119      * <p/>
120      * Default implementation checks whether the source is a {@link DOMSource}, and returns the {@linkplain
121      * DOMSource#getNode() node} of that. In all other cases, or when {@linkplain #setAlwaysTransform(boolean)
122      * alwaysTransform} is {@code true}, the source is transformed into a {@link DOMResult}, which is more expensive. If
123      * the passed source is {@code null}, {@code null} is returned.
124      *
125      * @param source          the source to return the root element of; can be {@code null}
126      * @param documentBuilder the document builder to be used for transformations
127      * @return the document element
128      * @throws TransformerException in case of errors
129      */
130     protected Element getDocumentElement(Source source, DocumentBuilder documentBuilder) throws TransformerException {
131         if (source == null) {
132             return null;
133         }
134         if (!alwaysTransform && source instanceof DOMSource) {
135             Node node = ((DOMSource) source).getNode();
136             if (node.getNodeType() == Node.ELEMENT_NODE) {
137                 return (Element) node;
138             }
139             else if (node.getNodeType() == Node.DOCUMENT_NODE) {
140                 return ((Document) node).getDocumentElement();
141             }
142         }
143         // we have no other option than to transform
144         Document requestDocument = documentBuilder.newDocument();
145         DOMResult domResult = new DOMResult(requestDocument);
146         transform(source, domResult);
147         return requestDocument.getDocumentElement();
148     }
149 
150     /**
151      * Template method that subclasses must implement to process the request.
152      * <p/>
153      * <p>Offers the request payload as a DOM <code>Element</code>, and allows subclasses to return a response
154      * <code>Element</code>.
155      * <p/>
156      * <p>The given DOM <code>Document</code> is to be used for constructing <code>Node</code>s, by using the various
157      * <code>create</code> methods.
158      *
159      * @param requestElement   the contents of the SOAP message as DOM elements
160      * @param responseDocument a DOM document to be used for constructing <code>Node</code>s
161      * @return the response element. Can be <code>null</code> to specify no response.
162      */
163     protected abstract Element invokeInternal(Element requestElement, Document responseDocument) throws Exception;
164 
165 }