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.fukurou.system.OgRuntimeException ;                 // 6.4.2.0 (2016/01/29)
019import org.opengion.fukurou.system.LogWriter;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.fukurou.util.HybsTimerTask;
022        // import org.opengion.fukurou.util.URLConnect;                         // 6.9.0.0 (2018/01/31) URLConnect 廃止
023import org.opengion.fukurou.util.HttpConnect;                                   // 6.9.0.0 (2018/01/31) 新規追加
024// import org.opengion.fukurou.util.XHTMLTag;
025import static org.opengion.fukurou.system.HybsConst.CR ;                // 6.1.0.0 (2014/12/26)
026
027import java.io.IOException;
028import java.util.Date;
029import java.util.Map;                                                                                   // 6.9.0.0 (2018/01/31) 新規追加
030
031/**
032 * 【URLアクセス】
033 * 指定したパラメータでURLに接続します。
034 * このクラスは、HybsTimerTask を継承した タイマータスククラスです。
035 * startDaemon() がタイマータスクによって、呼び出されます。
036 *
037 * 接続のためのパラメータは以下です
038 * url                   : 接続先URL(必須)
039 * proxyHost     : プロキシのホスト名
040 * proxyPort     : プロキシのポート番号
041 * useSystemUser : デフォルトのユーザ/パスワードを利用するか(初期値:true)
042 *                                      trueの場合はSYSTEM:*********を利用します。
043 * authUserPass  : ユーザとパスワードをUSER:PASSWORDの形で記述
044 * keys                  : リクエストパラメータのキー(CSV形式)
045 * vals                  : リクエストパラメータの値(CSV形式)
046 * 【廃止】method            : POSTかGETを指定(初期値:GET) (6.9.0.0 (2018/01/31) 廃止)
047 * debug                 : 接続したページを受信して、ログに書き出します(初期値:false)
048 *
049 * 接続エラー時のログはファイル(SYS_LOG_URL)に出力されます。
050 *
051 * @og.rev 4.3.4.4 (2009/01/01) 新規作成
052 * @og.group デーモン
053 *
054 * @version  4.0
055 * @author   Takahashi Masakazu
056 * @since    JDK5.0,
057 */
058public class Daemon_URLConnect extends HybsTimerTask {
059        /** このプログラムのVERSION文字列を設定します。   {@value} */
060        private static final String VERSION = "6.9.8.0 (2018/05/28)" ;
061
062        private static final String DEFAULT_USER = "SYSTEM:MANAGER" ;
063        private static final int LOOP_COUNTER = 24;     // カウンタを24回に設定
064
065        private int loopCnt ;
066
067        private boolean debug   ;
068        private String  urlStr                  ;
069
070//      private URLConnect conn                 ;
071        private HttpConnect conn                ;                       // 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。
072
073        /**
074         * デフォルトコンストラクター
075         *
076         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
077         */
078        public Daemon_URLConnect() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
079
080        /**
081         * このタイマータスクによって初期化されるアクションです。
082         * パラメータを使用した初期化を行います。
083         *
084         * @og.rev 6.2.5.1 (2015/06/12) StringUtil.nvalを、すべてのパラメータ取得時に適用します。
085         * @og.rev 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。
086         * @og.rev 6.9.8.0 (2018/05/28) FindBugs:ローカル変数への無効な代入(methodのGET/POST廃止)
087         */
088        @Override
089        public void initDaemon() {
090                debug                                           = StringUtil.nval( getValue( "DEBUG"         ) , debug ) ;
091                urlStr                                          = StringUtil.nval( getValue( "url"           ) , null  );
092                final boolean useSystemUser     = StringUtil.nval( getValue( "useSystemUser" ) , true  );
093//              final String method                     = StringUtil.nval( getValue( "method"        ) , "GET" );               // 6.9.8.0 (2018/05/28) FindBugs
094                final String proxyHost          = StringUtil.nval( getValue( "proxyHost"     ) , null );
095                final int    proxyPort          = StringUtil.nval( getValue( "proxyPort"     ) , -1   );
096//              final String keys                       = StringUtil.nval( getValue( "keys"          ) , null );
097//              final String vals                       = StringUtil.nval( getValue( "vals"          ) , null );
098                final String authUserPass       = useSystemUser ? DEFAULT_USER
099                                                                                                        : StringUtil.nval( getValue( "authUserPass"  ) , null );
100
101//              final String urlEnc = XHTMLTag.urlEncode( keys,vals );
102
103                conn = new HttpConnect( urlStr,authUserPass );          // HttpConnect は、GET でも後付で引数を渡せます。
104
105                // POST の場合は、すべてのパラメータを、GETの場合は、既存のパラメータにプラスされます。
106                for( final Map.Entry<String,String> params : getParameter().entrySet() ) {
107                        conn.addRequestProperty( params.getKey() , params.getValue() );
108                }
109
110//              if( ! "POST".equals( method ) ) {
111//                      urlStr = XHTMLTag.addUrlEncode( urlStr,urlEnc );
112//              }
113//              conn = new URLConnect( urlStr,authUserPass );
114
115                if( proxyHost != null ) {
116                        conn.setProxy( proxyHost,proxyPort );
117                }
118
119//              if( "POST".equals(method) && keys != null && vals != null ) {
120//                      conn.setPostData( urlEnc );
121//              }
122        }
123
124        /**
125         * タイマータスクのデーモン処理の開始ポイントです。
126         *
127         * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
128         * @og.rev 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。
129         */
130        @Override
131        protected void startDaemon() {
132                if( loopCnt % LOOP_COUNTER == 0 ) {
133                        loopCnt = 1;
134                        System.out.println( toString() + " " + new Date()  + " " );
135                }
136                else {
137                        loopCnt++ ;
138                }
139
140                // 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
141                if( conn == null ) {
142                        final String errMsg = "#initDaemon()を先に実行しておいてください。" ;
143                        throw new OgRuntimeException( errMsg );
144                }
145
146                // URLへのconnect及びデータ取得実行
147                try{
148//                      conn.connect();
149
150                        if( debug ){
151                                // System.out.println( conn.readData() );
152                                final String debugMsg = "Daemon_URLConnect:url=[" + urlStr + "]" + CR
153                                                                + conn.readData();
154                                LogWriter.log( debugMsg );
155                        }
156                }
157                catch( final IOException ex ) {
158                        System.out.println(ex);
159                        final String errMsg = "Daemon_URLConnect:データ取得中にエラーが発生しました。" + CR
160                                                + " url=[" + urlStr + "]" + CR
161                                                + ex;
162                        LogWriter.log( errMsg );
163                }
164//              finally {
165//                      // 6.3.9.1 (2015/11/27) null でないことがわかっている値の冗長な null チェック(findbugs)
166//                      conn.disconnect();
167//              }
168        }
169}