View Javadoc

1   /*
2    * Copyright 2005-2012 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.jdom2.Document;
26  import org.jdom2.Element;
27  import org.jdom2.input.DOMBuilder;
28  import org.jdom2.transform.JDOMResult;
29  import org.jdom2.transform.JDOMSource;
30  import org.w3c.dom.Node;
31  
32  /**
33   * Abstract base class for endpoints that handle the message payload as JDOM elements.
34   * <p/>
35   * <p>Offers the message payload as a JDOM {@link Element}, and allows subclasses to create a response by returning an
36   * <code>Element</code>.
37   * <p/>
38   * <pAn <code>AbstractJDomPayloadEndpoint</code> can accept only <i>one</i> payload element. Multiple payload elements
39   * are not in accordance with WS-I.
40   *
41   * @author Arjen Poutsma
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 AbstractJDomPayloadEndpoint 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 JDOMResult}.
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 = getDocumentElement(request);
61          Element responseElement = invokeInternal(requestElement);
62          return responseElement != null ? new JDOMSource(responseElement) : null;
63      }
64  
65      /**
66       * Returns the payload element of the given source.
67       * <p/>
68       * Default implementation checks whether the source is a {@link DOMSource}, and uses a {@link DOMBuilder} to create
69       * a JDOM {@link Element}. In all other cases, or when {@linkplain #setAlwaysTransform(boolean) alwaysTransform} is
70       * {@code true}, the source is transformed into a {@link JDOMResult}, which is more expensive. If the passed source
71       * is {@code null}, {@code null} is returned.
72       *
73       * @param source the source to return the root element of; can be {@code null}
74       * @return the document element
75       * @throws TransformerException in case of errors
76       */
77      protected Element getDocumentElement(Source source) throws TransformerException {
78          if (source == null) {
79              return null;
80          }
81          if (!alwaysTransform && source instanceof DOMSource) {
82              Node node = ((DOMSource) source).getNode();
83              DOMBuilder domBuilder = new DOMBuilder();
84              if (node.getNodeType() == Node.ELEMENT_NODE) {
85                  return domBuilder.build((org.w3c.dom.Element) node);
86              }
87              else if (node.getNodeType() == Node.DOCUMENT_NODE) {
88                  Document document = domBuilder.build((org.w3c.dom.Document) node);
89                  return document.getRootElement();
90              }
91          }
92          // we have no other option than to transform
93          JDOMResult jdomResult = new JDOMResult();
94          transform(source, jdomResult);
95          return jdomResult.getDocument().getRootElement();
96      }
97  
98      /**
99       * Template method. Subclasses must implement this. Offers the request payload as a JDOM <code>Element</code>, and
100      * allows subclasses to return a response <code>Element</code>.
101      *
102      * @param requestElement the contents of the SOAP message as JDOM element
103      * @return the response element. Can be <code>null</code> to specify no response.
104      */
105     protected abstract Element invokeInternal(Element requestElement) throws Exception;
106 }