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