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.plugin.daemon;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.fukurou.util.LogWriter;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.fukurou.util.HybsTimerTask;
022import org.opengion.fukurou.util.URLConnect;
023import org.opengion.fukurou.util.XHTMLTag;
024
025import java.io.IOException;
026import java.util.Date;
027
028/**
029 * 【URLアクセス】
030 * 指定したパラメータでURLに接続します。
031 * このクラスは、HybsTimerTask を継承した タイマータスククラスです。
032 * startDaemon() がタイマータスクによって、呼び出されます。
033 *
034 * 接続のためのパラメータは以下です
035 * url                   : 接続先URL(必須)
036 * proxyHost     : プロキシのホスト名
037 * proxyPort     : プロキシのポート番号
038 * useSystemUser : デフォルトのユーザ/パスワードを利用するか(初期値:true)
039 *                                      trueの場合はSYSTEM:*********を利用します。
040 * authUserPass  : ユーザとパスワードをUSER:PASSWORDの形で記述
041 * keys                  : リクエストパラメータのキー(カンマ区切り)
042 * vals                  : リクエストパラメータの値(カンマ区切り)
043 * method                : POSTかGETを指定(初期値:GET)
044 * debug                 : 接続したページを受信して、ログに書き出します(初期値:false)
045 *
046 * 接続エラー時のログはファイル(SYS_LOG_URL)に出力されます。
047 *
048 * @og.rev 4.3.4.4 (2009/01/01) 新規作成
049 * @og.group デーモン
050 *
051 * @version  4.0
052 * @author   Takahashi Masakazu
053 * @since    JDK5.0,
054 */
055public class Daemon_URLConnect extends HybsTimerTask {
056        //* このプログラムのVERSION文字列を設定します。   {@value} */
057        private static final String VERSION = "4.3.4.4 (2009/01/01)" ;
058
059        private static final String DEFAULT_USER = "SYSTEM:MANAGER" ;
060        private static final int LOOP_COUNTER = 24; // カウンタを24回に設定
061        
062        private static final String[] POST_LIST = new String[] {
063                        "POST","PUT","DELETE" }; // 5.10.10.0 (2019/03/29)
064
065        private int loopCnt = 0;
066
067        private boolean debug = false;
068        private boolean useSystemUser   = true;
069        private String  method                  = "GET";
070        private String  urlStr                  = null;
071        private int             proxyPort               = -1;
072
073        private URLConnect conn                 = null;
074
075        /**
076         * このタイマータスクによって初期化されるアクションです。
077         * パラメータを使用した初期化を行います。
078         *
079         * @og.rev 5.10.10.0 (2019/03/29) PUT,DELETE対応
080         */
081        @Override
082        public void initDaemon() {
083                debug = StringUtil.nval( getValue( "DEBUG" ),debug ) ;
084                useSystemUser           = StringUtil.nval( getValue( "useSystemUser" ), useSystemUser );
085                method                          = StringUtil.nval( getValue( "method" ), method );
086                urlStr                          = getValue( "url" );
087                proxyPort                       = StringUtil.nval( getValue( "proxyPort" ), proxyPort );
088                String proxyHost        = getValue( "proxyHost" );
089                String keys                     = getValue( "keys" );
090                String vals                     = getValue( "vals" );
091                String authUserPass     = getValue( "authUserPass" );
092
093                if( useSystemUser ) { authUserPass = DEFAULT_USER; }
094
095                String urlEnc = XHTMLTag.urlEncode( keys,vals );
096
097                if( ! "POST".equals( method ) ) {
098                        urlStr = XHTMLTag.addUrlEncode( urlStr,urlEnc );
099                }
100                conn = new URLConnect( urlStr,authUserPass );
101
102                if( proxyHost != null ) {
103                        conn.setProxy( proxyHost,proxyPort );
104                }
105
106//              if( "POST".equals(method) && keys != null && vals != null ) {
107                if( check(method,POST_LIST) && keys != null && vals != null ) { // 5.10.10.0 (2019/03/29)
108                        conn.setPostData( urlEnc );
109                        conn.setMethodOverride( method ); // 5.10.10.0 (2019/03/29)
110                }
111        }
112
113        /**
114         * タイマータスクのデーモン処理の開始ポイントです。
115         *
116         */
117        @Override
118        protected void startDaemon() {
119                if( loopCnt % LOOP_COUNTER == 0 ) {
120                        loopCnt = 1;
121                        System.out.println( toString() + " " + new Date()  + " " );
122                }
123                else {
124                        loopCnt++ ;
125                }
126                // URLへのconnect及びデータ取得実行
127                try{
128                        conn.connect();
129
130                        if(debug){
131                                // System.out.println( conn.readData() );
132                                String debugMsg = "Daemon_URLConnect:url=[" + urlStr + "]" + HybsSystem.CR
133                                                                + conn.readData();
134                                LogWriter.log( debugMsg );
135                        }
136                }
137                catch( IOException ex ) {
138                        System.out.println(ex);
139                        String errMsg = "Daemon_URLConnect:データ取得中にエラーが発生しました。" + HybsSystem.CR
140                                                + " url=[" + urlStr + "]" + HybsSystem.CR
141                                                + ex;
142                        LogWriter.log( errMsg );
143                }
144                finally {
145                        if( conn != null ) { conn.disconnect(); }
146                }
147        }
148        
149        /**
150         * 引数 in が、引数 check の文字列配列の中に存在すれば、 true を、存在しなければ、false を返します。
151         *
152         * check は、 String配列 を、in は、null でも構いません。
153         *
154         * @og.rev 5.10.10.0 (2019/03/29) 追加
155         *
156         * @param    in    チェックする文字列
157         * @param    check チェック用の基本文字列配列
158         *
159         * @return   存在する true /  存在しない false
160         */
161        private boolean check( final String in, final String[] check ) {
162                if( in == null || check == null ) { return false; }
163                for( int i=0; i<check.length; i++ ) {
164                        if( in.equals( check[i] ) ) { return true; }
165                }
166                return false ;
167        }
168
169        /**
170         * このタイマータスクのcancel() メソッドをオーバーライドします。
171         * HybsTimerTaskManager#cancelTask( int ) を実行します。
172         *
173         * @og.rev 4.3.1.1 (2008/08/23) super.cancel() のみ実行なら、オーバーライドの必要はない
174         *
175         * @see java.util.TimerTask#cancel()
176         */
177//      public boolean cancel() {
178//              return super.cancel();
179//      }
180}