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;
18  
19  import org.springframework.core.MethodParameter;
20  import org.springframework.oxm.GenericMarshaller;
21  import org.springframework.oxm.GenericUnmarshaller;
22  import org.springframework.oxm.Marshaller;
23  import org.springframework.oxm.Unmarshaller;
24  import org.springframework.util.Assert;
25  import org.springframework.ws.WebServiceMessage;
26  import org.springframework.ws.context.MessageContext;
27  import org.springframework.ws.support.MarshallingUtils;
28  
29  /**
30   * Implementation of {@link MethodArgumentResolver} and {@link MethodReturnValueHandler} that uses {@link Marshaller}
31   * and {@link Unmarshaller} to support marshalled objects.
32   *
33   * @author Arjen Poutsma
34   * @since 2.0
35   */
36  public class MarshallingPayloadMethodProcessor extends AbstractPayloadMethodProcessor {
37  
38      private Marshaller marshaller;
39  
40      private Unmarshaller unmarshaller;
41  
42      /**
43       * Creates a new {@code MarshallingPayloadMethodProcessor}. The {@link Marshaller} and {@link Unmarshaller} must be
44       * injected using properties.
45       *
46       * @see #setMarshaller(Marshaller)
47       * @see #setUnmarshaller(Unmarshaller)
48       */
49      public MarshallingPayloadMethodProcessor() {
50      }
51  
52      /**
53       * Creates a new {@code MarshallingPayloadMethodProcessor} with the given marshaller. If the given {@link
54       * Marshaller} also implements the {@link Unmarshaller} interface, it is used for both marshalling and
55       * unmarshalling. Otherwise, an exception is thrown.
56       * <p/>
57       * Note that all {@link Marshaller} implementations in Spring also implement the {@link Unmarshaller} interface, so
58       * that you can safely use this constructor.
59       *
60       * @param marshaller object used as marshaller and unmarshaller
61       * @throws IllegalArgumentException when {@code marshaller} does not implement the {@link Unmarshaller} interface
62       */
63      public MarshallingPayloadMethodProcessor(Marshaller marshaller) {
64          Assert.notNull(marshaller, "marshaller must not be null");
65          Assert.isInstanceOf(Unmarshaller.class, marshaller);
66          setMarshaller(marshaller);
67          setUnmarshaller((Unmarshaller) marshaller);
68      }
69  
70      /**
71       * Creates a new {@code MarshallingPayloadMethodProcessor} with the given marshaller and unmarshaller.
72       *
73       * @param marshaller   the marshaller to use
74       * @param unmarshaller the unmarshaller to use
75       */
76      public MarshallingPayloadMethodProcessor(Marshaller marshaller, Unmarshaller unmarshaller) {
77          Assert.notNull(marshaller, "marshaller must not be null");
78          Assert.notNull(unmarshaller, "unmarshaller must not be null");
79          setMarshaller(marshaller);
80          setUnmarshaller(unmarshaller);
81      }
82  
83      /**
84       * Returns the marshaller used for transforming objects into XML.
85       */
86      public Marshaller getMarshaller() {
87          return marshaller;
88      }
89  
90      /**
91       * Sets the marshaller used for transforming objects into XML.
92       */
93      public void setMarshaller(Marshaller marshaller) {
94          this.marshaller = marshaller;
95      }
96  
97      /**
98       * Returns the unmarshaller used for transforming XML into objects.
99       */
100     public Unmarshaller getUnmarshaller() {
101         return unmarshaller;
102     }
103 
104     /**
105      * Sets the unmarshaller used for transforming XML into objects.
106      */
107     public void setUnmarshaller(Unmarshaller unmarshaller) {
108         this.unmarshaller = unmarshaller;
109     }
110 
111     @Override
112     protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
113         Unmarshaller unmarshaller = getUnmarshaller();
114         if (unmarshaller == null) {
115             return false;
116         }
117         else if (unmarshaller instanceof GenericUnmarshaller) {
118             return ((GenericUnmarshaller) unmarshaller).supports(parameter.getGenericParameterType());
119         }
120         else {
121             return unmarshaller.supports(parameter.getParameterType());
122         }
123     }
124 
125     public Object resolveArgument(MessageContext messageContext, MethodParameter parameter) throws Exception {
126         Unmarshaller unmarshaller = getUnmarshaller();
127         Assert.state(unmarshaller != null, "unmarshaller must not be null");
128 
129         WebServiceMessage request = messageContext.getRequest();
130         Object argument = MarshallingUtils.unmarshal(unmarshaller, request);
131         if (logger.isDebugEnabled()) {
132             logger.debug("Unmarshalled payload request to [" + argument + "]");
133         }
134         return argument;
135     }
136 
137     @Override
138     protected boolean supportsResponsePayloadReturnType(MethodParameter returnType) {
139         Marshaller marshaller = getMarshaller();
140         if (marshaller == null) {
141             return false;
142         }
143         else if (marshaller instanceof GenericMarshaller) {
144             GenericMarshaller genericMarshaller = (GenericMarshaller) marshaller;
145             return genericMarshaller.supports(returnType.getGenericParameterType());
146         }
147         else {
148             return marshaller.supports(returnType.getParameterType());
149         }
150     }
151 
152     public void handleReturnValue(MessageContext messageContext, MethodParameter returnType, Object returnValue)
153             throws Exception {
154         Marshaller marshaller = getMarshaller();
155         Assert.state(marshaller != null, "marshaller must not be null");
156 
157         if (logger.isDebugEnabled()) {
158             logger.debug("Marshalling [" + returnValue + "] to response payload");
159         }
160         WebServiceMessage response = messageContext.getResponse();
161         MarshallingUtils.marshal(marshaller, returnValue, response);
162     }
163 
164 }