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.adapter.method.dom;
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.core.MethodParameter;
24  import org.springframework.ws.server.endpoint.adapter.method.AbstractPayloadSourceMethodProcessor;
25  
26  import org.dom4j.Document;
27  import org.dom4j.Element;
28  import org.dom4j.io.DOMReader;
29  import org.dom4j.io.DocumentResult;
30  import org.dom4j.io.DocumentSource;
31  
32  /**
33   * Implementation of [email protected] [email protected]entResolver
34   * MethodArgumentResolver} and [email protected] [email protected]nValueHandler
35   * MethodReturnValueHandler} that supports dom4j [email protected] Element elements}.
36   *
37   * @author Arjen Poutsma
38   * @since 2.0
39   */
40  public class Dom4jPayloadMethodProcessor 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)
49              throws TransformerException {
50          if (requestPayload instanceof DOMSource) {
51              org.w3c.dom.Node node = ((DOMSource) requestPayload).getNode();
52              if (node.getNodeType() == org.w3c.dom.Node.DOCUMENT_NODE) {
53                  DOMReader domReader = new DOMReader();
54                  Document document = domReader.read((org.w3c.dom.Document) node);
55                  return document.getRootElement();
56              }
57          }
58          // we have no other option than to transform
59          DocumentResult dom4jResult = new DocumentResult();
60          transform(requestPayload, dom4jResult);
61          return dom4jResult.getDocument().getRootElement();
62      }
63  
64      @Override
65      protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
66          return supports(returnType);
67      }
68  
69      @Override
70      protected Source createResponsePayload(MethodParameter returnType, Object returnValue) {
71          Element returnedElement = (Element) returnValue;
72          return new DocumentSource(returnedElement);
73      }
74  
75      private boolean supports(MethodParameter parameter) {
76          return Element.class.equals(parameter.getParameterType());
77      }
78  
79  }