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.system.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 ;
039        /** サーブレット出力ストリーム */
040        protected ServletOutputStream stream ;
041        /** 出力ライターオブジェクト */
042        protected PrintWriter writer ;
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         * @og.rtnNotNull
063         * @throws IOException 入出力エラーが発生したとき
064         */
065        public ServletOutputStream createOutputStream() throws IOException {
066                return new FileResponseStream(origResponse,filename);
067        }
068
069        /**
070         * 内部ストリーム を クローズします。
071         *
072         */
073        public void finishResponse() {
074                Closer.ioClose( writer );
075                Closer.ioClose( stream );
076        }
077
078        /**
079         * 内部ストリームの flush() メソッドを呼び出します。
080         *
081         * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
082         */
083        @Override
084        public void flushBuffer() throws IOException {
085                if( stream != null ) {
086                        stream.flush();
087                }
088        }
089
090        /**
091         * 内部ServletOutputStreamを返します。
092         *
093         * 内部オブジェクトが存在しない場合は、新規に作成します。
094         *
095         * @return ServletOutputStreamオブジェクト
096         */
097        @Override
098        public ServletOutputStream getOutputStream() throws IOException {
099                if( writer != null ) {
100                        throw new IllegalStateException("getWriter() has already been called!");
101                }
102
103                if( stream == null ) {
104                        stream = createOutputStream();
105                }
106                return stream;
107        }
108
109        /**
110         * 内部PrintWriterを返します。
111         *
112         * 内部オブジェクトが存在しない場合は、新規に作成します。
113         *
114         * @return PrintWriterオブジェクト
115         */
116        @Override
117        public PrintWriter getWriter() throws IOException {
118                if( writer != null ) {
119                        return writer;
120                }
121
122                if( stream != null ) {
123                        throw new IllegalStateException("getOutputStream() has already been called!");
124                }
125
126                stream = createOutputStream();
127                writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
128                return writer;
129        }
130
131        /**
132         * 内部ストリームのデータ長を設定します(何もしません)。
133         *
134         * @param       length  データ長
135         */
136        @Override
137        public void setContentLength(final int length) {
138                // ここでは処理を行いません。
139        }
140}