Browse Source

Revised ByteArrayOutputStream handling in MarshallingView and co

Issue: SPR-11646
pull/509/merge
Juergen Hoeller 12 years ago
parent
commit
8006696613
  1. 2
      spring-core/src/main/java/org/springframework/util/ResizableByteArrayOutputStream.java
  2. 10
      spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java
  3. 10
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java

2
spring-core/src/main/java/org/springframework/util/ResizableByteArrayOutputStream.java

@ -29,7 +29,7 @@ import java.io.ByteArrayOutputStream;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 4.0 * @since 4.0.3
*/ */
public class ResizableByteArrayOutputStream extends ByteArrayOutputStream { public class ResizableByteArrayOutputStream extends ByteArrayOutputStream {

10
spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java

@ -267,7 +267,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
} }
if (isIncludePayload() && request instanceof RequestCachingRequestWrapper) { if (isIncludePayload() && request instanceof RequestCachingRequestWrapper) {
RequestCachingRequestWrapper wrapper = (RequestCachingRequestWrapper) request; RequestCachingRequestWrapper wrapper = (RequestCachingRequestWrapper) request;
byte[] buf = wrapper.toByteArray(); byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) { if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength()); int length = Math.min(buf.length, getMaxPayloadLength());
String payload; String payload;
@ -304,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(1024); private final ByteArrayOutputStream cachedContent = new ByteArrayOutputStream(1024);
private final ServletInputStream inputStream; private final ServletInputStream inputStream;
@ -334,8 +334,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
return this.reader; return this.reader;
} }
private byte[] toByteArray() { private byte[] getContentAsByteArray() {
return this.bos.toByteArray(); return this.cachedContent.toByteArray();
} }
@ -351,7 +351,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
public int read() throws IOException { public int read() throws IOException {
int ch = this.is.read(); int ch = this.is.read();
if (ch != -1) { if (ch != -1) {
bos.write(ch); cachedContent.write(ch);
} }
return ch; return ch;
} }

10
spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java

@ -24,7 +24,6 @@ import javax.xml.transform.stream.StreamResult;
import org.springframework.oxm.Marshaller; import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
@ -104,13 +103,12 @@ public class MarshallingView extends AbstractView {
if (toBeMarshalled == null) { if (toBeMarshalled == null) {
throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model); throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model);
} }
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
this.marshaller.marshal(toBeMarshalled, new StreamResult(bos)); this.marshaller.marshal(toBeMarshalled, new StreamResult(baos));
setResponseContentType(request, response); setResponseContentType(request, response);
response.setContentLength(bos.size()); response.setContentLength(baos.size());
baos.writeTo(response.getOutputStream());
StreamUtils.copy(bos.toByteArray(), response.getOutputStream());
} }
/** /**

Loading…
Cancel
Save