提交 f92b7f10 编写于 作者: A Arjen Poutsma

Added readInternal template method

上级 bcf6f232
......@@ -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.
*
* <p>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<T> implements HttpMessageConv
}
/**
* <p>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} <p>This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. Future
* implementations might add some default behavior, however.
*/
public final T read(Class<T> 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<T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException;
/**
* {@inheritDoc} <p>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<T> implements HttpMessageConv
}
/**
* Returns the content type for the given type.
* <p>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. <p>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 <code>null</code> if not known
......@@ -110,8 +128,8 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* Returns the content length for the given type.
* <p>By default, this returns <code>null</code>. Can be overriden in subclasses.
* Returns the content length for the given type. <p>By default, this returns <code>null</code>. Can be overriden in
* subclasses.
*
* @param t the type to return the content length for
* @return the content length, or <code>null</code> if not known
......@@ -126,8 +144,9 @@ public abstract class AbstractHttpMessageConverter<T> 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;
}
......@@ -46,7 +46,8 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
return byte[].class.equals(clazz);
}
public byte[] read(Class<byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
@Override
public byte[] readInternal(Class<byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
long contentLength = inputMessage.getHeaders().getContentLength();
if (contentLength >= 0) {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) contentLength);
......
......@@ -37,9 +37,9 @@ import org.springframework.util.StringUtils;
/**
* Implementation of {@link HttpMessageConverter} that can read and write form data.
*
* <p>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&lt;String, String&gt;}.
* <p>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&lt;String, String&gt;}.
*
* @author Arjen Poutsma
* @see MultiValueMap
......@@ -58,8 +58,9 @@ public class FormHttpMessageConverter extends AbstractHttpMessageConverter<Multi
return MultiValueMap.class.isAssignableFrom(clazz);
}
public MultiValueMap<String, String> read(Class<MultiValueMap<String, String>> clazz, HttpInputMessage inputMessage)
throws IOException {
@Override
public MultiValueMap<String, String> readInternal(Class<MultiValueMap<String, String>> 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));
......
......@@ -33,33 +33,34 @@ public interface HttpMessageConverter<T> {
/**
* Indicate whether the given class is supported by this converter.
*
* @param clazz the class to test for support
* @return <code>true</code> if supported; <code>false</code> otherwise
*/
boolean supports(Class<? extends T> clazz);
/**
* Return the list of {@link MediaType} objects supported by this converter.
*/
/** Return the list of {@link MediaType} objects supported by this converter. */
List<MediaType> 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<T> clazz, HttpInputMessage inputMessage) throws IOException;
T read(Class<T> 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;
}
......@@ -54,7 +54,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
return String.class.equals(clazz);
}
public String read(Class<String> clazz, HttpInputMessage inputMessage) throws IOException {
@Override
public String readInternal(Class<String> 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 AbstractHttpMessageConverter<Str
}
/**
* Return the list of supported {@link Charset}.
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
* Return the list of supported {@link Charset}. <p>By default, returns {@link Charset#availableCharsets()}. Can be
* overridden in subclasses.
*
* @return the list of accepted charsets
*/
......
......@@ -55,7 +55,8 @@ public abstract class AbstractXmlHttpMessageConverter<T> extends AbstractHttpMes
}
/** Invokes {@link #readFromSource(Class, HttpHeaders, Source)}. */
public final T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException {
@Override
public final T readInternal(Class<T> clazz, HttpInputMessage inputMessage) throws IOException {
return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody()));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册