View Javadoc

1   /*
2    * Copyright 2005-2011 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;
18  
19  import java.io.IOException;
20  import java.lang.reflect.Method;
21  
22  import org.springframework.beans.factory.InitializingBean;
23  import org.springframework.oxm.Marshaller;
24  import org.springframework.oxm.Unmarshaller;
25  import org.springframework.util.Assert;
26  import org.springframework.ws.WebServiceMessage;
27  import org.springframework.ws.context.MessageContext;
28  import org.springframework.ws.server.EndpointMapping;
29  import org.springframework.ws.server.endpoint.MethodEndpoint;
30  import org.springframework.ws.support.MarshallingUtils;
31  
32  /**
33   * Adapter that supports endpoint methods that use marshalling. Supports methods with the following signature:
34   * <pre>
35   * void handleMyMessage(MyUnmarshalledType request);
36   * </pre>
37   * or
38   * <pre>
39   * MyMarshalledType handleMyMessage(MyUnmarshalledType request);
40   * </pre>
41   * I.e. methods that take a single parameter that [email protected] Unmarshaller#supports(Class) is supported} by the [email protected]
42   * Unmarshaller}, and return either <code>void</code> or a type [email protected] Marshaller#supports(Class) supported} by the
43   * [email protected] Marshaller}. The method can have any name, as long as it is mapped by an [email protected] EndpointMapping}.
44   * <p/>
45   * This endpoint needs a <code>Marshaller</code> and <code>Unmarshaller</code>, both of which can be set using
46   * properties.
47   *
48   * @author Arjen Poutsma
49   * @see #setMarshaller(org.springframework.oxm.Marshaller)
50   * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller)
51   * @since 1.0.0
52   * @deprecated as of Spring Web Services 2.0, in favor of [email protected] DefaultMethodEndpointAdapter} and [email protected]
53   *             org.springframework.ws.server.endpoint.adapter.method.MarshallingPayloadMethodProcessor
54   *             MarshallingPayloadMethodProcessor}.
55   */
56  @Deprecated
57  public class MarshallingMethodEndpointAdapter extends AbstractMethodEndpointAdapter implements InitializingBean {
58  
59      private Marshaller marshaller;
60  
61      private Unmarshaller unmarshaller;
62  
63      /**
64       * Creates a new <code>MarshallingMethodEndpointAdapter</code>. The [email protected] Marshaller} and [email protected] Unmarshaller} must
65       * be injected using properties.
66       *
67       * @see #setMarshaller(org.springframework.oxm.Marshaller)
68       * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller)
69       */
70      public MarshallingMethodEndpointAdapter() {
71      }
72  
73      /**
74       * Creates a new <code>MarshallingMethodEndpointAdapter</code> with the given marshaller. If the given [email protected]
75       * Marshaller} also implements the [email protected] Unmarshaller} interface, it is used for both marshalling and
76       * unmarshalling. Otherwise, an exception is thrown.
77       * <p/>
78       * Note that all [email protected] Marshaller} implementations in Spring also implement the [email protected] Unmarshaller} interface,
79       * so that you can safely use this constructor.
80       *
81       * @param marshaller object used as marshaller and unmarshaller
82       * @throws IllegalArgumentException when <code>marshaller</code> does not implement the [email protected] Unmarshaller}
83       *                                  interface
84       */
85      public MarshallingMethodEndpointAdapter(Marshaller marshaller) {
86          Assert.notNull(marshaller, "marshaller must not be null");
87          if (!(marshaller instanceof Unmarshaller)) {
88              throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
89                      "interface. Please set an Unmarshaller explicitly by using the " +
90                      "MarshallingMethodEndpointAdapter(Marshaller, Unmarshaller) constructor.");
91          }
92          else {
93              this.setMarshaller(marshaller);
94              this.setUnmarshaller((Unmarshaller) marshaller);
95          }
96      }
97  
98      /**
99       * Creates a new <code>MarshallingMethodEndpointAdapter</code> with the given marshaller and unmarshaller.
100      *
101      * @param marshaller   the marshaller to use
102      * @param unmarshaller the unmarshaller to use
103      */
104     public MarshallingMethodEndpointAdapter(Marshaller marshaller, Unmarshaller unmarshaller) {
105         Assert.notNull(marshaller, "marshaller must not be null");
106         Assert.notNull(unmarshaller, "unmarshaller must not be null");
107         this.setMarshaller(marshaller);
108         this.setUnmarshaller(unmarshaller);
109     }
110 
111     /** Returns the marshaller used for transforming objects into XML. */
112     public Marshaller getMarshaller() {
113         return marshaller;
114     }
115 
116     /** Sets the marshaller used for transforming objects into XML. */
117     public final void setMarshaller(Marshaller marshaller) {
118         this.marshaller = marshaller;
119     }
120 
121     /** Returns the unmarshaller used for transforming XML into objects. */
122     public Unmarshaller getUnmarshaller() {
123         return unmarshaller;
124     }
125 
126     /** Sets the unmarshaller used for transforming XML into objects. */
127     public final void setUnmarshaller(Unmarshaller unmarshaller) {
128         this.unmarshaller = unmarshaller;
129     }
130 
131     public void afterPropertiesSet() throws Exception {
132         Assert.notNull(getMarshaller(), "marshaller is required");
133         Assert.notNull(getUnmarshaller(), "unmarshaller is required");
134     }
135 
136     @Override
137     protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
138         WebServiceMessage request = messageContext.getRequest();
139         Object requestObject = unmarshalRequest(request);
140         Object responseObject = methodEndpoint.invoke(new Object[]{requestObject});
141         if (responseObject != null) {
142             WebServiceMessage response = messageContext.getResponse();
143             marshalResponse(responseObject, response);
144         }
145     }
146 
147     private Object unmarshalRequest(WebServiceMessage request) throws IOException {
148         Object requestObject = MarshallingUtils.unmarshal(getUnmarshaller(), request);
149         if (logger.isDebugEnabled()) {
150             logger.debug("Unmarshalled payload request to [" + requestObject + "]");
151         }
152         return requestObject;
153     }
154 
155     private void marshalResponse(Object responseObject, WebServiceMessage response) throws IOException {
156         if (logger.isDebugEnabled()) {
157             logger.debug("Marshalling [" + responseObject + "] to response payload");
158         }
159         MarshallingUtils.marshal(getMarshaller(), responseObject, response);
160     }
161 
162     /**
163      * Supports a method with a single, unmarshallable parameter, and that return <code>void</code> or a marshallable
164      * type.
165      *
166      * @see Marshaller#supports(Class)
167      * @see Unmarshaller#supports(Class)
168      */
169     @Override
170     protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
171         Method method = methodEndpoint.getMethod();
172         return supportsReturnType(method) && supportsParameters(method);
173     }
174 
175     private boolean supportsReturnType(Method method) {
176         return (Void.TYPE.equals(method.getReturnType()) || getMarshaller().supports(method.getReturnType()));
177     }
178 
179     private boolean supportsParameters(Method method) {
180         if (method.getParameterTypes().length != 1) {
181             return false;
182         }
183         else {
184             return getUnmarshaller().supports(method.getParameterTypes()[0]);
185         }
186     }
187 }