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