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.servlet;
017
018import java.io.UnsupportedEncodingException;
019import java.io.IOException;
020
021import jakarta.servlet.ServletException;
022import jakarta.servlet.ServletConfig;
023// import jakarta.servlet.RequestDispatcher;
024import jakarta.servlet.http.HttpServlet;
025import jakarta.servlet.http.HttpServletRequest;
026import jakarta.servlet.http.HttpServletResponse;
027
028import jakarta.servlet.annotation.WebInitParam;                                 // 7.3.0.0 (2021/01/06)
029import jakarta.servlet.annotation.WebServlet;                                           // 7.3.0.0 (2021/01/06)
030
031import org.opengion.hayabusa.common.HybsSystemException;
032
033/**
034 * FORM認証で、認証済みの場合、j_security_check に飛ばされてエラーになるので、強制的に sendRedirect する。
035 * Post,Get両方に対応しています。
036 *
037 * @og.rev 7.3.0.0 (2021/01/06) 新規作成。
038 * @version  7.3
039 * @author   Kazuhiko Hasegawa
040 * @since    JDK11
041 *
042 */
043@WebServlet(
044        urlPatterns = "/jsp/j_security_check" ,
045        initParams  = {
046                @WebInitParam(name="forwardURL", value="/jsp/index.jsp")
047        }
048)
049public class JSecurityCheckServlet extends HttpServlet {
050        private static final long serialVersionUID      = 730020210106L ;
051
052        private String forwardURL = "/jsp/index.jsp" ;
053
054        /**
055         * デフォルトコンストラクター
056         *
057         * @og.rev 7.3.0.0 (2021/01/06) 新規作成。
058         */
059        public JSecurityCheckServlet() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
060
061        /**
062         * Postメソッドで与えられたrequestをcallClassメソッドに渡します。
063         * callClassメソッドではclassパラメータの値を利用してクラスをロードし、処理を行います。
064         * 具体的な処理はcallClassメソッドをご覧下さい。
065         *
066         * @og.rev 7.3.0.0 (2021/01/06) 新規作成。
067         *
068         * @param request HttpServletRequestリクエスト
069         * @param response HttpServletResponseレスポンス
070         * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。
071         * @throws IOException 入出力エラーが発生したとき
072         */
073        @Override
074        public void doPost( final HttpServletRequest request, final HttpServletResponse response )      throws ServletException,IOException {
075                callClass( request, response );
076        }
077
078        /**
079         * Getメソッドで与えられたrequestをcallClassメソッドに渡します。
080         * callClassメソッドではclassパラメータの値を利用してクラスをロードし、処理を行います。
081         * 具体的な処理はcallClassメソッドをご覧下さい。
082         *
083         * @og.rev 7.3.0.0 (2021/01/06) 新規作成。
084         *
085         * @param request HttpServletRequestリクエスト
086         * @param response HttpServletResponseレスポンス
087         * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。
088         * @throws IOException 入出力エラーが発生したとき
089         */
090        @Override
091        public void doGet( final HttpServletRequest request, final HttpServletResponse response )       throws ServletException,IOException {
092                callClass( request, response );
093        }
094
095        /**
096         * Servlet の 初期値設定を行います。
097         *
098         * @og.rev 7.3.0.0 (2021/01/06) 新規作成。
099         *
100         * WEB-INF/web.xml ファイルで、<servlet> タグ内で初期値設定を行います。
101         * <init-param>
102         *     <param-name>forwardURL</param-name>
103         *     <param-value>/jsp/index.jsp</param-value>
104         * </init-param>
105         *
106         * @param       config  ServletConfigオブジェクト
107         */
108        @Override
109        public void init( final ServletConfig config ) throws ServletException {
110                super.init( config );
111
112                final String url = config.getInitParameter("forwardURL");
113                if( url != null && !url.isEmpty() ) {
114                        forwardURL = url;
115                }
116        }
117
118        /**
119         * POSTとGETに対する実際の処理です
120         * sendRedirect先の URL をforwardURL で初期設定されたアドレスに転送します。
121         *
122         * @param request リクエスト
123         * @param response レスポンス
124         * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。
125         * @throws IOException 入出力エラーが発生したとき
126         */
127        private void callClass( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException,IOException {
128                try {
129                        request.setCharacterEncoding( "UTF-8" );
130                }
131                catch( final UnsupportedEncodingException ex ) {
132                        throw new HybsSystemException( ex );
133                }
134
135        // forward は、元のアドレスが残ってしまう。
136        //      final RequestDispatcher dispatch = request.getRequestDispatcher(forwardURL);
137        //      dispatch.forward(request, response);
138
139                String url = "/" ;
140                if( request.authenticate(response) ) {          // 認証済みの場合に、もう一度呼ばれると、true になっている。
141                        final String refe = request.getHeader( "referer" );     // 元のアドレスにリダイレクトする。
142                        if( refe != null && !refe.isEmpty() ) { url = refe; }
143                }
144                else {
145                        // sendRedirect で、アドレスを変えてしまう。
146                        // ただし、forward は、自分のコンテキスト基準だが、sendRedirect はアプリケーション基準
147                        final String cntxPath = request.getContextPath();               // /gf などのコンテキストパス
148                        url = cntxPath + forwardURL ;
149                }
150                response.sendRedirect(url);
151        }
152}