Skip to content

Commit 2516bc0

Browse files
committed
polishing
1 parent 3963ff6 commit 2516bc0

File tree

4 files changed

+58
-84
lines changed

4 files changed

+58
-84
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ public interface HttpMessageConverter<T> {
3535
/**
3636
* Indicates whether the given class can be read by this converter.
3737
* @param clazz the class to test for readability
38-
* @param mediaType the media type to read, can be {@code null} if not specified. Typically the value of a
39-
* {@code Content-Type} header.
38+
* @param mediaType the media type to read, can be {@code null} if not specified.
39+
* Typically the value of a {@code Content-Type} header.
4040
* @return {@code true} if readable; {@code false} otherwise
4141
*/
4242
boolean canRead(Class<?> clazz, MediaType mediaType);
4343

4444
/**
4545
* Indicates whether the given class can be written by this converter.
4646
* @param clazz the class to test for writability
47-
* @param mediaType the media type to write, can be {@code null} if not specified. Typically the value of an
48-
* {@code Accept} header.
47+
* @param mediaType the media type to write, can be {@code null} if not specified.
48+
* Typically the value of an {@code Accept} header.
4949
* @return {@code true} if writable; {@code false} otherwise
5050
*/
5151
boolean canWrite(Class<?> clazz, MediaType mediaType);

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ public List<MediaType> getSupportedMediaTypes() {
6363

6464
public Resource read(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
6565
throws IOException, HttpMessageNotReadableException {
66+
6667
byte[] body = FileCopyUtils.copyToByteArray(inputMessage.getBody());
6768
return new ByteArrayResource(body);
6869
}
6970

7071
public void write(Resource resource, MediaType contentType, HttpOutputMessage outputMessage)
7172
throws IOException, HttpMessageNotWritableException {
73+
7274
HttpHeaders headers = outputMessage.getHeaders();
7375
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
7476
contentType = getContentType(resource);
@@ -87,20 +89,17 @@ public void write(Resource resource, MediaType contentType, HttpOutputMessage ou
8789
private MediaType getContentType(Resource resource) {
8890
if (jafPresent) {
8991
return ActivationMediaTypeFactory.getMediaType(resource);
90-
} else {
92+
}
93+
else {
9194
return MediaType.APPLICATION_OCTET_STREAM;
9295
}
9396
}
9497

95-
protected Long getContentLength(Resource resource, MediaType contentType) {
96-
try {
97-
return resource.getFile().length();
98-
}
99-
catch (IOException e) {
100-
return null;
101-
}
98+
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
99+
return resource.contentLength();
102100
}
103101

102+
104103
/**
105104
* Inner class to avoid hard-coded JAF dependency.
106105
*/
@@ -140,7 +139,7 @@ private static FileTypeMap loadFileTypeMapFromContextSupportModule() {
140139

141140
public static MediaType getMediaType(Resource resource) {
142141
String mediaType = fileTypeMap.getContentType(resource.getFilename());
143-
return StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null;
142+
return (StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null);
144143
}
145144
}
146145

org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,12 +33,12 @@
3333
import org.springframework.web.util.WebUtils;
3434

3535
/**
36-
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content on the response. This
37-
* ETag is compared to the <code>If-None-Match</code> header of the request. If these headers are equal, the resonse
38-
* content is not sent, but rather a 304 "Not Modified" status.
36+
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content on the response.
37+
* This ETag is compared to the <code>If-None-Match</code> header of the request. If these headers are equal,
38+
* the response content is not sent, but rather a <code>304 "Not Modified"</code> status instead.
3939
*
40-
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View}) is
41-
* still rendered. As such, this filter only saves bandwidth, not server performance.
40+
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View})
41+
* is still rendered. As such, this filter only saves bandwidth, not server performance.
4242
*
4343
* @author Arjen Poutsma
4444
* @since 3.0
@@ -49,6 +49,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
4949

5050
private static String HEADER_IF_NONE_MATCH = "If-None-Match";
5151

52+
5253
@Override
5354
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
5455
throws ServletException, IOException {
@@ -96,27 +97,22 @@ private void copyBodyToResponse(byte[] body, HttpServletResponse response) throw
9697

9798
/**
9899
* Indicates whether the given request and response are eligible for ETag generation.
99-
*
100-
* <p>Default implementation returns {@code true} for response status codes in the {@code 2xx} series.
101-
*
100+
* <p>The default implementation returns {@code true} for response status codes in the {@code 2xx} series.
102101
* @param request the HTTP request
103102
* @param response the HTTP response
104103
* @param responseStatusCode the HTTP response status code
105104
* @param responseBody the response body
106105
* @return {@code true} if eligible for ETag generation; {@code false} otherwise
107106
*/
108-
protected boolean isEligibleForEtag(HttpServletRequest request,
109-
HttpServletResponse response,
110-
int responseStatusCode,
111-
byte[] responseBody) {
107+
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
108+
int responseStatusCode, byte[] responseBody) {
109+
112110
return (responseStatusCode >= 200 && responseStatusCode < 300);
113111
}
114112

115113
/**
116114
* Generate the ETag header value from the given response body byte array.
117-
*
118115
* <p>The default implementation generates an MD5 hash.
119-
*
120116
* @param bytes the response bdoy as byte array
121117
* @return the ETag header value
122118
* @see org.springframework.util.DigestUtils
@@ -128,10 +124,11 @@ protected String generateETagHeaderValue(byte[] bytes) {
128124
return builder.toString();
129125
}
130126

127+
131128
/**
132-
* {@link HttpServletRequest} wrapper that buffers all content written to the {@linkplain #getOutputStream() output
133-
* stream} and {@linkplain #getWriter() writer}, and allows this content to be retrieved via a {@link #toByteArray()
134-
* byte array}.
129+
* {@link HttpServletRequest} wrapper that buffers all content written to the
130+
* {@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer},
131+
* and allows this content to be retrieved via a {@link #toByteArray() byte array}.
135132
*/
136133
private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper {
137134

@@ -216,7 +213,6 @@ public void write(int b) throws IOException {
216213
public void write(byte[] b, int off, int len) throws IOException {
217214
content.write(b, off, len);
218215
}
219-
220216
}
221217

222218
private class ResponsePrintWriter extends PrintWriter {
@@ -242,9 +238,7 @@ public void write(int c) {
242238
super.write(c);
243239
super.flush();
244240
}
245-
246241
}
247-
248242
}
249243

250244
}

org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
* </ul>
3737
*
3838
* @author Arjen Poutsma
39-
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
4039
* @since 3.0
40+
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
4141
*/
4242
public abstract class UriUtils {
4343

@@ -84,6 +84,7 @@ public abstract class UriUtils {
8484
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" +
8585
")?" + PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?");
8686

87+
8788
static {
8889
// variable names refer to RFC 3986, appendix A
8990
BitSet alpha = new BitSet(256);
@@ -183,11 +184,11 @@ public abstract class UriUtils {
183184
FRAGMENT.set('?');
184185
}
185186

187+
186188
/**
187-
* Encodes the given source URI into an encoded String. All various URI components are encoded according to their
188-
* respective valid character sets.
189-
*
190-
* @param uri the URI to be encoded
189+
* Encodes the given source URI into an encoded String. All various URI components
190+
* are encoded according to their respective valid character sets.
191+
* @param uri the URI to be encoded
191192
* @param encoding the character encoding to encode to
192193
* @return the encoded URI
193194
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
@@ -215,12 +216,10 @@ public static String encodeUri(String uri, String encoding) throws UnsupportedEn
215216
}
216217

217218
/**
218-
* Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their
219-
* respective valid character sets.
220-
*
221-
* <p><strong>Note</strong> that this method does not support fragments ({@code #}), as these are not supposed to be
222-
* sent to the server, but retained by the client.
223-
*
219+
* Encodes the given HTTP URI into an encoded String. All various URI components
220+
* are encoded according to their respective valid character sets.
221+
* <p><strong>Note</strong> that this method does not support fragments ({@code #}),
222+
* as these are not supposed to be sent to the server, but retained by the client.
224223
* @param httpUrl the HTTP URL to be encoded
225224
* @param encoding the character encoding to encode to
226225
* @return the encoded URL
@@ -248,31 +247,25 @@ public static String encodeHttpUrl(String httpUrl, String encoding) throws Unsup
248247
}
249248

250249
/**
251-
* Encodes the given source URI components into an encoded String. All various URI components are optional, but encoded according
250+
* Encodes the given source URI components into an encoded String.
251+
* All various URI components are optional, but encoded according
252252
* to their respective valid character sets.
253-
*
254-
* @param scheme the scheme
253+
* @param scheme the scheme
255254
* @param authority the authority
256-
* @param userinfo the user info
257-
* @param host the host
258-
* @param port the port
259-
* @param path the path
260-
* @param query the query
261-
* @param fragment the fragment
262-
* @param encoding the character encoding to encode to
255+
* @param userinfo the user info
256+
* @param host the host
257+
* @param port the port
258+
* @param path the path
259+
* @param query the query
260+
* @param fragment the fragment
261+
* @param encoding the character encoding to encode to
263262
* @return the encoded URI
264263
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
265264
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
266265
*/
267-
public static String encodeUriComponents(String scheme,
268-
String authority,
269-
String userinfo,
270-
String host,
271-
String port,
272-
String path,
273-
String query,
274-
String fragment,
275-
String encoding) throws UnsupportedEncodingException {
266+
public static String encodeUriComponents(String scheme, String authority, String userinfo,
267+
String host, String port, String path, String query, String fragment, String encoding)
268+
throws UnsupportedEncodingException {
276269

277270
Assert.hasLength(encoding, "'encoding' must not be empty");
278271
StringBuilder sb = new StringBuilder();
@@ -314,8 +307,7 @@ public static String encodeUriComponents(String scheme,
314307

315308
/**
316309
* Encodes the given URI scheme.
317-
*
318-
* @param scheme the scheme to be encoded
310+
* @param scheme the scheme to be encoded
319311
* @param encoding the character encoding to encode to
320312
* @return the encoded scheme
321313
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -326,7 +318,6 @@ public static String encodeScheme(String scheme, String encoding) throws Unsuppo
326318

327319
/**
328320
* Encodes the given URI user info.
329-
*
330321
* @param userInfo the user info to be encoded
331322
* @param encoding the character encoding to encode to
332323
* @return the encoded user info
@@ -338,8 +329,7 @@ public static String encodeUserInfo(String userInfo, String encoding) throws Uns
338329

339330
/**
340331
* Encodes the given URI host.
341-
*
342-
* @param host the host to be encoded
332+
* @param host the host to be encoded
343333
* @param encoding the character encoding to encode to
344334
* @return the encoded host
345335
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -350,8 +340,7 @@ public static String encodeHost(String host, String encoding) throws Unsupported
350340

351341
/**
352342
* Encodes the given URI port.
353-
*
354-
* @param port the port to be encoded
343+
* @param port the port to be encoded
355344
* @param encoding the character encoding to encode to
356345
* @return the encoded port
357346
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -362,8 +351,7 @@ public static String encodePort(String port, String encoding) throws Unsupported
362351

363352
/**
364353
* Encodes the given URI path.
365-
*
366-
* @param path the path to be encoded
354+
* @param path the path to be encoded
367355
* @param encoding the character encoding to encode to
368356
* @return the encoded path
369357
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -374,8 +362,7 @@ public static String encodePath(String path, String encoding) throws Unsupported
374362

375363
/**
376364
* Encodes the given URI path segment.
377-
*
378-
* @param segment the segment to be encoded
365+
* @param segment the segment to be encoded
379366
* @param encoding the character encoding to encode to
380367
* @return the encoded segment
381368
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -386,7 +373,6 @@ public static String encodePathSegment(String segment, String encoding) throws U
386373

387374
/**
388375
* Encodes the given URI query.
389-
*
390376
* @param query the query to be encoded
391377
* @param encoding the character encoding to encode to
392378
* @return the encoded query
@@ -398,9 +384,8 @@ public static String encodeQuery(String query, String encoding) throws Unsupport
398384

399385
/**
400386
* Encodes the given URI query parameter.
401-
*
402387
* @param queryParam the query parameter to be encoded
403-
* @param encoding the character encoding to encode to
388+
* @param encoding the character encoding to encode to
404389
* @return the encoded query parameter
405390
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
406391
*/
@@ -410,7 +395,6 @@ public static String encodeQueryParam(String queryParam, String encoding) throws
410395

411396
/**
412397
* Encodes the given URI fragment.
413-
*
414398
* @param fragment the fragment to be encoded
415399
* @param encoding the character encoding to encode to
416400
* @return the encoded fragment
@@ -422,6 +406,7 @@ public static String encodeFragment(String fragment, String encoding) throws Uns
422406

423407
private static String encode(String source, String encoding, BitSet notEncoded)
424408
throws UnsupportedEncodingException {
409+
425410
Assert.notNull(source, "'source' must not be null");
426411
Assert.hasLength(encoding, "'encoding' must not be empty");
427412

@@ -431,11 +416,8 @@ private static String encode(String source, String encoding, BitSet notEncoded)
431416

432417
private static byte[] encode(byte[] source, BitSet notEncoded) {
433418
Assert.notNull(source, "'source' must not be null");
434-
435419
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length * 2);
436-
437-
for (int i = 0; i < source.length; i++) {
438-
int b = source[i];
420+
for (byte b : source) {
439421
if (b < 0) {
440422
b += 256;
441423
}
@@ -468,7 +450,6 @@ private static byte[] encode(byte[] source, BitSet notEncoded) {
468450
* <li>A sequence "<code>%<i>xy</i></code>" is interpreted as a hexadecimal
469451
* representation of the character.
470452
* </ul>
471-
*
472453
* @param source the source string
473454
* @param encoding the encoding
474455
* @return the decoded URI

0 commit comments

Comments
 (0)