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.resource;
017    
018    import org.opengion.hayabusa.common.HybsSystem ;
019    import org.opengion.fukurou.util.StringUtil ;
020    import org.opengion.fukurou.util.ApplicationInfo;
021    import org.opengion.fukurou.db.DBUtil;
022    import 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     */
040    public 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               ;               // ???転送テーブルアクセスの接続?
052            private final String ADDRESS    ;               // ???転送テーブルアクセスアドレス
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,操?プログラ?D
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 = StringUtil.getMD5( key );
111                    String randomKey = HybsCryptography.getMD5( key );      // 5.2.2.0 (2010/11/01) クラス変更
112    
113                    String validYMD  = ( validDate != null ) ? validDate : "99999999" ;
114    
115                    String[] args = new String[] { SYSTEM_ID,randomKey,name,validYMD,redirectURL,DYSET,DYSET,userid,userid };
116                    DBUtil.dbExecute( INSERT_SQL,args,appInfo,DBID );
117    
118                    return ADDRESS + randomKey ;
119            }
120    
121            /**
122             * ラン?キー(XFER)の??より、?のURLを検索します?
123             * ??タはラン?キーを?に、GE17(URL転送テーブル)より取り出します?
124             * 取り出す条件は、SYSTEM_ID 、RANDOM_KEY と DYVALID ?現在時刻より大きい場合です?
125             *
126             * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得?為,ApplicationInfoオブジェクトを設?
127             *
128             * @param       randomKey       ハッシュ化されたキー??
129             *
130             * @return RedirectURL??
131             */
132            public String getRedirectURL( final String randomKey ) {
133                    String nowDate = HybsSystem.getDate( "yyyyMMdd" );
134    
135                    String[] args = new String[] { SYSTEM_ID,randomKey,nowDate };
136                    String[][] vals = DBUtil.dbExecute( SELECT_SQL,args,appInfo,DBID );
137    
138                    if( vals != null && vals.length > 0 && vals[0] != null && vals[0].length > 0 ) {
139                            return vals[0][0];
140                    }
141                    return null;
142            }
143    
144            /**
145             * オブジェクト?識別子として?詳細なユーザー??を返します?
146             *
147             * @return  詳細なユーザー??
148             */
149            @Override
150            public String toString() {
151                    StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
152                    rtn.append( "DBID :"            ).append( DBID );
153                    rtn.append( " ADDRESS :"        ).append( ADDRESS );
154                    rtn.append( " SYSTEM_ID :"      ).append( SYSTEM_ID );
155                    return rtn.toString();
156            }
157    }