Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.http.gzip;
 
 import java.util.Set;
 
 
 
 /*------------------------------------------------------------ */
 
 public abstract class CompressedResponseWrapper extends HttpServletResponseWrapper
 {
     
     public static final int DEFAULT_BUFFER_SIZE = 8192;
     public static final int DEFAULT_MIN_COMPRESS_SIZE = 256;
     
     private Set<String_mimeTypes;
     private int _bufferSize=;
     protected HttpServletRequest _request;
 
     private PrintWriter _writer;
     private long _contentLength=-1;
     private boolean _noCompression;
 
     {
         super(response);
          = request;
     }
 
     /* ------------------------------------------------------------ */
    
 
     public void setMimeTypes(Set<StringmimeTypes)
     {
          = mimeTypes;
     }
 
     /* ------------------------------------------------------------ */
    
 
     @Override
     public void setBufferSize(int bufferSize)
     {
          = bufferSize;
     }
 
     /* ------------------------------------------------------------ */
    
 
     public void setMinCompressSize(int minCompressSize)
     {
          = minCompressSize;
     }
 
     /* ------------------------------------------------------------ */
    
 
     @Override
     public void setContentType(String ct)
     {
         super.setContentType(ct);
     
         if (ct!=null)
         {
             int colon=ct.indexOf(";");
            if (colon>0)
                ct=ct.substring(0,colon);
        }
    
        if ((==null || .getOutputStream()==null) && 
            (==null && ct!=null && ct.contains("gzip") ||
             !=null && (ct==null||!.contains(StringUtil.asciiToLowerCase(ct)))))
        {
            noCompression();
        }
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void setStatus(int scString sm)
    {
        super.setStatus(sc,sm);
        if (sc<200 || sc==204 || sc==205 || sc>=300)
            noCompression();
    }
    /* ------------------------------------------------------------ */
    

See also:
setStatus(int)
    @Override
    public void setStatus(int sc)
    {
        super.setStatus(sc);
        if (sc<200 || sc==204 || sc==205 || sc>=300)
            noCompression();
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void setContentLength(int length)
    {
        setContentLength((long)length);
    }
    
    /* ------------------------------------------------------------ */
    protected void setContentLength(long length)
    {
        =length;
        if (!=null)
            .setContentLength(length);
        else if ( && >=0)
        {
            HttpServletResponse response = (HttpServletResponse)getResponse();
            if(<.)
            {
                response.setContentLength((int));
            }
            else
            {
                response.setHeader("Content-Length", Long.toString());
            }
        }
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void addHeader(String nameString value)
    {
        if ("content-length".equalsIgnoreCase(name))
        {
            =Long.parseLong(value);
            if (!=null)
                .setContentLength();
        }
        else if ("content-type".equalsIgnoreCase(name))
        {   
            setContentType(value);
        }
        else if ("content-encoding".equalsIgnoreCase(name))
        {   
            super.addHeader(name,value);
            if (!isCommitted())
            {
                noCompression();
            }
        }
        else
            super.addHeader(name,value);
    }
    /* ------------------------------------------------------------ */
    

See also:
flushBuffer()
    @Override
    public void flushBuffer() throws IOException
    {
        if (!=null)
            .flush();
        if (!=null)
            .finish();
        else
            getResponse().flushBuffer();
    }
    /* ------------------------------------------------------------ */
    

See also:
reset()
    @Override
    public void reset()
    {
        super.reset();
        if (!=null)
            .resetBuffer();
        =null;
        =null;
        =false;
        =-1;
    }
    /* ------------------------------------------------------------ */
    

See also:
resetBuffer()
    @Override
    public void resetBuffer()
    {
        super.resetBuffer();
        if (!=null)
            .resetBuffer();
        =null;
        =null;
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void sendError(int scString msgthrows IOException
    {
        resetBuffer();
        super.sendError(sc,msg);
    }
    /* ------------------------------------------------------------ */
    

See also:
sendError(int)
    @Override
    public void sendError(int scthrows IOException
    {
        resetBuffer();
        super.sendError(sc);
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void sendRedirect(String locationthrows IOException
    {
        resetBuffer();
        super.sendRedirect(location);
    }
    
    /* ------------------------------------------------------------ */
    

See also:
noCompression()
    public void noCompression()
    {
        =true;
        if (!=null)
        {
            try
            {
                .doNotCompress();
            }
            catch (IOException e)
            {
                throw new IllegalStateException(e);
            }
        }
    }
    
    /* ------------------------------------------------------------ */
    

See also:
finish()
    public void finish() throws IOException
    {
        if (!=null && !.isClosed())
            .flush();
        if (!=null)
            .finish();
    }
    
    /* ------------------------------------------------------------ */
    
    @Override
    public void setHeader(String nameString value)
    {
        if ("content-length".equalsIgnoreCase(name))
        {
            setContentLength(Long.parseLong(value));
        }
        else if ("content-type".equalsIgnoreCase(name))
        {   
            setContentType(value);
        }
        else if ("content-encoding".equalsIgnoreCase(name))
        {   
            super.setHeader(name,value);
            if (!isCommitted())
            {
                noCompression();
            }
        }
        else
            super.setHeader(name,value);
    }
    
    /* ------------------------------------------------------------ */
    
    @Override
    {
        if (==null)
        {
            if (getResponse().isCommitted() || )
            {
                setContentLength();
                return getResponse().getOutputStream();
            }
            
        }
        else if (!=null)
            throw new IllegalStateException("getWriter() called");
        
        return ;   
    }
    /* ------------------------------------------------------------ */
    

See also:
getWriter()
    @Override
    public PrintWriter getWriter() throws IOException
    {
        if (==null)
        { 
            if (!=null)
                throw new IllegalStateException("getOutputStream() called");
            
            if (getResponse().isCommitted() || )
            {
                setContentLength();
                return getResponse().getWriter();
            }
            
        }
        return ;   
    }
    
    /* ------------------------------------------------------------ */
    
    @Override
    public void setIntHeader(String nameint value)
    {
        if ("content-length".equalsIgnoreCase(name))
        {
            =value;
            if (!=null)
                .setContentLength();
        }
        else
            super.setIntHeader(name,value);
    }
    
    /* ------------------------------------------------------------ */
    
Allows derived implementations to replace PrintWriter implementation.

Parameters:
out the out
encoding the encoding
Returns:
the prints the writer
Throws:
java.io.UnsupportedEncodingException the unsupported encoding exception
    protected PrintWriter newWriter(OutputStream out,String encodingthrows UnsupportedEncodingException
    {
        return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding));
    }
    
    /* ------------------------------------------------------------ */
    

Returns:
the underlying CompressedStream implementation
    protected abstract AbstractCompressedStream newCompressedStream(HttpServletRequest _requestHttpServletResponse responselong _contentLength2int _bufferSize2int _minCompressedSize2throws IOException;
New to GrepCode? Check out our FAQ X