1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.oxm.support;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import javax.jms.BytesMessage;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.Session;
26 import javax.jms.TextMessage;
27 import javax.xml.transform.Result;
28 import javax.xml.transform.Source;
29 import javax.xml.transform.stream.StreamResult;
30 import javax.xml.transform.stream.StreamSource;
31
32 import org.springframework.beans.factory.InitializingBean;
33 import org.springframework.jms.support.converter.MessageConversionException;
34 import org.springframework.jms.support.converter.MessageConverter;
35 import org.springframework.oxm.Marshaller;
36 import org.springframework.oxm.MarshallingFailureException;
37 import org.springframework.oxm.Unmarshaller;
38 import org.springframework.oxm.UnmarshallingFailureException;
39 import org.springframework.util.Assert;
40 import org.springframework.xml.transform.StringResult;
41 import org.springframework.xml.transform.StringSource;
42
43
44
45
46
47
48
49
50
51
52
53 public class MarshallingMessageConverter implements MessageConverter, InitializingBean {
54
55
56 public static final int MARSHAL_TO_BYTES_MESSAGE = 1;
57
58
59 public static final int MARSHAL_TO_TEXT_MESSAGE = 2;
60
61 private Marshaller marshaller;
62
63 private Unmarshaller unmarshaller;
64
65 private int marshalTo = MARSHAL_TO_BYTES_MESSAGE;
66
67
68
69
70
71 public MarshallingMessageConverter() {
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public MarshallingMessageConverter(Marshaller marshaller) {
87 Assert.notNull(marshaller, "marshaller must not be null");
88 if (!(marshaller instanceof Unmarshaller)) {
89 throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
90 "interface. Please set an Unmarshaller explicitely by using the " +
91 "AbstractMarshallingPayloadEndpoint(Marshaller, Unmarshaller) constructor.");
92 }
93 else {
94 this.marshaller = marshaller;
95 this.unmarshaller = (Unmarshaller) marshaller;
96 }
97 }
98
99
100
101
102
103
104
105 public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
106 Assert.notNull(marshaller, "marshaller must not be null");
107 Assert.notNull(unmarshaller, "unmarshaller must not be null");
108 this.marshaller = marshaller;
109 this.unmarshaller = unmarshaller;
110 }
111
112
113
114
115
116
117
118
119
120 public void setMarshalTo(int marshalTo) {
121 this.marshalTo = marshalTo;
122 }
123
124
125 public void setMarshaller(Marshaller marshaller) {
126 this.marshaller = marshaller;
127 }
128
129
130 public void setUnmarshaller(Unmarshaller unmarshaller) {
131 this.unmarshaller = unmarshaller;
132 }
133
134 public void afterPropertiesSet() throws Exception {
135 Assert.notNull(marshaller, "Property 'marshaller' is required");
136 Assert.notNull(unmarshaller, "Property 'unmarshaller' is required");
137 }
138
139
140
141
142
143
144
145
146 public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
147 try {
148 switch (marshalTo) {
149 case MARSHAL_TO_TEXT_MESSAGE:
150 return marshalToTextMessage(object, session, marshaller);
151 case MARSHAL_TO_BYTES_MESSAGE:
152 return marshalToBytesMessage(object, session, marshaller);
153 default:
154 return marshalToMessage(object, session, marshaller);
155 }
156 }
157 catch (MarshallingFailureException ex) {
158 throw new MessageConversionException("Could not marshal [" + object + "]", ex);
159 }
160 catch (IOException ex) {
161 throw new MessageConversionException("Could not marshal [" + object + "]", ex);
162 }
163 }
164
165
166
167
168
169
170
171 public Object fromMessage(Message message) throws JMSException, MessageConversionException {
172 try {
173 if (message instanceof TextMessage) {
174 TextMessage textMessage = (TextMessage) message;
175 return unmarshalFromTextMessage(textMessage, unmarshaller);
176 }
177 else if (message instanceof BytesMessage) {
178 BytesMessage bytesMessage = (BytesMessage) message;
179 return unmarshalFromBytesMessage(bytesMessage, unmarshaller);
180 }
181 else {
182 return unmarshalFromMessage(message, unmarshaller);
183 }
184 }
185 catch (UnmarshallingFailureException ex) {
186 throw new MessageConversionException("Could not unmarshal message [" + message + "]", ex);
187 }
188 catch (IOException ex) {
189 throw new MessageConversionException("Could not unmarshal message [" + message + "]", ex);
190 }
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205 protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller)
206 throws JMSException, IOException {
207 StringResult result = new StringResult();
208 marshaller.marshal(object, result);
209 return session.createTextMessage(result.toString());
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224 protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller)
225 throws JMSException, IOException {
226 ByteArrayOutputStream bos = new ByteArrayOutputStream();
227 StreamResult streamResult = new StreamResult(bos);
228 marshaller.marshal(object, streamResult);
229 BytesMessage message = session.createBytesMessage();
230 message.writeBytes(bos.toByteArray());
231 return message;
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 protected Message marshalToMessage(Object object, Session session, Marshaller marshaller)
248 throws JMSException, IOException {
249 throw new MessageConversionException(
250 "Unknown 'marshalTo' value [" + marshalTo + "]. Cannot convert object to Message");
251 }
252
253
254
255
256
257
258
259
260
261
262
263 protected Object unmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller)
264 throws JMSException, IOException {
265 StringSource source = new StringSource(message.getText());
266 return unmarshaller.unmarshal(source);
267 }
268
269
270
271
272
273
274
275
276
277
278
279 protected Object unmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller)
280 throws JMSException, IOException {
281 byte[] bytes = new byte[(int) message.getBodyLength()];
282 message.readBytes(bytes);
283 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
284 StreamSource source = new StreamSource(bis);
285 return unmarshaller.unmarshal(source);
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300 protected Object unmarshalFromMessage(Message message, Unmarshaller unmarshaller) throws JMSException, IOException {
301 throw new MessageConversionException(
302 "MarshallingMessageConverter only supports TextMessages and BytesMessages");
303 }
304 }
305