polishing
This commit is contained in:
@@ -35,8 +35,8 @@ public interface HttpMessageConverter<T> {
|
||||
/**
|
||||
* Indicates whether the given class can be read by this converter.
|
||||
* @param clazz the class to test for readability
|
||||
* @param mediaType the media type to read, can be {@code null} if not specified. Typically the value of a
|
||||
* {@code Content-Type} header.
|
||||
* @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 readable; {@code false} otherwise
|
||||
*/
|
||||
boolean canRead(Class<?> clazz, MediaType mediaType);
|
||||
@@ -44,8 +44,8 @@ public interface HttpMessageConverter<T> {
|
||||
/**
|
||||
* Indicates whether the given class can be written by this converter.
|
||||
* @param clazz the class to test for writability
|
||||
* @param mediaType the media type to write, can be {@code null} if not specified. Typically the value of an
|
||||
* {@code Accept} header.
|
||||
* @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 writable; {@code false} otherwise
|
||||
*/
|
||||
boolean canWrite(Class<?> clazz, MediaType mediaType);
|
||||
|
||||
@@ -63,12 +63,14 @@ public class ResourceHttpMessageConverter implements HttpMessageConverter<Resour
|
||||
|
||||
public Resource read(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
|
||||
throws IOException, HttpMessageNotReadableException {
|
||||
|
||||
byte[] body = FileCopyUtils.copyToByteArray(inputMessage.getBody());
|
||||
return new ByteArrayResource(body);
|
||||
}
|
||||
|
||||
public void write(Resource resource, MediaType contentType, HttpOutputMessage outputMessage)
|
||||
throws IOException, HttpMessageNotWritableException {
|
||||
|
||||
HttpHeaders headers = outputMessage.getHeaders();
|
||||
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
|
||||
contentType = getContentType(resource);
|
||||
@@ -87,20 +89,17 @@ public class ResourceHttpMessageConverter implements HttpMessageConverter<Resour
|
||||
private MediaType getContentType(Resource resource) {
|
||||
if (jafPresent) {
|
||||
return ActivationMediaTypeFactory.getMediaType(resource);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
}
|
||||
|
||||
protected Long getContentLength(Resource resource, MediaType contentType) {
|
||||
try {
|
||||
return resource.getFile().length();
|
||||
}
|
||||
catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
|
||||
return resource.contentLength();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner class to avoid hard-coded JAF dependency.
|
||||
*/
|
||||
@@ -140,7 +139,7 @@ public class ResourceHttpMessageConverter implements HttpMessageConverter<Resour
|
||||
|
||||
public static MediaType getMediaType(Resource resource) {
|
||||
String mediaType = fileTypeMap.getContentType(resource.getFilename());
|
||||
return StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null;
|
||||
return (StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -33,12 +33,12 @@ import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
/**
|
||||
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content on the response. This
|
||||
* ETag is compared to the <code>If-None-Match</code> header of the request. If these headers are equal, the resonse
|
||||
* content is not sent, but rather a 304 "Not Modified" status.
|
||||
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content on the response.
|
||||
* This ETag is compared to the <code>If-None-Match</code> header of the request. If these headers are equal,
|
||||
* the response content is not sent, but rather a <code>304 "Not Modified"</code> status instead.
|
||||
*
|
||||
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View}) is
|
||||
* still rendered. As such, this filter only saves bandwidth, not server performance.
|
||||
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View})
|
||||
* is still rendered. As such, this filter only saves bandwidth, not server performance.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
@@ -49,6 +49,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||
|
||||
private static String HEADER_IF_NONE_MATCH = "If-None-Match";
|
||||
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
@@ -96,27 +97,22 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||
|
||||
/**
|
||||
* Indicates whether the given request and response are eligible for ETag generation.
|
||||
*
|
||||
* <p>Default implementation returns {@code true} for response status codes in the {@code 2xx} series.
|
||||
*
|
||||
* <p>The default implementation returns {@code true} for response status codes in the {@code 2xx} series.
|
||||
* @param request the HTTP request
|
||||
* @param response the HTTP response
|
||||
* @param responseStatusCode the HTTP response status code
|
||||
* @param responseBody the response body
|
||||
* @return {@code true} if eligible for ETag generation; {@code false} otherwise
|
||||
*/
|
||||
protected boolean isEligibleForEtag(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
int responseStatusCode,
|
||||
byte[] responseBody) {
|
||||
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
|
||||
int responseStatusCode, byte[] responseBody) {
|
||||
|
||||
return (responseStatusCode >= 200 && responseStatusCode < 300);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the ETag header value from the given response body byte array.
|
||||
*
|
||||
* <p>The default implementation generates an MD5 hash.
|
||||
*
|
||||
* @param bytes the response bdoy as byte array
|
||||
* @return the ETag header value
|
||||
* @see org.springframework.util.DigestUtils
|
||||
@@ -128,10 +124,11 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@link HttpServletRequest} wrapper that buffers all content written to the {@linkplain #getOutputStream() output
|
||||
* stream} and {@linkplain #getWriter() writer}, and allows this content to be retrieved via a {@link #toByteArray()
|
||||
* byte array}.
|
||||
* {@link HttpServletRequest} wrapper that buffers all content written to the
|
||||
* {@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer},
|
||||
* and allows this content to be retrieved via a {@link #toByteArray() byte array}.
|
||||
*/
|
||||
private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper {
|
||||
|
||||
@@ -216,7 +213,6 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
content.write(b, off, len);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class ResponsePrintWriter extends PrintWriter {
|
||||
@@ -242,9 +238,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||
super.write(c);
|
||||
super.flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ import org.springframework.util.Assert;
|
||||
* </ul>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
|
||||
* @since 3.0
|
||||
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
|
||||
*/
|
||||
public abstract class UriUtils {
|
||||
|
||||
@@ -84,6 +84,7 @@ public abstract class UriUtils {
|
||||
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" +
|
||||
")?" + PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?");
|
||||
|
||||
|
||||
static {
|
||||
// variable names refer to RFC 3986, appendix A
|
||||
BitSet alpha = new BitSet(256);
|
||||
@@ -183,11 +184,11 @@ public abstract class UriUtils {
|
||||
FRAGMENT.set('?');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes the given source URI into an encoded String. All various URI components are encoded according to their
|
||||
* respective valid character sets.
|
||||
*
|
||||
* @param uri the URI to be encoded
|
||||
* Encodes the given source URI into an encoded String. All various URI components
|
||||
* are encoded according to their respective valid character sets.
|
||||
* @param uri the URI to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
@@ -215,12 +216,10 @@ public abstract class UriUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their
|
||||
* respective valid character sets.
|
||||
*
|
||||
* <p><strong>Note</strong> that this method does not support fragments ({@code #}), as these are not supposed to be
|
||||
* sent to the server, but retained by the client.
|
||||
*
|
||||
* Encodes the given HTTP URI into an encoded String. All various URI components
|
||||
* are encoded according to their respective valid character sets.
|
||||
* <p><strong>Note</strong> that this method does not support fragments ({@code #}),
|
||||
* as these are not supposed to be sent to the server, but retained by the client.
|
||||
* @param httpUrl the HTTP URL to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded URL
|
||||
@@ -248,31 +247,25 @@ public abstract class UriUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given source URI components into an encoded String. All various URI components are optional, but encoded according
|
||||
* Encodes the given source URI components into an encoded String.
|
||||
* All various URI components are optional, but encoded according
|
||||
* to their respective valid character sets.
|
||||
*
|
||||
* @param scheme the scheme
|
||||
* @param scheme the scheme
|
||||
* @param authority the authority
|
||||
* @param userinfo the user info
|
||||
* @param host the host
|
||||
* @param port the port
|
||||
* @param path the path
|
||||
* @param query the query
|
||||
* @param fragment the fragment
|
||||
* @param encoding the character encoding to encode to
|
||||
* @param userinfo the user info
|
||||
* @param host the host
|
||||
* @param port the port
|
||||
* @param path the path
|
||||
* @param query the query
|
||||
* @param fragment the fragment
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded URI
|
||||
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeUriComponents(String scheme,
|
||||
String authority,
|
||||
String userinfo,
|
||||
String host,
|
||||
String port,
|
||||
String path,
|
||||
String query,
|
||||
String fragment,
|
||||
String encoding) throws UnsupportedEncodingException {
|
||||
public static String encodeUriComponents(String scheme, String authority, String userinfo,
|
||||
String host, String port, String path, String query, String fragment, String encoding)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -314,8 +307,7 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI scheme.
|
||||
*
|
||||
* @param scheme the scheme to be encoded
|
||||
* @param scheme the scheme to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded scheme
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
@@ -326,7 +318,6 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI user info.
|
||||
*
|
||||
* @param userInfo the user info to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded user info
|
||||
@@ -338,8 +329,7 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI host.
|
||||
*
|
||||
* @param host the host to be encoded
|
||||
* @param host the host to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded host
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
@@ -350,8 +340,7 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI port.
|
||||
*
|
||||
* @param port the port to be encoded
|
||||
* @param port the port to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded port
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
@@ -362,8 +351,7 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI path.
|
||||
*
|
||||
* @param path the path to be encoded
|
||||
* @param path the path to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded path
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
@@ -374,8 +362,7 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI path segment.
|
||||
*
|
||||
* @param segment the segment to be encoded
|
||||
* @param segment the segment to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded segment
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
@@ -386,7 +373,6 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI query.
|
||||
*
|
||||
* @param query the query to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded query
|
||||
@@ -398,9 +384,8 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI query parameter.
|
||||
*
|
||||
* @param queryParam the query parameter to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded query parameter
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
@@ -410,7 +395,6 @@ public abstract class UriUtils {
|
||||
|
||||
/**
|
||||
* Encodes the given URI fragment.
|
||||
*
|
||||
* @param fragment the fragment to be encoded
|
||||
* @param encoding the character encoding to encode to
|
||||
* @return the encoded fragment
|
||||
@@ -422,6 +406,7 @@ public abstract class UriUtils {
|
||||
|
||||
private static String encode(String source, String encoding, BitSet notEncoded)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
Assert.notNull(source, "'source' must not be null");
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
|
||||
@@ -431,11 +416,8 @@ public abstract class UriUtils {
|
||||
|
||||
private static byte[] encode(byte[] source, BitSet notEncoded) {
|
||||
Assert.notNull(source, "'source' must not be null");
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length * 2);
|
||||
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
int b = source[i];
|
||||
for (byte b : source) {
|
||||
if (b < 0) {
|
||||
b += 256;
|
||||
}
|
||||
@@ -468,7 +450,6 @@ public abstract class UriUtils {
|
||||
* <li>A sequence "<code>%<i>xy</i></code>" is interpreted as a hexadecimal
|
||||
* representation of the character.
|
||||
* </ul>
|
||||
*
|
||||
* @param source the source string
|
||||
* @param encoding the encoding
|
||||
* @return the decoded URI
|
||||
|
||||
Reference in New Issue
Block a user