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 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 [email protected] [email protected]entResolver
29   * MethodArgumentResolver} and [email protected] [email protected]nValueHandler
30   * MethodReturnValueHandler} that supports [email protected] 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      public JAXBElement<?> resolveArgument(MessageContext messageContext, MethodParameter parameter)
45              throws JAXBException {
46          ParameterizedType parameterizedType = (ParameterizedType) parameter.getGenericParameterType();
47          Class<?> clazz = (Class) parameterizedType.getActualTypeArguments()[0];
48          return unmarshalElementFromRequestPayload(messageContext, clazz);
49      }
50  
51      @Override
52      protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
53          Class<?> parameterType = returnType.getParameterType();
54          return JAXBElement.class.isAssignableFrom(parameterType);
55      }
56  
57      public void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
58              throws JAXBException {
59          JAXBElement<?> element = (JAXBElement<?>) returnValue;
60          marshalToResponsePayload(messageContext, element.getDeclaredType(), element);
61      }
62  }