avoid swallowing of IOException

This commit is contained in:
Juergen Hoeller 2011-12-06 22:51:57 +00:00
parent f74789ffbe
commit 426863f790
2 changed files with 27 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -88,8 +88,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
/**
* {@inheritDoc}
* <p>This implementation checks if the given class is {@linkplain #supports(Class) supported},
* This implementation checks if the given class is {@linkplain #supports(Class) supported},
* and if the {@linkplain #getSupportedMediaTypes() supported media types}
* {@linkplain MediaType#includes(MediaType) include} the given media type.
*/
@ -100,9 +99,10 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
/**
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types}
* include the given media type.
* @param mediaType the media type to read, can be {@code null} if not specified. Typically the value of a
* {@code Content-Type} header.
* @return true if the supported media types include the media type, or if the media type is {@code null}
* @param mediaType the media type to read, can be {@code null} if not specified.
* Typically the value of a {@code Content-Type} header.
* @return {@code true} if the supported media types include the media type,
* or if the media type is {@code null}
*/
protected boolean canRead(MediaType mediaType) {
if (mediaType == null) {
@ -117,8 +117,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* {@inheritDoc}
* <p>This implementation checks if the given class is {@linkplain #supports(Class) supported},
* This implementation checks if the given class is {@linkplain #supports(Class) supported},
* and if the {@linkplain #getSupportedMediaTypes() supported media types}
* {@linkplain MediaType#includes(MediaType) include} the given media type.
*/
@ -127,11 +126,12 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* Returns true if the given media type includes any of the
* Returns {@code true} if the given media type includes any of the
* {@linkplain #setSupportedMediaTypes(List) supported media types}.
* @param mediaType the media type to write, can be {@code null} if not specified. Typically the value of an
* {@code Accept} header.
* @return true if the supported media types are compatible with the media type, or if the media type is {@code null}
* @param mediaType the media type to write, can be {@code null} if not specified.
* Typically the value of an {@code Accept} header.
* @return {@code true} if the supported media types are compatible with the media type,
* or if the media type is {@code null}
*/
protected boolean canWrite(MediaType mediaType) {
if (mediaType == null || MediaType.ALL.equals(mediaType)) {
@ -146,8 +146,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* {@inheritDoc}
* <p>This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}.
* This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}.
* Future implementations might add some default behavior, however.
*/
public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException {
@ -155,8 +154,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* {@inheritDoc}
* <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a content
* This implementation delegates to {@link #getDefaultContentType(Object)} if a content
* type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
* on the output message. It then calls {@link #writeInternal}.
*/
@ -191,7 +189,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* @param t the type to return the content type for
* @return the content type, or <code>null</code> if not known
*/
protected MediaType getDefaultContentType(T t) {
protected MediaType getDefaultContentType(T t) throws IOException {
List<MediaType> mediaTypes = getSupportedMediaTypes();
return (!mediaTypes.isEmpty() ? mediaTypes.get(0) : null);
}
@ -203,7 +201,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* @param t the type to return the content length for
* @return the content length, or {@code null} if not known
*/
protected Long getContentLength(T t, MediaType contentType) {
protected Long getContentLength(T t, MediaType contentType) throws IOException {
return null;
}

View File

@ -34,9 +34,9 @@ import org.springframework.util.StringUtils;
/**
* Implementation of {@link HttpMessageConverter} that can read and write {@link Resource Resources}.
*
* <p>By default, this converter can read all media types. The Java Activation Framework (JAF) - if available - is used
* to determine the {@code Content-Type} of written resources. If JAF is not available, {@code application/octet-stream}
* is used.
* <p>By default, this converter can read all media types. The Java Activation Framework (JAF) -
* if available - is used to determine the {@code Content-Type} of written resources.
* If JAF is not available, {@code application/octet-stream} is used.
*
* @author Arjen Poutsma
* @since 3.0.2
@ -46,9 +46,12 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
private static final boolean jafPresent =
ClassUtils.isPresent("javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
public ResourceHttpMessageConverter() {
super(MediaType.ALL);
}
@Override
protected boolean supports(Class<?> clazz) {
return Resource.class.isAssignableFrom(clazz);
@ -57,6 +60,7 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
@Override
protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
byte[] body = FileCopyUtils.copyToByteArray(inputMessage.getBody());
return new ByteArrayResource(body);
}
@ -72,22 +76,19 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
}
@Override
protected Long getContentLength(Resource resource, MediaType contentType) {
try {
return resource.contentLength();
}
catch (IOException e) {
return null;
}
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
return resource.contentLength();
}
@Override
protected void writeInternal(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
FileCopyUtils.copy(resource.getInputStream(), outputMessage.getBody());
outputMessage.getBody().flush();
}
/**
* Inner class to avoid hard-coded JAF dependency.
*/