All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.seleniumhq.jetty7.http.gzip.GzipResponseWrapper Maven / Gradle / Ivy

There is a newer version: 9.4.12.v20180830.1
Show newest version
// ========================================================================
// Copyright (c) Webtide LLC
// ------------------------------------------------------------------------
// 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.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.eclipse.jetty.util.StringUtil;


/* ------------------------------------------------------------ */
/**
 */
public class GzipResponseWrapper extends HttpServletResponseWrapper
{
    public static final int DEFAULT_BUFFER_SIZE = 8192;
    public static final int DEFAULT_MIN_GZIP_SIZE = 256;
    
    private HttpServletRequest _request;
    private Set _mimeTypes;
    private int _bufferSize=DEFAULT_BUFFER_SIZE;
    private int _minGzipSize=DEFAULT_MIN_GZIP_SIZE;

    private PrintWriter _writer;
    private GzipStream _gzStream;
    private long _contentLength=-1;
    private boolean _noGzip;

    /**
     * Instantiates a new gzip response wrapper.
     *
     * @param request the request
     * @param response the response
     */
    public GzipResponseWrapper(HttpServletRequest request, HttpServletResponse response)
    {
        super(response);
        _request=request;
    }

    /* ------------------------------------------------------------ */
    /**
     * Sets the mime types.
     *
     * @param mimeTypes the new mime types
     */
    public void setMimeTypes(Set mimeTypes)
    {
        _mimeTypes = mimeTypes;
    }

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.ServletResponseWrapper#setBufferSize(int)
     */
    public void setBufferSize(int bufferSize)
    {
        _bufferSize = bufferSize;
    }

    /* ------------------------------------------------------------ */
    /**
     * Sets the min gzip size.
     *
     * @param minGzipSize the new min gzip size
     */
    public void setMinGzipSize(int minGzipSize)
    {
        _minGzipSize = minGzipSize;
    }

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.ServletResponseWrapper#setContentType(java.lang.String)
     */
    public void setContentType(String ct)
    {
        super.setContentType(ct);

        if (ct!=null)
        {
            int colon=ct.indexOf(";");
            if (colon>0)
                ct=ct.substring(0,colon);
        }

        if ((_gzStream==null || _gzStream._out==null) && 
            (_mimeTypes==null && "application/gzip".equalsIgnoreCase(ct) ||
             _mimeTypes!=null && (ct==null||!_mimeTypes.contains(StringUtil.asciiToLowerCase(ct)))))
        {
            noGzip();
        }
    }

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int, java.lang.String)
     */
    public void setStatus(int sc, String sm)
    {
        super.setStatus(sc,sm);
        if (sc<200 || sc==204 || sc==205 || sc>=300)
            noGzip();
    }

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
     */
    public void setStatus(int sc)
    {
        super.setStatus(sc);
        if (sc<200 || sc==204 || sc==205 ||sc>=300)
            noGzip();
    }

    /* ------------------------------------------------------------ */
    /**
     * @see javax.servlet.ServletResponseWrapper#setContentLength(int)
     */
    public void setContentLength(int length)
    {
        setContentLength((long)length);
    }
    
    /* ------------------------------------------------------------ */
    protected void setContentLength(long length)
    {
        _contentLength=length;
        if (_gzStream!=null)
            _gzStream.setContentLength(length);
        else if (_noGzip && _contentLength>=0)
        {
            HttpServletResponse response = (HttpServletResponse)getResponse();
            if(_contentLength




© 2015 - 2024 Weber Informatics LLC | Privacy Policy