From 640c8ff69355a86a20b3994a83a47915647804ab Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 14 Nov 2017 12:31:01 +0100 Subject: [PATCH] Polishing --- .../core/io/ByteArrayResource.java | 6 +-- .../jdbc/core/JdbcOperations.java | 12 +++--- .../jdbc/core/JdbcTemplate.java | 6 +++ .../NamedParameterBatchUpdateUtils.java | 11 ++--- .../NamedParameterJdbcTemplate.java | 6 +-- .../springframework/http/HttpRangeTests.java | 24 +++++------ .../HttpMessageConverterExtractorTests.java | 42 +++++++------------ .../annotation/ResourceHandlerRegistry.java | 18 ++++---- .../resource/PathResourceResolver.java | 5 +-- .../resource/ResourceHttpRequestHandler.java | 10 ++--- .../ResourceHttpRequestHandlerTests.java | 28 ++++++------- .../WebMvcStompEndpointRegistry.java | 6 +-- .../WebSocketConfigurationSupport.java | 2 +- ...cketMessageBrokerConfigurationSupport.java | 8 ++-- 14 files changed, 85 insertions(+), 99 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/ByteArrayResource.java b/spring-core/src/main/java/org/springframework/core/io/ByteArrayResource.java index a6ae157ed56..00407277f95 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ByteArrayResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/ByteArrayResource.java @@ -47,7 +47,7 @@ public class ByteArrayResource extends AbstractResource { /** - * Create a new ByteArrayResource. + * Create a new {@code ByteArrayResource}. * @param byteArray the byte array to wrap */ public ByteArrayResource(byte[] byteArray) { @@ -55,7 +55,7 @@ public class ByteArrayResource extends AbstractResource { } /** - * Create a new ByteArrayResource. + * Create a new {@code ByteArrayResource} with a description. * @param byteArray the byte array to wrap * @param description where the byte array comes from */ @@ -65,6 +65,7 @@ public class ByteArrayResource extends AbstractResource { this.description = (description != null ? description : ""); } + /** * Return the underlying byte array. */ @@ -72,7 +73,6 @@ public class ByteArrayResource extends AbstractResource { return this.byteArray; } - /** * This implementation always returns {@code true}. */ diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java index b28bd1d3332..0d0eb2fbfe7 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. @@ -755,7 +755,7 @@ public interface JdbcOperations { * list of arguments to bind to the query, expecting a SqlRowSet. *

The results will be mapped to an SqlRowSet which holds the data in a * disconnected fashion. This wrapper will translate any SQLExceptions thrown. - *

Note that that, for the default implementation, JDBC RowSet support needs to + *

Note that, for the default implementation, JDBC RowSet support needs to * be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl} * class is used, which is part of JDK 1.5+ and also available separately as part of * Sun's JDBC RowSet Implementations download (rowset.jar). @@ -778,7 +778,7 @@ public interface JdbcOperations { * list of arguments to bind to the query, expecting a SqlRowSet. *

The results will be mapped to an SqlRowSet which holds the data in a * disconnected fashion. This wrapper will translate any SQLExceptions thrown. - *

Note that that, for the default implementation, JDBC RowSet support needs to + *

Note that, for the default implementation, JDBC RowSet support needs to * be available at runtime: by default, Sun's {@code com.sun.rowset.CachedRowSetImpl} * class is used, which is part of JDK 1.5+ and also available separately as part of * Sun's JDBC RowSet Implementations download (rowset.jar). @@ -882,7 +882,7 @@ public interface JdbcOperations { * @param batchArgs the List of Object arrays containing the batch of arguments for the query * @return an array containing the numbers of rows affected by each update in the batch */ - public int[] batchUpdate(String sql, List batchArgs) throws DataAccessException; + int[] batchUpdate(String sql, List batchArgs) throws DataAccessException; /** * Execute a batch using the supplied SQL statement with the batch of supplied arguments. @@ -892,7 +892,7 @@ public interface JdbcOperations { * (constants from {@code java.sql.Types}) * @return an array containing the numbers of rows affected by each update in the batch */ - public int[] batchUpdate(String sql, List batchArgs, int[] argTypes) throws DataAccessException; + int[] batchUpdate(String sql, List batchArgs, int[] argTypes) throws DataAccessException; /** * Execute multiple batches using the supplied SQL statement with the collect of supplied arguments. @@ -905,7 +905,7 @@ public interface JdbcOperations { * @return an array containing for each batch another array containing the numbers of rows affected * by each update in the batch */ - public int[][] batchUpdate(String sql, Collection batchArgs, int batchSize, + int[][] batchUpdate(String sql, Collection batchArgs, int batchSize, ParameterizedPreparedStatementSetter pss) throws DataAccessException; diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 79e8b9b0c22..6a2bc943b6a 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -429,6 +429,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { if (logger.isDebugEnabled()) { logger.debug("Executing SQL statement [" + sql + "]"); } + class ExecuteStatementCallback implements StatementCallback, SqlProvider { @Override public Object doInStatement(Statement stmt) throws SQLException { @@ -440,6 +441,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return sql; } } + execute(new ExecuteStatementCallback()); } @@ -450,6 +452,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { if (logger.isDebugEnabled()) { logger.debug("Executing SQL query [" + sql + "]"); } + class QueryStatementCallback implements StatementCallback, SqlProvider { @Override public T doInStatement(Statement stmt) throws SQLException { @@ -471,6 +474,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return sql; } } + return execute(new QueryStatementCallback()); } @@ -521,6 +525,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { if (logger.isDebugEnabled()) { logger.debug("Executing SQL update [" + sql + "]"); } + class UpdateStatementCallback implements StatementCallback, SqlProvider { @Override public Integer doInStatement(Statement stmt) throws SQLException { @@ -535,6 +540,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { return sql; } } + return execute(new UpdateStatementCallback()); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java index 5fd0a6a24cf..cffc7a850ec 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterBatchUpdateUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. @@ -24,30 +24,31 @@ import org.springframework.jdbc.core.BatchUpdateUtils; import org.springframework.jdbc.core.JdbcOperations; /** - * Generic utility methods for working with JDBC batch statements using named parameters. Mainly for internal use - * within the framework. + * Generic utility methods for working with JDBC batch statements using named parameters. + * Mainly for internal use within the framework. * * @author Thomas Risberg + * @since 3.0 */ public class NamedParameterBatchUpdateUtils extends BatchUpdateUtils { public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) { + if (batchArgs.length <= 0) { return new int[] {0}; } + String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]); return jdbcOperations.batchUpdate( sqlToUse, new BatchPreparedStatementSetter() { - @Override public void setValues(PreparedStatement ps, int i) throws SQLException { Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null); int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]); setStatementParameters(values, ps, columnTypes); } - @Override public int getBatchSize() { return batchArgs.length; diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java index 94263807d9c..68e3491a784 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. @@ -331,8 +331,8 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations @Override public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) { - ParsedSql parsedSql = getParsedSql(sql); - return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(parsedSql, batchArgs, getJdbcOperations()); + return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters( + getParsedSql(sql), batchArgs, getJdbcOperations()); } /** diff --git a/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java b/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java index 5ddec37aa50..fe672951c94 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpRangeTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -40,57 +40,57 @@ import static org.mockito.Mockito.mock; public class HttpRangeTests { @Test(expected = IllegalArgumentException.class) - public void invalidFirstPosition() throws Exception { + public void invalidFirstPosition() { HttpRange.createByteRange(-1); } @Test(expected = IllegalArgumentException.class) - public void invalidLastLessThanFirst() throws Exception { + public void invalidLastLessThanFirst() { HttpRange.createByteRange(10, 9); } @Test(expected = IllegalArgumentException.class) - public void invalidSuffixLength() throws Exception { + public void invalidSuffixLength() { HttpRange.createSuffixRange(-1); } @Test - public void byteRange() throws Exception { + public void byteRange() { HttpRange range = HttpRange.createByteRange(0, 499); assertEquals(0, range.getRangeStart(1000)); assertEquals(499, range.getRangeEnd(1000)); } @Test - public void byteRangeWithoutLastPosition() throws Exception { + public void byteRangeWithoutLastPosition() { HttpRange range = HttpRange.createByteRange(9500); assertEquals(9500, range.getRangeStart(10000)); assertEquals(9999, range.getRangeEnd(10000)); } @Test - public void byteRangeOfZeroLength() throws Exception { + public void byteRangeOfZeroLength() { HttpRange range = HttpRange.createByteRange(9500, 9500); assertEquals(9500, range.getRangeStart(10000)); assertEquals(9500, range.getRangeEnd(10000)); } @Test - public void suffixRange() throws Exception { + public void suffixRange() { HttpRange range = HttpRange.createSuffixRange(500); assertEquals(500, range.getRangeStart(1000)); assertEquals(999, range.getRangeEnd(1000)); } @Test - public void suffixRangeShorterThanRepresentation() throws Exception { + public void suffixRangeShorterThanRepresentation() { HttpRange range = HttpRange.createSuffixRange(500); assertEquals(0, range.getRangeStart(350)); assertEquals(349, range.getRangeEnd(350)); } @Test - public void parseRanges() throws Exception { + public void parseRanges() { List ranges = HttpRange.parseRanges("bytes=0-0,500-,-1"); assertEquals(3, ranges.size()); assertEquals(0, ranges.get(0).getRangeStart(1000)); @@ -138,8 +138,8 @@ public class HttpRangeTests { @Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") - public void toResourceRegionExceptionLength() { - ByteArrayResource resource = mock(ByteArrayResource.class); + public void toResourceRegionExceptionLength() throws IOException { + InputStreamResource resource = mock(InputStreamResource.class); given(resource.contentLength()).willThrow(IOException.class); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); diff --git a/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java b/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java index 3a59757e6ca..3079a08774a 100644 --- a/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -22,7 +22,6 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import org.junit.Before; import org.junit.Test; import org.springframework.core.ParameterizedTypeReference; @@ -46,43 +45,36 @@ public class HttpMessageConverterExtractorTests { private HttpMessageConverterExtractor extractor; - private ClientHttpResponse response; + private final ClientHttpResponse response = mock(ClientHttpResponse.class); - @Before - public void createMocks() { - response = mock(ClientHttpResponse.class); - } @Test public void noContent() throws IOException { HttpMessageConverter converter = mock(HttpMessageConverter.class); - extractor = new HttpMessageConverterExtractor(String.class, createConverterList(converter)); + extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getStatusCode()).willReturn(HttpStatus.NO_CONTENT); Object result = extractor.extractData(response); - assertNull(result); } @Test public void notModified() throws IOException { HttpMessageConverter converter = mock(HttpMessageConverter.class); - extractor = new HttpMessageConverterExtractor(String.class, createConverterList(converter)); + extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getStatusCode()).willReturn(HttpStatus.NOT_MODIFIED); Object result = extractor.extractData(response); - assertNull(result); } @Test public void informational() throws IOException { HttpMessageConverter converter = mock(HttpMessageConverter.class); - extractor = new HttpMessageConverterExtractor(String.class, createConverterList(converter)); + extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getStatusCode()).willReturn(HttpStatus.CONTINUE); Object result = extractor.extractData(response); - assertNull(result); } @@ -91,12 +83,11 @@ public class HttpMessageConverterExtractorTests { HttpMessageConverter converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(0); - extractor = new HttpMessageConverterExtractor(String.class, createConverterList(converter)); + extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getStatusCode()).willReturn(HttpStatus.OK); given(response.getHeaders()).willReturn(responseHeaders); Object result = extractor.extractData(response); - assertNull(result); } @@ -104,10 +95,10 @@ public class HttpMessageConverterExtractorTests { @SuppressWarnings("unchecked") public void emptyMessageBody() throws IOException { HttpMessageConverter converter = mock(HttpMessageConverter.class); - List> converters = new ArrayList>(); + List> converters = new ArrayList<>(); converters.add(converter); HttpHeaders responseHeaders = new HttpHeaders(); - extractor = new HttpMessageConverterExtractor(String.class, createConverterList(converter)); + extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getStatusCode()).willReturn(HttpStatus.OK); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream("".getBytes())); @@ -120,13 +111,13 @@ public class HttpMessageConverterExtractorTests { @SuppressWarnings("unchecked") public void normal() throws IOException { HttpMessageConverter converter = mock(HttpMessageConverter.class); - List> converters = new ArrayList>(); + List> converters = new ArrayList<>(); converters.add(converter); HttpHeaders responseHeaders = new HttpHeaders(); MediaType contentType = MediaType.TEXT_PLAIN; responseHeaders.setContentType(contentType); String expected = "Foo"; - extractor = new HttpMessageConverterExtractor(String.class, converters); + extractor = new HttpMessageConverterExtractor<>(String.class, converters); given(response.getStatusCode()).willReturn(HttpStatus.OK); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes())); @@ -134,7 +125,6 @@ public class HttpMessageConverterExtractorTests { given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expected); Object result = extractor.extractData(response); - assertEquals(expected, result); } @@ -142,12 +132,12 @@ public class HttpMessageConverterExtractorTests { @SuppressWarnings("unchecked") public void cannotRead() throws IOException { HttpMessageConverter converter = mock(HttpMessageConverter.class); - List> converters = new ArrayList>(); + List> converters = new ArrayList<>(); converters.add(converter); HttpHeaders responseHeaders = new HttpHeaders(); MediaType contentType = MediaType.TEXT_PLAIN; responseHeaders.setContentType(contentType); - extractor = new HttpMessageConverterExtractor(String.class, converters); + extractor = new HttpMessageConverterExtractor<>(String.class, converters); given(response.getStatusCode()).willReturn(HttpStatus.OK); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream("Foobar".getBytes())); @@ -175,13 +165,11 @@ public class HttpMessageConverterExtractorTests { given(converter.read(eq(type), eq(null), any(HttpInputMessage.class))).willReturn(expected); Object result = extractor.extractData(response); - assertEquals(expected, result); } - private List> createConverterList( - HttpMessageConverter converter) { - List> converters = new ArrayList>(1); + private List> createConverterList(HttpMessageConverter converter) { + List> converters = new ArrayList<>(1); converters.add(converter); return converters; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java index b73f58fb064..402341f8299 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -90,8 +90,7 @@ public class ResourceHandlerRegistry { /** * A variant of * {@link #ResourceHandlerRegistry(ApplicationContext, ServletContext, ContentNegotiationManager)} - * that also accepts the {@link UrlPathHelper} used for mapping requests - * to static resources. + * that also accepts the {@link UrlPathHelper} used for mapping requests to static resources. * @since 4.3.13 */ public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext, @@ -106,13 +105,12 @@ public class ResourceHandlerRegistry { /** - * Add a resource handler for serving static resources based on the specified URL path - * patterns. The handler will be invoked for every incoming request that matches to - * one of the specified path patterns. - *

Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"} - * are allowed. See {@link org.springframework.util.AntPathMatcher} for more details on the - * syntax. - * @return A {@link ResourceHandlerRegistration} to use to further configure the + * Add a resource handler for serving static resources based on the specified URL path patterns. + * The handler will be invoked for every incoming request that matches to one of the specified + * path patterns. + *

Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"} are allowed. + * See {@link org.springframework.util.AntPathMatcher} for more details on the syntax. + * @return a {@link ResourceHandlerRegistration} to use to further configure the * registered resource handler */ public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 203654d5cf1..8732c39ecd6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -92,7 +92,6 @@ public class PathResourceResolver extends AbstractResourceResolver { *

Note: the charset is used only if the * {@link #setUrlPathHelper urlPathHelper} property is also configured and * its {@code urlDecode} property is set to true. - * @param locationCharsets charsets by location * @since 4.3.13 */ public void setLocationCharsets(Map locationCharsets) { @@ -105,14 +104,13 @@ public class PathResourceResolver extends AbstractResourceResolver { * @since 4.3.13 */ public Map getLocationCharsets() { - return Collections.unmodifiableMap(locationCharsets); + return Collections.unmodifiableMap(this.locationCharsets); } /** * Provide a reference to the {@link UrlPathHelper} used to map requests to * static resources. This helps to derive information about the lookup path * such as whether it is decoded or not. - * @param urlPathHelper a reference to the path helper * @since 4.3.13 */ public void setUrlPathHelper(UrlPathHelper urlPathHelper) { @@ -127,6 +125,7 @@ public class PathResourceResolver extends AbstractResourceResolver { return this.urlPathHelper; } + @Override protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List locations, ResourceResolverChain chain) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 047d8c997d0..9c465326786 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -152,10 +152,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator * {@link org.springframework.core.io.UrlResource URL resources} such as a * file or an HTTP URL location and is used in {@link PathResourceResolver} * to correctly encode paths relative to the location. - *

Note: the charset is used only if the + *

Note: The charset is used only if the * {@link #setUrlPathHelper urlPathHelper} property is also configured and - * its {@code urlDecode} property is set to true. - * @param locationCharsets charsets by location + * its {@code urlDecode} property is set to {@code true}. * @since 4.3.13 */ public void setLocationCharsets(Map locationCharsets) { @@ -168,7 +167,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator * @since 4.3.13 */ public Map getLocationCharsets() { - return Collections.unmodifiableMap(locationCharsets); + return Collections.unmodifiableMap(this.locationCharsets); } /** @@ -281,7 +280,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator * Provide a reference to the {@link UrlPathHelper} used to map requests to * static resources. This helps to derive information about the lookup path * such as whether it is decoded or not. - * @param urlPathHelper a reference to the path helper * @since 4.3.13 */ public void setUrlPathHelper(UrlPathHelper urlPathHelper) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index e835aff8c15..2b03c9def91 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -48,7 +48,7 @@ import org.springframework.web.servlet.HandlerMapping; import static org.junit.Assert.*; /** - * Unit tests for ResourceHttpRequestHandler. + * Unit tests for {@link ResourceHttpRequestHandler}. * * @author Keith Donald * @author Jeremy Grelle @@ -67,7 +67,7 @@ public class ResourceHttpRequestHandlerTests { @Before - public void setUp() throws Exception { + public void setup() throws Exception { dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); @@ -86,6 +86,7 @@ public class ResourceHttpRequestHandlerTests { this.response = new MockHttpServletResponse(); } + @Test public void getResource() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); @@ -240,7 +241,7 @@ public class ResourceHttpRequestHandlerTests { assertEquals("function foo() { console.log(\"hello world\"); }", this.response.getContentAsString()); } - @Test // SPR-13658 + @Test // SPR-13658 public void getResourceWithRegisteredMediaType() throws Exception { ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean(); factory.addMediaType("css", new MediaType("foo", "bar")); @@ -261,7 +262,7 @@ public class ResourceHttpRequestHandlerTests { assertEquals("h1 { color:red; }", this.response.getContentAsString()); } - @Test // SPR-14577 + @Test // SPR-14577 public void getMediaTypeWithFavorPathExtensionOff() throws Exception { ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean(); factory.setFavorPathExtension(false); @@ -282,18 +283,16 @@ public class ResourceHttpRequestHandlerTests { assertEquals("text/html", this.response.getContentType()); } - @Test // SPR-14368 + @Test // SPR-14368 public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception { MockServletContext servletContext = new MockServletContext() { - @Override public String getMimeType(String filePath) { return "foo/bar"; } - @Override public String getVirtualServerName() { - return null; + return ""; } }; @@ -606,8 +605,7 @@ public class ResourceHttpRequestHandlerTests { assertEquals("t.", ranges[11]); } - // SPR-14005 - @Test + @Test // SPR-14005 public void doOverwriteExistingCacheControlHeaders() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.response.setHeader("Cache-Control", "no-store"); @@ -618,10 +616,6 @@ public class ResourceHttpRequestHandlerTests { } - private long dateHeaderAsLong(String responseHeaderName) throws Exception { - return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime(); - } - private long resourceLastModified(String resourceName) throws IOException { return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); } @@ -631,6 +625,10 @@ public class ResourceHttpRequestHandlerTests { return dateFormat.format(lastModified); } + private long dateHeaderAsLong(String responseHeaderName) throws Exception { + return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime(); + } + private static class TestServletContext extends MockServletContext { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java index 048cd3a03b8..0cf530ed421 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -146,10 +146,8 @@ public class WebMvcStompEndpointRegistry implements StompEndpointRegistry { this.stompHandler.setApplicationEventPublisher(applicationContext); } - /** - * Return a handler mapping with the mapped ViewControllers; or {@code null} - * in case of no registrations. + * Return a handler mapping with the mapped ViewControllers. */ public AbstractHandlerMapping getHandlerMapping() { Map urlMap = new LinkedHashMap(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java index 35cd1565228..1b8de76bf3f 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java index b2055e3fac1..18b96ef3518 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java @@ -54,8 +54,8 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac @Override protected SimpAnnotationMethodMessageHandler createAnnotationMethodMessageHandler() { - return new WebSocketAnnotationMethodMessageHandler(clientInboundChannel(), - clientOutboundChannel(), brokerMessagingTemplate()); + return new WebSocketAnnotationMethodMessageHandler( + clientInboundChannel(), clientOutboundChannel(), brokerMessagingTemplate()); } @Override @@ -72,8 +72,8 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac @SuppressWarnings("deprecation") public HandlerMapping stompWebSocketHandlerMapping() { WebSocketHandler handler = decorateWebSocketHandler(subProtocolWebSocketHandler()); - WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(handler, - getTransportRegistration(), userSessionRegistry(), messageBrokerTaskScheduler()); + WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry( + handler, getTransportRegistration(), userSessionRegistry(), messageBrokerTaskScheduler()); registry.setApplicationContext(getApplicationContext()); registerStompEndpoints(registry); return registry.getHandlerMapping();