Browse Source

Consistently avoid close() call on Servlet OutputStream

Issue: SPR-11413
(cherry picked from commit 5f1592a)
pull/465/head
Juergen Hoeller 12 years ago
parent
commit
85e336e6da
  1. 17
      spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java
  2. 8
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

17
spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.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"); * 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.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
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.ServletOutputStream; import javax.servlet.ServletOutputStream;
@ -31,7 +30,7 @@ import javax.servlet.http.HttpServletResponseWrapper;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils; import org.springframework.util.StreamUtils;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
@ -48,9 +47,9 @@ import org.springframework.web.util.WebUtils;
*/ */
public class ShallowEtagHeaderFilter extends OncePerRequestFilter { public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private static String HEADER_ETAG = "ETag"; private static final String HEADER_ETAG = "ETag";
private static String HEADER_IF_NONE_MATCH = "If-None-Match"; private static final String HEADER_IF_NONE_MATCH = "If-None-Match";
/** /**
@ -117,7 +116,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws IOException { private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws IOException {
if (body.length > 0) { if (body.length > 0) {
response.setContentLength(body.length); response.setContentLength(body.length);
FileCopyUtils.copy(body, response.getOutputStream()); StreamUtils.copy(body, response.getOutputStream());
} }
} }
@ -166,7 +165,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private int statusCode = HttpServletResponse.SC_OK; private int statusCode = HttpServletResponse.SC_OK;
private ShallowEtagResponseWrapper(HttpServletResponse response) { public ShallowEtagResponseWrapper(HttpServletResponse response) {
super(response); super(response);
} }
@ -233,6 +232,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return this.content.toByteArray(); return this.content.toByteArray();
} }
private class ResponseServletOutputStream extends ServletOutputStream { private class ResponseServletOutputStream extends ServletOutputStream {
@Override @Override
@ -246,9 +246,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
} }
} }
private class ResponsePrintWriter extends PrintWriter { private class ResponsePrintWriter extends PrintWriter {
private ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException { public ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
super(new OutputStreamWriter(content, characterEncoding)); super(new OutputStreamWriter(content, characterEncoding));
} }

8
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.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"); * 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.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.resource;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import javax.activation.FileTypeMap; import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap; import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -28,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -35,7 +35,7 @@ import org.springframework.http.MediaType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils; import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler; import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.ServletWebRequest;
@ -249,7 +249,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
* @throws IOException in case of errors while writing the content * @throws IOException in case of errors while writing the content
*/ */
protected void writeContent(HttpServletResponse response, Resource resource) throws IOException { protected void writeContent(HttpServletResponse response, Resource resource) throws IOException {
FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream()); StreamUtils.copy(resource.getInputStream(), response.getOutputStream());
} }

Loading…
Cancel
Save