1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
32
32
import org .springframework .util .MimeType ;
33
33
34
34
/**
35
- * Abstract base class for {@link MessageConverter} implementations including support for
36
- * common properties and a partial implementation of the conversion methods mainly to
37
- * check if the converter supports the conversion based on the payload class and MIME
38
- * type.
35
+ * Abstract base class for {@link MessageConverter} implementations including support
36
+ * for common properties and a partial implementation of the conversion methods,
37
+ * mainly to check if the converter supports the conversion based on the payload class
38
+ * and MIME type.
39
39
*
40
40
* @author Rossen Stoyanchev
41
41
* @since 4.0
@@ -68,7 +68,7 @@ protected AbstractMessageConverter(MimeType supportedMimeType) {
68
68
* @param supportedMimeTypes the supported MIME types
69
69
*/
70
70
protected AbstractMessageConverter (Collection <MimeType > supportedMimeTypes ) {
71
- Assert .notNull (supportedMimeTypes , "SupportedMimeTypes must not be null" );
71
+ Assert .notNull (supportedMimeTypes , "supportedMimeTypes must not be null" );
72
72
this .supportedMimeTypes = new ArrayList <MimeType >(supportedMimeTypes );
73
73
}
74
74
@@ -83,13 +83,11 @@ public List<MimeType> getSupportedMimeTypes() {
83
83
/**
84
84
* Configure the {@link ContentTypeResolver} to use to resolve the content
85
85
* type of an input message.
86
- * <p>
87
- * Note that if no resolver is configured, then
86
+ * <p>Note that if no resolver is configured, then
88
87
* {@link #setStrictContentTypeMatch(boolean) strictContentTypeMatch} should
89
88
* be left as {@code false} (the default) or otherwise this converter will
90
89
* ignore all messages.
91
- * <p>
92
- * By default, a {@code DefaultContentTypeResolver} instance is used.
90
+ * <p>By default, a {@code DefaultContentTypeResolver} instance is used.
93
91
*/
94
92
public void setContentTypeResolver (ContentTypeResolver resolver ) {
95
93
this .contentTypeResolver = resolver ;
@@ -106,20 +104,17 @@ public ContentTypeResolver getContentTypeResolver() {
106
104
* Whether this converter should convert messages for which no content type
107
105
* could be resolved through the configured
108
106
* {@link org.springframework.messaging.converter.ContentTypeResolver}.
109
- * A converter can configured to be strict only when a
110
- * {@link #setContentTypeResolver(ContentTypeResolver) contentTypeResolver}
111
- * is configured and the list of {@link #getSupportedMimeTypes() supportedMimeTypes}
112
- * is not be empty.
113
- *
114
- * then requires the content type of a message to be resolved
115
- *
116
- * When set to true, #supportsMimeType(MessageHeaders) will return false if the
117
- * contentTypeResolver is not defined or if no content-type header is present.
107
+ * <p>A converter can configured to be strict only when a
108
+ * {@link #setContentTypeResolver contentTypeResolver} is configured and the
109
+ * list of {@link #getSupportedMimeTypes() supportedMimeTypes} is not be empty.
110
+ * <p>When this flag is set to {@code true}, {@link #supportsMimeType(MessageHeaders)}
111
+ * will return {@code false} if the {@link #setContentTypeResolver contentTypeResolver}
112
+ * is not defined or if no content-type header is present.
118
113
*/
119
114
public void setStrictContentTypeMatch (boolean strictContentTypeMatch ) {
120
115
if (strictContentTypeMatch ) {
121
- Assert .notEmpty (getSupportedMimeTypes (), "Strict match requires non-empty list of supported mime types. " );
122
- Assert .notNull (getContentTypeResolver (), "Strict match requires ContentTypeResolver. " );
116
+ Assert .notEmpty (getSupportedMimeTypes (), "Strict match requires non-empty list of supported mime types" );
117
+ Assert .notNull (getContentTypeResolver (), "Strict match requires ContentTypeResolver" );
123
118
}
124
119
this .strictContentTypeMatch = strictContentTypeMatch ;
125
120
}
@@ -166,14 +161,6 @@ protected MimeType getDefaultContentType(Object payload) {
166
161
return (!mimeTypes .isEmpty () ? mimeTypes .get (0 ) : null );
167
162
}
168
163
169
- /**
170
- * Whether the given class is supported by this converter.
171
- * @param clazz the class to test for support
172
- * @return {@code true} if supported; {@code false} otherwise
173
- */
174
- protected abstract boolean supports (Class <?> clazz );
175
-
176
-
177
164
@ Override
178
165
public final Object fromMessage (Message <?> message , Class <?> targetClass ) {
179
166
if (!canConvertFrom (message , targetClass )) {
@@ -186,14 +173,8 @@ protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
186
173
return (supports (targetClass ) && supportsMimeType (message .getHeaders ()));
187
174
}
188
175
189
- /**
190
- * Convert the message payload from serialized form to an Object.
191
- */
192
- public abstract Object convertFromInternal (Message <?> message , Class <?> targetClass );
193
-
194
176
@ Override
195
177
public final Message <?> toMessage (Object payload , MessageHeaders headers ) {
196
-
197
178
if (!canConvertTo (payload , headers )) {
198
179
return null ;
199
180
}
@@ -218,15 +199,10 @@ public final Message<?> toMessage(Object payload, MessageHeaders headers) {
218
199
}
219
200
220
201
protected boolean canConvertTo (Object payload , MessageHeaders headers ) {
221
- Class <?> clazz = (payload != null ) ? payload .getClass () : null ;
202
+ Class <?> clazz = (payload != null ? payload .getClass () : null ) ;
222
203
return (supports (clazz ) && supportsMimeType (headers ));
223
204
}
224
205
225
- /**
226
- * Convert the payload object to serialized form.
227
- */
228
- public abstract Object convertToInternal (Object payload , MessageHeaders headers );
229
-
230
206
protected boolean supportsMimeType (MessageHeaders headers ) {
231
207
if (getSupportedMimeTypes ().isEmpty ()) {
232
208
return true ;
@@ -249,7 +225,26 @@ protected boolean supportsMimeType(MessageHeaders headers) {
249
225
}
250
226
251
227
protected MimeType getMimeType (MessageHeaders headers ) {
252
- return (this .contentTypeResolver != null ) ? this .contentTypeResolver .resolve (headers ) : null ;
228
+ return (this .contentTypeResolver != null ? this .contentTypeResolver .resolve (headers ) : null ) ;
253
229
}
254
230
231
+
232
+ /**
233
+ * Whether the given class is supported by this converter.
234
+ * @param clazz the class to test for support
235
+ * @return {@code true} if supported; {@code false} otherwise
236
+ */
237
+ protected abstract boolean supports (Class <?> clazz );
238
+
239
+ /**
240
+ * Convert the message payload from serialized form to an Object.
241
+ */
242
+ public abstract Object convertFromInternal (Message <?> message , Class <?> targetClass );
243
+
244
+
245
+ /**
246
+ * Convert the payload object to serialized form.
247
+ */
248
+ public abstract Object convertToInternal (Object payload , MessageHeaders headers );
249
+
255
250
}
0 commit comments