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.taglib;
017
018import org.opengion.hayabusa.common.HybsSystemException;
019import org.opengion.fukurou.util.ToString;                                              // 6.1.1.0 (2015/01/17)
020
021import static org.opengion.fukurou.util.StringUtil.nval ;
022
023import jakarta.servlet.http.HttpServletResponse ;
024import java.io.IOException;
025
026/**
027 * レスポンスヘッダー情報をセットするタグです。
028 *
029 * レスポンスヘッダーには、キャッシュコントロールやリフレッシュ(ページ自動転送)などを行う
030 * ヘッダー情報をセットすることで、HTML の振る舞いを制御することができます。
031 *
032 * @og.formSample
033 * ●形式:<og:responseHeader cacheKey="[・・・]" />
034 * ●body:なし
035 *
036 * ●Tag定義:
037 *   <og:responseHeader
038 *       cacheControl       【TAG】レスポンスヘッダ に、Cache-Control の値を設定します(初期値:"max-age=0")
039 *       contentType        【TAG】レスポンスヘッダ に、content-Type の値を設定します
040 *       refresh            【TAG】レスポンスヘッダ に、refresh の値を設定します
041 *       refreshURL         【TAG】レスポンスヘッダ に、refresh の値を設定するときに、指定のURLをロードします
042 *       redirect           【TAG】指定されたURLへ一時的なリダイレクトレスポンスを送信します
043 *       status             【TAG】ステータスコードを設定します
044 *       location           【TAG】レスポンスヘッダ に、location の値を設定します
045 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
046 *   />
047 *
048 * ●使用例
049 *
050 * @og.rev 3.1.3.0 (2003/04/10) ResponseHeaderTag を 新規作成しました。
051 * @og.group 画面制御
052 *
053 * @version  4.0
054 * @author   Kazuhiko Hasegawa
055 * @since    JDK5.0,
056 */
057public class ResponseHeaderTag extends CommonTagSupport {
058        /** このプログラムのVERSION文字列を設定します。   {@value} */
059        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
060        private static final long serialVersionUID = 642020160129L ;
061
062        private String   pragma                 ;
063        private String   cacheControl   = "max-age=0";
064        private String   contentType    ;
065        private int      refresh                = -1;
066        private String   refreshURL             ;
067        private String   redirect               ;
068        private int      status                 = -1;
069        private String   location               ;
070
071        /**
072         * デフォルトコンストラクター
073         *
074         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
075         */
076        public ResponseHeaderTag() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
077
078        /**
079         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
080         *
081         * @og.rev 3.1.9.0 (2003/05/16) refresh 属性を設定した場合は、ページの残りを処理しないように変更。
082         *
083         * @return      後続処理の指示
084         */
085        @Override
086        public int doEndTag() {
087                debugPrint();           // 4.0.0 (2005/02/28)
088                int rtn = EVAL_PAGE;            // ページの残りを評価する。
089
090                final HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
091
092                if( pragma != null ) {
093                        response.setHeader( "Pragma",pragma );
094                }
095
096                if( cacheControl != null ) {
097                        response.setHeader( "Cache-Control",cacheControl );
098                }
099
100                if( contentType != null ) {
101                        response.setContentType( contentType );
102                }
103
104                if( refresh >= 0 ) {
105                        // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid if (x != y) ..; else ..;
106                        if( refreshURL == null ) {
107                                response.setIntHeader( "Refresh",refresh );
108                        }
109                        else {
110                                final StringBuilder ref = new StringBuilder( BUFFER_MIDDLE )
111                                        .append( refresh )
112                                        .append( "; URL=" )
113                                        .append( response.encodeRedirectURL( refreshURL ) );
114                                response.setHeader( "Refresh",ref.toString() );
115                        }
116                        rtn = SKIP_PAGE;                // ページの残りの処理を行わない。
117                }
118
119                if( redirect != null ) {
120                        try {
121                                response.sendRedirect( response.encodeRedirectURL( redirect ) );
122                        }
123                        catch( final IOException ex ) {
124                                final String errMsg = "sendRedirect に失敗しました。" + CR
125                                                        + " URL=" + redirect + CR
126                                                        + ex.getMessage();                                      // 5.1.8.0 (2010/07/01) errMsg 修正
127                                throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
128                        }
129                }
130
131                if( status >= 0 ) {
132                        response.setStatus( status );
133                }
134
135                if( location != null ) {
136                        response.setHeader( "Location",location );
137                }
138
139                return rtn ;
140        }
141
142        /**
143         * タグリブオブジェクトをリリースします。
144         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
145         *
146         */
147        @Override
148        protected void release2() {
149                super.release2();
150                pragma       = null;
151                cacheControl = "max-age=0";
152                contentType  = null;
153                refresh      = -1;
154                refreshURL   = null;
155                redirect     = null;
156                status       = -1;
157                location     = null;
158        }
159
160        /**
161         * 【TAG】レスポンスヘッダ に、Cache-Control の値を設定します(初期値:"max-age=0")。
162         *
163         * @og.tag
164         * このヘッダは、クライアントに対してドキュメントをキャッシュする場合の
165         * 条件を伝えます。初期値は、max-age=0 に設定しています。
166         * 指定する値は、以下のどれかです。
167         *
168         * public        : ドキュメントをキャッシュして良い
169         * private       : ドキュメントが共有されないプライベートの中なら、キャッシュして良い。
170         * no-cache      : ドキュメントをキャッシュしてはいけない。
171         * no-store      : ドキュメントのキャッシュや、ディスク上の一時ファイルも禁止する。
172         * must-revalidate
173         *               : クライアントは、ドキュメントをプロキシではなく、本来の
174         *                 サーバーに確認する必要がある。
175         * proxy-revalidate
176         *               : must-revalidate と同じであるが、共有キャッシュに対してのみ
177         *                 適用される。
178         * max-age=xxx   : ドキュメントが、xxx秒後に陳腐化する。Expires より優先される。
179         * s-max-age=xxx : 共有キャッシュは、ドキュメントが、xxx秒後に陳腐化する。
180         *
181         * @og.rev 3.1.5.1 (2003/04/24) 初期値を、"max-age=0" に変更。
182         * @param       cc Cache-Control
183         */
184        public void setCacheControl( final String cc ) {
185                cacheControl = nval( getRequestParameter( cc ),cacheControl );
186                if( "no-cache".equals( cacheControl ) ) {
187                        pragma = "no-cache";
188                }
189        }
190
191        /**
192         * 【TAG】レスポンスヘッダ に、content-Type の値を設定します。
193         *
194         * @og.tag
195         * このヘッダは、これから返すドキュメントのMIMEタイプを与えます。
196         * MIMEタイプの詳しい規格は、RFC1521 と、RFC1522 です。
197         * <a href="http://www.isi.edu/in-notes/iana/assignments/media-types/media-types" >最新リスト</a>
198         * <a href="http://www.ltsw.se/knbase/internet/mime.htp">未登録タイプ(x-タイプ)</a>
199         *
200         * @param       ct content-Type
201         */
202        public void setContentType( final String ct ) {
203                contentType = nval( getRequestParameter( ct ),contentType );
204        }
205
206        /**
207         * 【TAG】レスポンスヘッダ に、refresh の値を設定します。
208         *
209         * @og.tag
210         * レスポンスヘッダのrefresh の値は、更新されたページをブラウザが
211         * 今から何秒後にリクエストすればよいかということを伝えます。
212         * つまり、指定した秒数後に、再リクエストさせる事が可能になります。
213         *
214         * @param       ref 画面更新(秒)
215         */
216        public void setRefresh( final String ref ) {
217                refresh = nval( getRequestParameter( ref ),refresh );
218        }
219
220        /**
221         * 【TAG】レスポンスヘッダ に、refresh の値を設定するときに、指定のURLをロードします。
222         *
223         * @og.tag
224         * このヘッダは、refresh と共に使用され、リクエストする場合のURLを指定します。
225         *
226         * @og.rev 3.1.4.0 (2003/04/18) 属性名変更。(refreshUrl ⇒ refreshURL)
227         *
228         * @param       refurl 再リクエストさせるURL
229         */
230        public void setRefreshURL( final String refurl ) {
231                refreshURL = nval( getRequestParameter( refurl ),refreshURL );
232        }
233
234        /**
235         * 【TAG】指定されたURLへ一時的なリダイレクトレスポンスを送信します。
236         *
237         * @og.tag
238         * 指定されたリダイレクト先のURLを用いて、 クライアントに一時的な
239         * リダイレクトレスポンスを送信します。
240         * URLとしては相対URLを指定することができます。
241         *
242         * @og.rev 3.6.0.0 (2004/09/17) \\\\hn51d4 などのネットワーク名への対応
243         *
244         * @param       rd リダイレクするURL
245         */
246        public void setRedirect( final String rd ) {
247                redirect = nval( getRequestParameter( rd ),redirect );
248                if( redirect != null && redirect.startsWith( "\\\\" ) ) {
249                        redirect = "file://" + redirect;
250                }
251        }
252
253        /**
254         * 【TAG】ステータスコードを設定します。
255         *
256         * @og.tag
257         * ステータスコードを設定します。
258         * 100 ~ 199  100番台はおしらせ的な情報です。
259         * 200 ~ 299  200番台はリクエストが成功したことを表します。
260         * 300 ~ 399  300番台はファイルが移動したことを表します。
261         * 400 ~ 499  400番台はクライアント側のエラーを表します。
262         * 500 ~ 599  500番台はサーバー側のエラーを表します。
263         *
264         * @param       st ステータスコード
265         */
266        public void setStatus( final String st ) {
267                status = nval( getRequestParameter( st ),status );
268        }
269
270        /**
271         * 【TAG】レスポンスヘッダ に、location の値を設定します。
272         *
273         * @og.tag
274         * このヘッダは、ドキュメントのアドレスを通知します。
275         * 300番台のステータスコードには、このヘッダが必ず付随する必要があります。
276         *
277         * @param       lo ドキュメントのアドレス(ロケーション)
278         */
279        public void setLocation( final String lo ) {
280                location = nval( getRequestParameter( lo ),location );
281        }
282
283        /**
284         * このオブジェクトの文字列表現を返します。
285         * 基本的にデバッグ目的に使用します。
286         *
287         * @return このクラスの文字列表現
288         * @og.rtnNotNull
289         */
290        @Override
291        public String toString() {
292                return ToString.title( this.getClass().getName() )
293                                .println( "VERSION"             ,VERSION                )
294                                .println( "pragma"              ,pragma                 )
295                                .println( "cacheControl",cacheControl   )
296                                .println( "contentType" ,contentType    )
297                                .println( "refresh"             ,refresh                )
298                                .println( "refreshURL"  ,refreshURL             )
299                                .println( "redirect"    ,redirect               )
300                                .println( "status"              ,status                 )
301                                .println( "location"    ,location               )
302                                .println( "Other..."    ,getAttributes().getAttribute() )
303                                .fixForm().toString() ;
304        }
305}