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; @@ -29,7 +29,7 @@ import java.io.ByteArrayOutputStream;
*
* @author Brian Clozel
* @author Juergen Hoeller
* @since 4.0
* @since 4.0.3
*/
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 @@ -267,7 +267,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
}
if (isIncludePayload() && request instanceof RequestCachingRequestWrapper) {
RequestCachingRequestWrapper wrapper = (RequestCachingRequestWrapper) request;
byte[] buf = wrapper.toByteArray();
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength());
String payload;
@ -304,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter @@ -304,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper {
private final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
private final ByteArrayOutputStream cachedContent = new ByteArrayOutputStream(1024);
private final ServletInputStream inputStream;
@ -334,8 +334,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter @@ -334,8 +334,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
return this.reader;
}
private byte[] toByteArray() {
return this.bos.toByteArray();
private byte[] getContentAsByteArray() {
return this.cachedContent.toByteArray();
}
@ -351,7 +351,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter @@ -351,7 +351,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
public int read() throws IOException {
int ch = this.is.read();
if (ch != -1) {
bos.write(ch);
cachedContent.write(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; @@ -24,7 +24,6 @@ import javax.xml.transform.stream.StreamResult;
import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractView;
@ -104,13 +103,12 @@ public class MarshallingView extends AbstractView { @@ -104,13 +103,12 @@ public class MarshallingView extends AbstractView {
if (toBeMarshalled == null) {
throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
this.marshaller.marshal(toBeMarshalled, new StreamResult(bos));
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
this.marshaller.marshal(toBeMarshalled, new StreamResult(baos));
setResponseContentType(request, response);
response.setContentLength(bos.size());
StreamUtils.copy(bos.toByteArray(), response.getOutputStream());
response.setContentLength(baos.size());
baos.writeTo(response.getOutputStream());
}
/**

Loading…
Cancel
Save