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;
chain.doFilter(request, wrapper);
/** Dekoration auslösen, falls sie bisher nicht ausgelöst wurde... */
cacheControl.decorate(httpRequest, httpResponse, wrapper);
- wrapper.finish();
}
@Override
boolean zipped; // CacheControll greift direkt auf dieses Flag zu!
- private int buffer;
- private GZIPServletOutputStream out;
+ private CountingServletOutputStream out;
private ServletOutputStream stream;
private PrintWriter writer;
weak = AcceleratorFilter.this.weak;
cacheParams = new HashMap<String,String>();
- buffer = AcceleratorFilter.this.buffer;
zipped = false;
Enumeration values = request.getHeaders(Headers.HEADER_ACCEPT_ENCODING);
while (values.hasMoreElements()) {
break;
}
}
- }
-
-
- public void finish() throws IOException {
- if (zipped && out != null)
- out.zout.finish();
+ if (zipped)
+ out = new GZIPServletOutputStream();
+ else
+ out = new WrappedServletOutputStream();
}
throw new IllegalStateException("ServletOutputStream and PrintWriter cannot be requested both!");
if (stream == null) {
- out = new GZIPServletOutputStream();
stream = out;
}
throw new IllegalStateException("ServletOutputStream and PrintWriter cannot be requested both!");
if (writer == null) {
- out = new GZIPServletOutputStream();
writer = new PrintWriter(out);
}
@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);
}
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;
@Override
public boolean isZipped() {
- return zipped && out != null && !out.empty;
+ return out.isZipped();
}
@Override
}
- 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 {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
- if (!empty && zout != null)
- zout.finish();
out.close();
}
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);
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);
+ }
+ }
}
}