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 java.lang.reflect.ParameterizedType;
20 import java.lang.reflect.Type;
21 import javax.xml.bind.JAXBElement;
22 import javax.xml.bind.JAXBException;
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 {@link JAXBElement} objects.
31  *
32  * @author Arjen Poutsma
33  * @since 2.0
34  */

35 public class JaxbElementPayloadMethodProcessor extends AbstractJaxb2PayloadMethodProcessor {
36
37     @Override
38     protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
39         Class<?> parameterType = parameter.getParameterType();
40         Type genericType = parameter.getGenericParameterType();
41         return JAXBElement.class.equals(parameterType) && genericType instanceof ParameterizedType;
42     }
43
44     @Override
45     public JAXBElement<?> resolveArgument(MessageContext messageContext, MethodParameter parameter)
46             throws JAXBException {
47         ParameterizedType parameterizedType = (ParameterizedType) parameter.getGenericParameterType();
48         Class<?> clazz = (Class<?>) parameterizedType.getActualTypeArguments()[0];
49         return unmarshalElementFromRequestPayload(messageContext, clazz);
50     }
51
52     @Override
53     protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
54         Class<?> parameterType = returnType.getParameterType();
55         return JAXBElement.class.isAssignableFrom(parameterType);
56     }
57
58     @Override
59     protected void handleReturnValueInternal(MessageContext messageContext, MethodParameter returnType, Object returnValue)
60             throws JAXBException {
61         JAXBElement<?> element = (JAXBElement<?>) returnValue;
62         marshalToResponsePayload(messageContext, element.getDeclaredType(), element);
63     }
64 }
65