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.jaxb;
18  
19  import javax.xml.bind.JAXBElement;
20  import javax.xml.bind.JAXBException;
21  import javax.xml.bind.annotation.XmlRootElement;
22  import javax.xml.bind.annotation.XmlType;
23  
24  import org.springframework.core.MethodParameter;
25  import org.springframework.ws.context.MessageContext;
26  
27  /**
28   * Implementation of {@link org.springframework.ws.server.endpoint.adapter.method.MethodArgumentResolver
29   * MethodArgumentResolver} and {@link org.springframework.ws.server.endpoint.adapter.method.MethodReturnValueHandler
30   * MethodReturnValueHandler} that supports parameters annotated with {@link XmlRootElement @XmlRootElement} or {@link
31   * XmlType @XmlType}, and return values annotated with {@link XmlRootElement @XmlRootElement}.
32   *
33   * @author Arjen Poutsma
34   * @since 2.0
35   */
36  public class XmlRootElementPayloadMethodProcessor extends AbstractJaxb2PayloadMethodProcessor {
37  
38      @Override
39      protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
40          Class<?> parameterType = parameter.getParameterType();
41          return parameterType.isAnnotationPresent(XmlRootElement.class) ||
42                  parameterType.isAnnotationPresent(XmlType.class);
43      }
44  
45      public Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws JAXBException {
46          Class<?> parameterType = parameter.getParameterType();
47  
48          if (parameterType.isAnnotationPresent(XmlRootElement.class)) {
49              return unmarshalFromRequestPayload(messageContext, parameterType);
50          }
51          else {
52              JAXBElement<?> element = unmarshalElementFromRequestPayload(messageContext, parameterType);
53              return element != null ? element.getValue() : null;
54          }
55      }
56  
57      @Override
58      protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
59          Class<?> parameterType = returnType.getParameterType();
60          return parameterType.isAnnotationPresent(XmlRootElement.class);
61      }
62  
63      public void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
64              throws JAXBException {
65          Class<?> parameterType = returnType.getParameterType();
66          marshalToResponsePayload(messageContext, parameterType, returnValue);
67      }
68  
69  
70  }