001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet.filters.gzip;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.IOException;
021    import java.io.OutputStreamWriter;
022    import java.io.PrintWriter;
023    
024    import javax.servlet.ServletOutputStream;
025    import javax.servlet.http.HttpServletResponse;
026    import javax.servlet.http.HttpServletResponseWrapper;
027    
028    /**
029     * @author Jayson Falkner
030     * @author Brian Wing Shun Chan
031     */
032    public class GZipResponse extends HttpServletResponseWrapper {
033    
034            public GZipResponse(HttpServletResponse response) {
035                    super(response);
036    
037                    _response = response;
038            }
039    
040            public void finishResponse() {
041                    try {
042                            if (_writer != null) {
043                                    _writer.close();
044                            }
045                            else if (_stream != null) {
046                                    _stream.close();
047                            }
048                    }
049                    catch (IOException e) {
050                    }
051            }
052    
053            public void flushBuffer() throws IOException {
054                    if (_stream != null) {
055                            _stream.flush();
056                    }
057            }
058    
059            public ServletOutputStream getOutputStream() throws IOException {
060                    if (_writer != null) {
061                            throw new IllegalStateException();
062                    }
063    
064                    if (_stream == null) {
065                            _stream = _createOutputStream();
066                    }
067    
068                    return _stream;
069            }
070    
071            public PrintWriter getWriter() throws IOException {
072                    if (_writer != null) {
073                            return _writer;
074                    }
075    
076                    if (_stream != null) {
077                            throw new IllegalStateException();
078                    }
079    
080                    _stream = _createOutputStream();
081    
082                    _writer = new UnsyncPrintWriter(new OutputStreamWriter(
083                            //_stream, _res.getCharacterEncoding()));
084                            _stream, StringPool.UTF8));
085    
086                    return _writer;
087            }
088    
089            private ServletOutputStream _createOutputStream() throws IOException {
090                    return new GZipStream(_response);
091            }
092    
093            private HttpServletResponse _response;
094            private ServletOutputStream _stream;
095            private PrintWriter _writer;
096    
097    }