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;
18  
19  import java.io.IOException;
20  
21  import org.springframework.beans.factory.InitializingBean;
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  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Endpoint that unmarshals the request payload, and marshals the response object. This endpoint needs a
34   * <code>Marshaller</code> and <code>Unmarshaller</code>, both of which can be set using properties. An abstract
35   * template method is invoked using the request object as a parameter, and allows for a response object to be returned.
36   *
37   * @author Arjen Poutsma
38   * @see #setMarshaller(org.springframework.oxm.Marshaller)
39   * @see Marshaller
40   * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller)
41   * @see Unmarshaller
42   * @see #invokeInternal(Object)
43   * @since 1.0.0
44   * @deprecated as of Spring Web Services 2.0, in favor of annotated endpoints
45   */
46  @Deprecated
47  public abstract class AbstractMarshallingPayloadEndpoint implements MessageEndpoint, InitializingBean {
48  
49      /** Logger available to subclasses. */
50      protected final Log logger = LogFactory.getLog(getClass());
51  
52      private Marshaller marshaller;
53  
54      private Unmarshaller unmarshaller;
55  
56      /**
57       * Creates a new <code>AbstractMarshallingPayloadEndpoint</code>. The {@link Marshaller} and {@link Unmarshaller}
58       * must be injected using properties.
59       *
60       * @see #setMarshaller(org.springframework.oxm.Marshaller)
61       * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller)
62       */
63      protected AbstractMarshallingPayloadEndpoint() {
64      }
65  
66      /**
67       * Creates a new <code>AbstractMarshallingPayloadEndpoint</code> with the given marshaller. The given {@link
68       * Marshaller} should also implements the {@link Unmarshaller}, since it is used for both marshalling and
69       * unmarshalling. If it is not, an exception is thrown.
70       * <p/>
71       * Note that all {@link Marshaller} implementations in Spring-WS also implement the {@link Unmarshaller} interface,
72       * so that you can safely use this constructor.
73       *
74       * @param marshaller object used as marshaller and unmarshaller
75       * @throws IllegalArgumentException when <code>marshaller</code> does not implement the {@link Unmarshaller}
76       *                                  interface
77       * @see #AbstractMarshallingPayloadEndpoint(Marshaller,Unmarshaller)
78       */
79      protected AbstractMarshallingPayloadEndpoint(Marshaller marshaller) {
80          Assert.notNull(marshaller, "marshaller must not be null");
81          if (!(marshaller instanceof Unmarshaller)) {
82              throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
83                      "interface. Please set an Unmarshaller explicitly by using the " +
84                      "AbstractMarshallingPayloadEndpoint(Marshaller, Unmarshaller) constructor.");
85          }
86          else {
87              setMarshaller(marshaller);
88              setUnmarshaller((Unmarshaller) marshaller);
89          }
90      }
91  
92      /**
93       * Creates a new <code>AbstractMarshallingPayloadEndpoint</code> with the given marshaller and unmarshaller.
94       *
95       * @param marshaller   the marshaller to use
96       * @param unmarshaller the unmarshaller to use
97       */
98      protected AbstractMarshallingPayloadEndpoint(Marshaller marshaller, Unmarshaller unmarshaller) {
99          Assert.notNull(marshaller, "marshaller must not be null");
100         Assert.notNull(unmarshaller, "unmarshaller must not be null");
101         setMarshaller(marshaller);
102         setUnmarshaller(unmarshaller);
103     }
104 
105     /** Returns the marshaller used for transforming objects into XML. */
106     public Marshaller getMarshaller() {
107         return marshaller;
108     }
109 
110     /** Sets the marshaller used for transforming objects into XML. */
111     public final void setMarshaller(Marshaller marshaller) {
112         this.marshaller = marshaller;
113     }
114 
115     /** Returns the unmarshaller used for transforming XML into objects. */
116     public Unmarshaller getUnmarshaller() {
117         return unmarshaller;
118     }
119 
120     /** Sets the unmarshaller used for transforming XML into objects. */
121     public final void setUnmarshaller(Unmarshaller unmarshaller) {
122         this.unmarshaller = unmarshaller;
123     }
124 
125     public void afterPropertiesSet() throws Exception {
126         afterMarshallerSet();
127     }
128 
129     public final void invoke(MessageContext messageContext) throws Exception {
130         WebServiceMessage request = messageContext.getRequest();
131         Object requestObject = unmarshalRequest(request);
132         if (onUnmarshalRequest(messageContext, requestObject)) {
133             Object responseObject = invokeInternal(requestObject);
134             if (responseObject != null) {
135                 WebServiceMessage response = messageContext.getResponse();
136                 marshalResponse(responseObject, response);
137                 onMarshalResponse(messageContext, requestObject, responseObject);
138             }
139         }
140     }
141 
142     private Object unmarshalRequest(WebServiceMessage request) throws IOException {
143         Unmarshaller unmarshaller = getUnmarshaller();
144         Assert.notNull(unmarshaller, "No unmarshaller registered. Check configuration of endpoint.");
145         Object requestObject = MarshallingUtils.unmarshal(unmarshaller, request);
146         if (logger.isDebugEnabled()) {
147             logger.debug("Unmarshalled payload request to [" + requestObject + "]");
148         }
149         return requestObject;
150     }
151 
152     /**
153      * Callback for post-processing in terms of unmarshalling. Called on each message request, after standard
154      * unmarshalling.
155      * <p/>
156      * Default implementation returns <code>true</code>.
157      *
158      * @param messageContext the message context
159      * @param requestObject  the object unmarshalled from the {@link MessageContext#getRequest() request}
160      * @return <code>true</code> to continue and call {@link #invokeInternal(Object)}; <code>false</code> otherwise
161      */
162     protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception {
163         return true;
164     }
165 
166     private void marshalResponse(Object responseObject, WebServiceMessage response) throws IOException {
167         Marshaller marshaller = getMarshaller();
168         Assert.notNull(marshaller, "No marshaller registered. Check configuration of endpoint.");
169         if (logger.isDebugEnabled()) {
170             logger.debug("Marshalling [" + responseObject + "] to response payload");
171         }
172         MarshallingUtils.marshal(marshaller, responseObject, response);
173     }
174 
175     /**
176      * Callback for post-processing in terms of marshalling. Called on each message request, after standard marshalling
177      * of the response. Only invoked when {@link #invokeInternal(Object)} returns an object.
178      * <p/>
179      * Default implementation is empty.
180      *
181      * @param messageContext the message context
182      * @param requestObject  the object unmarshalled from the {@link MessageContext#getRequest() request}
183      * @param responseObject the object marshalled to the {@link MessageContext#getResponse()}  request}
184      */
185     protected void onMarshalResponse(MessageContext messageContext, Object requestObject, Object responseObject) {
186     }
187 
188     /**
189      * Template method that gets called after the marshaller and unmarshaller have been set.
190      * <p/>
191      * The default implementation does nothing.
192      *
193      * @deprecated as of Spring Web Services 1.5: {@link #afterPropertiesSet()} is no longer final, so this can safely
194      *             be overridden in subclasses
195      */
196     @Deprecated
197     public void afterMarshallerSet() throws Exception {
198     }
199 
200     /**
201      * Template method that subclasses must implement to process a request.
202      * <p/>
203      * The unmarshalled request object is passed as a parameter, and the returned object is marshalled to a response. If
204      * no response is required, return <code>null</code>.
205      *
206      * @param requestObject the unmarshalled message payload as an object
207      * @return the object to be marshalled as response, or <code>null</code> if a response is not required
208      */
209     protected abstract Object invokeInternal(Object requestObject) throws Exception;
210 }