1 /*
2  * Copyright 2005-2014 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     @Override
46     public Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws JAXBException {
47         Class<?> parameterType = parameter.getParameterType();
48
49         if (parameterType.isAnnotationPresent(XmlRootElement.class)) {
50             return unmarshalFromRequestPayload(messageContext, parameterType);
51         }
52         else {
53             JAXBElement<?> element = unmarshalElementFromRequestPayload(messageContext, parameterType);
54             return element != null ? element.getValue() : null;
55         }
56     }
57
58     @Override
59     protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
60         Class<?> parameterType = returnType.getParameterType();
61         return parameterType.isAnnotationPresent(XmlRootElement.class);
62     }
63
64     @Override
65     protected void handleReturnValueInternal(MessageContext messageContext, MethodParameter returnType, Object returnValue)
66             throws JAXBException {
67         Class<?> parameterType = returnType.getParameterType();
68         marshalToResponsePayload(messageContext, parameterType, returnValue);
69     }
70
71
72 }
73