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.resource;
017
018import org.opengion.hayabusa.common.HybsSystem ;
019import org.opengion.fukurou.util.StringUtil ;
020import org.opengion.fukurou.util.ApplicationInfo;
021import org.opengion.fukurou.db.DBUtil;
022import org.opengion.fukurou.security.HybsCryptography;
023
024/**
025 * 指定のURLをランダムキー化したり、そのキーより実URLへ転送したりします。
026 *
027 * 通常のURLは、引数にキーとバリュー値をセットして文字列連結します。そのままでは
028 * URLが長くなったり、引数の一部を書き換えたりできてしまいます。
029 * このクラスでは、GE17(URL転送テーブル)に引数付きURLを登録するとともに、
030 * ランダムキーを作成します。
031 * また、このランダムキーを元にGE17を検索し、元のURLに転送する機能があります。
032 *
033 * @og.rev 4.0.0.0 (2004/12/31) 新規作成
034 * @og.group リソース管理
035 *
036 * @version  4.0
037 * @author   Kazuhiko Hasegawa
038 * @since    JDK5.0,
039 */
040public final class URLXfer {
041
042        // URL転送テーブル(GE17)にデータを書き込むためのSQLです。
043        private static final String INSERT_SQL = "INSERT INTO GE17 (SYSTEM_ID,RANDOM_KEY,NAME_JA,DYVALID,REDIRECT_URL,"
044                                                                                        + "FGJ,DYSET,DYUPD,USRSET,USRUPD,PGUPD) "
045                                                                                        + " VALUES ( ?,?,?,?,?,'1',?,?,?,?,'URLXfer' )" ;
046
047        // URL転送テーブル(GE17)からデータを読み込むためのSQLです。
048        private static final String SELECT_SQL = "SELECT REDIRECT_URL FROM GE17"
049                                                                                        + " WHERE SYSTEM_ID = ? AND RANDOM_KEY = ? AND DYVALID >= ?" ;
050
051        private final String DBID               ;               // URL転送テーブルアクセスの接続先
052        private final String ADDRESS    ;               // URL転送テーブルアクセスアドレス
053        private final String SYSTEM_ID  ;               // システムID
054
055        /** コネクションにアプリケーション情報を追記するかどうか指定 */
056        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
057
058        // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
059        private final ApplicationInfo appInfo;
060
061        /**
062         * コンストラクター
063         *
064         * アドレスが存在しない場合は、初期化メソッドを呼び出します。
065         *
066         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
067         */
068        public URLXfer() {
069                DBID = HybsSystem.sys( "RESOURCE_DBID" );
070                ADDRESS = StringUtil.nval(
071                                                                HybsSystem.sys( "RESOURCE_ADDRESS" ) ,
072                                                                HybsSystem.sys( "CONTEXT_URL" ) + "jsp/index.jsp"
073                                        )  + "?XFER=";
074
075                SYSTEM_ID = HybsSystem.sys( "SYSTEM_ID" );
076
077                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
078                if( USE_DB_APPLICATION_INFO ) {
079                        appInfo = new ApplicationInfo();
080                        // ユーザーID,IPアドレス,ホスト名
081                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
082                        // 画面ID,操作,プログラムID
083                        appInfo.setModuleInfo( "URLXfer",null,null );
084                }
085                else {
086                        appInfo = null;
087                }
088        }
089
090        /**
091         * URL文字列のデータを元に、URLXferオブジェクトを構築します。
092         * データをGE17(URL転送テーブル)に登録するとともに、ランダムキーを作成(XFER)して、
093         * そのランダム引数を用いたアドレスを返します。
094         * アドレスは、システムパラメータ URL_XFER_ADDRESS + "?XFER=" + ランダムキーです。
095         *
096         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
097         * @og.rev 5.2.2.0 (2010/11/01) util.StringUtil から security.HybsCryptography へ移動
098         *
099         * @param       redirectURL     転送URL
100         * @param       name                    表示名称
101         * @param       validDate               有効日時(yyyyMMdd)
102         * @param       userid          ユーザーID
103         *
104         * @return      RandomURL文字列
105         */
106        public String getRandomURL( final String redirectURL,final String name,final String validDate,final String userid ) {
107
108                String DYSET = HybsSystem.getDate( "yyyyMMddHHmmss" );
109                String key = redirectURL + Math.random() ;
110                String randomKey = HybsCryptography.getMD5( key );      // 5.2.2.0 (2010/11/01) クラス変更
111
112                String validYMD  = ( validDate != null ) ? validDate : "99999999" ;
113
114                String[] args = new String[] { SYSTEM_ID,randomKey,name,validYMD,redirectURL,DYSET,DYSET,userid,userid };
115                DBUtil.dbExecute( INSERT_SQL,args,appInfo,DBID );
116
117                return ADDRESS + randomKey ;
118        }
119
120        /**
121         * ランダムキー(XFER)の文字列より、元のURLを検索します。
122         * データはランダムキーを元に、GE17(URL転送テーブル)より取り出します。
123         * 取り出す条件は、SYSTEM_ID 、RANDOM_KEY と DYVALID が 現在時刻より大きい場合です。
124         *
125         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
126         *
127         * @param       randomKey       ハッシュ化されたキー文字列
128         *
129         * @return RedirectURL文字列
130         */
131        public String getRedirectURL( final String randomKey ) {
132                String nowDate = HybsSystem.getDate( "yyyyMMdd" );
133
134                String[] args = new String[] { SYSTEM_ID,randomKey,nowDate };
135                String[][] vals = DBUtil.dbExecute( SELECT_SQL,args,appInfo,DBID );
136
137                if( vals != null && vals.length > 0 && vals[0] != null && vals[0].length > 0 ) {
138                        return vals[0][0];
139                }
140                return null;
141        }
142
143        /**
144         * オブジェクトの識別子として,詳細なユーザー情報を返します。
145         *
146         * @return  詳細なユーザー情報
147         */
148        @Override
149        public String toString() {
150                StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
151                rtn.append( "DBID :"            ).append( DBID );
152                rtn.append( " ADDRESS :"        ).append( ADDRESS );
153                rtn.append( " SYSTEM_ID :"      ).append( SYSTEM_ID );
154                return rtn.toString();
155        }
156}