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.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020import org.opengion.hayabusa.resource.URLXfer;
021import org.opengion.fukurou.util.StringUtil ;
022
023import javax.servlet.http.HttpSession ;
024import javax.servlet.http.HttpServletResponse;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.jsp.tagext.TagSupport ;
027import javax.servlet.jsp.JspWriter ;
028
029import java.io.IOException;
030import java.io.UnsupportedEncodingException;
031
032/**
033 * マルチセッション起動チェックを行います。
034 *
035 * このタグは、特殊で、一番最上位のJSP(通常は、jsp/index.jsp)に仕込むことで、
036 * マルチセッション起動チェックを行います。
037 * とくに、TopMenuTag と関連しており、このタグが存在しないと、メニューが
038 * 動作しません。
039 * このタグでは、URLXfer による、リンク変換転送をサポートします。
040 * URLのXFER変数をキーに、GE17 テーブルを検索し、指定のURLへ sendRedirect します。
041 *
042 * @og.formSample
043 * ●形式:<og:jspInit />
044 * ●body:なし
045 *
046 * ●使用例
047 *    <og:jspInit />
048 *
049 * @og.rev 4.0.0.0 (2005/08/31) 新規作成
050 * @og.group メニュー制御
051 *
052 * @version  4.0
053 * @author   Kohei Naruse
054 * @since    JDK5.0,
055 */
056public class JspInitTag extends TagSupport {
057        //* このプログラムのVERSION文字列を設定します。   {@value} */
058        private static final String VERSION = "5.7.6.2 (2014/05/16)" ;
059
060        private static final long serialVersionUID = 576220140516L ;
061
062        // 3.8.0.0 (2005/06/07)
063        private static int count = 0;
064
065        /**
066         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
067         *
068         * @og.rev 4.1.1.0 (2008/02/07) UserInfo の再作成する機能を追加
069         * @og.rev 4.2.2.0 (2008/05/28) Guestユーザ対応
070         * @og.rev 4.3.4.1 (2008/12/08) UserInfo の再作成する機能を削除
071         * @og.rev 5.7.4.3 (2014/03/28) 出力する HTML は、フィルターします。
072         * @og.rev 5.7.6.2 (2014/05/16) IEのHTML5機能が有効か無効かの判定キーを削除(初期化)します。
073         * @og.rev 5.10.14.0 (2019/08/01) リクエスト変数の文字コードをUTF-8に指定
074         *
075         * @return      後続処理の指示
076         */
077        @Override
078        public int doEndTag() {
079                // 5.7.4.3 (2014/03/28) エラー時でも、debug=true があれば、継続する。
080                HttpServletRequest request = ( HttpServletRequest )pageContext.getRequest();
081                
082                // 5.10.14.0 (2019/08/01) 文字コード指定
083                try {
084                        request.setCharacterEncoding("UTF-8");
085                }catch(UnsupportedEncodingException ue) {
086                        String errMsg = "文字コードの変換に失敗しました。";
087                        throw new HybsSystemException( errMsg, ue);
088                }
089                
090                boolean debug = "true".equalsIgnoreCase( request.getParameter( "debug" ) );
091
092                // Tomcat 初期起動時の common/SystemParameter.java でエラーが発生した場合。
093                String errMsg = HybsSystem.sys( HybsSystem.LOCAL_CONTX_ERR_KEY );
094                if( errMsg != null && !debug ) {
095                        try {
096                                JspWriter out = pageContext.getOut();
097                                out.println( "<html><body><pre>" );
098                                // 5.7.4.3 (2014/03/28) 出力する HTML は、フィルターします。
099                                out.println( StringUtil.htmlFilter( errMsg ) );
100                                out.println( "</pre></body></html>" );
101                        }
102                        catch(IOException ex) {
103                                errMsg = "画面出力時の PageContext の取得時にエラーが発生しました。"
104                                                + HybsSystem.CR
105                                                + errMsg ;
106                                throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
107                        }
108                        return SKIP_PAGE ;              // ページの残りの処理を行わない。
109                }
110
111                HttpSession session = pageContext.getSession();
112                synchronized( JspInitTag.class ) {
113                        // 3.8.0.0 (2005/06/07) 同一セッションでのマルチ起動対策を行います。
114                        String cnt = String.valueOf( count++ ) ;
115                        session.setAttribute( HybsSystem.MULTI_SESSION_CHECK, cnt );
116                }
117
118                // 5.7.6.2 (2014/05/16) IEのHTML5機能が有効か無効かの判定キーを削除(初期化)します。
119                session.removeAttribute( HybsSystem.IE_HTML5_KEY );
120
121                // URLXfer による、リンク変換転送機能
122                String key = request.getParameter( "XFER" );
123                if( key != null ) {
124                        URLXfer xfer = new URLXfer();
125                        String url = xfer.getRedirectURL( key );
126                        if( url != null ) {
127                                try {
128                                        HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
129                                        response.sendRedirect( url );
130                                        return SKIP_PAGE ;              // ページの残りの処理を行わない。
131                                } catch ( IOException ex ) {
132                                        String errMsg2 = "URLの振り分け処理時に IOException が発生しました。 XFER=" + key;
133                                        throw new HybsSystemException( errMsg2,ex );
134                                }
135                        }
136                }
137
138                return EVAL_PAGE ;              // ページの残りを評価する。
139        }
140}