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     * FileFilter で使用する、File圧縮されたレスポンスのラ?クラスです?
029     *
030     * @og.group フィルター処?
031     *
032     * @version  4.0
033     * @author   Kazuhiko Hasegawa
034     * @since    JDK5.0,
035     */
036    public class FileResponseWrapper extends HttpServletResponseWrapper {
037            /** レスポンスオブジェク?*/
038            protected HttpServletResponse origResponse = null;
039            /** サーブレ?出力ストリー?*/
040            protected ServletOutputStream stream = null;
041            /** 出力ライターオブジェク?*/
042            protected PrintWriter writer = null;
043    
044            private final String filename ;
045    
046            /**
047             * コンストラクター
048             *
049             * @param       response        レスポンス
050             * @param       filename        ファイル?
051             */
052            public FileResponseWrapper(final HttpServletResponse response,final String filename ) {
053                    super(response);
054                    this.filename = filename;
055                    origResponse = response;
056            }
057    
058            /**
059             * ServletOutputStream の実体である FileResponseStream を作?して返します?
060             *
061             * @return      ServletOutputStreamオブジェク?
062             * @throws IOException 入出力エラーが発生したと?
063             */
064            public ServletOutputStream createOutputStream() throws IOException {
065                    return (new FileResponseStream(origResponse,filename));
066            }
067    
068            /**
069             * ?ストリー??クローズします?
070             *
071             */
072            public void finishResponse() {
073                    Closer.ioClose( writer );
074                    Closer.ioClose( stream );
075            }
076    
077            /**
078             * ?ストリー?? flush() メソ?を呼び出します?
079             *
080             */
081            @Override
082            public void flushBuffer() throws IOException {
083                    stream.flush();
084            }
085    
086            /**
087             * ?ServletOutputStreamを返します?
088             *
089             * ?オブジェクトが存在しな??合?、新規に作?します?
090             *
091             * @return ServletOutputStreamオブジェク?
092             */
093            @Override
094            public ServletOutputStream getOutputStream() throws IOException {
095                    if(writer != null) {
096                            throw new IllegalStateException("getWriter() has already been called!");
097                    }
098    
099                    if(stream == null) {
100                            stream = createOutputStream();
101                    }
102                    return (stream);
103            }
104    
105            /**
106             * ?PrintWriterを返します?
107             *
108             * ?オブジェクトが存在しな??合?、新規に作?します?
109             *
110             * @return PrintWriterオブジェク?
111             */
112            @Override
113            public PrintWriter getWriter() throws IOException {
114                    if(writer != null) {
115                            return (writer);
116                    }
117    
118                    if(stream != null) {
119                            throw new IllegalStateException("getOutputStream() has already been called!");
120                    }
121    
122                    stream = createOutputStream();
123                    writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
124                    return (writer);
125            }
126    
127            /**
128             * ?ストリー????タ長を設定しま?何もしません)?
129             *
130             * @param       length  ??タ長
131             */
132            @Override
133            public void setContentLength(final int length) {
134                    // ここでは処?行いません?
135            }
136    }