Consistently applied appropriate ByteArrayOutputStream initial capacities across the codebase
Issue: SPR-11594
(cherry picked from commit dd7f54c)
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -33,7 +33,7 @@ final class PropertiesToStringConverter implements Converter<Properties, String>
|
|||||||
|
|
||||||
public String convert(Properties source) {
|
public String convert(Properties source) {
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
ByteArrayOutputStream os = new ByteArrayOutputStream(256);
|
||||||
source.store(os, null);
|
source.store(os, null);
|
||||||
return os.toString("ISO-8859-1");
|
return os.toString("ISO-8859-1");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -56,7 +56,7 @@ public class SerializingConverter implements Converter<Object, byte[]> {
|
|||||||
* Serializes the source object and returns the byte array result.
|
* Serializes the source object and returns the byte array result.
|
||||||
*/
|
*/
|
||||||
public byte[] convert(Object source) {
|
public byte[] convert(Object source) {
|
||||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
|
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(256);
|
||||||
try {
|
try {
|
||||||
this.serializer.serialize(source, byteStream);
|
this.serializer.serialize(source, byteStream);
|
||||||
return byteStream.toByteArray();
|
return byteStream.toByteArray();
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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);
|
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;
|
private boolean styledFirstField;
|
||||||
|
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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) {
|
if (object == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
|
||||||
try {
|
try {
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||||
oos.writeObject(object);
|
oos.writeObject(object);
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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)
|
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper)
|
||||||
throws JMSException, IOException {
|
throws JMSException, IOException {
|
||||||
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
|
OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
|
||||||
objectMapper.writeValue(writer, object);
|
objectMapper.writeValue(writer, object);
|
||||||
|
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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)
|
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper)
|
||||||
throws JMSException, IOException {
|
throws JMSException, IOException {
|
||||||
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
|
OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
|
||||||
objectMapper.writeValue(writer, object);
|
objectMapper.writeValue(writer, object);
|
||||||
|
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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)
|
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller)
|
||||||
throws JMSException, IOException, XmlMappingException {
|
throws JMSException, IOException, XmlMappingException {
|
||||||
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
||||||
StreamResult streamResult = new StreamResult(bos);
|
StreamResult streamResult = new StreamResult(bos);
|
||||||
marshaller.marshal(object, streamResult);
|
marshaller.marshal(object, streamResult);
|
||||||
BytesMessage message = session.createBytesMessage();
|
BytesMessage message = session.createBytesMessage();
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
|||||||
public void setTargetPackage(String targetPackage) {
|
public void setTargetPackage(String targetPackage) {
|
||||||
this.targetPackage = targetPackage;
|
this.targetPackage = targetPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the optional binding name for this instance.
|
* 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 {
|
private void transformAndMarshal(Object graph, Result result) throws IOException {
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
|
||||||
marshalOutputStream(graph, os);
|
marshalOutputStream(graph, os);
|
||||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||||
Transformer transformer = this.transformerFactory.newTransformer();
|
Transformer transformer = this.transformerFactory.newTransformer();
|
||||||
@@ -341,7 +342,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
|||||||
}
|
}
|
||||||
catch (TransformerException ex) {
|
catch (TransformerException ex) {
|
||||||
throw new MarshallingFailureException(
|
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
|
// Unsupported Unmarshalling
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -420,7 +422,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
|||||||
if (encoding != null) {
|
if (encoding != null) {
|
||||||
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
|
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
|
||||||
}
|
}
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
|
||||||
transformer.transform(source, new StreamResult(os));
|
transformer.transform(source, new StreamResult(os));
|
||||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||||
return unmarshalInputStream(is);
|
return unmarshalInputStream(is);
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.mock.http;
|
package org.springframework.mock.http;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -36,7 +37,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
|
|||||||
|
|
||||||
private final HttpHeaders headers = new HttpHeaders();
|
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) {
|
public String getBodyAsString(Charset charset) {
|
||||||
byte[] bytes = getBodyAsBytes();
|
byte[] bytes = getBodyAsBytes();
|
||||||
try {
|
try {
|
||||||
// Use
|
|
||||||
return new String(bytes, charset.name());
|
return new String(bytes, charset.name());
|
||||||
}
|
}
|
||||||
catch (UnsupportedEncodingException ex) {
|
catch (UnsupportedEncodingException ex) {
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 boolean charset = false;
|
||||||
|
|
||||||
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
|
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
|
||||||
|
|
||||||
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
|
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
|
||||||
|
|
||||||
@@ -183,8 +183,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||||||
|
|
||||||
public String getContentAsString() throws UnsupportedEncodingException {
|
public String getContentAsString() throws UnsupportedEncodingException {
|
||||||
flushBuffer();
|
flushBuffer();
|
||||||
return (this.characterEncoding != null) ?
|
return (this.characterEncoding != null ?
|
||||||
this.content.toString(this.characterEncoding) : this.content.toString();
|
this.content.toString(this.characterEncoding) : this.content.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContentLength(int contentLength) {
|
public void setContentLength(int contentLength) {
|
||||||
@@ -201,8 +201,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||||||
if (contentType != null) {
|
if (contentType != null) {
|
||||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||||
if (charsetIndex != -1) {
|
if (charsetIndex != -1) {
|
||||||
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||||
this.characterEncoding = encoding;
|
|
||||||
this.charset = true;
|
this.charset = true;
|
||||||
}
|
}
|
||||||
updateContentTypeHeader();
|
updateContentTypeHeader();
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 int bufferSize = 4096;
|
||||||
|
|
||||||
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
|
||||||
|
|
||||||
private final CacheControl cacheControl = new MockCacheControl();
|
private final CacheControl cacheControl = new MockCacheControl();
|
||||||
|
|
||||||
@@ -126,9 +126,9 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
|||||||
|
|
||||||
public PrintWriter getWriter() throws UnsupportedEncodingException {
|
public PrintWriter getWriter() throws UnsupportedEncodingException {
|
||||||
if (this.writer == null) {
|
if (this.writer == null) {
|
||||||
Writer targetWriter = (this.characterEncoding != null
|
Writer targetWriter = (this.characterEncoding != null ?
|
||||||
? new OutputStreamWriter(this.outputStream, this.characterEncoding)
|
new OutputStreamWriter(this.outputStream, this.characterEncoding) :
|
||||||
: new OutputStreamWriter(this.outputStream));
|
new OutputStreamWriter(this.outputStream));
|
||||||
this.writer = new PrintWriter(targetWriter);
|
this.writer = new PrintWriter(targetWriter);
|
||||||
}
|
}
|
||||||
return this.writer;
|
return this.writer;
|
||||||
@@ -141,9 +141,8 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
|||||||
|
|
||||||
public String getContentAsString() throws UnsupportedEncodingException {
|
public String getContentAsString() throws UnsupportedEncodingException {
|
||||||
flushBuffer();
|
flushBuffer();
|
||||||
return (this.characterEncoding != null)
|
return (this.characterEncoding != null ?
|
||||||
? this.outputStream.toString(this.characterEncoding)
|
this.outputStream.toString(this.characterEncoding) : this.outputStream.toString());
|
||||||
: this.outputStream.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocale(Locale locale) {
|
public void setLocale(Locale locale) {
|
||||||
@@ -166,13 +165,11 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
|||||||
if (this.writer != null) {
|
if (this.writer != null) {
|
||||||
this.writer.flush();
|
this.writer.flush();
|
||||||
}
|
}
|
||||||
if (this.outputStream != null) {
|
try {
|
||||||
try {
|
this.outputStream.flush();
|
||||||
this.outputStream.flush();
|
}
|
||||||
}
|
catch (IOException ex) {
|
||||||
catch (IOException ex) {
|
throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage());
|
||||||
throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.committed = true;
|
this.committed = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
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
|
* @author Arjen Poutsma
|
||||||
* @since 3.0.6
|
* @since 3.0.6
|
||||||
*/
|
*/
|
||||||
abstract class AbstractBufferingClientHttpRequest extends AbstractClientHttpRequest {
|
abstract class AbstractBufferingClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
|
|
||||||
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream();
|
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream(1024);
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
|
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.
|
* Implementation of {@link HttpMessageConverter} that can read and write byte arrays.
|
||||||
*
|
*
|
||||||
* <p>By default, this converter supports all media types ({@code */*}), and writes with a {@code
|
* <p>By default, this converter supports all media types ({@code */*}), and
|
||||||
* Content-Type} of {@code application/octet-stream}. This can be overridden by setting the {@link
|
* writes with a {@code Content-Type} of {@code application/octet-stream}. This can be
|
||||||
* #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
|
* overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
@@ -47,9 +47,10 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException {
|
public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
|
||||||
long contentLength = inputMessage.getHeaders().getContentLength();
|
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);
|
StreamUtils.copy(inputMessage.getBody(), bos);
|
||||||
return bos.toByteArray();
|
return bos.toByteArray();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
@@ -54,6 +53,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
|
|||||||
|
|
||||||
private static final String METHOD_POST = "POST";
|
private static final String METHOD_POST = "POST";
|
||||||
|
|
||||||
|
|
||||||
private final HttpServletRequest servletRequest;
|
private final HttpServletRequest servletRequest;
|
||||||
|
|
||||||
private HttpHeaders headers;
|
private HttpHeaders headers;
|
||||||
@@ -64,7 +64,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
|
|||||||
* @param servletRequest the servlet request
|
* @param servletRequest the servlet request
|
||||||
*/
|
*/
|
||||||
public ServletServerHttpRequest(HttpServletRequest servletRequest) {
|
public ServletServerHttpRequest(HttpServletRequest servletRequest) {
|
||||||
Assert.notNull(servletRequest, "'servletRequest' must not be null");
|
Assert.notNull(servletRequest, "HttpServletRequest must not be null");
|
||||||
this.servletRequest = servletRequest;
|
this.servletRequest = servletRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +144,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
|
|||||||
* to access a parameter thus causing the input stream to be "consumed".
|
* to access a parameter thus causing the input stream to be "consumed".
|
||||||
*/
|
*/
|
||||||
private InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException {
|
private InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException {
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
||||||
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
|
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
|
||||||
|
|
||||||
Map<String, String[]> form = request.getParameterMap();
|
Map<String, String[]> form = request.getParameterMap();
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletInputStream;
|
import javax.servlet.ServletInputStream;
|
||||||
@@ -53,9 +52,9 @@ import org.springframework.web.util.WebUtils;
|
|||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @since 1.2.5
|
||||||
* @see #beforeRequest
|
* @see #beforeRequest
|
||||||
* @see #afterRequest
|
* @see #afterRequest
|
||||||
* @since 1.2.5
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter {
|
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 static final int DEFAULT_MAX_PAYLOAD_LENGTH = 50;
|
||||||
|
|
||||||
|
|
||||||
private boolean includeQueryString = false;
|
private boolean includeQueryString = false;
|
||||||
|
|
||||||
private boolean includeClientInfo = false;
|
private boolean includeClientInfo = false;
|
||||||
@@ -85,6 +85,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
|||||||
|
|
||||||
private String afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX;
|
private String afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether or not the query string should be included in the log message. <p>Should be configured using an
|
* Set whether or not the query string should be included in the log message. <p>Should be configured using an
|
||||||
* {@code <init-param>} for parameter name "includeQueryString" in the filter definition in
|
* {@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;
|
this.afterMessageSuffix = afterMessageSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default value is "false" so that the filter may log a "before" message
|
* 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
|
* 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
|
* Forwards the request to the next filter in the chain and delegates down to the subclasses
|
||||||
* request logging both before and after the request is processed.
|
* to perform the actual request logging both before and after the request is processed.
|
||||||
*
|
|
||||||
* @see #beforeRequest
|
* @see #beforeRequest
|
||||||
* @see #afterRequest
|
* @see #afterRequest
|
||||||
*/
|
*/
|
||||||
@@ -221,7 +222,6 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message to write to the log before the request.
|
* Get the message to write to the log before the request.
|
||||||
*
|
|
||||||
* @see #createMessage
|
* @see #createMessage
|
||||||
*/
|
*/
|
||||||
private String getBeforeMessage(HttpServletRequest request) {
|
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.
|
* Get the message to write to the log after the request.
|
||||||
*
|
|
||||||
* @see #createMessage
|
* @see #createMessage
|
||||||
*/
|
*/
|
||||||
private String getAfterMessage(HttpServletRequest request) {
|
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. <p>If {@code includeQueryString} is
|
* Create a log message for the given request, prefix and suffix.
|
||||||
* {@code true} then the inner part of the log message will take the form {@code request_uri?query_string}
|
* <p>If {@code includeQueryString} is {@code true}, then the inner part
|
||||||
* otherwise the message will simply be of the form {@code request_uri}. <p>The final message is composed of the
|
* of the log message will take the form {@code request_uri?query_string};
|
||||||
* inner part as described and the supplied prefix and suffix.
|
* otherwise the message will simply be of the form {@code request_uri}.
|
||||||
|
* <p>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) {
|
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
|
||||||
StringBuilder msg = new StringBuilder();
|
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 <i>before</i> the request is processed.
|
* Concrete subclasses should implement this method to write a log message
|
||||||
*
|
* <i>before</i> the request is processed.
|
||||||
* @param request current HTTP request
|
* @param request current HTTP request
|
||||||
* @param message the message to log
|
* @param message the message to log
|
||||||
*/
|
*/
|
||||||
protected abstract void beforeRequest(HttpServletRequest request, String message);
|
protected abstract void beforeRequest(HttpServletRequest request, String message);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Concrete subclasses should implement this method to write a log message <i>after</i> the request is processed.
|
* Concrete subclasses should implement this method to write a log message
|
||||||
*
|
* <i>after</i> the request is processed.
|
||||||
* @param request current HTTP request
|
* @param request current HTTP request
|
||||||
* @param message the message to log
|
* @param message the message to log
|
||||||
*/
|
*/
|
||||||
@@ -303,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
|||||||
|
|
||||||
private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper {
|
private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper {
|
||||||
|
|
||||||
private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
private final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
|
||||||
|
|
||||||
private final ServletInputStream inputStream;
|
private final ServletInputStream inputStream;
|
||||||
|
|
||||||
@@ -316,19 +317,19 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServletInputStream getInputStream() throws IOException {
|
public ServletInputStream getInputStream() throws IOException {
|
||||||
return inputStream;
|
return this.inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCharacterEncoding() {
|
public String getCharacterEncoding() {
|
||||||
return super.getCharacterEncoding() != null ? super.getCharacterEncoding() :
|
String enc = super.getCharacterEncoding();
|
||||||
WebUtils.DEFAULT_CHARACTER_ENCODING;
|
return (enc != null ? enc : WebUtils.DEFAULT_CHARACTER_ENCODING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BufferedReader getReader() throws IOException {
|
public BufferedReader getReader() throws IOException {
|
||||||
if (this.reader == null) {
|
if (this.reader == null) {
|
||||||
this.reader = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding()));
|
this.reader = new BufferedReader(new InputStreamReader(this.inputStream, getCharacterEncoding()));
|
||||||
}
|
}
|
||||||
return this.reader;
|
return this.reader;
|
||||||
}
|
}
|
||||||
@@ -337,17 +338,18 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
|
|||||||
return this.bos.toByteArray();
|
return this.bos.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private class RequestCachingInputStream extends ServletInputStream {
|
private class RequestCachingInputStream extends ServletInputStream {
|
||||||
|
|
||||||
private final ServletInputStream is;
|
private final ServletInputStream is;
|
||||||
|
|
||||||
private RequestCachingInputStream(ServletInputStream is) {
|
public RequestCachingInputStream(ServletInputStream is) {
|
||||||
this.is = is;
|
this.is = is;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
int ch = is.read();
|
int ch = this.is.read();
|
||||||
if (ch != -1) {
|
if (ch != -1) {
|
||||||
bos.write(ch);
|
bos.write(ch);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,12 +34,14 @@ import org.springframework.util.StreamUtils;
|
|||||||
import org.springframework.web.util.WebUtils;
|
import org.springframework.web.util.WebUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link javax.servlet.Filter} that generates an {@code ETag} value based on the content on the response.
|
* {@link javax.servlet.Filter} that generates an {@code ETag} value based on the
|
||||||
* This ETag is compared to the {@code If-None-Match} header of the request. If these headers are equal,
|
* content on the response. This ETag is compared to the {@code If-None-Match}
|
||||||
* the response content is not sent, but rather a {@code 304 "Not Modified"} status instead.
|
* header of the request. If these headers are equal, the response content is
|
||||||
|
* not sent, but rather a {@code 304 "Not Modified"} status instead.
|
||||||
*
|
*
|
||||||
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View})
|
* <p>Since the ETag is based on the response content, the response
|
||||||
* is still rendered. As such, this filter only saves bandwidth, not server performance.
|
* (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 Arjen Poutsma
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
@@ -77,12 +79,11 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
private void updateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||||
|
ShallowEtagResponseWrapper responseWrapper =
|
||||||
ShallowEtagResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ShallowEtagResponseWrapper.class);
|
WebUtils.getNativeResponse(response, ShallowEtagResponseWrapper.class);
|
||||||
Assert.notNull(responseWrapper, "ShallowEtagResponseWrapper not found");
|
Assert.notNull(responseWrapper, "ShallowEtagResponseWrapper not found");
|
||||||
|
|
||||||
response = (HttpServletResponse) responseWrapper.getResponse();
|
response = (HttpServletResponse) responseWrapper.getResponse();
|
||||||
|
|
||||||
byte[] body = responseWrapper.toByteArray();
|
byte[] body = responseWrapper.toByteArray();
|
||||||
int statusCode = responseWrapper.getStatusCode();
|
int statusCode = responseWrapper.getStatusCode();
|
||||||
|
|
||||||
@@ -157,7 +158,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
|||||||
*/
|
*/
|
||||||
private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper {
|
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();
|
private final ServletOutputStream outputStream = new ResponseServletOutputStream();
|
||||||
|
|
||||||
@@ -213,22 +214,22 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
|||||||
return this.writer;
|
return this.writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void resetBuffer() {
|
|
||||||
this.content.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reset() {
|
public void reset() {
|
||||||
super.reset();
|
super.reset();
|
||||||
resetBuffer();
|
resetBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getStatusCode() {
|
@Override
|
||||||
return statusCode;
|
public void resetBuffer() {
|
||||||
|
this.content.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] toByteArray() {
|
public int getStatusCode() {
|
||||||
|
return this.statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] toByteArray() {
|
||||||
return this.content.toByteArray();
|
return this.content.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ public class MarshallingView extends AbstractView {
|
|||||||
if (toBeMarshalled == null) {
|
if (toBeMarshalled == null) {
|
||||||
throw new ServletException("Unable to locate object to be marshalled in model: " + model);
|
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));
|
this.marshaller.marshal(toBeMarshalled, new StreamResult(bos));
|
||||||
|
|
||||||
setResponseContentType(request, response);
|
setResponseContentType(request, response);
|
||||||
|
|||||||
Reference in New Issue
Block a user