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