package de.halbekunst.juplo.cachecontrol;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
httpRequest.setAttribute(RESPONSE_WRAPPER, wrapper);
cacheControl.init(wrapper);
chain.doFilter(request, wrapper);
- /** Dekoration auslösen, falls sie bisher nicht ausgelöst wurde... */
- cacheControl.decorate(httpRequest, httpResponse, wrapper);
+ wrapper.finish();
}
@Override
private final HttpServletResponse response;
boolean zipped; // CacheControll greift direkt auf dieses Flag zu!
+ boolean forceCompression = false;
private CountingServletOutputStream out;
private ServletOutputStream stream;
if (zipped)
out = new GZIPServletOutputStream();
else
- out = new WrappedServletOutputStream();
+ out = new CountingServletOutputStream();
+ }
+
+ public void finish() throws IOException {
+ out.close();
}
@Override
public void flushBuffer() throws IOException {
+ forceCompression = true;
cacheControl.decorate(request, response, this);
response.flushBuffer();
}
public void resetBuffer() {
response.resetBuffer();
+ try {
+ if (zipped)
+ out = new GZIPServletOutputStream();
+ else
+ out = new CountingServletOutputStream();
+ }
+ catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
stream = null;
writer = null;
}
if (zipped)
out = new GZIPServletOutputStream();
else
- out = new WrappedServletOutputStream();
+ out = new CountingServletOutputStream();
}
catch (IOException e) {
throw new IllegalStateException(e);
writer = null;
/** Cookies has been cleared! Reinitialize decorator... */
+ forceCompression = false;
cacheControl.init(this);
}
}
- abstract class CountingServletOutputStream extends ServletOutputStream {
-
- abstract void setBuffer(int size) throws IllegalStateException;
- abstract boolean isZipped();
- }
+ class CountingServletOutputStream extends ServletOutputStream {
+ private OutputStream out;
+ int left;
+ boolean empty;
- final class GZIPServletOutputStream extends CountingServletOutputStream {
-
- final static int MINMAL_BUFFER_SIZE = 128;
-
- private final GZIPOutputStream out;
- private int buffer, left;
- private boolean empty;
-
-
- public GZIPServletOutputStream() throws IOException {
- this.out = new GZIPOutputStream(response.getOutputStream());
- empty = true;
- setBuffer(AcceleratorFilter.this.buffer);
+ CountingServletOutputStream() throws IOException {
+ out = response.getOutputStream();
left = buffer;
+ empty = true;
}
- @Override
+ void setBuffer(int size) throws IllegalStateException {}
+
boolean isZipped() {
- return !empty;
+ return false;
}
- @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;
- }
+ void finish() throws IOException {
+ decorate();
}
-
- @Override
- public void close() throws IOException {
+ void decorate() throws IOException {
try {
AcceleratorFilter.this.cacheControl.decorate(AccelerationWrapper.this.request, response, AccelerationWrapper.this);
}
log.error("Error while guessing Cache-Header's", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
+ }
+
+ @Override
+ public void close() throws IOException {
+ decorate();
out.close();
}
@Override
public void flush() throws IOException {
- try {
- AcceleratorFilter.this.cacheControl.decorate(AccelerationWrapper.this.request, response, AccelerationWrapper.this);
- }
- catch (Exception e) {
- log.error("Error while guessing Cache-Header's", e);
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- }
-
+ decorate();
out.flush();
}
@Override
public void write(int i) throws IOException {
- if (left == 0) {
- try {
- AcceleratorFilter.this.cacheControl.decorate(AccelerationWrapper.this.request, response, AccelerationWrapper.this);
- }
- catch (Exception e) {
- log.error("Error while guessing Cache-Header's", e);
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- }
- }
empty = false;
+ if (left == 0)
+ decorate();
left--;
out.write(i);
}
}
- final class WrappedServletOutputStream extends CountingServletOutputStream {
+ final class GZIPServletOutputStream extends CountingServletOutputStream {
- private final ServletOutputStream out;
- private boolean empty;
+ final static int MINIMAL_BUFFER_SIZE = 128;
+ private ByteArrayOutputStream buffer;
+ private OutputStream out;
+ private GZIPOutputStream zout;
+ private int bufferSize;
+ private boolean decorated = false;
- public WrappedServletOutputStream() throws IOException {
- this.out = response.getOutputStream();
- empty = true;
+
+ public GZIPServletOutputStream() throws IOException {
+ setBuffer(AcceleratorFilter.this.buffer);
}
+ @Override
+ void finish() throws IOException {
+ decorate();
+ zout.finish();
+ super.out.write(buffer.toByteArray());
+ }
+
@Override
boolean isZipped() {
- return false;
+ if (decorated)
+ return true;
+ return !empty;
}
@Override
- void setBuffer(int size) {
+ void setBuffer(int size) throws IllegalStateException {
if (!empty)
throw new IllegalStateException("attemp to change buffer size after writing data to response!");
+
+ if (size > MINIMAL_BUFFER_SIZE) {
+ left = size;
+ try {
+ bufferSize = size;
+ out = response.getOutputStream();
+ buffer = new ByteArrayOutputStream(size);
+ zout = new GZIPOutputStream(buffer, size);
+ super.out = zout;
+ }
+ catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
}
+
@Override
public void close() throws IOException {
+ decorate();
+ zout.close();
+ out.write(buffer.toByteArray());
out.close();
}
@Override
public void flush() throws IOException {
+ decorate();
+ out.write(buffer.toByteArray());
out.flush();
}
@Override
public void write(int i) throws IOException {
empty = false;
- out.write(i);
+ if (left == 0) {
+ if (!decorated) {
+ decorate();
+ decorated = true;
+ }
+ out.write(buffer.toByteArray());
+ buffer.reset();
+ left = bufferSize;
+ }
+ left--;
+ zout.write(i);
}
}
}