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 int loopCnt = 0;
063
064        private boolean debug = false;
065        private boolean useSystemUser   = true;
066        private String  method                  = "GET";
067        private String  urlStr                  = null;
068        private int             proxyPort               = -1;
069
070        private URLConnect conn                 = null;
071
072        /**
073         * このタイマータスクによって初期化されるアクションです。
074         * パラメータを使用した初期化を行います。
075         *
076         */
077        @Override
078        public void initDaemon() {
079                debug = StringUtil.nval( getValue( "DEBUG" ),debug ) ;
080                useSystemUser           = StringUtil.nval( getValue( "useSystemUser" ), useSystemUser );
081                method                          = StringUtil.nval( getValue( "method" ), method );
082                urlStr                          = getValue( "url" );
083                proxyPort                       = StringUtil.nval( getValue( "proxyPort" ), proxyPort );
084                String proxyHost        = getValue( "proxyHost" );
085                String keys                     = getValue( "keys" );
086                String vals                     = getValue( "vals" );
087                String authUserPass     = getValue( "authUserPass" );
088
089                if( useSystemUser ) { authUserPass = DEFAULT_USER; }
090
091                String urlEnc = XHTMLTag.urlEncode( keys,vals );
092
093                if( ! "POST".equals( method ) ) {
094                        urlStr = XHTMLTag.addUrlEncode( urlStr,urlEnc );
095                }
096                conn = new URLConnect( urlStr,authUserPass );
097
098                if( proxyHost != null ) {
099                        conn.setProxy( proxyHost,proxyPort );
100                }
101
102                if( "POST".equals(method) && keys != null && vals != null ) {
103                        conn.setPostData( urlEnc );
104                }
105        }
106
107        /**
108         * タイマータスクのデーモン処理の開始ポイントです。
109         *
110         */
111        @Override
112        protected void startDaemon() {
113                if( loopCnt % LOOP_COUNTER == 0 ) {
114                        loopCnt = 1;
115                        System.out.println( toString() + " " + new Date()  + " " );
116                }
117                else {
118                        loopCnt++ ;
119                }
120                // URLへのconnect及びデータ取得実行
121                try{
122                        conn.connect();
123
124                        if(debug){
125                                // System.out.println( conn.readData() );
126                                String debugMsg = "Daemon_URLConnect:url=[" + urlStr + "]" + HybsSystem.CR
127                                                                + conn.readData();
128                                LogWriter.log( debugMsg );
129                        }
130                }
131                catch( IOException ex ) {
132                        System.out.println(ex);
133                        String errMsg = "Daemon_URLConnect:データ取得中にエラーが発生しました。" + HybsSystem.CR
134                                                + " url=[" + urlStr + "]" + HybsSystem.CR
135                                                + ex;
136                        LogWriter.log( errMsg );
137                }
138                finally {
139                        if( conn != null ) { conn.disconnect(); }
140                }
141        }
142
143        /**
144         * このタイマータスクのcancel() メソッドをオーバーライドします。
145         * HybsTimerTaskManager#cancelTask( int ) を実行します。
146         *
147         * @og.rev 4.3.1.1 (2008/08/23) super.cancel() のみ実行なら、オーバーライドの必要はない
148         *
149         * @see java.util.TimerTask#cancel()
150         */
151//      public boolean cancel() {
152//              return super.cancel();
153//      }
154}