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 */
016 package org.opengion.fukurou.util;
017
018 import java.io.BufferedInputStream;
019 import java.io.BufferedReader;
020 import java.io.File;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.InputStreamReader;
024 import java.io.OutputStream;
025 import java.io.PrintStream;
026 import java.io.PrintWriter;
027 import java.io.UnsupportedEncodingException;
028 import java.net.HttpURLConnection;
029 import java.net.InetSocketAddress;
030 import java.net.Proxy;
031 import java.net.SocketAddress;
032 import java.net.URL;
033 import java.net.URLConnection;
034
035 import org.apache.commons.codec.binary.Base64;
036
037 /**
038 * URLConnect は、指定ã?URL にアクセスして、情報/ãƒ??タを取得しますã?
039 * URL へのアクセスにより、エンジンではå�?¨®処ç�?‚’実行させることが可能になりますã?
040 * 例えば、帳票ãƒ??モンの起動やã€?•·時間かかるå?ç�??実行などですã?
041 * なおã?URLに引数が付く場合ã?、ダブルコーãƒ??ションで括って下さã�??
042 * - 付き引数は、指定é?番はã€?–¢係ありませんã€? 無し引数(url,user:passwd)はã€?
043 * é ?•ªがありますã?
044 *
045 * Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]
046 *
047 * args[*] : [-info/-data] æƒ??の取得か、データの取得かを指定しまã�?初期値:-data)ã€?
048 * args[*] : [-post=ファイル名] POSTメソãƒ?ƒ‰を指定して、ファイルãƒ??タをé?信しまã�?初期値:-get)ã€?
049 * args[*] : [-encode=UTF-8] エンコードをæŒ?®šしまã�?通常は接続å?のencodeを使用)ã€?
050 * args[*] : [-out=ファイル名] 結果を指定されたファイルエンコードでファイルに出力しますã?
051 * args[*] : [-errEx=true/false] trueの場合ã??šス?Ž゚ンス?º?°??¾žがã€?XX,5XX の時に RuntimeException を投げまã�?初期値:false)ã€?
052 * args[A] : url ?µ?²?¬を指定しますã?GETの場合ã?パラメータは ?KEY=VALですã?
053 * args[B] : [user:passwd] BASIC認証のエリアへのアクセス時にæŒ?®šしますã?
054 *
055 * ※ プロキシ設定ã??“つの方æ³?
056 * プロキシ設定にはã€?¼“つの方法がありますã?
057 * 1.
058 * URL url = URL( "http",proxyHost,proxyPort, url );
059 * URLConnection urlConn = url.openConnection();
060 * 2.
061 * SocketAddress scaddr = new InetSocketAddress( proxyHost, proxyPort );
062 * Proxy proxy = new Proxy( Proxy.Type.HTTP, scaddr );
063 * URL url = new Url( url );
064 * URLConnection urlConn = url.openConnection( proxy );
065 * 3.
066 * System.setProperty( "http.proxyHost",host );
067 * System.setProperty( "http.proxyPort",String.valueOf( port ) );
068 * URL url = new Url( url );
069 * URLConnection urlConn = url.openConnection();
070 * System.clearProperty( "http.proxyHost" );
071 * System.clearProperty( "http.proxyPort" );
072 *
073 * 1. ã€?. の方法ã?、urlConn.getContentType() を実行すると、エラーになりますã?(原因不æ?)
074 * 3. の方法では、ã?ルチスレãƒ?ƒ‰で実行するå?合に、問題が発生しますã?
075 * 本クラスでは、方法ï¼?を使用してã�?�¾すã?
076 *
077 * @version 4.0
078 * @author Kazuhiko Hasegawa
079 * @since JDK5.0,
080 */
081 public class URLConnect {
082 private static final String CR = System.getProperty("line.separator");
083
084 // private static final String ENCODE = "UTF-8";
085
086 private final String urlStr ;
087 private final String userPass ;
088
089 private int rpsCode = -1;
090 private String rpsMethod = null;
091 private String rpsMessage = null;
092 private String type = null;
093 private String charset = null;
094 private String postData = null;
095 private long length = -1;
096 private long date = -1;
097 private long modified = -1;
098 private boolean isPost = false;
099 private URLConnection conn = null;
100 private Proxy proxy = Proxy.NO_PROXY;
101
102 /**
103 * コンストラクター
104 *
105 * @param url 接続するアドレスを指定しますã?(http://server:port/dir/file.html)
106 * @param pass ユーザー?šパスワーãƒ?認証接続がå¿?¦�な場å�?
107 */
108 public URLConnect( final String url, final String pass ) {
109 urlStr = url;
110 userPass = pass;
111 }
112
113 /**
114 * æŒ?®šã?URLに対して、コネクトするã?に使用するプロキシ設定を行いますã?
115 * こã?ときに、ã?ãƒ?ƒ€ーæƒ??をå?部変数に設定しておきますã?
116 *
117 * @param host 接続するã?ロキシのホスト名
118 * @param port 接続するã?ロキシのポã?ト番号
119 */
120 public void setProxy( final String host,final int port ) {
121 // 方æ³?.
122 SocketAddress scaddr = new InetSocketAddress( host, port );
123 proxy = new Proxy( Proxy.Type.HTTP, scaddr );
124 }
125
126 /**
127 * æŒ?®šã?URLに対して、コネクトしますã?
128 * こã?ときに、ã?ãƒ?ƒ€ーæƒ??をå?部変数に設定しておきますã?
129 *
130 * @og.rev 4.0.1.0 (2007/12/12) Postでè¤?•°キーを使えるように修正
131 * @og.rev 5.1.6.0 (2010/05/01) charsetを指定できるようにする
132 * @throws IOException 入出力エラーが発生したとã�?
133 */
134 public void connect() throws IOException {
135 conn = getConnection();
136
137 if( isPost ) {
138 conn.setDoOutput( true ); // POST可能にする
139
140 OutputStream os = null; // POST用のOutputStream
141 PrintStream ps = null;
142 try {
143 os = conn.getOutputStream(); // POST用のOutputStreamを取å¾?
144 // 5.1.6.0 (2010/05/01)
145 if( charset != null ) {
146 ps = new PrintStream( os, false, charset );
147 }
148 else {
149 ps = new PrintStream( os );
150 }
151 ps.print( postData ); // 4.1.0.0 (2007/12/22)
152 }
153 finally {
154 Closer.ioClose( ps ); // close 処ç�?™‚の IOException を無è¦?
155 Closer.ioClose( os ); // close 処ç�?™‚の IOException を無è¦?
156 }
157 }
158 else {
159 // GET 時ã?コネクション接ç¶?
160 conn.connect();
161 }
162
163 setInfo( conn );
164 }
165
166 /**
167 * U接続å?のãƒ??タを取得しますã?
168 *
169 * こã?処ç�??前に、connect() 処ç�?‚’実行しておくå¿?¦�がありますã?
170 * 取得したデータは、指定ã?URL へのアクセスのみですã?
171 * 通常のWebブラウザは、イメージã‚??JavaScriptファイル、CSSファイルなどã€?
172 * å�?¨®ファイル毎にHTTP接続を行い、取得して、レンãƒ?ƒªングしますã?
173 * こã?メソãƒ?ƒ‰での処ç�?�§は、それらのファイルå†?�«æŒ?®šされてã�?‚‹URLの
174 * 再帰çš?�ª取得ã?行いませんã€?
175 * よって、フレーãƒ??ç�?�ªども行いませんã€?
176 * 本来は、Stream のまま処ç�?�™ることで、バイナリãƒ??タも扱えますが、ここではã€?
177 * ãƒ?‚ストデータ(String)に変換して使用できるãƒ??タのみ扱えますã?
178 *
179 * @return 接続結果
180 * @throws IOException 入出力エラーが発生したとã�?
181 */
182 public String readData() throws IOException {
183 if( conn == null ) {
184 String errMsg = "connect() されてã�?�¾せん。データ取得前にconnect()してくださいã€?;
185 throw new RuntimeException( errMsg );
186 }
187
188 BufferedReader reader = null;
189 StringBuilder buf = new StringBuilder();
190 try {
191 reader = getReader();
192
193 String line ;
194 while( (line = reader.readLine()) != null ) {
195 buf.append( line ).append( CR );
196 }
197 }
198 catch( UnsupportedEncodingException ex ) {
199 String errMsg = "æŒ?®šされたæ–?—エンコーãƒ?‚£ングがサポã?トされてã�?�¾せんã€? + CR
200 + " url=[" + urlStr + "]"
201 + " charset=[" + charset + "]" ;
202 throw new RuntimeException( errMsg,ex );
203 }
204 finally {
205 Closer.ioClose( reader );
206 disconnect();
207 }
208
209 return buf.toString();
210 }
211
212 /**
213 * サーバへのほかã?要求が今後発生しそうになã�?�“とを示しますã?
214 *
215 */
216 public void disconnect() {
217 if( conn instanceof HttpURLConnection ) {
218 ((HttpURLConnection)conn).disconnect() ;
219 }
220 }
221
222 /**
223 * URL と ユーザー?šパスワードを与えて、URLConnectionを返しますã?
224 *
225 * ユーザー?šパスワーãƒ?ã�?null でなã�??合ã?、BASCI認証エリアへのアクセスの為ã€?
226 * BASE64Encoder を行って、Authorization プロパティーを設定しますã?
227 * ここで返す URLConnection は、すでに、connect() メソãƒ?ƒ‰実行済みの
228 * リモート接続が完äº?�—た状態ã?オブジェクトですã?
229 *
230 * @return URLConnectionオブジェクãƒ?
231 * @throws IOException 入出力エラーが発生したとã�?
232 */
233 // 5.1.5.0 (2010/04/01) SOAP対応により、PROTECTEDåŒ?
234 protected URLConnection getConnection() throws IOException {
235 // private URLConnection getConnection() throws IOException {
236 final URL url = new URL( urlStr );
237
238 // 方æ³?.
239 URLConnection urlConn = url.openConnection( proxy );
240
241 if( userPass != null ) {
242 // byte[] encoded = Base64.encodeBase64( userPass.getBytes() );
243 // String userPassEnc = new String( encoded );
244 byte[] encoded = Base64.encodeBase64( userPass.getBytes( StringUtil.DEFAULT_CHARSET ) ); // 5.5.2.6 (2012/05/25) findbugs対å¿?
245 String userPassEnc = new String( encoded,StringUtil.DEFAULT_CHARSET ); // 5.5.2.6 (2012/05/25) findbugs対å¿?
246 urlConn.setRequestProperty( "Authorization","Basic " + userPassEnc );
247 }
248
249 return urlConn ;
250 }
251
252 /**
253 * 接続å?のæƒ??をå?部変数に設定しますã?
254 *
255 * ここでは、タイãƒ?エンコーãƒ?レスポンスコーãƒ?レスポンスメãƒ?‚»ージ を設定しますã?
256 * レスポンスコーãƒ?レスポンスメãƒ?‚»ージは、接続コネクションがã?HttpURLConnection の
257 * 場合ã?みセãƒ?ƒˆされますã?
258 * 途中でエラーが発生したå?合でもã?継続å?ç�?�§きるようにしますã?これは、ã?ロキシ
259 * 設定ã?方法により、conn.getContentType() でエラーが発生するå?合があるためですã?
260 *
261 * @og.rev 5.5.9.1 (2012/12/07) charsetは、null の場合ã?み設定しますã?
262 *
263 * @param conn 接続å?のコネクション
264 */
265 private void setInfo( final URLConnection conn ) {
266 try {
267 // 5.5.9.1 (2012/12/07) charsetは、null の場合ã?み設定しますã?
268 if( charset == null ) { charset = conn.getContentEncoding(); }
269 type = conn.getContentType() ;
270 length = conn.getContentLength();
271 date = conn.getDate();
272 modified= conn.getLastModified();
273
274 if( charset == null && type != null ) {
275 int adrs = type.indexOf( "charset" );
276 int adrs2 = type.indexOf( '=',adrs );
277 if( adrs > 0 && adrs2 > adrs ) {
278 charset = type.substring( adrs2+1 ).trim();
279 }
280 }
281
282 if( conn instanceof HttpURLConnection ) {
283 HttpURLConnection httpConn = (HttpURLConnection) conn;
284 rpsCode = httpConn.getResponseCode();
285 rpsMethod = httpConn.getRequestMethod();
286 rpsMessage = httpConn.getResponseMessage() + code2Message( rpsCode );
287 }
288 }
289 // 4.0.0.0 (2007/11/29) Exception から、IOException と RuntimeException に変更
290 catch( IOException ex ) {
291 System.out.println( ex.getMessage() );
292 }
293 catch( RuntimeException ex ) {
294 System.out.println( ex.getMessage() );
295 }
296 }
297
298 /**
299 * URL æƒ??を取得しますã?
300 *
301 * @og.rev 4.3.4.4 (2009/01/01) メソãƒ?ƒ‰名変更
302 *
303 * @return URL�?
304 */
305 public String getUrl() { return urlStr; }
306
307 /**
308 * POSTするãƒ??タを設定しますã?
309 *
310 * POSTする場合ã?、connect() 処ç�?‚’行う前に、データを設定しておくå¿?¦�がありますã?
311 *
312 * @og.rev 4.1.0.0 (2007/12/22) キーと値のセãƒ?ƒˆを取得するよã�?¤‰更
313 * @param data POSTãƒ??タ
314 */
315 public void setPostData( final String data ) {
316 postData = data;
317 if( postData != null && "?".indexOf( postData ) == 0 ) { // 先é?の?を抜ã�?
318 postData = postData.substring(1);
319 }
320 if( postData != null ) { isPost = true; }
321 }
322
323 /**
324 * タイãƒ?æƒ??を取得しますã?
325 *
326 * @return タイãƒ?æƒ??
327 */
328 public String getType() { return type; }
329
330 /**
331 * ãƒ??タé‡?æƒ??を取得しますã?
332 *
333 * @return ãƒ??タé‡?æƒ??
334 */
335 public long getLength() { return length; }
336
337 /**
338 * 作æ?日æ™?æƒ??を取得しますã?
339 *
340 * @return 作æ?日æ™?
341 */
342 public long getDate() { return date; }
343
344 /**
345 * 更新日æ™?æƒ??を取得しますã?
346 *
347 * @return 更新日æ™?
348 */
349 public long getModified() { return modified; }
350
351 /**
352 * 結果コーãƒ?æƒ??(HttpURLConnection)を取得しますã?
353 *
354 * @return 結果コーãƒ?æƒ??
355 */
356 public int getCode() { return rpsCode; }
357
358 /**
359 * メソãƒ?ƒ‰ æƒ??(HttpURLConnection)を取得しますã?
360 *
361 * @return メソãƒ?ƒ‰ æƒ??
362 */
363 public String getMethod() { return rpsMethod; }
364
365 /**
366 * メãƒ?‚»ージ æƒ??(HttpURLConnection)を取得しますã?
367 *
368 * @return メãƒ?‚»ージ æƒ??
369 */
370 public String getMessage() { return rpsMessage; }
371
372 /**
373 * キャラクタ æƒ??を取得しますã?
374 *
375 * @return キャラクタ æƒ??
376 */
377 public String getCharset() { return charset; }
378
379 /**
380 * キャラクタ æƒ??を設定しますã?
381 *
382 * @param chset キャラクタ æƒ??
383 */
384 public void setCharset( final String chset ) { charset = chset; }
385
386 /**
387 * 接続å?のãƒ??タのリーãƒ??を取得しますã?
388 *
389 * こã?処ç�??前に、connect() 処ç�?‚’実行しておくå¿?¦�がありますã?
390 * 取得したデータは、指定ã?URL へのアクセスのみですã?
391 * 通常のWebブラウザは、イメージã‚??JavaScriptファイル、CSSファイルなどã€?
392 * å�?¨®ファイル毎にHTTP接続を行い、取得して、レンãƒ?ƒªングしますã?
393 * こã?メソãƒ?ƒ‰での処ç�?�§は、それらのファイルå†?�«æŒ?®šされてã�?‚‹URLの
394 * 再帰çš?�ª取得ã?行いませんã€?
395 * よって、フレーãƒ??ç�?�ªども行いませんã€?
396 *
397 * @return 接続結果のリーãƒ??
398 * @throws IOException 入出力エラーが発生したとã�?
399 */
400 public BufferedReader getReader() throws IOException {
401 InputStream in = conn.getInputStream();
402
403 final BufferedReader reader ;
404 if( charset != null ) {
405 reader = new BufferedReader( new InputStreamReader( in,charset ) );
406 }
407 else {
408 // reader = new BufferedReader( new InputStreamReader( in ) );
409 reader = new BufferedReader( new InputStreamReader( in,StringUtil.DEFAULT_CHARSET ) ); // 5.5.2.6 (2012/05/25) findbugs対å¿?
410 }
411
412 return reader;
413 }
414
415 /**
416 * 接続å?のãƒ??タの入力ストリーãƒ?‚’取得しますã?
417 *
418 * こã?処ç�??前に、connect() 処ç�?‚’実行しておくå¿?¦�がありますã?
419 * 取得したデータは、指定ã?URL へのアクセスのみですã?
420 * 通常のWebブラウザは、イメージã‚??JavaScriptファイル、CSSファイルなどã€?
421 * å�?¨®ファイル毎にHTTP接続を行い、取得して、レンãƒ?ƒªングしますã?
422 * こã?メソãƒ?ƒ‰での処ç�?�§は、それらのファイルå†?�«æŒ?®šされてã�?‚‹URLの
423 * 再帰çš?�ª取得ã?行いませんã€?
424 * よって、フレーãƒ??ç�?�ªども行いませんã€?
425 *
426 * @og.rev 5.4.2.0 (2011/12/01) 新規追åŠ?
427 *
428 * @return 接続結果の入力を出力しますã?
429 * @throws IOException 入出力エラーが発生したとã�?
430 */
431 public InputStream getInputStream() throws IOException {
432 // InputStream in = new BufferedInputStream( conn.getInputStream() );
433 // return in;
434 return new BufferedInputStream( conn.getInputStream() ); // 5.5.2.4 (2012/05/16)
435 }
436
437 /**
438 * HttpURLConnection のレスポンスコードに対応するメãƒ?‚»ージæ–?—å?を返しますã?
439 *
440 * HttpURLConnection の getResponseCode() メソãƒ?ƒ‰により取得された、HTTPレスポンスコーãƒ?
441 * に対応する文字å?を返しますã?こã?æ–?—å?は、HttpURLConnection で定義されã�?
442 * static 定数のコメントを、定義してã�?�¾すã?
443 *
444 * @og.rev 5.6.7.0 (2013/07/27) レスポンスコードä¾?追åŠ?
445 *
446 * @param code HTTPレスポンスコーãƒ?
447 *
448 * @return レスポンスコードに対応する文字å?
449 * @see HttpURLConnection#HTTP_ACCEPTED
450 */
451 public static String code2Message( final int code ) {
452 final String msg ;
453 switch( code ) {
454 case 100 : msg = "100: 要求ã?続行可能ですã?" ; break; // 5.6.7.0 (2013/07/27)
455 case 101 : msg = "101: プロトコルをå?り替えますã?" ; break; // 5.6.7.0 (2013/07/27)
456 case HttpURLConnection.HTTP_OK : msg = "200: OK ですã?" ; break;
457 case HttpURLConnection.HTTP_CREATED : msg = "201: 作æ?されましたã€? ; break;
458 case HttpURLConnection.HTTP_ACCEPTED : msg = "202: 許可されましたã€? ; break;
459 case HttpURLConnection.HTTP_NOT_AUTHORITATIVE : msg = "203: 不当なæƒ??ですã?" ; break;
460 case HttpURLConnection.HTTP_NO_CONTENT : msg = "204: コンãƒ?ƒ³ãƒ?�Œありませんã€? ; break;
461 case HttpURLConnection.HTTP_RESET : msg = "205: コンãƒ?ƒ³ãƒ?‚’リセãƒ?ƒˆしますã?" ; break;
462 case HttpURLConnection.HTTP_PARTIAL : msg = "206: 部åˆ?š„なコンãƒ?ƒ³ãƒ?�§すã?" ; break;
463 case HttpURLConnection.HTTP_MULT_CHOICE : msg = "300: è¤?•°選択されてã�?�¾すã?" ; break;
464 case HttpURLConnection.HTTP_MOVED_PERM : msg = "301: 永続的に移動されましたã€? ; break;
465 case HttpURLConnection.HTTP_MOVED_TEMP : msg = "302: ä¸?™‚çš?�«åˆ?‚Š替えますã?" ; break;
466 case HttpURLConnection.HTTP_SEE_OTHER : msg = "303: 他を参ç?してくださいã€? ; break;
467 case HttpURLConnection.HTTP_NOT_MODIFIED : msg = "304: 修正されませんでしたã€? ; break;
468 case HttpURLConnection.HTTP_USE_PROXY : msg = "305: プロキシを使用してくださいã€? ; break;
469 case 306 : msg = "306: 仕様ã?拡張案ですã?" ; break; // 5.6.7.0 (2013/07/27)
470 case 307 : msg = "307: ä¸?™‚çš?�ªリãƒ?‚¤レクトですã?" ; break; // 5.6.7.0 (2013/07/27)
471 case HttpURLConnection.HTTP_BAD_REQUEST : msg = "400: 不当な要求ですã?" ; break;
472 case HttpURLConnection.HTTP_UNAUTHORIZED : msg = "401: 認証されませんでしたã€? ; break;
473 case HttpURLConnection.HTTP_PAYMENT_REQUIRED : msg = "402: 支払いがå¿?¦�ですã?" ; break;
474 case HttpURLConnection.HTTP_FORBIDDEN : msg = "403: 禁止されてã�?�¾すã?" ; break;
475 case HttpURLConnection.HTTP_NOT_FOUND : msg = "404: 見つかりませんでしたã€? ; break;
476 case HttpURLConnection.HTTP_BAD_METHOD : msg = "405: メソãƒ?ƒ‰は許可されませんã€? ; break;
477 case HttpURLConnection.HTTP_NOT_ACCEPTABLE : msg = "406: 許容されませんã€? ; break;
478 case HttpURLConnection.HTTP_PROXY_AUTH : msg = "407: プロキシの認証がå¿?¦�ですã?" ; break;
479 case HttpURLConnection.HTTP_CLIENT_TIMEOUT : msg = "408: 要求が時間åˆ?‚Œですã?" ; break;
480 case HttpURLConnection.HTTP_CONFLICT : msg = "409: 重è¤?�—てã�?�¾すã?" ; break;
481 case HttpURLConnection.HTTP_GONE : msg = "410: 存在しませんã€? ; break;
482 case HttpURLConnection.HTTP_LENGTH_REQUIRED : msg = "411: 長さがå¿?¦�ですã?" ; break;
483 case HttpURLConnection.HTTP_PRECON_FAILED : msg = "412: 前提条件が正しくありませんã€? ; break;
484 case HttpURLConnection.HTTP_ENTITY_TOO_LARGE : msg = "413: 要求エンãƒ?‚£ãƒ?‚£が長すぎますã?" ; break;
485 case HttpURLConnection.HTTP_REQ_TOO_LONG : msg = "414: 要æ±?URL が長すぎますã?" ; break;
486 case HttpURLConnection.HTTP_UNSUPPORTED_TYPE : msg = "415: サポã?トされなã�?ƒ¡ãƒ?‚£アタイプですã?" ; break;
487 case 416 : msg = "416: 要求されたç¯?›²は不十åˆ?�§すã?" ; break; // 5.6.7.0 (2013/07/27)
488 case 417 : msg = "417: 要求どおりの処ç�?�Œ不可能ですã?" ; break; // 5.6.7.0 (2013/07/27)
489 case HttpURLConnection.HTTP_INTERNAL_ERROR : msg = "500: å†?ƒ¨サーバエラーですã?" ; break;
490 case HttpURLConnection.HTTP_NOT_IMPLEMENTED : msg = "501: 実è£?�•れてã�?�¾せんã€? ; break;
491 case HttpURLConnection.HTTP_BAD_GATEWAY : msg = "502: 誤ったゲートウェイですã?" ; break;
492 case HttpURLConnection.HTTP_UNAVAILABLE : msg = "503: サービスが利用できませんã€? ; break;
493 case HttpURLConnection.HTTP_GATEWAY_TIMEOUT : msg = "504: ゲートウェイが時間å?れですã?" ; break;
494 case HttpURLConnection.HTTP_VERSION : msg = "505: HTTP バã?ジョンがサポã?トされてã�?�¾せんã€?; break;
495 // default : msg = "-1: 未定義" ;
496 default : msg = code + ": 未定義" ; // 5.6.7.0 (2013/07/27)
497 }
498 return msg ;
499 }
500
501 /**
502 * サンプル実行用のメインメソãƒ?ƒ‰
503 *
504 * Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]
505 *
506 * args[*] : [-info/-data] æƒ??の取得か、データの取得かを指定しまã�?初期値:-data)ã€?
507 * args[*] : [-post=ファイル名] POSTメソãƒ?ƒ‰を指定して、ファイルãƒ??タをé?信しまã�?初期値:-get)ã€?
508 * args[*] : [-encode=UTF-8] エンコードをæŒ?®šしまã�?通常は接続å?のencodeを使用)
509 * args[*] : [-out=ファイル名] 結果をファイルに出力しますã?ファイルエンコードもæŒ?®šしますã?
510 * args[*] : [-errEx=true/false] trueの場合ã??šス?Ž゚ンス?º?°??¾žがã€?XX,5XX の時に RuntimeException を投げまã�?初期値:false)ã€?
511 * args[A] : url ?µ?²?¬を指定しますã?GETの場合ã?パラメータは ?KEY=VALですã?
512 * args[B] : [user:passwd] BASIC認証のエリアへのアクセス時にæŒ?®šしますã?
513 *
514 * @og.rev 5.6.7.0 (2013/07/27) -errEx 追åŠ?
515 *
516 * @param args コマンド引数配å?
517 * @throws IOException 入出力エラーが発生したとã�?
518 */
519 public static void main( final String[] args ) throws IOException {
520 if( args.length < 3 ) {
521 LogWriter.log( "Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]" );
522 LogWriter.log( " args[*] : [-info/-data] æƒ??の取得か、データの取得かを指定しまã�?初期値:-data)" );
523 LogWriter.log( " args[*] : [-post=ファイル名] POSTメソãƒ?ƒ‰を指定して、ファイルãƒ??タをé?信しまã�?初期値:-get)" );
524 LogWriter.log( " args[*] : [-encode=UTF-8] エンコードをæŒ?®šしますã?(通常は接続å?のencodeを使用)" );
525 LogWriter.log( " args[*] : [-out=ファイル名] 結果をファイルに出力しますã?ファイルエンコードもæŒ?®šしまã�? );
526 LogWriter.log( " args[*] : [-errEx=true/false] trueの場合ã??šス?Ž゚ンス?º?°??¾žがã€?XX,5XX の時に RuntimeException を投げまã�?初期値:false)" );
527 LogWriter.log( " args[A] : url ?µ?²?¬を指定しますã?GETの場合ã?パラメータは ?KEY=VALでã�? );
528 LogWriter.log( " args[B] : [user:passwd] BASIC認証のエリアへのアクセス時にæŒ?®šしまã�? );
529 return;
530 }
531
532 boolean isInfo = false ;
533 boolean isPost = false ;
534 String postKey = null ;
535 String postFile = null ;
536 String encode = null ;
537 String outFile = null ;
538 boolean isEx = false ; // 5.6.7.0 (2013/07/27) 追åŠ?
539 String[] vals = new String[2]; // url,userPass のé ?�«引数設å®?
540
541 int adrs = 0;
542 for( int i=0; i<args.length; i++ ) {
543 String arg = args[i];
544 if( arg.equalsIgnoreCase( "-info" ) ) {
545 isInfo = true;
546 }
547 else if( arg.equalsIgnoreCase( "-data" ) ) {
548 isInfo = false;
549 }
550 else if( arg.startsWith( "-post=" ) ) {
551 isPost = true;
552 int sepAdrs = arg.indexOf( ':',6 );
553 postKey = arg.substring( 6,sepAdrs );
554 postFile = arg.substring( sepAdrs+1 );
555 }
556 else if( arg.startsWith( "-encode=" ) ) {
557 encode = arg.substring( 8 );
558 }
559 else if( arg.startsWith( "-out=" ) ) {
560 outFile = arg.substring( 5 );
561 }
562 else if( arg.startsWith( "-errEx=" ) ) { // 5.6.7.0 (2013/07/27) 追åŠ?
563 isEx = "true".equalsIgnoreCase( arg.substring( 7 ) );
564 }
565 else if( arg.startsWith( "-" ) ) {
566 System.out.println( "Error Argment:" + arg );
567 }
568 else {
569 vals[adrs++] = arg;
570 }
571 }
572
573 String urlStr = vals[0] ;
574 String userPass = vals[1] ;
575
576 URLConnect conn = new URLConnect( urlStr,userPass );
577
578 // POST ãƒ??タは、connect() する前に、設定しますã?
579 if( isPost ) {
580 FileString file = new FileString();
581 file.setFilename( postFile );
582 String postData = file.getValue();
583
584 conn.setPostData( XHTMLTag.urlEncode(postKey, postData) );
585 }
586
587 conn.connect();
588 if( encode != null ) {
589 conn.setCharset( encode ); // encode æŒ?®?
590 }
591 else {
592 encode = conn.getCharset(); // æŒ?®šがなければ、接続å?の charset を使用
593 }
594
595 final PrintWriter writer ;
596 if( outFile != null ) {
597 writer = FileUtil.getPrintWriter( new File( outFile ),encode );
598 }
599 else {
600 writer = FileUtil.getLogWriter( "System.out" );
601 }
602
603 int code = conn.getCode(); // 5.6.7.0 (2013/07/27) レスポンスコードã?、常に拾っておきますã?
604 if( isInfo ) {
605 writer.println( "URL :" + conn.getUrl() );
606 writer.println( "Type :" + conn.getType() );
607 // writer.println( "Code :" + conn.getCode() );
608 writer.println( "Code :" + code ); // 5.6.7.0 (2013/07/27) 取得済みの値を利用ã€?
609 writer.println( "Message:" + conn.getMessage() );
610 writer.println( "Charset:" + conn.getCharset() );
611 }
612 else {
613 writer.println( conn.readData() );
614 }
615
616 conn.disconnect();
617
618 Closer.ioClose( writer );
619
620 // 5.6.7.0 (2013/07/27) trueの場合ã??šス?Ž゚ンス?º?°??¾žがã€?XX,5XX の時に RuntimeException を投げまã�?
621 if( isEx && code >= 400 ) {
622 String errMsg = URLConnect.code2Message( code );
623 throw new RuntimeException( errMsg );
624 }
625 }
626 }