1
1
/*
2
- * Copyright 2002-2009 the original author or authors.
2
+ * Copyright 2002-2010 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.
34
34
/**
35
35
* Abstract base class for most {@link HttpMessageConverter} implementations.
36
36
*
37
- * <p>This base class adds support for setting supported {@code MediaTypes}, through the {@link
38
- * #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for {@code Content-Type} and
39
- * {@code Content-Length} when writing to output messages.
37
+ * <p>This base class adds support for setting supported {@code MediaTypes}, through the
38
+ * {@link #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds
39
+ * support for {@code Content-Type} and {@code Content-Length} when writing to output messages.
40
40
*
41
41
* @author Arjen Poutsma
42
+ * @author Juergen Hoeller
42
43
* @since 3.0
43
44
*/
44
45
public abstract class AbstractHttpMessageConverter <T > implements HttpMessageConverter <T > {
@@ -48,17 +49,16 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
48
49
49
50
private List <MediaType > supportedMediaTypes = Collections .emptyList ();
50
51
52
+
51
53
/**
52
54
* Construct an {@code AbstractHttpMessageConverter} with no supported media types.
53
- *
54
55
* @see #setSupportedMediaTypes
55
56
*/
56
57
protected AbstractHttpMessageConverter () {
57
58
}
58
59
59
60
/**
60
61
* Construct an {@code AbstractHttpMessageConverter} with one supported media type.
61
- *
62
62
* @param supportedMediaType the supported media type
63
63
*/
64
64
protected AbstractHttpMessageConverter (MediaType supportedMediaType ) {
@@ -67,14 +67,16 @@ protected AbstractHttpMessageConverter(MediaType supportedMediaType) {
67
67
68
68
/**
69
69
* Construct an {@code AbstractHttpMessageConverter} with multiple supported media type.
70
- *
71
70
* @param supportedMediaTypes the supported media types
72
71
*/
73
72
protected AbstractHttpMessageConverter (MediaType ... supportedMediaTypes ) {
74
73
setSupportedMediaTypes (Arrays .asList (supportedMediaTypes ));
75
74
}
76
75
77
- /** Set the list of {@link MediaType} objects supported by this converter. */
76
+
77
+ /**
78
+ * Set the list of {@link MediaType} objects supported by this converter.
79
+ */
78
80
public void setSupportedMediaTypes (List <MediaType > supportedMediaTypes ) {
79
81
Assert .notEmpty (supportedMediaTypes , "'supportedMediaTypes' must not be empty" );
80
82
this .supportedMediaTypes = new ArrayList <MediaType >(supportedMediaTypes );
@@ -84,21 +86,20 @@ public List<MediaType> getSupportedMediaTypes() {
84
86
return Collections .unmodifiableList (this .supportedMediaTypes );
85
87
}
86
88
89
+
87
90
/**
88
91
* {@inheritDoc}
89
- *
90
- * <p>This implementation checks if the given class is {@linkplain #supports(Class) supported}, and if the {@linkplain
91
- * #getSupportedMediaTypes() supported media types} {@linkplain MediaType#includes(MediaType) include} the given media
92
- * type.
92
+ * <p>This implementation checks if the given class is {@linkplain #supports(Class) supported},
93
+ * and if the {@linkplain #getSupportedMediaTypes() supported media types}
94
+ * {@linkplain MediaType#includes(MediaType) include} the given media type.
93
95
*/
94
96
public boolean canRead (Class <?> clazz , MediaType mediaType ) {
95
97
return supports (clazz ) && canRead (mediaType );
96
98
}
97
99
98
100
/**
99
- * Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types} include the given media
100
- * type.
101
- *
101
+ * Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types}
102
+ * include the given media type.
102
103
* @param mediaType the media type
103
104
* @return true if the supported media types include the media type, or if the media type is {@code null}
104
105
*/
@@ -116,10 +117,9 @@ protected boolean canRead(MediaType mediaType) {
116
117
117
118
/**
118
119
* {@inheritDoc}
119
- *
120
- * <p>This implementation checks if the given class is {@linkplain #supports(Class) supported}, and if the {@linkplain
121
- * #getSupportedMediaTypes() supported media types} {@linkplain MediaType#includes(MediaType) include} the given media
122
- * type.
120
+ * <p>This implementation checks if the given class is {@linkplain #supports(Class) supported},
121
+ * and if the {@linkplain #getSupportedMediaTypes() supported media types}
122
+ * {@linkplain MediaType#includes(MediaType) include} the given media type.
123
123
*/
124
124
public boolean canWrite (Class <?> clazz , MediaType mediaType ) {
125
125
return supports (clazz ) && canWrite (mediaType );
@@ -128,7 +128,6 @@ public boolean canWrite(Class<?> clazz, MediaType mediaType) {
128
128
/**
129
129
* Returns true if the given media type includes any of the
130
130
* {@linkplain #setSupportedMediaTypes(List) supported media types}.
131
- *
132
131
* @param mediaType the media type
133
132
* @return true if the supported media types include the media type, or if the media type is {@code null}
134
133
*/
@@ -144,45 +143,24 @@ protected boolean canWrite(MediaType mediaType) {
144
143
return false ;
145
144
}
146
145
147
- /**
148
- * Indicates whether the given class is supported by this converter.
149
- *
150
- * @param clazz the class to test for support
151
- * @return <code>true</code> if supported; <code>false</code> otherwise
152
- */
153
- protected abstract boolean supports (Class <?> clazz );
154
-
155
146
/**
156
147
* {@inheritDoc}
157
- *
158
- * <p>This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. Future implementations
159
- * might add some default behavior, however.
148
+ * <p>This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}.
149
+ * Future implementations might add some default behavior, however.
160
150
*/
161
- public final T read (Class <T > clazz , HttpInputMessage inputMessage ) throws IOException {
151
+ public final T read (Class <? extends T > clazz , HttpInputMessage inputMessage ) throws IOException {
162
152
return readInternal (clazz , inputMessage );
163
153
}
164
154
165
- /**
166
- * Abstract template method that reads the actualy object. Invoked from {@link #read(Class, HttpInputMessage)}.
167
- *
168
- * @param clazz the type of object to return
169
- * @param inputMessage the HTTP input message to read from
170
- * @return the converted object
171
- * @throws IOException in case of I/O errors
172
- * @throws HttpMessageNotReadableException in case of conversion errors
173
- */
174
- protected abstract T readInternal (Class <T > clazz , HttpInputMessage inputMessage )
175
- throws IOException , HttpMessageNotReadableException ;
176
-
177
155
/**
178
156
* {@inheritDoc}
179
- *
180
- * <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content type was not provided, calls
181
- * {@link #getContentLength}, and sets the corresponding headers on the output message. It then calls {@link
182
- * #writeInternal}.
157
+ * <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content
158
+ * type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
159
+ * on the output message. It then calls {@link #writeInternal}.
183
160
*/
184
161
public final void write (T t , MediaType contentType , HttpOutputMessage outputMessage )
185
162
throws IOException , HttpMessageNotWritableException {
163
+
186
164
HttpHeaders headers = outputMessage .getHeaders ();
187
165
if (contentType == null || contentType .isWildcardType () || contentType .isWildcardSubtype ()) {
188
166
contentType = getDefaultContentType (t );
@@ -199,12 +177,11 @@ public final void write(T t, MediaType contentType, HttpOutputMessage outputMess
199
177
}
200
178
201
179
/**
202
- * Returns the default content type for the given type. Called when {@link #write} is invoked without a specified
203
- * content type parameter.
204
- *
205
- * <p>By default, this returns the first element of the {@link #setSupportedMediaTypes(List) supportedMediaTypes}
206
- * property, if any. Can be overriden in subclasses.
207
- *
180
+ * Returns the default content type for the given type. Called when {@link #write}
181
+ * is invoked without a specified content type parameter.
182
+ * <p>By default, this returns the first element of the
183
+ * {@link #setSupportedMediaTypes(List) supportedMediaTypes} property, if any.
184
+ * Can be overridden in subclasses.
208
185
* @param t the type to return the content type for
209
186
* @return the content type, or <code>null</code> if not known
210
187
*/
@@ -215,20 +192,36 @@ protected MediaType getDefaultContentType(T t) {
215
192
216
193
/**
217
194
* Returns the content length for the given type.
218
- *
219
- * <p>By default, this returns {@code null}, meaning that the content length is unknown. Can be overriden in
220
- * subclasses.
221
- *
195
+ * <p>By default, this returns {@code null}, meaning that the content length is unknown.
196
+ * Can be overridden in subclasses.
222
197
* @param t the type to return the content length for
223
198
* @return the content length, or {@code null} if not known
224
199
*/
225
200
protected Long getContentLength (T t , MediaType contentType ) {
226
201
return null ;
227
202
}
228
203
204
+
205
+ /**
206
+ * Indicates whether the given class is supported by this converter.
207
+ * @param clazz the class to test for support
208
+ * @return <code>true</code> if supported; <code>false</code> otherwise
209
+ */
210
+ protected abstract boolean supports (Class <?> clazz );
211
+
212
+ /**
213
+ * Abstract template method that reads the actualy object. Invoked from {@link #read}.
214
+ * @param clazz the type of object to return
215
+ * @param inputMessage the HTTP input message to read from
216
+ * @return the converted object
217
+ * @throws IOException in case of I/O errors
218
+ * @throws HttpMessageNotReadableException in case of conversion errors
219
+ */
220
+ protected abstract T readInternal (Class <? extends T > clazz , HttpInputMessage inputMessage )
221
+ throws IOException , HttpMessageNotReadableException ;
222
+
229
223
/**
230
224
* Abstract template method that writes the actual body. Invoked from {@link #write}.
231
- *
232
225
* @param t the object to write to the output message
233
226
* @param outputMessage the message to write to
234
227
* @throws IOException in case of I/O errors
0 commit comments