From da63898d5fdefa7fc9946791a580bb420fdc3c5d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 21 Nov 2016 17:36:04 +0100 Subject: [PATCH] Polishing --- .../support/DisposableBeanAdapter.java | 9 +---- .../support/ScriptFactoryPostProcessor.java | 4 +- .../SimpleClientHttpRequestFactory.java | 5 +-- .../springframework/web/util/UriTemplate.java | 8 ++-- .../client/AbstractJettyServerTestCase.java | 3 +- .../AsyncRestTemplateIntegrationTests.java | 2 - .../web/servlet/ResourceServlet.java | 38 ++++++++++--------- .../web/socket/WebSocketMessage.java | 5 +-- .../session/AbstractSockJsSession.java | 2 +- 9 files changed, 33 insertions(+), 43 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java index d5c4754f44..9f4e33d7f3 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java @@ -211,14 +211,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable { for (BeanPostProcessor processor : processors) { if (processor instanceof DestructionAwareBeanPostProcessor) { DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor; - try { - if (dabpp.requiresDestruction(bean)) { - filteredPostProcessors.add(dabpp); - } - } - catch (AbstractMethodError err) { - // A pre-4.3 third-party DestructionAwareBeanPostProcessor... - // As of 5.0, we can let requiresDestruction be a Java 8 default method which returns true. + if (dabpp.requiresDestruction(bean)) { filteredPostProcessors.add(dabpp); } } diff --git a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java index 395efb7813..d655e266ff 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java @@ -206,8 +206,8 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces @Override public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof ConfigurableBeanFactory)) { - throw new IllegalStateException("ScriptFactoryPostProcessor doesn't work with a BeanFactory " - + "which does not implement ConfigurableBeanFactory: " + beanFactory.getClass()); + throw new IllegalStateException("ScriptFactoryPostProcessor doesn't work with " + + "non-ConfigurableBeanFactory: " + beanFactory.getClass()); } this.beanFactory = (ConfigurableBeanFactory) beanFactory; diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java index 076e7b37ed..bf982d6e24 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -151,8 +151,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory, */ @Override public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException { - Assert.state(this.taskExecutor != null, - "Asynchronous execution requires an AsyncTaskExecutor to be set"); + Assert.state(this.taskExecutor != null, "Asynchronous execution requires TaskExecutor to be set"); HttpURLConnection connection = openConnection(uri.toURL(), this.proxy); prepareConnection(connection, httpMethod.name()); diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index c925758732..a5c0bff5fd 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -173,7 +173,6 @@ public class UriTemplate implements Serializable { private final Pattern pattern; - private TemplateInfo(List vars, Pattern pattern) { this.variableNames = vars; this.pattern = pattern; @@ -187,7 +186,7 @@ public class UriTemplate implements Serializable { return this.pattern; } - private static TemplateInfo parse(String uriTemplate) { + public static TemplateInfo parse(String uriTemplate) { int level = 0; List variableNames = new ArrayList<>(); StringBuilder pattern = new StringBuilder(); @@ -216,8 +215,7 @@ public class UriTemplate implements Serializable { else { if (idx + 1 == variable.length()) { throw new IllegalArgumentException( - "No custom regular expression specified after ':' " + - "in \"" + variable + "\""); + "No custom regular expression specified after ':' in \"" + variable + "\""); } String regex = variable.substring(idx + 1, variable.length()); pattern.append('('); @@ -238,7 +236,7 @@ public class UriTemplate implements Serializable { } private static String quote(StringBuilder builder) { - return builder.length() != 0 ? Pattern.quote(builder.toString()) : ""; + return (builder.length() > 0 ? Pattern.quote(builder.toString()) : ""); } } diff --git a/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java b/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java index 9da3a8f7cc..d5fd221a2e 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java +++ b/spring-web/src/test/java/org/springframework/web/client/AbstractJettyServerTestCase.java @@ -17,6 +17,7 @@ package org.springframework.web.client; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.Map; @@ -77,7 +78,7 @@ public class AbstractJettyServerTestCase { jettyServer = new Server(0); ServletContextHandler handler = new ServletContextHandler(); - byte[] bytes = helloWorld.getBytes("utf-8"); + byte[] bytes = helloWorld.getBytes(StandardCharsets.UTF_8); handler.addServlet(new ServletHolder(new GetServlet(bytes, textContentType)), "/get"); handler.addServlet(new ServletHolder(new GetServlet(new byte[0], textContentType)), "/get/nothing"); handler.addServlet(new ServletHolder(new GetServlet(bytes, null)), "/get/nocontenttype"); diff --git a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java index ba97d767e4..f33e59bf58 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java @@ -119,7 +119,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa assertNull("Invalid content", entity.getBody()); } - @Test public void getNoContentTypeHeader() throws Exception { Future> futureEntity = template.getForEntity(baseUrl + "/get/nocontenttype", byte[].class); @@ -127,7 +126,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa assertArrayEquals("Invalid content", helloWorld.getBytes("UTF-8"), responseEntity.getBody()); } - @Test public void getNoContent() throws Exception { Future> responseFuture = template.getForEntity(baseUrl + "/status/nocontent", String.class); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/ResourceServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/ResourceServlet.java index 9905909acc..776c99c03d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/ResourceServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/ResourceServlet.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -175,8 +175,8 @@ public class ResourceServlet extends HttpServletBean { } /** - * Return a PathMatcher to use for matching the "allowedResources" URL pattern. - * Default is AntPathMatcher. + * Return a {@link PathMatcher} to use for matching the "allowedResources" URL pattern. + *

The default is {@link AntPathMatcher}. * @see #setAllowedResources * @see org.springframework.util.AntPathMatcher */ @@ -191,9 +191,9 @@ public class ResourceServlet extends HttpServletBean { */ @Override protected final void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws ServletException, IOException { - // determine URL of resource to include + // Determine URL of resource to include... String resourceUrl = determineResourceUrl(request); if (resourceUrl != null) { @@ -220,7 +220,7 @@ public class ResourceServlet extends HttpServletBean { } } - // no resource URL specified -> try to include default URL. + // No resource URL specified -> try to include default URL. else if (!includeDefaultUrl(request, response)) { throw new ServletException("No target resource URL found for request"); } @@ -265,23 +265,23 @@ public class ResourceServlet extends HttpServletBean { * @throws IOException if thrown by the RequestDispatcher */ private void doInclude(HttpServletRequest request, HttpServletResponse response, String resourceUrl) - throws ServletException, IOException { + throws ServletException, IOException { if (this.contentType != null) { response.setContentType(this.contentType); } - String[] resourceUrls = - StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS); - for (int i = 0; i < resourceUrls.length; i++) { + + String[] resourceUrls = StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS); + for (String url : resourceUrls) { // check whether URL matches allowed resources - if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, resourceUrls[i])) { - throw new ServletException("Resource [" + resourceUrls[i] + + if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, url)) { + throw new ServletException("Resource [" + url + "] does not match allowed pattern [" + this.allowedResources + "]"); } if (logger.isDebugEnabled()) { - logger.debug("Including resource [" + resourceUrls[i] + "]"); + logger.debug("Including resource [" + url + "]"); } - RequestDispatcher rd = request.getRequestDispatcher(resourceUrls[i]); + RequestDispatcher rd = request.getRequestDispatcher(url); rd.include(request, response); } } @@ -309,8 +309,8 @@ public class ResourceServlet extends HttpServletBean { if (resourceUrl != null) { String[] resourceUrls = StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS); long latestTimestamp = -1; - for (int i = 0; i < resourceUrls.length; i++) { - long timestamp = getFileTimestamp(resourceUrls[i]); + for (String url : resourceUrls) { + long timestamp = getFileTimestamp(url); if (timestamp > latestTimestamp) { latestTimestamp = timestamp; } @@ -336,8 +336,10 @@ public class ResourceServlet extends HttpServletBean { return lastModifiedTime; } catch (IOException ex) { - logger.warn("Couldn't retrieve last-modified timestamp of [" + resource + - "] - using ResourceServlet startup time"); + if (logger.isWarnEnabled()) { + logger.warn("Couldn't retrieve last-modified timestamp of " + resource + + " - using ResourceServlet startup time"); + } return -1; } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketMessage.java b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketMessage.java index 5881ed0c34..fd5383d87e 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketMessage.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 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. @@ -25,11 +25,10 @@ package org.springframework.web.socket; public interface WebSocketMessage { /** - * Returns the message payload. This will never be {@code null}. + * Return the message payload (never {@code null}). */ T getPayload(); - /** * Return the number of bytes contained in the message. */ diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java index c5d456de49..ba8cdc94d8 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java @@ -208,7 +208,7 @@ public abstract class AbstractSockJsSession implements SockJsSession { writeFrameInternal(SockJsFrame.closeFrame(status.getCode(), status.getReason())); } catch (Throwable ex) { - logger.debug("Failure while send SockJS close frame", ex); + logger.debug("Failure while sending SockJS close frame", ex); } } updateLastActiveTime();