/
MarshallingMessageConverter.java
193 lines (168 loc) · 5.9 KB
/
MarshallingMessageConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.converter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.beans.TypeMismatchException;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
/**
* Implementation of {@link MessageConverter} that can read and write XML using Spring's
* {@link Marshaller} and {@link Unmarshaller} abstractions.
*
* <p>This converter requires a {@code Marshaller} and {@code Unmarshaller} before it can
* be used. These can be injected by the {@linkplain #MarshallingMessageConverter(Marshaller)
* constructor} or {@linkplain #setMarshaller(Marshaller) bean properties}.
*
* @author Arjen Poutsma
* @since 4.2
*/
public class MarshallingMessageConverter extends AbstractMessageConverter {
@Nullable
private Marshaller marshaller;
@Nullable
private Unmarshaller unmarshaller;
/**
* Default construct allowing for {@link #setMarshaller(Marshaller)} and/or
* {@link #setUnmarshaller(Unmarshaller)} to be invoked separately.
*/
public MarshallingMessageConverter() {
this(new MimeType("application", "xml"), new MimeType("text", "xml"), new MimeType("application", "*+xml"));
}
/**
* Constructor with a given list of MIME types to support.
* @param supportedMimeTypes the MIME types
*/
public MarshallingMessageConverter(MimeType... supportedMimeTypes) {
super(supportedMimeTypes);
}
/**
* Constructor with {@link Marshaller}. If the given {@link Marshaller} also
* implements {@link Unmarshaller}, it is also used for unmarshalling.
* <p>Note that all {@code Marshaller} implementations in Spring also implement
* {@code Unmarshaller} so that you can safely use this constructor.
* @param marshaller object used as marshaller and unmarshaller
*/
public MarshallingMessageConverter(Marshaller marshaller) {
this();
Assert.notNull(marshaller, "Marshaller must not be null");
this.marshaller = marshaller;
if (marshaller instanceof Unmarshaller) {
this.unmarshaller = (Unmarshaller) marshaller;
}
}
/**
* Set the {@link Marshaller} to be used by this message converter.
*/
public void setMarshaller(@Nullable Marshaller marshaller) {
this.marshaller = marshaller;
}
/**
* Return the configured Marshaller.
*/
@Nullable
public Marshaller getMarshaller() {
return this.marshaller;
}
/**
* Set the {@link Unmarshaller} to be used by this message converter.
*/
public void setUnmarshaller(@Nullable Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
/**
* Return the configured unmarshaller.
*/
@Nullable
public Unmarshaller getUnmarshaller() {
return this.unmarshaller;
}
@Override
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null &&
this.unmarshaller.supports(targetClass));
}
@Override
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
return (supportsMimeType(headers) && this.marshaller != null &&
this.marshaller.supports(payload.getClass()));
}
@Override
protected boolean supports(Class<?> clazz) {
// should not be called, since we override canConvertFrom/canConvertTo instead
throw new UnsupportedOperationException();
}
@Override
@Nullable
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
try {
Source source = getSource(message.getPayload());
Object result = this.unmarshaller.unmarshal(source);
if (!targetClass.isInstance(result)) {
throw new TypeMismatchException(result, targetClass);
}
return result;
}
catch (Exception ex) {
throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
}
}
private Source getSource(Object payload) {
if (payload instanceof byte[]) {
return new StreamSource(new ByteArrayInputStream((byte[]) payload));
}
else {
return new StreamSource(new StringReader((String) payload));
}
}
@Override
@Nullable
protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers,
@Nullable Object conversionHint) {
Assert.notNull(this.marshaller, "Property 'marshaller' is required");
try {
if (byte[].class == getSerializedPayloadClass()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Result result = new StreamResult(out);
this.marshaller.marshal(payload, result);
payload = out.toByteArray();
}
else {
Writer writer = new StringWriter();
Result result = new StreamResult(writer);
this.marshaller.marshal(payload, result);
payload = writer.toString();
}
}
catch (Throwable ex) {
throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
}
return payload;
}
}