View Javadoc

1   /*
2    * Copyright 2007 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 {@link Unmarshaller#supports(Class) is supported} by the {@link
42   * Unmarshaller}, and return either <code>void</code> or a type {@link Marshaller#supports(Class) supported} by the
43   * {@link Marshaller}. The method can have any name, as long as it is mapped by an {@link 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   */
53  public class MarshallingMethodEndpointAdapter extends AbstractMethodEndpointAdapter implements InitializingBean {
54  
55      private Marshaller marshaller;
56  
57      private Unmarshaller unmarshaller;
58  
59      /**
60       * Creates a new <code>MarshallingMethodEndpointAdapter</code>. The {@link Marshaller} and {@link Unmarshaller} must
61       * be injected using properties.
62       *
63       * @see #setMarshaller(org.springframework.oxm.Marshaller)
64       * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller)
65       */
66      public MarshallingMethodEndpointAdapter() {
67      }
68  
69      /**
70       * Creates a new <code>MarshallingMethodEndpointAdapter</code> with the given marshaller. If the given {@link
71       * Marshaller} also implements the {@link Unmarshaller} interface, it is used for both marshalling and
72       * unmarshalling. Otherwise, an exception is thrown.
73       * <p/>
74       * Note that all {@link Marshaller} implementations in Spring-WS also implement the {@link Unmarshaller} interface,
75       * so that you can safely use this constructor.
76       *
77       * @param marshaller object used as marshaller and unmarshaller
78       * @throws IllegalArgumentException when <code>marshaller</code> does not implement the {@link Unmarshaller}
79       *                                  interface
80       */
81      public MarshallingMethodEndpointAdapter(Marshaller marshaller) {
82          Assert.notNull(marshaller, "marshaller must not be null");
83          if (!(marshaller instanceof Unmarshaller)) {
84              throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
85                      "interface. Please set an Unmarshaller explicitely by using the " +
86                      "MarshallingMethodEndpointAdapter(Marshaller, Unmarshaller) constructor.");
87          }
88          else {
89              this.setMarshaller(marshaller);
90              this.setUnmarshaller((Unmarshaller) marshaller);
91          }
92      }
93  
94      /**
95       * Creates a new <code>MarshallingMethodEndpointAdapter</code> with the given marshaller and unmarshaller.
96       *
97       * @param marshaller   the marshaller to use
98       * @param unmarshaller the unmarshaller to use
99       */
100     public MarshallingMethodEndpointAdapter(Marshaller marshaller, Unmarshaller unmarshaller) {
101         Assert.notNull(marshaller, "marshaller must not be null");
102         Assert.notNull(unmarshaller, "unmarshaller must not be null");
103         this.setMarshaller(marshaller);
104         this.setUnmarshaller(unmarshaller);
105     }
106 
107     /** Returns the marshaller used for transforming objects into XML. */
108     public Marshaller getMarshaller() {
109         return marshaller;
110     }
111 
112     /** Sets the marshaller used for transforming objects into XML. */
113     public final void setMarshaller(Marshaller marshaller) {
114         this.marshaller = marshaller;
115     }
116 
117     /** Returns the unmarshaller used for transforming XML into objects. */
118     public Unmarshaller getUnmarshaller() {
119         return unmarshaller;
120     }
121 
122     /** Sets the unmarshaller used for transforming XML into objects. */
123     public final void setUnmarshaller(Unmarshaller unmarshaller) {
124         this.unmarshaller = unmarshaller;
125     }
126 
127     public void afterPropertiesSet() throws Exception {
128         Assert.notNull(getMarshaller(), "marshaller is required");
129         Assert.notNull(getUnmarshaller(), "unmarshaller is required");
130     }
131 
132     protected void invokeInternal(MessageContext messageContext, MethodEndpoint methodEndpoint) throws Exception {
133         WebServiceMessage request = messageContext.getRequest();
134         Object requestObject = unmarshalRequest(request);
135         Object responseObject = methodEndpoint.invoke(new Object[]{requestObject});
136         if (responseObject != null) {
137             WebServiceMessage response = messageContext.getResponse();
138             marshalResponse(responseObject, response);
139         }
140     }
141 
142     private Object unmarshalRequest(WebServiceMessage request) throws IOException {
143         Object requestObject = MarshallingUtils.unmarshal(getUnmarshaller(), request);
144         if (logger.isDebugEnabled()) {
145             logger.debug("Unmarshalled payload request to [" + requestObject + "]");
146         }
147         return requestObject;
148     }
149 
150     private void marshalResponse(Object responseObject, WebServiceMessage response) throws IOException {
151         if (logger.isDebugEnabled()) {
152             logger.debug("Marshalling [" + responseObject + "] to response payload");
153         }
154         MarshallingUtils.marshal(getMarshaller(), responseObject, response);
155     }
156 
157     /**
158      * Supports a method with a single, unmarshallable parameter, and that return <code>void</code> or a marshallable
159      * type.
160      *
161      * @see Marshaller#supports(Class)
162      * @see Unmarshaller#supports(Class)
163      */
164     protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
165         Method method = methodEndpoint.getMethod();
166         return supportsReturnType(method) && supportsParameters(method);
167     }
168 
169     private boolean supportsReturnType(Method method) {
170         return (Void.TYPE.equals(method.getReturnType()) || getMarshaller().supports(method.getReturnType()));
171     }
172 
173     private boolean supportsParameters(Method method) {
174         if (method.getParameterTypes().length != 1) {
175             return false;
176         }
177         else {
178             return getUnmarshaller().supports(method.getParameterTypes()[0]);
179         }
180     }
181 }