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 */
016package org.opengion.hayabusa.filter;
017
018import org.opengion.fukurou.util.Closer;
019
020import java.io.PrintWriter;
021import java.io.IOException;
022import java.io.OutputStreamWriter;
023import javax.servlet.ServletOutputStream;
024import javax.servlet.http.HttpServletResponseWrapper;
025import 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 */
036public 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}