Skip to content

Commit f92b7f1

Browse files
committed
Added readInternal template method
1 parent bcf6f23 commit f92b7f1

File tree

6 files changed

+55
-31
lines changed

6 files changed

+55
-31
lines changed

org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java

+32-13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.commons.logging.LogFactory;
2727

2828
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpInputMessage;
2930
import org.springframework.http.HttpOutputMessage;
3031
import org.springframework.http.MediaType;
3132
import org.springframework.util.Assert;
@@ -34,8 +35,8 @@
3435
* Abstract base class for most {@link HttpMessageConverter} implementations.
3536
*
3637
* <p>This base class adds support for setting supported {@code MediaTypes}, through the {@link
37-
* #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for
38-
* {@code Content-Type} and {@code Content-Length} when writing to output messages.
38+
* #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for {@code Content-Type} and
39+
* {@code Content-Length} when writing to output messages.
3940
*
4041
* @author Arjen Poutsma
4142
* @since 3.0
@@ -76,11 +77,29 @@ public List<MediaType> getSupportedMediaTypes() {
7677
}
7778

7879
/**
79-
* <p>This implementation delegates to {@link #getContentType(Object)} and {@link #getContentLength(Object)},
80-
* and sets the corresponding headers on the output message. It then calls
81-
* {@link #writeInternal(Object, HttpOutputMessage)}.
80+
* {@inheritDoc} <p>This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. Future
81+
* implementations might add some default behavior, however.
82+
*/
83+
public final T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException {
84+
return readInternal(clazz, inputMessage);
85+
}
86+
87+
/**
88+
* Abstract template method that reads the actualy object. Invoked from {@link #read(Class, HttpInputMessage)}.
8289
*
83-
* @throws HttpMessageConversionException in case of conversion errors
90+
* @param clazz the type of object to return
91+
* @param inputMessage the HTTP input message to read from
92+
* @return the converted object
93+
* @throws IOException in case of I/O errors
94+
* @throws HttpMessageNotReadableException in case of conversion errors
95+
*/
96+
protected abstract T readInternal(Class<T> clazz, HttpInputMessage inputMessage)
97+
throws IOException, HttpMessageNotReadableException;
98+
99+
/**
100+
* {@inheritDoc} <p>This implementation delegates to {@link #getContentType(Object)} and {@link
101+
* #getContentLength(Object)}, and sets the corresponding headers on the output message. It then calls {@link
102+
* #writeInternal(Object, HttpOutputMessage)}.
84103
*/
85104
public final void write(T t, HttpOutputMessage outputMessage) throws IOException {
86105
HttpHeaders headers = outputMessage.getHeaders();
@@ -97,9 +116,8 @@ public final void write(T t, HttpOutputMessage outputMessage) throws IOException
97116
}
98117

99118
/**
100-
* Returns the content type for the given type.
101-
* <p>By default, this returns the first element of the {@link #setSupportedMediaTypes(List) supportedMediaTypes}
102-
* property, if any. Can be overriden in subclasses.
119+
* Returns the content type for the given type. <p>By default, this returns the first element of the {@link
120+
* #setSupportedMediaTypes(List) supportedMediaTypes} property, if any. Can be overriden in subclasses.
103121
*
104122
* @param t the type to return the content type for
105123
* @return the content type, or <code>null</code> if not known
@@ -110,8 +128,8 @@ protected MediaType getContentType(T t) {
110128
}
111129

112130
/**
113-
* Returns the content length for the given type.
114-
* <p>By default, this returns <code>null</code>. Can be overriden in subclasses.
131+
* Returns the content length for the given type. <p>By default, this returns <code>null</code>. Can be overriden in
132+
* subclasses.
115133
*
116134
* @param t the type to return the content length for
117135
* @return the content length, or <code>null</code> if not known
@@ -126,8 +144,9 @@ protected Long getContentLength(T t) {
126144
* @param t the object to write to the output message
127145
* @param outputMessage the message to write to
128146
* @throws IOException in case of I/O errors
129-
* @throws HttpMessageConversionException in case of conversion errors
147+
* @throws HttpMessageNotWritableException in case of conversion errors
130148
*/
131-
protected abstract void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException;
149+
protected abstract void writeInternal(T t, HttpOutputMessage outputMessage)
150+
throws IOException, HttpMessageNotWritableException;
132151

133152
}

org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public boolean supports(Class<? extends byte[]> clazz) {
4646
return byte[].class.equals(clazz);
4747
}
4848

49-
public byte[] read(Class<byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
49+
@Override
50+
public byte[] readInternal(Class<byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
5051
long contentLength = inputMessage.getHeaders().getContentLength();
5152
if (contentLength >= 0) {
5253
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) contentLength);

org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
/**
3838
* Implementation of {@link HttpMessageConverter} that can read and write form data.
3939
*
40-
* <p>By default, this converter reads and writes the media type ({@code application/x-www-form-urlencoded}). This
41-
* can be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. Form
42-
* data is read from and written into a {@link MultiValueMap MultiValueMap&lt;String, String&gt;}.
40+
* <p>By default, this converter reads and writes the media type ({@code application/x-www-form-urlencoded}). This can
41+
* be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. Form data
42+
* is read from and written into a {@link MultiValueMap MultiValueMap&lt;String, String&gt;}.
4343
*
4444
* @author Arjen Poutsma
4545
* @see MultiValueMap
@@ -58,8 +58,9 @@ public boolean supports(Class<? extends MultiValueMap<String, String>> clazz) {
5858
return MultiValueMap.class.isAssignableFrom(clazz);
5959
}
6060

61-
public MultiValueMap<String, String> read(Class<MultiValueMap<String, String>> clazz, HttpInputMessage inputMessage)
62-
throws IOException {
61+
@Override
62+
public MultiValueMap<String, String> readInternal(Class<MultiValueMap<String, String>> clazz,
63+
HttpInputMessage inputMessage) throws IOException {
6364
MediaType contentType = inputMessage.getHeaders().getContentType();
6465
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
6566
String body = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));

org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,34 @@ public interface HttpMessageConverter<T> {
3333

3434
/**
3535
* Indicate whether the given class is supported by this converter.
36+
*
3637
* @param clazz the class to test for support
3738
* @return <code>true</code> if supported; <code>false</code> otherwise
3839
*/
3940
boolean supports(Class<? extends T> clazz);
4041

41-
/**
42-
* Return the list of {@link MediaType} objects supported by this converter.
43-
*/
42+
/** Return the list of {@link MediaType} objects supported by this converter. */
4443
List<MediaType> getSupportedMediaTypes();
4544

4645
/**
4746
* Read an object of the given type form the given input message, and returns it.
47+
*
4848
* @param clazz the type of object to return
4949
* @param inputMessage the HTTP input message to read from
5050
* @return the converted object
5151
* @throws IOException in case of I/O errors
52-
* @throws HttpMessageConversionException in case of conversion errors
52+
* @throws HttpMessageNotReadableException in case of conversion errors
5353
*/
54-
T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException;
54+
T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
5555

5656
/**
5757
* Write an given object to the given output message.
58-
* @param t the object to write to the output message
58+
*
59+
* @param t the object to write to the output message
5960
* @param outputMessage the message to write to
6061
* @throws IOException in case of I/O errors
61-
* @throws HttpMessageConversionException in case of conversion errors
62+
* @throws HttpMessageNotWritableException in case of conversion errors
6263
*/
63-
void write(T t, HttpOutputMessage outputMessage) throws IOException;
64+
void write(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
6465

6566
}

org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public boolean supports(Class<? extends String> clazz) {
5454
return String.class.equals(clazz);
5555
}
5656

57-
public String read(Class<String> clazz, HttpInputMessage inputMessage) throws IOException {
57+
@Override
58+
public String readInternal(Class<String> clazz, HttpInputMessage inputMessage) throws IOException {
5859
MediaType contentType = inputMessage.getHeaders().getContentType();
5960
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
6061
return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
@@ -86,8 +87,8 @@ protected void writeInternal(String s, HttpOutputMessage outputMessage) throws I
8687
}
8788

8889
/**
89-
* Return the list of supported {@link Charset}.
90-
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
90+
* Return the list of supported {@link Charset}. <p>By default, returns {@link Charset#availableCharsets()}. Can be
91+
* overridden in subclasses.
9192
*
9293
* @return the list of accepted charsets
9394
*/

org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected AbstractXmlHttpMessageConverter() {
5555
}
5656

5757
/** Invokes {@link #readFromSource(Class, HttpHeaders, Source)}. */
58-
public final T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException {
58+
@Override
59+
public final T readInternal(Class<T> clazz, HttpInputMessage inputMessage) throws IOException {
5960
return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody()));
6061
}
6162

0 commit comments

Comments
 (0)