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.adapter.method.dom;
18  
19  import javax.xml.transform.Source;
20  import javax.xml.transform.dom.DOMSource;
21  
22  import org.springframework.core.MethodParameter;
23  import org.springframework.ws.server.endpoint.adapter.method.AbstractPayloadSourceMethodProcessor;
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   * Implementation of [email protected] [email protected]entResolver
34   * MethodArgumentResolver} and [email protected] [email protected]nValueHandler
35   * MethodReturnValueHandler} that supports JDOM [email protected] Element elements}.
36   *
37   * @author Arjen Poutsma
38   * @since 2.0
39   */
40  public class JDomPayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
41  
42      @Override
43      protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
44          return supports(parameter);
45      }
46  
47      @Override
48      protected Element resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload) throws Exception {
49          if (requestPayload instanceof DOMSource) {
50              Node node = ((DOMSource) requestPayload).getNode();
51              DOMBuilder domBuilder = new DOMBuilder();
52              if (node.getNodeType() == Node.ELEMENT_NODE) {
53                  return domBuilder.build((org.w3c.dom.Element) node);
54              }
55              else if (node.getNodeType() == Node.DOCUMENT_NODE) {
56                  Document document = domBuilder.build((org.w3c.dom.Document) node);
57                  return document.getRootElement();
58              }
59          }
60          // we have no other option than to transform
61          JDOMResult jdomResult = new JDOMResult();
62          transform(requestPayload, jdomResult);
63          return jdomResult.getDocument().getRootElement();
64      }
65  
66      @Override
67      protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
68          return supports(returnType);
69      }
70  
71      @Override
72      protected Source createResponsePayload(MethodParameter returnType, Object returnValue) {
73          Element returnedElement = (Element) returnValue;
74          return new JDOMSource(returnedElement);
75      }
76  
77      private boolean supports(MethodParameter parameter) {
78          return Element.class.equals(parameter.getParameterType());
79      }
80  
81  }