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) 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.0.0 (2018/01/31)" ; 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 */ 087 @Override 088 public void initDaemon() { 089 debug = StringUtil.nval( getValue( "DEBUG" ) , debug ) ; 090 urlStr = StringUtil.nval( getValue( "url" ) , null ); 091 final boolean useSystemUser = StringUtil.nval( getValue( "useSystemUser" ) , true ); 092 final String method = StringUtil.nval( getValue( "method" ) , "GET" ); 093 final String proxyHost = StringUtil.nval( getValue( "proxyHost" ) , null ); 094 final int proxyPort = StringUtil.nval( getValue( "proxyPort" ) , -1 ); 095// final String keys = StringUtil.nval( getValue( "keys" ) , null ); 096// final String vals = StringUtil.nval( getValue( "vals" ) , null ); 097 final String authUserPass = useSystemUser ? DEFAULT_USER 098 : StringUtil.nval( getValue( "authUserPass" ) , null ); 099 100// final String urlEnc = XHTMLTag.urlEncode( keys,vals ); 101 102 conn = new HttpConnect( urlStr,authUserPass ); // HttpConnect は、GET でも後付で引数を渡せます。 103 104 // POST の場合は、すべてのパラメータを、GETの場合は、既存のパラメータにプラスされます。 105 for( final Map.Entry<String,String> params : getParameter().entrySet() ) { 106 conn.addRequestProperty( params.getKey() , params.getValue() ); 107 } 108 109// if( ! "POST".equals( method ) ) { 110// urlStr = XHTMLTag.addUrlEncode( urlStr,urlEnc ); 111// } 112// conn = new URLConnect( urlStr,authUserPass ); 113 114 if( proxyHost != null ) { 115 conn.setProxy( proxyHost,proxyPort ); 116 } 117 118// if( "POST".equals(method) && keys != null && vals != null ) { 119// conn.setPostData( urlEnc ); 120// } 121 } 122 123 /** 124 * タイマータスクのデーモン処理の開始ポイントです。 125 * 126 * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs) 127 * @og.rev 6.9.0.0 (2018/01/31) URLConnect 廃止、HttpConnect に置き換えます。 128 */ 129 @Override 130 protected void startDaemon() { 131 if( loopCnt % LOOP_COUNTER == 0 ) { 132 loopCnt = 1; 133 System.out.println( toString() + " " + new Date() + " " ); 134 } 135 else { 136 loopCnt++ ; 137 } 138 139 // 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs) 140 if( conn == null ) { 141 final String errMsg = "#initDaemon()を先に実行しておいてください。" ; 142 throw new OgRuntimeException( errMsg ); 143 } 144 145 // URLへのconnect及びデータ取得実行 146 try{ 147// conn.connect(); 148 149 if( debug ){ 150 // System.out.println( conn.readData() ); 151 final String debugMsg = "Daemon_URLConnect:url=[" + urlStr + "]" + CR 152 + conn.readData(); 153 LogWriter.log( debugMsg ); 154 } 155 } 156 catch( final IOException ex ) { 157 System.out.println(ex); 158 final String errMsg = "Daemon_URLConnect:データ取得中にエラーが発生しました。" + CR 159 + " url=[" + urlStr + "]" + CR 160 + ex; 161 LogWriter.log( errMsg ); 162 } 163// finally { 164// // 6.3.9.1 (2015/11/27) null でないことがわかっている値の冗長な null チェック(findbugs) 165// conn.disconnect(); 166// } 167 } 168}