home *** CD-ROM | disk | FTP | other *** search
- package compressionFilters;
-
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpServletResponseWrapper;
-
- public class CompressionServletResponseWrapper extends HttpServletResponseWrapper {
- protected HttpServletResponse origResponse = null;
- protected static final String info = "CompressionServletResponseWrapper";
- protected ServletOutputStream stream = null;
- protected PrintWriter writer = null;
- protected int threshold = 0;
- private int debug = 0;
- protected String contentType = null;
-
- public CompressionServletResponseWrapper(HttpServletResponse response) {
- super(response);
- this.origResponse = response;
- if (this.debug > 1) {
- System.out.println("CompressionServletResponseWrapper constructor gets called");
- }
-
- }
-
- public void setContentType(String contentType) {
- if (this.debug > 1) {
- System.out.println("setContentType to " + contentType);
- }
-
- this.contentType = contentType;
- this.origResponse.setContentType(contentType);
- }
-
- public void setCompressionThreshold(int threshold) {
- if (this.debug > 1) {
- System.out.println("setCompressionThreshold to " + threshold);
- }
-
- this.threshold = threshold;
- }
-
- public void setDebugLevel(int debug) {
- this.debug = debug;
- }
-
- public ServletOutputStream createOutputStream() throws IOException {
- if (this.debug > 1) {
- System.out.println("createOutputStream gets called");
- }
-
- CompressionResponseStream stream = new CompressionResponseStream(this.origResponse);
- stream.setDebugLevel(this.debug);
- stream.setBuffer(this.threshold);
- return stream;
- }
-
- public void finishResponse() {
- try {
- if (this.writer != null) {
- this.writer.close();
- } else if (this.stream != null) {
- this.stream.close();
- }
- } catch (IOException var2) {
- }
-
- }
-
- public void flushBuffer() throws IOException {
- if (this.debug > 1) {
- System.out.println("flush buffer @ CompressionServletResponseWrapper");
- }
-
- ((CompressionResponseStream)this.stream).flush();
- }
-
- public ServletOutputStream getOutputStream() throws IOException {
- if (this.writer != null) {
- throw new IllegalStateException("getWriter() has already been called for this response");
- } else {
- if (this.stream == null) {
- this.stream = this.createOutputStream();
- }
-
- if (this.debug > 1) {
- System.out.println("stream is set to " + this.stream + " in getOutputStream");
- }
-
- return this.stream;
- }
- }
-
- public PrintWriter getWriter() throws IOException {
- if (this.writer != null) {
- return this.writer;
- } else if (this.stream != null) {
- throw new IllegalStateException("getOutputStream() has already been called for this response");
- } else {
- this.stream = this.createOutputStream();
- if (this.debug > 1) {
- System.out.println("stream is set to " + this.stream + " in getWriter");
- }
-
- String charEnc = this.origResponse.getCharacterEncoding();
- if (this.debug > 1) {
- System.out.println("character encoding is " + charEnc);
- }
-
- if (charEnc != null) {
- this.writer = new PrintWriter(new OutputStreamWriter(this.stream, charEnc));
- } else {
- this.writer = new PrintWriter(this.stream);
- }
-
- return this.writer;
- }
- }
-
- public void setContentLength(int length) {
- }
-
- private static String getCharsetFromContentType(String type) {
- if (type == null) {
- return null;
- } else {
- int semi = type.indexOf(";");
- if (semi == -1) {
- return null;
- } else {
- String afterSemi = type.substring(semi + 1);
- int charsetLocation = afterSemi.indexOf("charset=");
- if (charsetLocation == -1) {
- return null;
- } else {
- String afterCharset = afterSemi.substring(charsetLocation + 8);
- String encoding = afterCharset.trim();
- return encoding;
- }
- }
- }
- }
- }
-