diff options
author | Stefan Beller <sbeller@google.com> | 2015-02-04 17:12:37 -0800 |
---|---|---|
committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2015-04-20 19:28:45 +0900 |
commit | a92bcf416c11fa69761bcc91cb5915621e16ce89 (patch) | |
tree | f51ca938acdd0348c1c6912fa3a124ca9729f743 | |
parent | 373794473a8a7cf6f6efe8f07b8363d58a9a44a7 (diff) |
RestApiServlet: Leave OutputStream open when flushing base64 padding
Some Java servlet containers fail if the response's OutputStream is
closed twice by the application. This appears to contradict standard
behavior in Java where most streams gracefully ignore extra close.
Unfortunately the container is required to power gerrit-review and
as such Gerrit needs to try to tolerate its behavior.
Wrap the supplied OutputStream delegating all calls except for
close(). No-op the close() method so the Java 7 try-with-resources
block does not automatically close the servlet OutputStream, leaving
this for the caller's finally block.
Change-Id: I84bd3c8031580f805d5d4ef5d70f09b89e170450
-rw-r--r-- | gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java index 1279339215..45144d7179 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java @@ -103,6 +103,7 @@ import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.EOFException; +import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -740,10 +741,15 @@ public class RestApiServlet extends HttpServlet { b64 = new BinaryResult() { @Override public void writeTo(OutputStream out) throws IOException { - try (OutputStreamWriter w = new OutputStreamWriter(out, ISO_8859_1); + try (OutputStreamWriter w = new OutputStreamWriter( + new FilterOutputStream(out) { + @Override + public void close() { + // Do not close out, but only w and e. + } + }, ISO_8859_1); OutputStream e = BaseEncoding.base64().encodingStream(w)) { src.writeTo(e); - e.flush(); } } }; |