View Javadoc

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