You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by am...@apache.org on 2002/06/18 21:53:26 UTC
cvs commit: jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters CompressionFilter.java CompressionFilterTestServlet.java CompressionResponseStream.java CompressionServletResponseWrapper.java
amyroh 2002/06/18 12:53:26
Modified: webapps/examples/WEB-INF/classes/compressionFilters
CompressionFilter.java
CompressionFilterTestServlet.java
CompressionResponseStream.java
CompressionServletResponseWrapper.java
Log:
Fix for CompressionFilter to work with Weblogic 6.1. It used to work incorrect
for Web logic 6.1 in case the threshold is less than the len parameter in
CompressionResponseStream.write(byte b[], int off, int len). The filter worked
correctly on Tomcat since this situation never happens, but fails with Weblogic 6.1.
Submitted by Dmitri Valdin. Thanks Dmitri.
Revision Changes Path
1.6 +61 -13 jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionFilter.java
Index: CompressionFilter.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionFilter.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CompressionFilter.java 6 Sep 2001 23:05:08 -0000 1.5
+++ CompressionFilter.java 18 Jun 2002 19:53:25 -0000 1.6
@@ -74,12 +74,14 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
+
+
/**
* Implementation of <code>javax.servlet.Filter</code> used to compress
* the ServletResponse if it is bigger than a threshold.
*
* @author Amy Roh
+ * @author Dmitri Valdin
* @version $Revision$, $Date$
*/
@@ -92,6 +94,12 @@
private FilterConfig config = null;
/**
+ * Minimal reasonable threshold
+ */
+ private int minThreshold = 128;
+
+
+ /**
* The threshold number to compress
*/
protected int compressionThreshold;
@@ -111,18 +119,26 @@
config = filterConfig;
if (filterConfig != null) {
- String str = filterConfig.getInitParameter("compressionThreshold");
- if (str!=null) {
- compressionThreshold = Integer.parseInt(str);
- } else {
- compressionThreshold = 0;
- }
String value = filterConfig.getInitParameter("debug");
if (value!=null) {
debug = Integer.parseInt(value);
} else {
debug = 0;
}
+ String str = filterConfig.getInitParameter("compressionThreshold");
+ if (str!=null) {
+ compressionThreshold = Integer.parseInt(str);
+ if (compressionThreshold != 0 && compressionThreshold < minThreshold) {
+ if (debug > 0) {
+ System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold);
+ System.out.println("compressionThreshold set to " + minThreshold);
+ }
+ compressionThreshold = minThreshold;
+ }
+ } else {
+ compressionThreshold = 0;
+ }
+
} else {
compressionThreshold = 0;
}
@@ -162,19 +178,31 @@
System.out.println("@doFilter");
}
+ if (compressionThreshold == 0) {
+ if (debug > 0) {
+ System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression");
+ }
+ chain.doFilter(request, response);
+ return;
+ }
+
boolean supportCompression = false;
if (request instanceof HttpServletRequest) {
+ if (debug > 1) {
+ System.out.println("requestURI = " + ((HttpServletRequest)request).getRequestURI());
+ }
Enumeration e =
((HttpServletRequest)request).getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
- String name = (String)e.nextElement();
if (name.indexOf("gzip") != -1) {
+ String name = (String)e.nextElement();
+ if (name.indexOf("gzip") != -1) {
if (debug > 0) {
System.out.println("supports compression");
}
supportCompression = true;
} else {
if (debug > 0) {
- System.out.println("no suuport for compresion");
+ System.out.println("no support for compresion");
}
}
}
@@ -190,6 +218,7 @@
if (response instanceof HttpServletResponse) {
CompressionServletResponseWrapper wrappedResponse =
new CompressionServletResponseWrapper((HttpServletResponse)response);
+ wrappedResponse.setDebugLevel(debug);
wrappedResponse.setCompressionThreshold(compressionThreshold);
if (debug > 0) {
System.out.println("doFilter gets called with compression");
@@ -203,5 +232,24 @@
}
}
}
+
+ /**
+ * Set filter config
+ * This function is equivalent to init. Required by Weblogic 6.1
+ *
+ * @param filterConfig The filter configuration object
+ */
+ public void setFilterConfig(FilterConfig filterConfig) {
+ init(filterConfig);
+ }
+
+ /**
+ * Return filter config
+ * Required by Weblogic 6.1
+ */
+ public FilterConfig getFilterConfig() {
+ return config;
+ }
+
}
1.4 +6 -5 jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionFilterTestServlet.java
Index: CompressionFilterTestServlet.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionFilterTestServlet.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CompressionFilterTestServlet.java 6 Sep 2001 04:18:54 -0000 1.3
+++ CompressionFilterTestServlet.java 18 Jun 2002 19:53:25 -0000 1.4
@@ -86,7 +86,8 @@
Enumeration e = ((HttpServletRequest)request).getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
- String name = (String)e.nextElement();
out.println(name);
+ String name = (String)e.nextElement();
+ out.println(name);
if (name.indexOf("gzip") != -1) {
out.println("gzip supported -- able to compress");
}
1.5 +63 -69 jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionResponseStream.java
Index: CompressionResponseStream.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionResponseStream.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CompressionResponseStream.java 6 Sep 2001 23:05:08 -0000 1.4
+++ CompressionResponseStream.java 18 Jun 2002 19:53:25 -0000 1.5
@@ -76,6 +76,7 @@
* the CompressionServletResponseWrapper implementation.
*
* @author Amy Roh
+ * @author Dmitri Valdin
* @version $Revision$, $Date$
*/
@@ -95,7 +96,6 @@
super();
closed = false;
- count = 0;
this.response = response;
this.output = response.getOutputStream();
@@ -112,14 +112,14 @@
protected int compressionThreshold = 0;
/**
- * The buffer through which all of our output bytes are passed.
+ * Debug level
*/
- protected byte[] buffer = null;
-
+ private int debug = 0;
+
/**
- * Is it big enough to compress?
+ * The buffer through which all of our output bytes are passed.
*/
- protected boolean compressionThresholdReached = false;
+ protected byte[] buffer = null;
/**
* The number of data bytes currently in the buffer.
@@ -130,19 +130,13 @@
* The underlying gzip output stream to which we should write data.
*/
protected GZIPOutputStream gzipstream = null;
-
+
/**
* Has this stream been closed?
*/
protected boolean closed = false;
/**
- * The number of bytes which have already been written to this stream.
- */
- protected int count = 0;
-
-
- /**
* The content length past which we will not write, or -1 if there is
* no defined content length.
*/
@@ -161,6 +155,13 @@
// --------------------------------------------------------- Public Methods
+ /**
+ * Set debug level
+ */
+ public void setDebugLevel(int debug) {
+ this.debug = debug;
+ }
+
/**
* Set the compressionThreshold number and create buffer for this size
@@ -168,7 +169,9 @@
protected void setBuffer(int threshold) {
compressionThreshold = threshold;
buffer = new byte[compressionThreshold];
- //System.out.println("buffer is set to "+compressionThreshold);
+ if (debug > 1) {
+ System.out.println("buffer is set to "+compressionThreshold);
+ }
}
/**
@@ -177,7 +180,9 @@
*/
public void close() throws IOException {
- //System.out.println("close() @ CompressionResponseStream");
+ if (debug > 1) {
+ System.out.println("close() @ CompressionResponseStream");
+ }
if (closed)
throw new IOException("This output stream has already been closed");
@@ -203,12 +208,14 @@
*/
public void flush() throws IOException {
- //System.out.println("flush() @ CompressionResponseStream");
+ if (debug > 1) {
+ System.out.println("flush() @ CompressionResponseStream");
+ }
if (closed) {
throw new IOException("Cannot flush a closed output stream");
}
- if (gzipstream!=null) {
+ if (gzipstream != null) {
gzipstream.flush();
}
@@ -216,11 +223,14 @@
public void flushToGZip() throws IOException {
- //System.out.println("flushToGZip() @ CompressionResponseStream");
-
+ if (debug > 1) {
+ System.out.println("flushToGZip() @ CompressionResponseStream");
+ }
if (bufferCount > 0) {
- //System.out.println("flushing out to GZipStream");
- gzipstream.write(buffer, 0, bufferCount);
+ if (debug > 1) {
+ System.out.println("flushing out to GZipStream, bufferCount = " + bufferCount);
+ }
+ writeToGZip(buffer, 0, bufferCount);
bufferCount = 0;
}
@@ -235,44 +245,22 @@
*/
public void write(int b) throws IOException {
- //System.out.print("write "+b+" in CompressionResponseStream ");
+ if (debug > 1) {
+ System.out.println("write "+b+" in CompressionResponseStream ");
+ }
if (closed)
throw new IOException("Cannot write to a closed output stream");
- if ((bufferCount >= buffer.length) || (count>=compressionThreshold)) {
- compressionThresholdReached = true;
+ if (bufferCount >= buffer.length) {
+ flushToGZip();
}
- if (compressionThresholdReached) {
- writeToGZip(b);
- } else {
- buffer[bufferCount++] = (byte) b;
- count++;
- }
+ buffer[bufferCount++] = (byte) b;
}
/**
- * Write the specified byte to our compressed GZip output stream.
- *
- * @param b The byte to be written
- *
- * @exception IOException if an input/output error occurs
- */
-
- public void writeToGZip(int b) throws IOException {
-
- //System.out.println("writeToGZip (int b) compressing");
- if (gzipstream == null) {
- gzipstream = new GZIPOutputStream(output);
- response.addHeader("Content-Encoding", "gzip");
- }
- gzipstream.write(b);
-
- }
-
- /**
* Write <code>b.length</code> bytes from the specified byte array
* to our output stream.
*
@@ -299,29 +287,45 @@
*/
public void write(byte b[], int off, int len) throws IOException {
- //System.out.println("second write in CompressionResponseStream");
+ if (debug > 1) {
+ System.out.println("write, bufferCount = " + bufferCount + " len = " + len + " off = " + off);
+ }
+ if (debug > 2) {
+ System.out.write(b, off, len);
+ }
+
if (closed)
throw new IOException("Cannot write to a closed output stream");
if (len == 0)
return;
+
+ // Can we write into buffer ?
if (len <= (buffer.length - bufferCount)) {
System.arraycopy(b, off, buffer, bufferCount, len);
bufferCount += len;
- count += len;
return;
}
- // buffer full, start writing to gzipstream
+ // There is not enough space in buffer. Flush it ...
+ flushToGZip();
- writeToGZip(b, off, len);
- count += len;
+ // ... and try again. Note, that bufferCount = 0 here !
+ if (len <= (buffer.length - bufferCount)) {
+ System.arraycopy(b, off, buffer, bufferCount, len);
+ bufferCount += len;
+ return;
+ }
+ // write direct to gzip
+ writeToGZip(b, off, len);
}
public void writeToGZip(byte b[], int off, int len) throws IOException {
- //System.out.println("writeToGZip 2 compressing");
+ if (debug > 1) {
+ System.out.println("***** writeToGZip, len = " + len);
+ }
if (gzipstream == null) {
gzipstream = new GZIPOutputStream(output);
response.addHeader("Content-Encoding", "gzip");
@@ -340,16 +344,6 @@
public boolean closed() {
return (this.closed);
-
- }
-
-
- /**
- * Reset the count of bytes written to this stream to zero.
- */
- public void reset() {
-
- count = 0;
}
1.6 +42 -18 jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionServletResponseWrapper.java
Index: CompressionServletResponseWrapper.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/examples/WEB-INF/classes/compressionFilters/CompressionServletResponseWrapper.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CompressionServletResponseWrapper.java 11 Mar 2002 20:11:05 -0000 1.5
+++ CompressionServletResponseWrapper.java 18 Jun 2002 19:53:26 -0000 1.6
@@ -83,6 +83,7 @@
* the CompressionServletResponseStream implementation..
*
* @author Amy Roh
+ * @author Dmitri Valdin
* @version $Revision$, $Date$
*/
@@ -96,10 +97,11 @@
*/
public CompressionServletResponseWrapper(HttpServletResponse response) {
-
super(response);
origResponse = response;
-
+ if (debug > 1) {
+ System.out.println("CompressionServletResponseWrapper constructor gets called");
+ }
}
@@ -137,6 +139,10 @@
*/
protected int threshold = 0;
+ /**
+ * Debug level
+ */
+ private int debug = 0;
// --------------------------------------------------------- Public Methods
@@ -145,11 +151,21 @@
* Set threshold number
*/
public void setCompressionThreshold(int threshold) {
-
+ if (debug > 1) {
+ System.out.println("setCompressionThreshold to " + threshold);
+ }
this.threshold = threshold;
+ }
+
+ /**
+ * Set debug level
+ */
+ public void setDebugLevel(int debug) {
+ this.debug = debug;
}
+
/**
* Create and return a ServletOutputStream to write the content
* associated with this Response.
@@ -157,8 +173,15 @@
* @exception IOException if an input/output error occurs
*/
public ServletOutputStream createOutputStream() throws IOException {
+ if (debug > 1) {
+ System.out.println("createOutputStream gets called");
+ }
- return (new CompressionResponseStream(origResponse));
+ CompressionResponseStream stream = new CompressionResponseStream(origResponse);
+ stream.setDebugLevel(debug);
+ stream.setBuffer(threshold);
+
+ return stream;
}
@@ -167,7 +190,6 @@
* Finish a response.
*/
public void finishResponse() {
-
try {
if (writer != null) {
writer.close();
@@ -177,7 +199,6 @@
}
} catch (IOException e) {
}
-
}
@@ -190,12 +211,10 @@
* @exception IOException if an input/output error occurs
*/
public void flushBuffer() throws IOException {
-
- if (writer != null) {
- writer.flush();
- } else {
- ((CompressionResponseStream)stream).flush();
+ if (debug > 1) {
+ System.out.println("flush buffer @ CompressionServletResponseWrapper");
}
+ ((CompressionResponseStream)stream).flush();
}
@@ -213,8 +232,11 @@
if (stream == null)
stream = createOutputStream();
- ((CompressionResponseStream) stream).setBuffer(threshold);
- return (stream);
+ if (debug > 1) {
+ System.out.println("stream is set to "+stream+" in getOutputStream");
+ }
+
+ return (stream);
}
@@ -234,7 +256,9 @@
throw new IllegalStateException("getOutputStream() has already been called for this response");
stream = createOutputStream();
- ((CompressionResponseStream) stream).setBuffer(threshold);
+ if (debug > 1) {
+ System.out.println("stream is set to "+stream+" in getWriter");
+ }
writer = new PrintWriter(stream);
return (writer);
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>