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.dom.DOMResult;
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.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.Node;
29  
30  /**
31   * Implementation of {@link org.springframework.ws.server.endpoint.adapter.method.MethodArgumentResolver
32   * MethodArgumentResolver} and {@link org.springframework.ws.server.endpoint.adapter.method.MethodReturnValueHandler
33   * MethodReturnValueHandler} that supports W3C DOM {@linkplain Element elements}.
34   *
35   * @author Arjen Poutsma
36   * @since 2.0
37   */
38  public class DomPayloadMethodProcessor extends AbstractPayloadSourceMethodProcessor {
39  
40      // MethodArgumentResolver
41  
42      @Override
43      protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
44          return supports(parameter);
45      }
46  
47      @Override
48      protected Node resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload) throws Exception {
49          if (requestPayload instanceof DOMSource) {
50              return resolveArgumentDomSource(parameter, (DOMSource) requestPayload);
51          }
52          else {
53              DOMResult domResult = new DOMResult();
54              transform(requestPayload, domResult);
55              DOMSource domSource = new DOMSource(domResult.getNode());
56              return resolveArgumentDomSource(parameter, domSource);
57          }
58      }
59  
60      private Node resolveArgumentDomSource(MethodParameter parameter, DOMSource requestSource) {
61          Class<?> parameterType = parameter.getParameterType();
62          Node requestNode = requestSource.getNode();
63          if (parameterType.isAssignableFrom(requestNode.getClass())) {
64              return requestNode;
65          }
66          else if (Element.class.equals(parameterType) && requestNode instanceof Document) {
67              Document document = (Document) requestNode;
68              return document.getDocumentElement();
69          }
70          // should not happen
71          throw new UnsupportedOperationException();
72      }
73  
74      // MethodReturnValueHandler
75  
76      @Override
77      protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
78          return supports(returnType);
79      }
80  
81      @Override
82      protected DOMSource createResponsePayload(MethodParameter returnType, Object returnValue) {
83          Element returnedElement = (Element) returnValue;
84          return new DOMSource(returnedElement);
85      }
86  
87      private boolean supports(MethodParameter parameter) {
88          return Element.class.equals(parameter.getParameterType());
89      }
90  
91  }