001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.hayabusa.filter;
017    
018    import org.opengion.fukurou.util.Closer;
019    
020    import java.io.PrintWriter;
021    import java.io.IOException;
022    import java.io.OutputStreamWriter;
023    import javax.servlet.ServletOutputStream;
024    import javax.servlet.http.HttpServletResponseWrapper;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * GZIPFilter で使用する、GZIP圧縮されたレスポンスのラãƒ?ƒ‘クラスですã?
029     *
030     * @og.group フィルター処ç�?
031     *
032     * @version  4.0
033     * @author   Kazuhiko Hasegawa
034     * @since    JDK5.0,
035     */
036    public class GZIPResponseWrapper extends HttpServletResponseWrapper {
037            protected HttpServletResponse origResponse = null;
038            protected ServletOutputStream stream = null;
039            protected PrintWriter writer = null;
040    
041            /**
042             * コンストラクター
043             *
044             * @param       response        HttpServletResponseオブジェクãƒ?
045             */
046            public GZIPResponseWrapper(final HttpServletResponse response) {
047                    super(response);
048                    origResponse = response;
049            }
050    
051            /**
052             * ServletOutputStream の実体である GZIPResponseStream を作æ?して返しますã?
053             *
054             * @return      ServletOutputStreamオブジェクãƒ?
055             * @throws IOException オブジェクトã?作æ?に失敗したときã?throw されますã?
056             */
057            public ServletOutputStream createOutputStream() throws IOException {
058                    return (new GZIPResponseStream(origResponse));
059            }
060    
061            /**
062             * å†?ƒ¨ストリーãƒ?ã‚?クローズしますã?
063             *
064             */
065            public void finishResponse() {
066                    Closer.ioClose( writer );
067                    Closer.ioClose( stream );
068            }
069    
070            /**
071             * å†?ƒ¨ストリーãƒ?? flush() メソãƒ?ƒ‰を呼び出しますã?
072             *
073             */
074            @Override
075            public void flushBuffer() throws IOException {
076                    stream.flush();
077            }
078    
079            /**
080             * å†?ƒ¨ServletOutputStreamを返しますã?
081             *
082             * å†?ƒ¨オブジェクトが存在しなã�??合ã?、新規に作æ?しますã?
083             *
084             * @return      ServletOutputStreamオブジェクãƒ?
085             */
086            @Override
087            public ServletOutputStream getOutputStream() throws IOException {
088                    if(writer != null) {
089                            throw new IllegalStateException("getWriter() has already been called!");
090                    }
091    
092                    if(stream == null) {
093                            stream = createOutputStream();
094                    }
095                    return (stream);
096            }
097    
098            /**
099             * å†?ƒ¨PrintWriterを返しますã?
100             *
101             * å†?ƒ¨オブジェクトが存在しなã�??合ã?、新規に作æ?しますã?
102             *
103             * @return PrintWriterオブジェクãƒ?
104             */
105            @Override
106            public PrintWriter getWriter() throws IOException {
107                    if(writer != null) {
108                            return (writer);
109                    }
110    
111                    if(stream != null) {
112                            throw new IllegalStateException("getOutputStream() has already been called!");
113                    }
114    
115                    stream = createOutputStream();
116                    writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
117                    return (writer);
118            }
119    
120            /**
121             * å†?ƒ¨ストリーãƒ??ãƒ??タ長を設定しまã�?何もしません)ã€?
122             *
123             * @param       length  ãƒ??タ長
124             */
125            @Override
126            public void setContentLength(final int length) {
127                    // ここでは処ç�?‚’行いませんã€?
128            }
129    }