From 4f2efacd03f5660a1ba4b1d7b0f2380d8a9ec8ef Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 25 Dec 2011 13:12:22 +0100 Subject: [PATCH] AccelerationWrapper.out ist jetzt nie null Conflicts: cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java --- .../juplo/cachecontrol/AcceleratorFilter.java | 133 ++++++++++++------ 1 file changed, 93 insertions(+), 40 deletions(-) diff --git a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java index 7c250a24..9125554c 100644 --- a/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java +++ b/cachecontrol/src/main/java/de/halbekunst/juplo/cachecontrol/AcceleratorFilter.java @@ -1,7 +1,6 @@ package de.halbekunst.juplo.cachecontrol; import java.io.IOException; -import java.io.OutputStream; import java.io.PrintWriter; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -74,7 +73,6 @@ public class AcceleratorFilter implements Filter { chain.doFilter(request, wrapper); /** Dekoration auslösen, falls sie bisher nicht ausgelöst wurde... */ cacheControl.decorate(httpRequest, httpResponse, wrapper); - wrapper.finish(); } @Override @@ -93,8 +91,7 @@ public class AcceleratorFilter implements Filter { boolean zipped; // CacheControll greift direkt auf dieses Flag zu! - private int buffer; - private GZIPServletOutputStream out; + private CountingServletOutputStream out; private ServletOutputStream stream; private PrintWriter writer; @@ -124,7 +121,6 @@ public class AcceleratorFilter implements Filter { weak = AcceleratorFilter.this.weak; cacheParams = new HashMap(); - buffer = AcceleratorFilter.this.buffer; zipped = false; Enumeration values = request.getHeaders(Headers.HEADER_ACCEPT_ENCODING); while (values.hasMoreElements()) { @@ -134,12 +130,10 @@ public class AcceleratorFilter implements Filter { break; } } - } - - - public void finish() throws IOException { - if (zipped && out != null) - out.zout.finish(); + if (zipped) + out = new GZIPServletOutputStream(); + else + out = new WrappedServletOutputStream(); } @@ -278,7 +272,6 @@ public class AcceleratorFilter implements Filter { throw new IllegalStateException("ServletOutputStream and PrintWriter cannot be requested both!"); if (stream == null) { - out = new GZIPServletOutputStream(); stream = out; } @@ -292,7 +285,6 @@ public class AcceleratorFilter implements Filter { throw new IllegalStateException("ServletOutputStream and PrintWriter cannot be requested both!"); if (writer == null) { - out = new GZIPServletOutputStream(); writer = new PrintWriter(out); } @@ -310,15 +302,7 @@ public class AcceleratorFilter implements Filter { @Override public void setBufferSize(int size) { - if (out != null && this.buffer != out.left) - throw new IllegalStateException("setBufferSize() cannot be called after content has been written!"); - - if (size < 0) - size = 0; - - this.buffer = size; - if (out != null) - out.left = size; + out.setBuffer(size); response.setBufferSize(size); } @@ -341,7 +325,15 @@ public class AcceleratorFilter implements Filter { public void reset() { response.reset(); - out = null; + try { + if (zipped) + out = new GZIPServletOutputStream(); + else + out = new WrappedServletOutputStream(); + } + catch (IOException e) { + throw new IllegalStateException(e); + } stream = null; writer = null; @@ -353,7 +345,7 @@ public class AcceleratorFilter implements Filter { @Override public boolean isZipped() { - return zipped && out != null && !out.empty; + return out.isZipped(); } @Override @@ -545,27 +537,48 @@ public class AcceleratorFilter implements Filter { } - class GZIPServletOutputStream extends ServletOutputStream { + abstract class CountingServletOutputStream extends ServletOutputStream { + + abstract void setBuffer(int size) throws IllegalStateException; + abstract boolean isZipped(); + } + + + final class GZIPServletOutputStream extends CountingServletOutputStream { + + final static int MINMAL_BUFFER_SIZE = 128; + + + private final GZIPOutputStream out; + private int buffer, left; + private boolean empty; - private final OutputStream out; - private final GZIPOutputStream zout; - int left; - boolean empty; public GZIPServletOutputStream() throws IOException { - if (zipped) { - this.zout = new GZIPOutputStream(response.getOutputStream(), buffer); - this.out = this.zout; - } - else { - this.zout = null; - this.out = response.getOutputStream(); - } + this.out = new GZIPOutputStream(response.getOutputStream()); empty = true; + setBuffer(AcceleratorFilter.this.buffer); left = buffer; } + @Override + boolean isZipped() { + return !empty; + } + + @Override + void setBuffer(int size) { + if (!empty) + throw new IllegalStateException("attemp to change buffer size after writing data to response!"); + + if (size > MINMAL_BUFFER_SIZE) { + buffer = size; + left = buffer; + } + } + + @Override public void close() throws IOException { try { @@ -576,8 +589,6 @@ public class AcceleratorFilter implements Filter { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } - if (!empty && zout != null) - zout.finish(); out.close(); } @@ -598,7 +609,7 @@ public class AcceleratorFilter implements Filter { public void write(int i) throws IOException { if (left == 0) { try { - AcceleratorFilter.this.cacheControl.decorate(AccelerationWrapper.this.request, response, buffer); + AcceleratorFilter.this.cacheControl.decorate(AccelerationWrapper.this.request, response, AccelerationWrapper.this); } catch (Exception e) { log.error("Error while guessing Cache-Header's", e); @@ -610,5 +621,47 @@ public class AcceleratorFilter implements Filter { out.write(i); } } + + + final class WrappedServletOutputStream extends CountingServletOutputStream { + + private final ServletOutputStream out; + private boolean empty; + + + public WrappedServletOutputStream() throws IOException { + this.out = response.getOutputStream(); + empty = true; + } + + + @Override + boolean isZipped() { + return false; + } + + @Override + void setBuffer(int size) { + if (!empty) + throw new IllegalStateException("attemp to change buffer size after writing data to response!"); + } + + + @Override + public void close() throws IOException { + out.close(); + } + + @Override + public void flush() throws IOException { + out.flush(); + } + + @Override + public void write(int i) throws IOException { + empty = false; + out.write(i); + } + } } } -- 2.20.1