home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / CompressionServletResponseWrapper.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-05-17  |  4.2 KB  |  146 lines

  1. package compressionFilters;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStreamWriter;
  5. import java.io.PrintWriter;
  6. import javax.servlet.ServletOutputStream;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpServletResponseWrapper;
  9.  
  10. public class CompressionServletResponseWrapper extends HttpServletResponseWrapper {
  11.    protected HttpServletResponse origResponse = null;
  12.    protected static final String info = "CompressionServletResponseWrapper";
  13.    protected ServletOutputStream stream = null;
  14.    protected PrintWriter writer = null;
  15.    protected int threshold = 0;
  16.    private int debug = 0;
  17.    protected String contentType = null;
  18.  
  19.    public CompressionServletResponseWrapper(HttpServletResponse response) {
  20.       super(response);
  21.       this.origResponse = response;
  22.       if (this.debug > 1) {
  23.          System.out.println("CompressionServletResponseWrapper constructor gets called");
  24.       }
  25.  
  26.    }
  27.  
  28.    public void setContentType(String contentType) {
  29.       if (this.debug > 1) {
  30.          System.out.println("setContentType to " + contentType);
  31.       }
  32.  
  33.       this.contentType = contentType;
  34.       this.origResponse.setContentType(contentType);
  35.    }
  36.  
  37.    public void setCompressionThreshold(int threshold) {
  38.       if (this.debug > 1) {
  39.          System.out.println("setCompressionThreshold to " + threshold);
  40.       }
  41.  
  42.       this.threshold = threshold;
  43.    }
  44.  
  45.    public void setDebugLevel(int debug) {
  46.       this.debug = debug;
  47.    }
  48.  
  49.    public ServletOutputStream createOutputStream() throws IOException {
  50.       if (this.debug > 1) {
  51.          System.out.println("createOutputStream gets called");
  52.       }
  53.  
  54.       CompressionResponseStream stream = new CompressionResponseStream(this.origResponse);
  55.       stream.setDebugLevel(this.debug);
  56.       stream.setBuffer(this.threshold);
  57.       return stream;
  58.    }
  59.  
  60.    public void finishResponse() {
  61.       try {
  62.          if (this.writer != null) {
  63.             this.writer.close();
  64.          } else if (this.stream != null) {
  65.             this.stream.close();
  66.          }
  67.       } catch (IOException var2) {
  68.       }
  69.  
  70.    }
  71.  
  72.    public void flushBuffer() throws IOException {
  73.       if (this.debug > 1) {
  74.          System.out.println("flush buffer @ CompressionServletResponseWrapper");
  75.       }
  76.  
  77.       ((CompressionResponseStream)this.stream).flush();
  78.    }
  79.  
  80.    public ServletOutputStream getOutputStream() throws IOException {
  81.       if (this.writer != null) {
  82.          throw new IllegalStateException("getWriter() has already been called for this response");
  83.       } else {
  84.          if (this.stream == null) {
  85.             this.stream = this.createOutputStream();
  86.          }
  87.  
  88.          if (this.debug > 1) {
  89.             System.out.println("stream is set to " + this.stream + " in getOutputStream");
  90.          }
  91.  
  92.          return this.stream;
  93.       }
  94.    }
  95.  
  96.    public PrintWriter getWriter() throws IOException {
  97.       if (this.writer != null) {
  98.          return this.writer;
  99.       } else if (this.stream != null) {
  100.          throw new IllegalStateException("getOutputStream() has already been called for this response");
  101.       } else {
  102.          this.stream = this.createOutputStream();
  103.          if (this.debug > 1) {
  104.             System.out.println("stream is set to " + this.stream + " in getWriter");
  105.          }
  106.  
  107.          String charEnc = this.origResponse.getCharacterEncoding();
  108.          if (this.debug > 1) {
  109.             System.out.println("character encoding is " + charEnc);
  110.          }
  111.  
  112.          if (charEnc != null) {
  113.             this.writer = new PrintWriter(new OutputStreamWriter(this.stream, charEnc));
  114.          } else {
  115.             this.writer = new PrintWriter(this.stream);
  116.          }
  117.  
  118.          return this.writer;
  119.       }
  120.    }
  121.  
  122.    public void setContentLength(int length) {
  123.    }
  124.  
  125.    private static String getCharsetFromContentType(String type) {
  126.       if (type == null) {
  127.          return null;
  128.       } else {
  129.          int semi = type.indexOf(";");
  130.          if (semi == -1) {
  131.             return null;
  132.          } else {
  133.             String afterSemi = type.substring(semi + 1);
  134.             int charsetLocation = afterSemi.indexOf("charset=");
  135.             if (charsetLocation == -1) {
  136.                return null;
  137.             } else {
  138.                String afterCharset = afterSemi.substring(charsetLocation + 8);
  139.                String encoding = afterCharset.trim();
  140.                return encoding;
  141.             }
  142.          }
  143.       }
  144.    }
  145. }
  146.