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.transform.Source;
20  import javax.xml.transform.TransformerException;
21  import javax.xml.transform.dom.DOMSource;
22  
23  import org.springframework.xml.transform.TransformerObjectSupport;
24  
25  import org.dom4j.Document;
26  import org.dom4j.DocumentHelper;
27  import org.dom4j.Element;
28  import org.dom4j.io.DOMReader;
29  import org.dom4j.io.DocumentResult;
30  import org.dom4j.io.DocumentSource;
31  import org.w3c.dom.Node;
32  
33  /**
34   * Abstract base class for endpoints that handle the message payload as dom4j elements. Offers the message payload as a
35   * dom4j <code>Element</code>, and allows subclasses to create a response by returning an <code>Element</code>.
36   * <p/>
37   * An <code>AbstractDom4JPayloadEndpoint</code> only accept one payload element. Multiple payload elements are not in
38   * accordance with WS-I.
39   *
40   * @author Arjen Poutsma
41   * @see org.dom4j.Element
42   * @since 1.0.0
43   * @deprecated as of Spring Web Services 2.0, in favor of annotated endpoints
44   */
45  @Deprecated
46  public abstract class AbstractDom4jPayloadEndpoint extends TransformerObjectSupport implements PayloadEndpoint {
47  
48      private boolean alwaysTransform = false;
49  
50      /**
51       * Set if the request {@link Source} should always be transformed into a new {@link DocumentResult}.
52       * <p/>
53       * Default is {@code false}, which is faster.
54       */
55      public void setAlwaysTransform(boolean alwaysTransform) {
56          this.alwaysTransform = alwaysTransform;
57      }
58  
59      public final Source invoke(Source request) throws Exception {
60          Element requestElement = null;
61          if (request != null) {
62              DocumentResult dom4jResult = new DocumentResult();
63              transform(request, dom4jResult);
64              requestElement = dom4jResult.getDocument().getRootElement();
65          }
66          Document responseDocument = DocumentHelper.createDocument();
67          Element responseElement = invokeInternal(requestElement, responseDocument);
68          return responseElement != null ? new DocumentSource(responseElement) : null;
69      }
70  
71      /**
72       * Returns the payload element of the given source.
73       * <p/>
74       * Default implementation checks whether the source is a {@link javax.xml.transform.dom.DOMSource}, and uses a
75       * {@link org.jdom.input.DOMBuilder} to create a JDOM {@link org.jdom.Element}. In all other cases, or when
76       * {@linkplain #setAlwaysTransform(boolean) alwaysTransform} is {@code true}, the source is transformed into a
77       * {@link org.jdom.transform.JDOMResult}, which is more expensive. If the passed source is {@code null}, {@code
78       * null} is returned.
79       *
80       * @param source the source to return the root element of; can be {@code null}
81       * @return the document element
82       * @throws javax.xml.transform.TransformerException
83       *          in case of errors
84       */
85      protected Element getDocumentElement(Source source) throws TransformerException {
86          if (source == null) {
87              return null;
88          }
89          if (!alwaysTransform && source instanceof DOMSource) {
90              Node node = ((DOMSource) source).getNode();
91              if (node.getNodeType() == Node.DOCUMENT_NODE) {
92                  DOMReader domReader = new DOMReader();
93                  Document document = domReader.read((org.w3c.dom.Document) node);
94                  return document.getRootElement();
95              }
96          }
97          // we have no other option than to transform
98          DocumentResult dom4jResult = new DocumentResult();
99          transform(source, dom4jResult);
100         return dom4jResult.getDocument().getRootElement();
101     }
102 
103     /**
104      * Template method. Subclasses must implement this. Offers the request payload as a dom4j <code>Element</code>, and
105      * allows subclasses to return a response <code>Element</code>.
106      * <p/>
107      * The given dom4j <code>Document</code> is to be used for constructing a response element, by using
108      * <code>addElement</code>.
109      *
110      * @param requestElement   the contents of the SOAP message as dom4j elements
111      * @param responseDocument a dom4j document to be used for constructing a response
112      * @return the response element. Can be <code>null</code> to specify no response.
113      */
114     protected abstract Element invokeInternal(Element requestElement, Document responseDocument) throws Exception;
115 }