From f92b7f101148e1789eea661e9d62e330d9471024 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 27 Mar 2009 17:57:41 +0000 Subject: [PATCH] Added readInternal template method --- .../AbstractHttpMessageConverter.java | 45 +++++++++++++------ .../ByteArrayHttpMessageConverter.java | 3 +- .../converter/FormHttpMessageConverter.java | 11 ++--- .../http/converter/HttpMessageConverter.java | 17 +++---- .../converter/StringHttpMessageConverter.java | 7 +-- .../xml/AbstractXmlHttpMessageConverter.java | 3 +- 6 files changed, 55 insertions(+), 31 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java index 116ecf60dc..6513cc949b 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java @@ -26,6 +26,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.util.Assert; @@ -34,8 +35,8 @@ import org.springframework.util.Assert; * Abstract base class for most {@link HttpMessageConverter} implementations. * *

This base class adds support for setting supported {@code MediaTypes}, through the {@link - * #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for - * {@code Content-Type} and {@code Content-Length} when writing to output messages. + * #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for {@code Content-Type} and + * {@code Content-Length} when writing to output messages. * * @author Arjen Poutsma * @since 3.0 @@ -76,11 +77,29 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv } /** - *

This implementation delegates to {@link #getContentType(Object)} and {@link #getContentLength(Object)}, - * and sets the corresponding headers on the output message. It then calls - * {@link #writeInternal(Object, HttpOutputMessage)}. + * {@inheritDoc}

This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. Future + * implementations might add some default behavior, however. + */ + public final T read(Class clazz, HttpInputMessage inputMessage) throws IOException { + return readInternal(clazz, inputMessage); + } + + /** + * Abstract template method that reads the actualy object. Invoked from {@link #read(Class, HttpInputMessage)}. * - * @throws HttpMessageConversionException in case of conversion errors + * @param clazz the type of object to return + * @param inputMessage the HTTP input message to read from + * @return the converted object + * @throws IOException in case of I/O errors + * @throws HttpMessageNotReadableException in case of conversion errors + */ + protected abstract T readInternal(Class clazz, HttpInputMessage inputMessage) + throws IOException, HttpMessageNotReadableException; + + /** + * {@inheritDoc}

This implementation delegates to {@link #getContentType(Object)} and {@link + * #getContentLength(Object)}, and sets the corresponding headers on the output message. It then calls {@link + * #writeInternal(Object, HttpOutputMessage)}. */ public final void write(T t, HttpOutputMessage outputMessage) throws IOException { HttpHeaders headers = outputMessage.getHeaders(); @@ -97,9 +116,8 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv } /** - * Returns the content type for the given type. - *

By default, this returns the first element of the {@link #setSupportedMediaTypes(List) supportedMediaTypes} - * property, if any. Can be overriden in subclasses. + * Returns the content type for the given type.

By default, this returns the first element of the {@link + * #setSupportedMediaTypes(List) supportedMediaTypes} property, if any. Can be overriden in subclasses. * * @param t the type to return the content type for * @return the content type, or null if not known @@ -110,8 +128,8 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv } /** - * Returns the content length for the given type. - *

By default, this returns null. Can be overriden in subclasses. + * Returns the content length for the given type.

By default, this returns null. Can be overriden in + * subclasses. * * @param t the type to return the content length for * @return the content length, or null if not known @@ -126,8 +144,9 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv * @param t the object to write to the output message * @param outputMessage the message to write to * @throws IOException in case of I/O errors - * @throws HttpMessageConversionException in case of conversion errors + * @throws HttpMessageNotWritableException in case of conversion errors */ - protected abstract void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException; + protected abstract void writeInternal(T t, HttpOutputMessage outputMessage) + throws IOException, HttpMessageNotWritableException; } diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java index 5e56f4cb08..614765d320 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java @@ -46,7 +46,8 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter< return byte[].class.equals(clazz); } - public byte[] read(Class clazz, HttpInputMessage inputMessage) throws IOException { + @Override + public byte[] readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { long contentLength = inputMessage.getHeaders().getContentLength(); if (contentLength >= 0) { ByteArrayOutputStream bos = new ByteArrayOutputStream((int) contentLength); diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index 6d935aef7a..65981ed644 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -37,9 +37,9 @@ import org.springframework.util.StringUtils; /** * Implementation of {@link HttpMessageConverter} that can read and write form data. * - *

By default, this converter reads and writes the media type ({@code application/x-www-form-urlencoded}). This - * can be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. Form - * data is read from and written into a {@link MultiValueMap MultiValueMap<String, String>}. + *

By default, this converter reads and writes the media type ({@code application/x-www-form-urlencoded}). This can + * be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. Form data + * is read from and written into a {@link MultiValueMap MultiValueMap<String, String>}. * * @author Arjen Poutsma * @see MultiValueMap @@ -58,8 +58,9 @@ public class FormHttpMessageConverter extends AbstractHttpMessageConverter read(Class> clazz, HttpInputMessage inputMessage) - throws IOException { + @Override + public MultiValueMap readInternal(Class> clazz, + HttpInputMessage inputMessage) throws IOException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET; String body = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset)); diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java index ff7242abfa..6c5500e1e8 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java @@ -33,33 +33,34 @@ public interface HttpMessageConverter { /** * Indicate whether the given class is supported by this converter. + * * @param clazz the class to test for support * @return true if supported; false otherwise */ boolean supports(Class clazz); - /** - * Return the list of {@link MediaType} objects supported by this converter. - */ + /** Return the list of {@link MediaType} objects supported by this converter. */ List getSupportedMediaTypes(); /** * Read an object of the given type form the given input message, and returns it. + * * @param clazz the type of object to return * @param inputMessage the HTTP input message to read from * @return the converted object * @throws IOException in case of I/O errors - * @throws HttpMessageConversionException in case of conversion errors + * @throws HttpMessageNotReadableException in case of conversion errors */ - T read(Class clazz, HttpInputMessage inputMessage) throws IOException; + T read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException; /** * Write an given object to the given output message. - * @param t the object to write to the output message + * + * @param t the object to write to the output message * @param outputMessage the message to write to * @throws IOException in case of I/O errors - * @throws HttpMessageConversionException in case of conversion errors + * @throws HttpMessageNotWritableException in case of conversion errors */ - void write(T t, HttpOutputMessage outputMessage) throws IOException; + void write(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException; } diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java index 2e39061c5a..7d5808ddee 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java @@ -54,7 +54,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter clazz, HttpInputMessage inputMessage) throws IOException { + @Override + public String readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET; return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset)); @@ -86,8 +87,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverterBy default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses. + * Return the list of supported {@link Charset}.

By default, returns {@link Charset#availableCharsets()}. Can be + * overridden in subclasses. * * @return the list of accepted charsets */ diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java index 373aafed8b..9220e277f4 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java @@ -55,7 +55,8 @@ public abstract class AbstractXmlHttpMessageConverter extends AbstractHttpMes } /** Invokes {@link #readFromSource(Class, HttpHeaders, Source)}. */ - public final T read(Class clazz, HttpInputMessage inputMessage) throws IOException { + @Override + public final T readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody())); } -- GitLab