From ab9fea6b8d05da402ef01e3dcc3c925805dc801f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 31 Aug 2016 01:53:03 +0200 Subject: [PATCH] Polishing (cherry picked from commit 3767092) --- .../htmlunit/HtmlUnitRequestBuilder.java | 30 +++++++++---------- ...ionListenerMethodTransactionalAdapter.java | 15 ++++++---- .../org/springframework/http/HttpHeaders.java | 12 ++++---- .../org/springframework/http/HttpRange.java | 10 +++---- .../org/springframework/http/MediaType.java | 5 ++++ .../web/socket/WebSocketExtension.java | 19 ++++++------ 6 files changed, 51 insertions(+), 40 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java index 81391ebb1b..f95408cdd3 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java @@ -213,8 +213,9 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { private void authType(MockHttpServletRequest request) { String authorization = header("Authorization"); - if (authorization != null) { - request.setAuthType(StringUtils.split(authorization, ": ")[0]); + String[] authSplit = StringUtils.split(authorization, ": "); + if (authSplit != null) { + request.setAuthType(authSplit[0]); } } @@ -434,20 +435,19 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { @Override public Object merge(Object parent) { - if (parent == null) { - return this; - } - if (parent instanceof MockHttpServletRequestBuilder) { - MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/"); - copiedParent.merge(parent); - this.parentBuilder = copiedParent; - } else if (parent instanceof RequestBuilder) { - this.parentBuilder = (RequestBuilder) parent; - } - if (parent instanceof SmartRequestBuilder) { - this.parentPostProcessor = (SmartRequestBuilder) parent; + if (parent instanceof RequestBuilder) { + if (parent instanceof MockHttpServletRequestBuilder) { + MockHttpServletRequestBuilder copiedParent = MockMvcRequestBuilders.get("/"); + copiedParent.merge(parent); + this.parentBuilder = copiedParent; + } + else { + this.parentBuilder = (RequestBuilder) parent; + } + if (parent instanceof SmartRequestBuilder) { + this.parentPostProcessor = (SmartRequestBuilder) parent; + } } - return this; } diff --git a/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java b/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java index 5998522c69..a33b07bc63 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java +++ b/spring-tx/src/main/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapter.java @@ -32,10 +32,10 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * an event to a {@link TransactionalEventListener} annotated method. Supports * the exact same features as any regular {@link EventListener} annotated method * but is aware of the transactional context of the event publisher. - *

- * Processing of {@link TransactionalEventListener} is enabled automatically when - * Spring's transaction management is enabled. For other cases, registering a - * bean of type {@link TransactionalEventListenerFactory} is required. + * + *

Processing of {@link TransactionalEventListener} is enabled automatically + * when Spring's transaction management is enabled. For other cases, registering + * a bean of type {@link TransactionalEventListenerFactory} is required. * * @author Stephane Nicoll * @author Juergen Hoeller @@ -69,8 +69,11 @@ class ApplicationListenerMethodTransactionalAdapter extends ApplicationListenerM } processEvent(event); } - else if (logger.isDebugEnabled()) { - logger.debug("No transaction is running - skipping " + event); + else { + // No transactional event execution at all + if (logger.isDebugEnabled()) { + logger.debug("No transaction is active - skipping " + event); + } } } diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index d3b4a04e66..7214bbb452 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -808,13 +808,14 @@ public class HttpHeaders implements MultiValueMap, Serializable /** * Set the (new) value of the {@code Host} header. - *

If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0}, the host header - * will only contain the {@linkplain InetSocketAddress#getHostString() hostname}. + *

If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0}, + * the host header will only contain the + * {@linkplain InetSocketAddress#getHostString() hostname}. + * @since 5.0 */ public void setHost(InetSocketAddress host) { - String value = - host.getPort() != 0 ? String.format("%s:%d", host.getHostString(), host.getPort()) : - host.getHostString(); + String value = (host.getPort() != 0 ? + String.format("%s:%d", host.getHostString(), host.getPort()) : host.getHostString()); set(HOST, value); } @@ -822,6 +823,7 @@ public class HttpHeaders implements MultiValueMap, Serializable * Return the value of the required {@code Host} header. *

If the header value does not contain a port, the returned * {@linkplain InetSocketAddress#getPort() port} will be {@code 0}. + * @since 5.0 */ public InetSocketAddress getHost() { String value = getFirst(HOST); diff --git a/spring-web/src/main/java/org/springframework/http/HttpRange.java b/spring-web/src/main/java/org/springframework/http/HttpRange.java index 421c8bd777..02e7f10859 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpRange.java +++ b/spring-web/src/main/java/org/springframework/http/HttpRange.java @@ -56,8 +56,8 @@ public abstract class HttpRange { public ResourceRegion toResourceRegion(Resource resource) { // Don't try to determine contentLength on InputStreamResource - cannot be read afterwards... // Note: custom InputStreamResource subclasses could provide a pre-calculated content length! - Assert.isTrue(InputStreamResource.class != resource.getClass(), - "Can't convert an InputStreamResource to a ResourceRegion"); + Assert.isTrue(resource.getClass() != InputStreamResource.class, + "Cannot convert an InputStreamResource to a ResourceRegion"); try { long contentLength = resource.contentLength(); Assert.isTrue(contentLength > 0, "Resource content length should be > 0"); @@ -163,12 +163,12 @@ public abstract class HttpRange { } /** - * Convert each {@code HttpRange} into a {@code ResourceRegion}, - * selecting the appropriate segment of the given {@code Resource} - * using the HTTP Range information. + * Convert each {@code HttpRange} into a {@code ResourceRegion}, selecting the + * appropriate segment of the given {@code Resource} using HTTP Range information. * @param ranges the list of ranges * @param resource the resource to select the regions from * @return the list of regions for the given resource + * @since 4.3 */ public static List toResourceRegions(List ranges, Resource resource) { if (CollectionUtils.isEmpty(ranges)) { diff --git a/spring-web/src/main/java/org/springframework/http/MediaType.java b/spring-web/src/main/java/org/springframework/http/MediaType.java index ef81ac5ed0..07fec35da5 100644 --- a/spring-web/src/main/java/org/springframework/http/MediaType.java +++ b/spring-web/src/main/java/org/springframework/http/MediaType.java @@ -114,11 +114,13 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code application/pdf}. + * @since 4.3 */ public final static MediaType APPLICATION_PDF; /** * A String equivalent of {@link MediaType#APPLICATION_PDF}. + * @since 4.3 */ public final static String APPLICATION_PDF_VALUE = "application/pdf"; @@ -194,11 +196,13 @@ public class MediaType extends MimeType implements Serializable { /** * Public constant media type for {@code text/markdown}. + * @since 4.3 */ public final static MediaType TEXT_MARKDOWN; /** * A String equivalent of {@link MediaType#TEXT_MARKDOWN}. + * @since 4.3 */ public final static String TEXT_MARKDOWN_VALUE = "text/markdown"; @@ -296,6 +300,7 @@ public class MediaType extends MimeType implements Serializable { * @param other the other media type * @param charset the character set * @throws IllegalArgumentException if any of the parameters contain illegal characters + * @since 4.3 */ public MediaType(MediaType other, Charset charset) { super(other, charset); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketExtension.java b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketExtension.java index 8cf8562661..212a14bde4 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketExtension.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketExtension.java @@ -118,11 +118,11 @@ public class WebSocketExtension { public String toString() { StringBuilder str = new StringBuilder(); str.append(this.name); - for (String param : parameters.keySet()) { + for (Map.Entry entry : this.parameters.entrySet()) { str.append(';'); - str.append(param); + str.append(entry.getKey()); str.append('='); - str.append(this.parameters.get(param)); + str.append(entry.getValue()); } return str.toString(); } @@ -136,16 +136,17 @@ public class WebSocketExtension { * @throws IllegalArgumentException if the string cannot be parsed */ public static List parseExtensions(String extensions) { - if (!StringUtils.hasText(extensions)) { - return Collections.emptyList(); - } - else { - List result = new ArrayList<>(); - for (String token : StringUtils.tokenizeToStringArray(extensions, ",")) { + if (StringUtils.hasText(extensions)) { + String[] tokens = StringUtils.tokenizeToStringArray(extensions, ","); + List result = new ArrayList(tokens.length); + for (String token : tokens) { result.add(parseExtension(token)); } return result; } + else { + return Collections.emptyList(); + } } private static WebSocketExtension parseExtension(String extension) {