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    }