From 50c578c5a04c477470cff322f965839a76eeca9b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 28 Nov 2014 20:59:35 +0100 Subject: [PATCH] Polishing --- build.gradle | 42 +++++----- .../DefaultListableBeanFactoryTests.java | 5 +- .../beans/factory/xml/testUtilNamespace.xml | 2 +- .../support/ArrayToArrayConverter.java | 5 +- .../support/ArrayToCollectionConverter.java | 19 +++-- .../support/ArrayToObjectConverter.java | 7 +- .../support/ArrayToStringConverter.java | 6 +- .../convert/support/MapToMapConverter.java | 13 +++- .../support/ObjectToArrayConverter.java | 8 +- .../support/ObjectToCollectionConverter.java | 11 ++- .../support/StringToCollectionConverter.java | 8 +- .../support/StringToEnumConverterFactory.java | 7 +- .../filter/AbstractRequestLoggingFilter.java | 77 ++++++++++--------- 13 files changed, 125 insertions(+), 85 deletions(-) diff --git a/build.gradle b/build.gradle index b94a6080401..ceb4cfd933f 100644 --- a/build.gradle +++ b/build.gradle @@ -441,7 +441,7 @@ project("spring-tx") { compile(project(":spring-beans")) compile(project(":spring-core")) optional(project(":spring-aop")) - optional(project(":spring-context")) // for JCA, @EnableTransactionManagement + optional(project(":spring-context")) // for JCA, @EnableTransactionManagement optional("aopalliance:aopalliance:1.0") optional("javax.transaction:javax.transaction-api:1.2") optional("javax.resource:connector-api:1.5") @@ -516,7 +516,7 @@ project("spring-jdbc") { compile(project(":spring-beans")) compile(project(":spring-core")) compile(project(":spring-tx")) - optional(project(":spring-context")) // for JndiDataSourceLookup + optional(project(":spring-context")) // for JndiDataSourceLookup optional("javax.transaction:javax.transaction-api:1.2") optional("com.mchange:c3p0:0.9.2.1") optional("org.hsqldb:hsqldb:${hsqldbVersion}") @@ -533,8 +533,8 @@ project("spring-context-support") { compile(project(":spring-core")) compile(project(":spring-beans")) compile(project(":spring-context")) - optional(project(":spring-jdbc")) // for Quartz support - optional(project(":spring-tx")) // for Quartz support + optional(project(":spring-jdbc")) // for Quartz support + optional(project(":spring-tx")) // for Quartz support optional("javax.mail:javax.mail-api:1.4.7") optional("javax.cache:cache-api:1.0.0") optional("com.google.guava:guava:17.0") @@ -550,8 +550,8 @@ project("spring-context-support") { exclude group: "xml-apis", module: "xml-apis" } testCompile("org.apache.poi:poi:3.10.1") - testCompile("commons-beanutils:commons-beanutils:1.8.0") // for Velocity/JasperReports - testCompile("commons-digester:commons-digester:1.8.1") // for Velocity/JasperReports + testCompile("commons-beanutils:commons-beanutils:1.8.0") // for Velocity/JasperReports + testCompile("commons-digester:commons-digester:1.8.1") // for Velocity/JasperReports testCompile("org.hsqldb:hsqldb:${hsqldbVersion}") testCompile("org.slf4j:slf4j-api:${slf4jVersion}") testRuntime("com.sun.mail:javax.mail:1.4.7") @@ -565,12 +565,12 @@ project("spring-web") { description = "Spring Web" dependencies { - compile(project(":spring-aop")) // for JaxWsPortProxyFactoryBean - compile(project(":spring-beans")) // for MultiPartFilter + compile(project(":spring-aop")) // for JaxWsPortProxyFactoryBean + compile(project(":spring-beans")) // for MultipartFilter compile(project(":spring-context")) compile(project(":spring-core")) provided("javax.servlet:javax.servlet-api:3.0.1") - optional(project(":spring-oxm")) // for MarshallingHttpMessageConverter + optional(project(":spring-oxm")) // for MarshallingHttpMessageConverter optional("javax.servlet.jsp:javax.servlet.jsp-api:2.2.1") optional("javax.portlet:portlet-api:2.0") optional("javax.el:javax.el-api:2.2.5") @@ -578,7 +578,7 @@ project("spring-web") { optional("aopalliance:aopalliance:1.0") optional("com.caucho:hessian:4.0.7") optional("commons-fileupload:commons-fileupload:1.3.1") - optional("org.apache.httpcomponents:httpclient:4.3.5") + optional("org.apache.httpcomponents:httpclient:4.3.6") optional("org.apache.httpcomponents:httpasyncclient:4.0.2") optional("org.codehaus.jackson:jackson-mapper-asl:${jackson1Version}") optional("com.fasterxml.jackson.core:jackson-databind:${jackson2Version}") @@ -711,8 +711,8 @@ project("spring-webmvc") { compile(project(":spring-expression")) compile(project(":spring-web")) provided("javax.servlet:javax.servlet-api:3.0.1") - optional(project(":spring-context-support")) // for Velocity support - optional(project(":spring-oxm")) // for MarshallingView + optional(project(":spring-context-support")) // for Velocity support + optional(project(":spring-oxm")) // for MarshallingView optional("javax.servlet.jsp:javax.servlet.jsp-api:2.2.1") optional("javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1") optional("net.sourceforge.jexcelapi:jxl:2.6.12") @@ -764,7 +764,7 @@ project("spring-webmvc") { } testCompile("javax.validation:validation-api:1.0.0.GA") testCompile("org.hibernate:hibernate-validator:${hibVal4Version}") - testCompile("org.apache.httpcomponents:httpclient:4.3.5") + testCompile("org.apache.httpcomponents:httpclient:4.3.6") testCompile("commons-fileupload:commons-fileupload:1.3.1") testCompile("commons-io:commons-io:1.3") testCompile("joda-time:joda-time:${jodaVersion}") @@ -915,13 +915,13 @@ project("spring-aspects") { rt("org.aspectj:aspectjrt:${aspectjVersion}") compile("org.aspectj:aspectjweaver:${aspectjVersion}") provided("org.eclipse.persistence:javax.persistence:2.0.0") - optional(project(":spring-aop")) // for @Async support - optional(project(":spring-beans")) // for @Configurable support - optional(project(":spring-context")) // for @Enable* support - optional(project(":spring-context-support")) // for JavaMail support - optional(project(":spring-orm")) // for JPA exception translation support - optional(project(":spring-tx")) // for JPA, @Transactional support - testCompile(project(":spring-core")) // for CodeStyleAspect + optional(project(":spring-aop")) // for @Async support + optional(project(":spring-beans")) // for @Configurable support + optional(project(":spring-context")) // for @Enable* support + optional(project(":spring-context-support")) // for JavaMail support + optional(project(":spring-orm")) // for JPA exception translation support + optional(project(":spring-tx")) // for JPA, @Transactional support + testCompile(project(":spring-core")) // for CodeStyleAspect testCompile(project(":spring-test")) testCompile("javax.mail:javax.mail-api:1.4.7") } @@ -1157,7 +1157,7 @@ configure(rootProject) { } } - // Create an distribution that contains all dependencies (required and optional). + // Create a distribution that contains all dependencies (required and optional). // Not published by default; only for use when building from source. task depsZip(type: Zip, dependsOn: distZip) { zipTask -> group = "Distribution" diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index 484445cd905..4847dfdddf5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -104,9 +104,11 @@ public class DefaultListableBeanFactoryTests { private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class); + @Rule public ExpectedException thrown = ExpectedException.none(); + @Test public void testUnreferencedSingletonWasInstantiated() { KnowsIfInstantiated.clearInstantiationRecord(); @@ -2748,6 +2750,7 @@ public class DefaultListableBeanFactoryTests { } } + @SuppressWarnings("unused") private static class KnowsIfInstantiated { diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml index 2262cd038e7..2b71bca48f5 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/testUtilNamespace.xml @@ -15,7 +15,7 @@ + "/> diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java index bdad578fc5d..df3b730d11b 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java @@ -27,8 +27,9 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter; import org.springframework.util.ObjectUtils; /** - * Converts an Array to another Array. First adapts the source array to a List, then - * delegates to {@link CollectionToArrayConverter} to perform the target array conversion. + * Converts an array to another array. First adapts the source array to a List, + * then delegates to {@link CollectionToArrayConverter} to perform the target + * array conversion. * * @author Keith Donald * @author Phillip Webb diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java index 30f3de5593d..50c5787059c 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -27,23 +27,27 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; /** - * Converts an Array to a Collection. + * Converts an array to a Collection. * - *

First, creates a new Collection of the requested targetType. + *

First, creates a new Collection of the requested target type. * Then adds each array element to the target collection. - * Will perform an element conversion from the source component type to the collection's parameterized type if necessary. + * Will perform an element conversion from the source component type + * to the collection's parameterized type if necessary. * * @author Keith Donald + * @author Juergen Hoeller * @since 3.0 */ final class ArrayToCollectionConverter implements ConditionalGenericConverter { private final ConversionService conversionService; + public ArrayToCollectionConverter(ConversionService conversionService) { this.conversionService = conversionService; } + @Override public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object[].class, Collection.class)); @@ -60,9 +64,12 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter { if (source == null) { return null; } + int length = Array.getLength(source); + TypeDescriptor elementDesc = targetType.getElementTypeDescriptor(); Collection target = CollectionFactory.createCollection(targetType.getType(), length); - if (targetType.getElementTypeDescriptor() == null) { + + if (elementDesc == null) { for (int i = 0; i < length; i++) { Object sourceElement = Array.get(source, i); target.add(sourceElement); @@ -72,7 +79,7 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter { for (int i = 0; i < length; i++) { Object sourceElement = Array.get(source, i); Object targetElement = this.conversionService.convert(sourceElement, - sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor()); + sourceType.elementTypeDescriptor(sourceElement), elementDesc); target.add(targetElement); } } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java index 78d2f93548e..ef92f0bd120 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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,7 +25,8 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; /** - * Converts an Array to an Object by returning the first array element after converting it to the desired targetType. + * Converts an array to an Object by returning the first array element + * after converting it to the desired target type. * * @author Keith Donald * @since 3.0 @@ -34,10 +35,12 @@ final class ArrayToObjectConverter implements ConditionalGenericConverter { private final ConversionService conversionService; + public ArrayToObjectConverter(ConversionService conversionService) { this.conversionService = conversionService; } + @Override public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object[].class, Object.class)); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java index ff9db8ef3f2..80f986a6e44 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java @@ -26,9 +26,9 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter; import org.springframework.util.ObjectUtils; /** - * Converts an Array to a comma-delimited String. - * This implementation first adapts the source Array to a List, - * then delegates to {@link CollectionToStringConverter} to perform the target String conversion. + * Converts an array to a comma-delimited String. First adapts the source array + * to a List, then delegates to {@link CollectionToStringConverter} to perform + * the target String conversion. * * @author Keith Donald * @since 3.0 diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java index 0a300b2588d..cd99976c1f6 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java @@ -36,6 +36,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter; * map's parameterized types K,V if necessary. * * @author Keith Donald + * @author Juergen Hoeller * @since 3.0 */ final class MapToMapConverter implements ConditionalGenericConverter { @@ -64,17 +65,22 @@ final class MapToMapConverter implements ConditionalGenericConverter { if (source == null) { return null; } - boolean copyRequired = !targetType.getType().isInstance(source); Map sourceMap = (Map) source; + + // Shortcut if possible... + boolean copyRequired = !targetType.getType().isInstance(source); if (!copyRequired && sourceMap.isEmpty()) { return sourceMap; } + TypeDescriptor keyDesc = targetType.getMapKeyTypeDescriptor(); + TypeDescriptor valueDesc = targetType.getMapValueTypeDescriptor(); + List targetEntries = new ArrayList(sourceMap.size()); for (Map.Entry entry : sourceMap.entrySet()) { Object sourceKey = entry.getKey(); Object sourceValue = entry.getValue(); - Object targetKey = convertKey(sourceKey, sourceType, targetType.getMapKeyTypeDescriptor()); - Object targetValue = convertValue(sourceValue, sourceType, targetType.getMapValueTypeDescriptor()); + Object targetKey = convertKey(sourceKey, sourceType, keyDesc); + Object targetValue = convertValue(sourceValue, sourceType, valueDesc); targetEntries.add(new MapEntry(targetKey, targetValue)); if (sourceKey != targetKey || sourceValue != targetValue) { copyRequired = true; @@ -83,6 +89,7 @@ final class MapToMapConverter implements ConditionalGenericConverter { if (!copyRequired) { return sourceMap; } + Map targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size()); for (MapEntry entry : targetEntries) { entry.addToMap(targetMap); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java index bf64daf8923..1531926a9e8 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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,8 +25,8 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; /** - * Converts an Object to a single-element Array containing the Object. - * Will convert the Object to the target Array's component type if necessary. + * Converts an Object to a single-element array containing the Object. + * Will convert the Object to the target array's component type if necessary. * * @author Keith Donald * @since 3.0 @@ -35,10 +35,12 @@ final class ObjectToArrayConverter implements ConditionalGenericConverter { private final ConversionService conversionService; + public ObjectToArrayConverter(ConversionService conversionService) { this.conversionService = conversionService; } + @Override public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object.class, Object[].class)); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java index a2e98b321ae..8900ba0c2d9 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -37,10 +37,12 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter { private final ConversionService conversionService; + public ObjectToCollectionConverter(ConversionService conversionService) { this.conversionService = conversionService; } + @Override public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object.class, Collection.class)); @@ -56,12 +58,15 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter { if (source == null) { return null; } + + TypeDescriptor elementDesc = targetType.getElementTypeDescriptor(); Collection target = CollectionFactory.createCollection(targetType.getType(), 1); - if (targetType.getElementTypeDescriptor() == null || targetType.getElementTypeDescriptor().isCollection()) { + + if (elementDesc == null || elementDesc.isCollection()) { target.add(source); } else { - Object singleElement = this.conversionService.convert(source, sourceType, targetType.getElementTypeDescriptor()); + Object singleElement = this.conversionService.convert(source, sourceType, elementDesc); target.add(singleElement); } return target; diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCollectionConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCollectionConverter.java index 9c23d2c7690..15d7df1169f 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToCollectionConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToCollectionConverter.java @@ -32,6 +32,7 @@ import org.springframework.util.StringUtils; * {@code String.class} can be converted to it. * * @author Keith Donald + * @author Juergen Hoeller * @since 3.0 */ final class StringToCollectionConverter implements ConditionalGenericConverter { @@ -61,16 +62,19 @@ final class StringToCollectionConverter implements ConditionalGenericConverter { return null; } String string = (String) source; + String[] fields = StringUtils.commaDelimitedListToStringArray(string); + TypeDescriptor elementDesc = targetType.getElementTypeDescriptor(); Collection target = CollectionFactory.createCollection(targetType.getType(), fields.length); - if (targetType.getElementTypeDescriptor() == null) { + + if (elementDesc == null) { for (String field : fields) { target.add(field.trim()); } } else { for (String field : fields) { - Object targetElement = this.conversionService.convert(field.trim(), sourceType, targetType.getElementTypeDescriptor()); + Object targetElement = this.conversionService.convert(field.trim(), sourceType, elementDesc); target.add(targetElement); } } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java index 7fec5eb48f8..923239b919e 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java @@ -20,12 +20,12 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.ConverterFactory; /** - * Converts from a String to a java.lang.Enum by calling {@link Enum#valueOf(Class, String)}. + * Converts from a String to a {@link java.lang.Enum} by calling {@link Enum#valueOf(Class, String)}. * * @author Keith Donald * @since 3.0 */ -@SuppressWarnings({ "unchecked", "rawtypes" }) +@SuppressWarnings({"unchecked", "rawtypes"}) final class StringToEnumConverterFactory implements ConverterFactory { @Override @@ -35,7 +35,8 @@ final class StringToEnumConverterFactory implements ConverterFactorySubclasses should override the {@code beforeRequest(HttpServletRequest, String)} and - * {@code afterRequest(HttpServletRequest, String)} methods to perform the actual logging around the request. + * {@code afterRequest(HttpServletRequest, String)} methods to perform the actual logging + * around the request. * *

Subclasses are passed the message to write to the log in the {@code beforeRequest} and - * {@code afterRequest} methods. By default, only the URI of the request is logged. However, setting the - * {@code includeQueryString} property to {@code true} will cause the query string of the request to be - * included also. The payload (body) of the request can be logged via the {@code includePayload} flag. Note that - * this will only log that which is read, which might not be the entire payload. + * {@code afterRequest} methods. By default, only the URI of the request is logged. However, + * setting the {@code includeQueryString} property to {@code true} will cause the query string + * of the request to be included also. The payload (body) of the request can be logged via the + * {@code includePayload} flag. Note that this will only log that which is read, which might + * not be the entire payload. * *

Prefixes and suffixes for the before and after messages can be configured using the * {@code beforeMessagePrefix}, {@code afterMessagePrefix}, {@code beforeMessageSuffix} and @@ -87,41 +90,43 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter /** - * Set whether or not the query string should be included in the log message.

Should be configured using an - * {@code <init-param>} for parameter name "includeQueryString" in the filter definition in - * {@code web.xml}. + * Set whether the query string should be included in the log message. + *

Should be configured using an {@code <init-param>} for parameter name + * "includeQueryString" in the filter definition in {@code web.xml}. */ public void setIncludeQueryString(boolean includeQueryString) { this.includeQueryString = includeQueryString; } /** - * Return whether or not the query string should be included in the log message. + * Return whether the query string should be included in the log message. */ protected boolean isIncludeQueryString() { return this.includeQueryString; } /** - * Set whether or not the client address and session id should be included in the log message.

Should be configured - * using an {@code <init-param>} for parameter name "includeClientInfo" in the filter definition in - * {@code web.xml}. + * Set whether the client address and session id should be included in the + * log message. + *

Should be configured using an {@code <init-param>} for parameter name + * "includeClientInfo" in the filter definition in {@code web.xml}. */ public void setIncludeClientInfo(boolean includeClientInfo) { this.includeClientInfo = includeClientInfo; } /** - * Return whether or not the client address and session id should be included in the log message. + * Return whether the client address and session id should be included in the + * log message. */ protected boolean isIncludeClientInfo() { return this.includeClientInfo; } /** - * Set whether or not the request payload (body) should be included in the log message.

Should be configured using - * an {@code <init-param>} for parameter name "includePayload" in the filter definition in - * {@code web.xml}. + * Set whether the request payload (body) should be included in the log message. + *

Should be configured using an {@code <init-param>} for parameter name + * "includePayload" in the filter definition in {@code web.xml}. */ public void setIncludePayload(boolean includePayload) { @@ -129,14 +134,15 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter } /** - * Return whether or not the request payload (body) should be included in the log message. + * Return whether the request payload (body) should be included in the log message. */ protected boolean isIncludePayload() { - return includePayload; + return this.includePayload; } /** - * Sets the maximum length of the payload body to be included in the log message. Default is 50 characters. + * Sets the maximum length of the payload body to be included in the log message. + * Default is 50 characters. */ public void setMaxPayloadLength(int maxPayloadLength) { Assert.isTrue(maxPayloadLength >= 0, "'maxPayloadLength' should be larger than or equal to 0"); @@ -147,32 +153,36 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter * Return the maximum length of the payload body to be included in the log message. */ protected int getMaxPayloadLength() { - return maxPayloadLength; + return this.maxPayloadLength; } /** - * Set the value that should be prepended to the log message written before a request is processed. + * Set the value that should be prepended to the log message written + * before a request is processed. */ public void setBeforeMessagePrefix(String beforeMessagePrefix) { this.beforeMessagePrefix = beforeMessagePrefix; } /** - * Set the value that should be apppended to the log message written before a request is processed. + * Set the value that should be appended to the log message written + * before a request is processed. */ public void setBeforeMessageSuffix(String beforeMessageSuffix) { this.beforeMessageSuffix = beforeMessageSuffix; } /** - * Set the value that should be prepended to the log message written after a request is processed. + * Set the value that should be prepended to the log message written + * after a request is processed. */ public void setAfterMessagePrefix(String afterMessagePrefix) { this.afterMessagePrefix = afterMessagePrefix; } /** - * Set the value that should be appended to the log message written after a request is processed. + * Set the value that should be appended to the log message written + * after a request is processed. */ public void setAfterMessageSuffix(String afterMessageSuffix) { this.afterMessageSuffix = afterMessageSuffix; @@ -200,22 +210,21 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter throws ServletException, IOException { boolean isFirstRequest = !isAsyncDispatch(request); + HttpServletRequest requestToUse = request; - if (isIncludePayload()) { - if (isFirstRequest) { - request = new RequestCachingRequestWrapper(request); - } + if (isIncludePayload() && isFirstRequest) { + requestToUse = new RequestCachingRequestWrapper(request); } if (isFirstRequest) { - beforeRequest(request, getBeforeMessage(request)); + beforeRequest(requestToUse, getBeforeMessage(requestToUse)); } try { - filterChain.doFilter(request, response); + filterChain.doFilter(requestToUse, response); } finally { - if (!isAsyncStarted(request)) { - afterRequest(request, getAfterMessage(request)); + if (!isAsyncStarted(requestToUse)) { + afterRequest(requestToUse, getAfterMessage(requestToUse)); } } } @@ -355,9 +364,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter } return ch; } - } - } }