Package org.internna.iwebmvc.performance

Source Code of org.internna.iwebmvc.performance.GZIPResponseWrapper

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.performance;

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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* A servlet filter that compresses the response contents.
*
* @author Jayson Falkner [http://www.jspbook.com/]
* @author Jose Noheda
*
*/
public class GZIPResponseWrapper extends HttpServletResponseWrapper {

    private static Log log = LogFactory.getLog(GZIPResponseWrapper.class);

    protected HttpServletResponse origResponse = null;
    protected ServletOutputStream stream = null;
    protected PrintWriter writer = null;
    protected int error = -1000;

    public GZIPResponseWrapper(HttpServletResponse response) {
        super(response);
        origResponse = response;
    }

    public ServletOutputStream createOutputStream() throws IOException {
        return (new GZIPResponseStream(origResponse));
    }

    public void finishResponse() {
        try {
            if (writer != null) writer.close();
            else if (stream != null) stream.close();
        } catch (IOException e) {
            if (log.isWarnEnabled()) log.warn("Could not close GZIP response wrapper", e);
        }
    }

    @Override
    public void flushBuffer() throws IOException {
        stream.flush();
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (writer != null) throw new IllegalStateException("getWriter() has already been called!");
        if (stream == null) stream = createOutputStream();
        return stream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (this.error == HttpServletResponse.SC_FORBIDDEN) return super.getWriter();
        if (writer == null) {
            if (stream != null) throw new IllegalStateException("getOutputStream() has already been called!");
            stream = createOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(stream, origResponse.getCharacterEncoding()));
        }
        return writer;
    }

    @Override
    public void setContentLength(int length) {
        return;
    }

    @Override
    public void sendError(int error, String message) throws IOException {
        super.sendError(error, message);
        this.error = error;
    }

}
TOP

Related Classes of org.internna.iwebmvc.performance.GZIPResponseWrapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.