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 String DEFAULT_USER = "admin:admin" ; // 6.9.0.1 (2018/02/05) この際、変更しておきます。 064 private static final int LOOP_COUNTER = 24; // カウンタを24回に設定 065 066 private int loopCnt ; 067 068 private boolean debug ; 069 private String urlStr ; 070 071// private URLConnect conn ; 072 private HttpConnect conn ; // 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。 073 074 /** 075 * デフォルトコンストラクター 076 * 077 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 078 */ 079 public Daemon_URLConnect() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 080 081 /** 082 * このタイマータスクによって初期化されるアクションです。 083 * パラメータを使用した初期化を行います。 084 * 085 * @og.rev 6.2.5.1 (2015/06/12) StringUtil.nvalを、すべてのパラメータ取得時に適用します。 086 * @og.rev 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。 087 * @og.rev 6.9.8.0 (2018/05/28) FindBugs:ローカル変数への無効な代入(methodのGET/POST廃止) 088 */ 089 @Override 090 public void initDaemon() { 091 debug = StringUtil.nval( getValue( "DEBUG" ) , debug ) ; 092 urlStr = StringUtil.nval( getValue( "url" ) , null ); 093 final boolean useSystemUser = StringUtil.nval( getValue( "useSystemUser" ) , true ); 094// final String method = StringUtil.nval( getValue( "method" ) , "GET" ); // 6.9.8.0 (2018/05/28) FindBugs 095 final String proxyHost = StringUtil.nval( getValue( "proxyHost" ) , null ); 096 final int proxyPort = StringUtil.nval( getValue( "proxyPort" ) , -1 ); 097// final String keys = StringUtil.nval( getValue( "keys" ) , null ); 098// final String vals = StringUtil.nval( getValue( "vals" ) , null ); 099 final String authUserPass = useSystemUser ? DEFAULT_USER 100 : StringUtil.nval( getValue( "authUserPass" ) , null ); 101 102// final String urlEnc = XHTMLTag.urlEncode( keys,vals ); 103 104 conn = new HttpConnect( urlStr,authUserPass ); // HttpConnect は、GET でも後付で引数を渡せます。 105 106 // POST の場合は、すべてのパラメータを、GETの場合は、既存のパラメータにプラスされます。 107 for( final Map.Entry<String,String> params : getParameter().entrySet() ) { 108 conn.addRequestProperty( params.getKey() , params.getValue() ); 109 } 110 111// if( ! "POST".equals( method ) ) { 112// urlStr = XHTMLTag.addUrlEncode( urlStr,urlEnc ); 113// } 114// conn = new URLConnect( urlStr,authUserPass ); 115 116 if( proxyHost != null ) { 117 conn.setProxy( proxyHost,proxyPort ); 118 } 119 120// if( "POST".equals(method) && keys != null && vals != null ) { 121// conn.setPostData( urlEnc ); 122// } 123 } 124 125 /** 126 * タイマータスクのデーモン処理の開始ポイントです。 127 * 128 * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs) 129 * @og.rev 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。 130 */ 131 @Override 132 protected void startDaemon() { 133 if( loopCnt % LOOP_COUNTER == 0 ) { 134 loopCnt = 1; 135 System.out.println( toString() + " " + new Date() + " " ); 136 } 137 else { 138 loopCnt++ ; 139 } 140 141 // 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs) 142 if( conn == null ) { 143 final String errMsg = "#initDaemon()を先に実行しておいてください。" ; 144 throw new OgRuntimeException( errMsg ); 145 } 146 147 // URLへのconnect及びデータ取得実行 148 try{ 149// conn.connect(); 150 151 if( debug ){ 152 // System.out.println( conn.readData() ); 153 final String debugMsg = "Daemon_URLConnect:url=[" + urlStr + "]" + CR 154 + conn.readData(); 155 LogWriter.log( debugMsg ); 156 } 157 } 158 catch( final IOException ex ) { 159 System.out.println(ex); 160 final String errMsg = "Daemon_URLConnect:データ取得中にエラーが発生しました。" + CR 161 + " url=[" + urlStr + "]" + CR 162 + ex; 163 LogWriter.log( errMsg ); 164 } 165// finally { 166// // 6.3.9.1 (2015/11/27) null でないことがわかっている値の冗長な null チェック(findbugs) 167// conn.disconnect(); 168// } 169 } 170}