From dbd5f67498741f92dbfe2b8dd2b5d5d697a185eb Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 25 Mar 2014 00:35:33 +0100 Subject: [PATCH] Consistently applied appropriate ByteArrayOutputStream initial capacities across the codebase Issue: SPR-11594 (cherry picked from commit dd7f54c) --- .../support/PropertiesToStringConverter.java | 4 +- .../support/SerializingConverter.java | 4 +- .../core/style/ToStringCreator.java | 8 ++-- .../util/SerializationUtils.java | 4 +- .../MappingJackson2MessageConverter.java | 4 +- .../MappingJacksonMessageConverter.java | 4 +- .../MarshallingMessageConverter.java | 4 +- .../oxm/jibx/JibxMarshaller.java | 8 ++-- .../mock/http/MockHttpOutputMessage.java | 6 +-- .../mock/web/MockHttpServletResponse.java | 11 ++--- .../mock/web/portlet/MockMimeResponse.java | 27 +++++------ .../AbstractBufferingClientHttpRequest.java | 8 ++-- .../ByteArrayHttpMessageConverter.java | 13 ++--- .../http/server/ServletServerHttpRequest.java | 8 ++-- .../filter/AbstractRequestLoggingFilter.java | 48 ++++++++++--------- .../web/filter/ShallowEtagHeaderFilter.java | 35 +++++++------- .../web/servlet/view/xml/MarshallingView.java | 2 +- 17 files changed, 101 insertions(+), 97 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/PropertiesToStringConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/PropertiesToStringConverter.java index 8902d0e9d3..f4962d016f 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/PropertiesToStringConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/PropertiesToStringConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 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. @@ -33,7 +33,7 @@ final class PropertiesToStringConverter implements Converter public String convert(Properties source) { try { - ByteArrayOutputStream os = new ByteArrayOutputStream(); + ByteArrayOutputStream os = new ByteArrayOutputStream(256); source.store(os, null); return os.toString("ISO-8859-1"); } diff --git a/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java b/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java index 3ae7ca3b12..b9909f61e5 100644 --- a/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java +++ b/spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 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. @@ -56,7 +56,7 @@ public class SerializingConverter implements Converter { * Serializes the source object and returns the byte array result. */ public byte[] convert(Object source) { - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(256); try { this.serializer.serialize(source, byteStream); return byteStream.toByteArray(); diff --git a/spring-core/src/main/java/org/springframework/core/style/ToStringCreator.java b/spring-core/src/main/java/org/springframework/core/style/ToStringCreator.java index 5871f8dfcc..6f682939cc 100644 --- a/spring-core/src/main/java/org/springframework/core/style/ToStringCreator.java +++ b/spring-core/src/main/java/org/springframework/core/style/ToStringCreator.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. @@ -36,11 +36,11 @@ public class ToStringCreator { new DefaultToStringStyler(StylerUtils.DEFAULT_VALUE_STYLER); - private StringBuilder buffer = new StringBuilder(512); + private final StringBuilder buffer = new StringBuilder(256); - private ToStringStyler styler; + private final ToStringStyler styler; - private Object object; + private final Object object; private boolean styledFirstField; diff --git a/spring-core/src/main/java/org/springframework/util/SerializationUtils.java b/spring-core/src/main/java/org/springframework/util/SerializationUtils.java index 943996702d..f85b1c72dd 100644 --- a/spring-core/src/main/java/org/springframework/util/SerializationUtils.java +++ b/spring-core/src/main/java/org/springframework/util/SerializationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 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. @@ -39,7 +39,7 @@ public abstract class SerializationUtils { if (object == null) { return null; } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java index dd3d4c0437..c9c0cea6f3 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.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. @@ -213,7 +213,7 @@ public class MappingJackson2MessageConverter implements MessageConverter, BeanCl protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); objectMapper.writeValue(writer, object); diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJacksonMessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJacksonMessageConverter.java index f09761410c..759e5f0c37 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJacksonMessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJacksonMessageConverter.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. @@ -214,7 +214,7 @@ public class MappingJacksonMessageConverter implements MessageConverter, BeanCla protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); objectMapper.writeValue(writer, object); diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java index 9378d21c47..963f2d9293 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.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. @@ -228,7 +228,7 @@ public class MarshallingMessageConverter implements MessageConverter, Initializi protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); StreamResult streamResult = new StreamResult(bos); marshaller.marshal(object, streamResult); BytesMessage message = session.createBytesMessage(); diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index b841c5d688..c30fce3ab4 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -128,6 +128,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe public void setTargetPackage(String targetPackage) { this.targetPackage = targetPackage; } + /** * Set the optional binding name for this instance. */ @@ -333,7 +334,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe private void transformAndMarshal(Object graph, Result result) throws IOException { try { - ByteArrayOutputStream os = new ByteArrayOutputStream(); + ByteArrayOutputStream os = new ByteArrayOutputStream(1024); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = this.transformerFactory.newTransformer(); @@ -341,7 +342,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } catch (TransformerException ex) { throw new MarshallingFailureException( - "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]"); + "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex); } } @@ -395,6 +396,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } } + // Unsupported Unmarshalling @Override @@ -420,7 +422,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } - ByteArrayOutputStream os = new ByteArrayOutputStream(); + ByteArrayOutputStream os = new ByteArrayOutputStream(1024); transformer.transform(source, new StreamResult(os)); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); return unmarshalInputStream(is); diff --git a/spring-test/src/main/java/org/springframework/mock/http/MockHttpOutputMessage.java b/spring-test/src/main/java/org/springframework/mock/http/MockHttpOutputMessage.java index 43fa1b3e7e..43ee32f319 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/MockHttpOutputMessage.java +++ b/spring-test/src/main/java/org/springframework/mock/http/MockHttpOutputMessage.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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.mock.http; import java.io.ByteArrayOutputStream; @@ -36,7 +37,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage { private final HttpHeaders headers = new HttpHeaders(); - private final ByteArrayOutputStream body = new ByteArrayOutputStream(); + private final ByteArrayOutputStream body = new ByteArrayOutputStream(1024); /** @@ -74,7 +75,6 @@ public class MockHttpOutputMessage implements HttpOutputMessage { public String getBodyAsString(Charset charset) { byte[] bytes = getBodyAsBytes(); try { - // Use return new String(bytes, charset.name()); } catch (UnsupportedEncodingException ex) { diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 75a7a1fc3b..b575c402f7 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.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. @@ -69,7 +69,7 @@ public class MockHttpServletResponse implements HttpServletResponse { private boolean charset = false; - private final ByteArrayOutputStream content = new ByteArrayOutputStream(); + private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content); @@ -183,8 +183,8 @@ public class MockHttpServletResponse implements HttpServletResponse { public String getContentAsString() throws UnsupportedEncodingException { flushBuffer(); - return (this.characterEncoding != null) ? - this.content.toString(this.characterEncoding) : this.content.toString(); + return (this.characterEncoding != null ? + this.content.toString(this.characterEncoding) : this.content.toString()); } public void setContentLength(int contentLength) { @@ -201,8 +201,7 @@ public class MockHttpServletResponse implements HttpServletResponse { if (contentType != null) { int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); if (charsetIndex != -1) { - String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); - this.characterEncoding = encoding; + this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.charset = true; } updateContentTypeHeader(); diff --git a/spring-test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java b/spring-test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java index e606fc808d..4944ab64ac 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/portlet/MockMimeResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 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. @@ -56,7 +56,7 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons private int bufferSize = 4096; - private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024); private final CacheControl cacheControl = new MockCacheControl(); @@ -126,9 +126,9 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons public PrintWriter getWriter() throws UnsupportedEncodingException { if (this.writer == null) { - Writer targetWriter = (this.characterEncoding != null - ? new OutputStreamWriter(this.outputStream, this.characterEncoding) - : new OutputStreamWriter(this.outputStream)); + Writer targetWriter = (this.characterEncoding != null ? + new OutputStreamWriter(this.outputStream, this.characterEncoding) : + new OutputStreamWriter(this.outputStream)); this.writer = new PrintWriter(targetWriter); } return this.writer; @@ -141,9 +141,8 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons public String getContentAsString() throws UnsupportedEncodingException { flushBuffer(); - return (this.characterEncoding != null) - ? this.outputStream.toString(this.characterEncoding) - : this.outputStream.toString(); + return (this.characterEncoding != null ? + this.outputStream.toString(this.characterEncoding) : this.outputStream.toString()); } public void setLocale(Locale locale) { @@ -166,13 +165,11 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons if (this.writer != null) { this.writer.flush(); } - if (this.outputStream != null) { - try { - this.outputStream.flush(); - } - catch (IOException ex) { - throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage()); - } + try { + this.outputStream.flush(); + } + catch (IOException ex) { + throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage()); } this.committed = true; } diff --git a/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java index 3048b76009..4af7348798 100644 --- a/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 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. @@ -23,14 +23,16 @@ import java.io.OutputStream; import org.springframework.http.HttpHeaders; /** - * Abstract base for {@link ClientHttpRequest} that buffers output in a byte array before sending it over the wire. + * Base implementation of {@link ClientHttpRequest} that buffers output + * in a byte array before sending it over the wire. * * @author Arjen Poutsma * @since 3.0.6 */ abstract class AbstractBufferingClientHttpRequest extends AbstractClientHttpRequest { - private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream(); + private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream(1024); + @Override protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException { diff --git a/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java index d1b25f7c35..800a9febaa 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.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. @@ -27,9 +27,9 @@ import org.springframework.util.StreamUtils; /** * Implementation of {@link HttpMessageConverter} that can read and write byte arrays. * - *

By default, this converter supports all media types ({@code */*}), and writes with a {@code - * Content-Type} of {@code application/octet-stream}. This can be overridden by setting the {@link - * #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. + *

By default, this converter supports all media types ({@code */*}), and + * writes with a {@code Content-Type} of {@code application/octet-stream}. This can be + * overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property. * * @author Arjen Poutsma * @since 3.0 @@ -47,9 +47,10 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter< } @Override - public byte[] readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { + public byte[] readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException { long contentLength = inputMessage.getHeaders().getContentLength(); - ByteArrayOutputStream bos = new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE); + ByteArrayOutputStream bos = + new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE); StreamUtils.copy(inputMessage.getBody(), bos); return bos.toByteArray(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 5dd0dbd424..a7a03ea76b 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.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. @@ -32,7 +32,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; - import javax.servlet.http.HttpServletRequest; import org.springframework.http.HttpHeaders; @@ -54,6 +53,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { private static final String METHOD_POST = "POST"; + private final HttpServletRequest servletRequest; private HttpHeaders headers; @@ -64,7 +64,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { * @param servletRequest the servlet request */ public ServletServerHttpRequest(HttpServletRequest servletRequest) { - Assert.notNull(servletRequest, "'servletRequest' must not be null"); + Assert.notNull(servletRequest, "HttpServletRequest must not be null"); this.servletRequest = servletRequest; } @@ -144,7 +144,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { * to access a parameter thus causing the input stream to be "consumed". */ private InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); Writer writer = new OutputStreamWriter(bos, FORM_CHARSET); Map form = request.getParameterMap(); diff --git a/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java b/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java index 03c15891d7..b68f10e8d7 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.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. @@ -21,7 +21,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; - import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; @@ -53,9 +52,9 @@ import org.springframework.web.util.WebUtils; * @author Rob Harrop * @author Juergen Hoeller * @author Rossen Stoyanchev + * @since 1.2.5 * @see #beforeRequest * @see #afterRequest - * @since 1.2.5 */ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter { @@ -69,6 +68,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 50; + private boolean includeQueryString = false; private boolean includeClientInfo = false; @@ -85,6 +85,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter private String afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX; + /** * 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 @@ -177,6 +178,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter this.afterMessageSuffix = afterMessageSuffix; } + /** * The default value is "false" so that the filter may log a "before" message * at the start of request processing and an "after" message at the end from @@ -188,9 +190,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter } /** - * Forwards the request to the next filter in the chain and delegates down to the subclasses to perform the actual - * request logging both before and after the request is processed. - * + * Forwards the request to the next filter in the chain and delegates down to the subclasses + * to perform the actual request logging both before and after the request is processed. * @see #beforeRequest * @see #afterRequest */ @@ -221,7 +222,6 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter /** * Get the message to write to the log before the request. - * * @see #createMessage */ private String getBeforeMessage(HttpServletRequest request) { @@ -230,7 +230,6 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter /** * Get the message to write to the log after the request. - * * @see #createMessage */ private String getAfterMessage(HttpServletRequest request) { @@ -238,10 +237,12 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter } /** - * Create a log message for the given request, prefix and suffix.

If {@code includeQueryString} is - * {@code true} then the inner part of the log message will take the form {@code request_uri?query_string} - * otherwise the message will simply be of the form {@code request_uri}.

The final message is composed of the - * inner part as described and the supplied prefix and suffix. + * Create a log message for the given request, prefix and suffix. + *

If {@code includeQueryString} is {@code true}, then the inner part + * of the log message will take the form {@code request_uri?query_string}; + * otherwise the message will simply be of the form {@code request_uri}. + *

The final message is composed of the inner part as described and + * the supplied prefix and suffix. */ protected String createMessage(HttpServletRequest request, String prefix, String suffix) { StringBuilder msg = new StringBuilder(); @@ -285,16 +286,16 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter } /** - * Concrete subclasses should implement this method to write a log message before the request is processed. - * + * Concrete subclasses should implement this method to write a log message + * before the request is processed. * @param request current HTTP request * @param message the message to log */ protected abstract void beforeRequest(HttpServletRequest request, String message); /** - * Concrete subclasses should implement this method to write a log message after the request is processed. - * + * Concrete subclasses should implement this method to write a log message + * after the request is processed. * @param request current HTTP request * @param message the message to log */ @@ -303,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper { - private final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + private final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); private final ServletInputStream inputStream; @@ -316,19 +317,19 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter @Override public ServletInputStream getInputStream() throws IOException { - return inputStream; + return this.inputStream; } @Override public String getCharacterEncoding() { - return super.getCharacterEncoding() != null ? super.getCharacterEncoding() : - WebUtils.DEFAULT_CHARACTER_ENCODING; + String enc = super.getCharacterEncoding(); + return (enc != null ? enc : WebUtils.DEFAULT_CHARACTER_ENCODING); } @Override public BufferedReader getReader() throws IOException { if (this.reader == null) { - this.reader = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding())); + this.reader = new BufferedReader(new InputStreamReader(this.inputStream, getCharacterEncoding())); } return this.reader; } @@ -337,17 +338,18 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter return this.bos.toByteArray(); } + private class RequestCachingInputStream extends ServletInputStream { private final ServletInputStream is; - private RequestCachingInputStream(ServletInputStream is) { + public RequestCachingInputStream(ServletInputStream is) { this.is = is; } @Override public int read() throws IOException { - int ch = is.read(); + int ch = this.is.read(); if (ch != -1) { bos.write(ch); } diff --git a/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java b/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java index 620b326f0f..f68e7d6c66 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java @@ -34,12 +34,14 @@ import org.springframework.util.StreamUtils; import org.springframework.web.util.WebUtils; /** - * {@link javax.servlet.Filter} that generates an {@code ETag} value based on the content on the response. - * This ETag is compared to the {@code If-None-Match} header of the request. If these headers are equal, - * the response content is not sent, but rather a {@code 304 "Not Modified"} status instead. + * {@link javax.servlet.Filter} that generates an {@code ETag} value based on the + * content on the response. This ETag is compared to the {@code If-None-Match} + * header of the request. If these headers are equal, the response content is + * not sent, but rather a {@code 304 "Not Modified"} status instead. * - *

Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View}) - * is still rendered. As such, this filter only saves bandwidth, not server performance. + *

Since the ETag is based on the response content, the response + * (e.g. a {@link org.springframework.web.servlet.View}) is still rendered. + * As such, this filter only saves bandwidth, not server performance. * * @author Arjen Poutsma * @author Rossen Stoyanchev @@ -77,12 +79,11 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { } private void updateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException { - - ShallowEtagResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ShallowEtagResponseWrapper.class); + ShallowEtagResponseWrapper responseWrapper = + WebUtils.getNativeResponse(response, ShallowEtagResponseWrapper.class); Assert.notNull(responseWrapper, "ShallowEtagResponseWrapper not found"); response = (HttpServletResponse) responseWrapper.getResponse(); - byte[] body = responseWrapper.toByteArray(); int statusCode = responseWrapper.getStatusCode(); @@ -157,7 +158,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { */ private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper { - private final ByteArrayOutputStream content = new ByteArrayOutputStream(); + private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); private final ServletOutputStream outputStream = new ResponseServletOutputStream(); @@ -213,22 +214,22 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { return this.writer; } - @Override - public void resetBuffer() { - this.content.reset(); - } - @Override public void reset() { super.reset(); resetBuffer(); } - private int getStatusCode() { - return statusCode; + @Override + public void resetBuffer() { + this.content.reset(); } - private byte[] toByteArray() { + public int getStatusCode() { + return this.statusCode; + } + + public byte[] toByteArray() { return this.content.toByteArray(); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java index 7a0c5ab622..748ea07d34 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java @@ -105,7 +105,7 @@ public class MarshallingView extends AbstractView { if (toBeMarshalled == null) { throw new ServletException("Unable to locate object to be marshalled in model: " + model); } - ByteArrayOutputStream bos = new ByteArrayOutputStream(2048); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); this.marshaller.marshal(toBeMarshalled, new StreamResult(bos)); setResponseContentType(request, response);