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 // 5.8.3.0 (2015/01/09) 追�
103 private String[] propKeys;
104 private String[] propVals;
105
106 /**
107 * コンストラクター
108 *
109 * @param url 接続ã™ã‚‹ã‚¢ãƒ‰ãƒ¬ã‚¹ã‚’指定ã—ã¾ã™ã?(http://server:port/dir/file.html)
110 * @param pass ユーザー?šãƒ‘スワーãƒ?èªè¨¼æŽ¥ç¶šãŒå¿?¦ãªå ´å?
111 */
112 public URLConnect( final String url, final String pass ) {
113 urlStr = url;
114 userPass = pass;
115 }
116
117 /**
118 * æŒ?®šã?URLã«å¯¾ã—ã¦ã€ã‚³ãƒã‚¯ãƒˆã™ã‚‹ã?ã«ä½¿ç”¨ã™ã‚‹ãƒ—ãƒã‚ã‚·è¨å®šã‚’行ã„ã¾ã™ã?
119 * ã“ã?ã¨ãã«ã€ã?ãƒ?ƒ€ãƒ¼æƒ??ã‚’å?部変数ã«è¨å®šã—ã¦ãŠãã¾ã™ã?
120 *
121 * @param host 接続ã™ã‚‹ã?ãƒã‚ã‚·ã®ãƒ›ã‚¹ãƒˆå
122 * @param port 接続ã™ã‚‹ã?ãƒã‚ã‚·ã®ãƒã?ト番å·
123 */
124 public void setProxy( final String host,final int port ) {
125 // 方�.
126 SocketAddress scaddr = new InetSocketAddress( host, port );
127 proxy = new Proxy( Proxy.Type.HTTP, scaddr );
128 }
129
130 /**
131 * æŒ?®šã?URLã«å¯¾ã—ã¦ã€ã‚³ãƒã‚¯ãƒˆã—ã¾ã™ã?
132 * ã“ã?ã¨ãã«ã€ã?ãƒ?ƒ€ãƒ¼æƒ??ã‚’å?部変数ã«è¨å®šã—ã¦ãŠãã¾ã™ã?
133 *
134 * @og.rev 4.0.1.0 (2007/12/12) Postã§è¤?•°ã‚ーを使ãˆã‚‹ã‚ˆã†ã«ä¿®æ£
135 * @og.rev 5.1.6.0 (2010/05/01) charsetを指定ã§ãるよã†ã«ã™ã‚‹
136 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
137 */
138 public void connect() throws IOException {
139 conn = getConnection();
140
141 if( isPost ) {
142 conn.setDoOutput( true ); // POSTå¯èƒ½ã«ã™ã‚‹
143
144 OutputStream os = null; // POST用ã®OutputStream
145 PrintStream ps = null;
146 try {
147 os = conn.getOutputStream(); // POST用ã®OutputStreamã‚’å–å¾?
148 // 5.1.6.0 (2010/05/01)
149 if( charset != null ) {
150 ps = new PrintStream( os, false, charset );
151 }
152 else {
153 ps = new PrintStream( os );
154 }
155 ps.print( postData ); // 4.1.0.0 (2007/12/22)
156 }
157 finally {
158 Closer.ioClose( ps ); // close 処ç?™‚ã® IOException ã‚’ç„¡è¦?
159 Closer.ioClose( os ); // close 処ç?™‚ã® IOException ã‚’ç„¡è¦?
160 }
161 }
162 else {
163 // GET 時ã?コãƒã‚¯ã‚·ãƒ§ãƒ³æŽ¥ç¶?
164 conn.connect();
165 }
166
167 setInfo( conn );
168 }
169
170 /**
171 * U接続å?ã®ãƒ??ã‚¿ã‚’å–å¾—ã—ã¾ã™ã?
172 *
173 * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
174 * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã?
175 * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€?
176 * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã?
177 * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã®
178 * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€?
179 * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€?
180 * 本æ¥ã¯ã€Stream ã®ã¾ã¾å‡¦ç?™ã‚‹ã“ã¨ã§ã€ãƒã‚¤ãƒŠãƒªãƒ??タも扱ãˆã¾ã™ãŒã€ã“ã“ã§ã¯ã€?
181 * ãƒ?‚ストデータ(String)ã«å¤‰æ›ã—ã¦ä½¿ç”¨ã§ãã‚‹ãƒ??ã‚¿ã®ã¿æ‰±ãˆã¾ã™ã?
182 *
183 * @return æŽ¥ç¶šçµæžœ
184 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
185 */
186 public String readData() throws IOException {
187 if( conn == null ) {
188 String errMsg = "connect() ã•れã¦ã?¾ã›ã‚“。データå–å¾—å‰ã«connect()ã—ã¦ãã ã•ã„ã€?;
189 throw new RuntimeException( errMsg );
190 }
191
192 BufferedReader reader = null;
193 StringBuilder buf = new StringBuilder();
194 try {
195 reader = getReader();
196
197 String line ;
198 while( (line = reader.readLine()) != null ) {
199 buf.append( line ).append( CR );
200 }
201 }
202 catch( UnsupportedEncodingException ex ) {
203 String errMsg = "æŒ?®šã•ã‚ŒãŸæ–?—エンコーãƒ?‚£ãƒ³ã‚°ãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? + CR
204 + " url=[" + urlStr + "]"
205 + " charset=[" + charset + "]" ;
206 throw new RuntimeException( errMsg,ex );
207 }
208 finally {
209 Closer.ioClose( reader );
210 disconnect();
211 }
212
213 return buf.toString();
214 }
215
216 /**
217 * サーãƒã¸ã®ã»ã‹ã?è¦æ±‚ãŒä»Šå¾Œç™ºç”Ÿã—ãã†ã«ãªã?“ã¨ã‚’示ã—ã¾ã™ã?
218 *
219 */
220 public void disconnect() {
221 if( conn instanceof HttpURLConnection ) {
222 ((HttpURLConnection)conn).disconnect() ;
223 }
224 }
225
226 /**
227 * URL 㨠ユーザー?šãƒ‘スワードを与ãˆã¦ã€URLConnectionã‚’è¿”ã—ã¾ã™ã?
228 *
229 * ユーザー?šãƒ‘スワーãƒ?ã?null ã§ãªã??åˆã?ã€BASCIèªè¨¼ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ç‚ºã€?
230 * BASE64Encoder を行ã£ã¦ã€Authorization プãƒãƒ‘ティーをè¨å®šã—ã¾ã™ã?
231 * ã“ã“ã§è¿”ã™ URLConnection ã¯ã€ã™ã§ã«ã€connect() メソãƒ?ƒ‰å®Ÿè¡Œæ¸ˆã¿ã®
232 * リモート接続ãŒå®Œäº?—ãŸçŠ¶æ…‹ã?オブジェクトã§ã™ã?
233 *
234 * @og.rev 5.8.3.0 (2015/01/09)
235 *
236 * @return URLConnectionオブジェク�
237 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
238 */
239 // 5.1.5.0 (2010/04/01) SOAP対応ã«ã‚ˆã‚Šã€PROTECTEDåŒ?
240 protected URLConnection getConnection() throws IOException {
241 // private URLConnection getConnection() throws IOException {
242 final URL url = new URL( urlStr );
243
244 // 方�.
245 URLConnection urlConn = url.openConnection( proxy );
246
247 if( userPass != null ) {
248 // byte[] encoded = Base64.encodeBase64( userPass.getBytes() );
249 // String userPassEnc = new String( encoded );
250 byte[] encoded = Base64.encodeBase64( userPass.getBytes( StringUtil.DEFAULT_CHARSET ) ); // 5.5.2.6 (2012/05/25) findbugs対�
251 String userPassEnc = new String( encoded,StringUtil.DEFAULT_CHARSET ); // 5.5.2.6 (2012/05/25) findbugs対�
252 urlConn.setRequestProperty( "Authorization","Basic " + userPassEnc );
253 }
254
255 // 5.8.3.0 (2015/01/09) RequestPropertyã®ã‚»ãƒ?ƒˆ
256 if( propKeys != null && propKeys.length > 0 ){
257 for(int i = 0; i < propKeys.length; i++){
258 urlConn.setRequestProperty( propKeys[i], propVals[i] );
259 }
260 }
261
262 return urlConn ;
263 }
264
265 /**
266 * 接続å?ã®æƒ??ã‚’å?部変数ã«è¨å®šã—ã¾ã™ã?
267 *
268 * ã“ã“ã§ã¯ã€ã‚¿ã‚¤ãƒ?エンコーãƒ?レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?レスãƒãƒ³ã‚¹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸ ã‚’è¨å®šã—ã¾ã™ã?
269 * レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?レスãƒãƒ³ã‚¹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸ã¯ã€æŽ¥ç¶šã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ãŒã?HttpURLConnection ã®
270 * å ´åˆã?ã¿ã‚»ãƒ?ƒˆã•れã¾ã™ã?
271 * 途ä¸ã§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã—ãŸå?åˆã§ã‚‚ã?継続å?ç?§ãるよã†ã«ã—ã¾ã™ã?ã“れã¯ã€ã?ãƒã‚ã‚·
272 * è¨å®šã?方法ã«ã‚ˆã‚Šã€conn.getContentType() ã§ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã™ã‚‹å?åˆãŒã‚ã‚‹ãŸã‚ã§ã™ã?
273 *
274 * @og.rev 5.5.9.1 (2012/12/07) charsetã¯ã€null ã®å ´åˆã?ã¿è¨å®šã—ã¾ã™ã?
275 *
276 * @param conn 接続å?ã®ã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³
277 */
278 private void setInfo( final URLConnection conn ) {
279 try {
280 // 5.5.9.1 (2012/12/07) charsetã¯ã€null ã®å ´åˆã?ã¿è¨å®šã—ã¾ã™ã?
281 if( charset == null ) { charset = conn.getContentEncoding(); }
282 type = conn.getContentType() ;
283 length = conn.getContentLength();
284 date = conn.getDate();
285 modified= conn.getLastModified();
286
287 if( charset == null && type != null ) {
288 int adrs = type.indexOf( "charset" );
289 int adrs2 = type.indexOf( '=',adrs );
290 if( adrs > 0 && adrs2 > adrs ) {
291 charset = type.substring( adrs2+1 ).trim();
292 }
293 }
294
295 if( conn instanceof HttpURLConnection ) {
296 HttpURLConnection httpConn = (HttpURLConnection) conn;
297 rpsCode = httpConn.getResponseCode();
298 rpsMethod = httpConn.getRequestMethod();
299 rpsMessage = httpConn.getResponseMessage() + code2Message( rpsCode );
300 }
301 }
302 // 4.0.0.0 (2007/11/29) Exception ã‹ã‚‰ã€IOException 㨠RuntimeException ã«å¤‰æ›´
303 catch( IOException ex ) {
304 System.out.println( ex.getMessage() );
305 }
306 catch( RuntimeException ex ) {
307 System.out.println( ex.getMessage() );
308 }
309 }
310
311 /**
312 * URL æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
313 *
314 * @og.rev 4.3.4.4 (2009/01/01) メソãƒ?ƒ‰å変更
315 *
316 * @return URL�?
317 */
318 public String getUrl() { return urlStr; }
319
320 /**
321 * setRequestPropertyã§ã‚»ãƒ?ƒˆã™ã‚‹ãƒ??ã‚¿ã‚’è¨å®šã—ã¾ã™ã?
322 *
323 * keys,valså??ã€ã‚«ãƒ³ãƒžåŒºåˆ?‚Šã§åˆ?§£ã—ã¾ã™ã?
324 *
325 * @og.rev 5.8.3.0 (2007/12/22) 追�
326 * @param keys パラメータã‚ー?ˆã‚«ãƒ³ãƒžåŒºåˆ?‚Š??
327 * @param vals パラメータ?ˆã‚«ãƒ³ãƒžåŒºåˆ?‚Š??
328 */
329 public void setRequestProperty( final String keys, final String vals ) {
330 if( keys != null && keys.length() > 0 && vals != null && vals.length() > 0 ){
331 propKeys = StringUtil.csv2Array( keys );
332 propVals = StringUtil.csv2Array( vals );
333
334 if( propKeys.length != propVals.length ) {
335 final String errMsg = "パラメータã®ã‚ーã¨ã€å?ã®æ•°ãŒä¸??ã—ã¾ã›ã‚“ã€? + CR
336 + " key=[" + keys + "]" + CR
337 + " val=[" + vals + "]" ;
338 throw new IllegalArgumentException( errMsg );
339 }
340 }
341 }
342
343 /**
344 * POSTã™ã‚‹ãƒ??ã‚¿ã‚’è¨å®šã—ã¾ã™ã?
345 *
346 * POSTã™ã‚‹å ´åˆã?ã€connect() 処ç?‚’行ã†å‰ã«ã€ãƒ‡ãƒ¼ã‚¿ã‚’è¨å®šã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
347 *
348 * @og.rev 4.1.0.0 (2007/12/22) ã‚ーã¨å€¤ã®ã‚»ãƒ?ƒˆã‚’å–å¾—ã™ã‚‹ã‚ˆã?¤‰æ›´
349 * @param data POST�?タ
350 */
351 public void setPostData( final String data ) {
352 postData = data;
353 if( postData != null && "?".indexOf( postData ) == 0 ) { // å…ˆé?ã®?を抜ã?
354 postData = postData.substring(1);
355 }
356 if( postData != null ) { isPost = true; }
357 }
358
359 /**
360 * タイãƒ?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
361 *
362 * @return タイ��?
363 */
364 public String getType() { return type; }
365
366 /**
367 * ãƒ??ã‚¿é‡?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
368 *
369 * @return �?タ��?
370 */
371 public long getLength() { return length; }
372
373 /**
374 * 作æ?æ—¥æ™?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
375 *
376 * @return 作æ?æ—¥æ™?
377 */
378 public long getDate() { return date; }
379
380 /**
381 * æ›´æ–°æ—¥æ™?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
382 *
383 * @return 更新日�
384 */
385 public long getModified() { return modified; }
386
387 /**
388 * çµæžœã‚³ãƒ¼ãƒ?æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã?
389 *
390 * @return çµæžœã‚³ãƒ¼ãƒ?æƒ??
391 */
392 public int getCode() { return rpsCode; }
393
394 /**
395 * メソãƒ?ƒ‰ æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã?
396 *
397 * @return メソãƒ?ƒ‰ æƒ??
398 */
399 public String getMethod() { return rpsMethod; }
400
401 /**
402 * メãƒ?‚»ãƒ¼ã‚¸ æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã?
403 *
404 * @return メãƒ?‚»ãƒ¼ã‚¸ æƒ??
405 */
406 public String getMessage() { return rpsMessage; }
407
408 /**
409 * ã‚ャラクタ æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
410 *
411 * @return ã‚ャラクタ æƒ??
412 */
413 public String getCharset() { return charset; }
414
415 /**
416 * ã‚ャラクタ æƒ??ã‚’è¨å®šã—ã¾ã™ã?
417 *
418 * @param chset ã‚ャラクタ æƒ??
419 */
420 public void setCharset( final String chset ) { charset = chset; }
421
422 /**
423 * 接続å?ã®ãƒ??ã‚¿ã®ãƒªãƒ¼ãƒ??ã‚’å–å¾—ã—ã¾ã™ã?
424 *
425 * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
426 * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã?
427 * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€?
428 * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã?
429 * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã®
430 * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€?
431 * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€?
432 *
433 * @return æŽ¥ç¶šçµæžœã®ãƒªãƒ¼ãƒ??
434 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
435 */
436 public BufferedReader getReader() throws IOException {
437 InputStream in = conn.getInputStream();
438
439 final BufferedReader reader ;
440 if( charset != null ) {
441 reader = new BufferedReader( new InputStreamReader( in,charset ) );
442 }
443 else {
444 // reader = new BufferedReader( new InputStreamReader( in ) );
445 reader = new BufferedReader( new InputStreamReader( in,StringUtil.DEFAULT_CHARSET ) ); // 5.5.2.6 (2012/05/25) findbugs対�
446 }
447
448 return reader;
449 }
450
451 /**
452 * 接続å?ã®ãƒ??ã‚¿ã®å…¥åŠ›ã‚¹ãƒˆãƒªãƒ¼ãƒ?‚’å–å¾—ã—ã¾ã™ã?
453 *
454 * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
455 * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã?
456 * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€?
457 * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã?
458 * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã®
459 * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€?
460 * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€?
461 *
462 * @og.rev 5.4.2.0 (2011/12/01) æ–°è¦è¿½åŠ?
463 *
464 * @return æŽ¥ç¶šçµæžœã®å…¥åŠ›ã‚’å‡ºåŠ›ã—ã¾ã™ã?
465 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
466 */
467 public InputStream getInputStream() throws IOException {
468 // InputStream in = new BufferedInputStream( conn.getInputStream() );
469 // return in;
470 return new BufferedInputStream( conn.getInputStream() ); // 5.5.2.4 (2012/05/16)
471 }
472
473 /**
474 * HttpURLConnection ã®ãƒ¬ã‚¹ãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã«å¯¾å¿œã™ã‚‹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸æ–?—å?ã‚’è¿”ã—ã¾ã™ã?
475 *
476 * HttpURLConnection ã® getResponseCode() メソãƒ?ƒ‰ã«ã‚ˆã‚Šå–å¾—ã•れãŸã€HTTPレスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?
477 * ã«å¯¾å¿œã™ã‚‹æ–‡å—å?ã‚’è¿”ã—ã¾ã™ã?ã“ã?æ–?—å?ã¯ã€HttpURLConnection ã§å®šç¾©ã•れã?
478 * static 定数ã®ã‚³ãƒ¡ãƒ³ãƒˆã‚’ã€å®šç¾©ã—ã¦ã?¾ã™ã?
479 *
480 * @og.rev 5.6.7.0 (2013/07/27) レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ä¾?追åŠ?
481 *
482 * @param code HTTPレスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?
483 *
484 * @return レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã«å¯¾å¿œã™ã‚‹æ–‡å—å?
485 * @see HttpURLConnection#HTTP_ACCEPTED
486 */
487 public static String code2Message( final int code ) {
488 final String msg ;
489 switch( code ) {
490 case 100 : msg = "100: è¦æ±‚ã?続行å¯èƒ½ã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27)
491 case 101 : msg = "101: プãƒãƒˆã‚³ãƒ«ã‚’å?り替ãˆã¾ã™ã?" ; break; // 5.6.7.0 (2013/07/27)
492 case HttpURLConnection.HTTP_OK : msg = "200: OK ã§ã™ã?" ; break;
493 case HttpURLConnection.HTTP_CREATED : msg = "201: 作æ?ã•れã¾ã—ãŸã€? ; break;
494 case HttpURLConnection.HTTP_ACCEPTED : msg = "202: 許å¯ã•れã¾ã—ãŸã€? ; break;
495 case HttpURLConnection.HTTP_NOT_AUTHORITATIVE : msg = "203: ä¸å½“ãªæƒ??ã§ã™ã?" ; break;
496 case HttpURLConnection.HTTP_NO_CONTENT : msg = "204: コンãƒ?ƒ³ãƒ?Œã‚りã¾ã›ã‚“ã€? ; break;
497 case HttpURLConnection.HTTP_RESET : msg = "205: コンãƒ?ƒ³ãƒ?‚’リセãƒ?ƒˆã—ã¾ã™ã?" ; break;
498 case HttpURLConnection.HTTP_PARTIAL : msg = "206: 部åˆ?š„ãªã‚³ãƒ³ãƒ?ƒ³ãƒ?§ã™ã?" ; break;
499 case HttpURLConnection.HTTP_MULT_CHOICE : msg = "300: è¤?•°é¸æŠžã•れã¦ã?¾ã™ã?" ; break;
500 case HttpURLConnection.HTTP_MOVED_PERM : msg = "301: 永続的ã«ç§»å‹•ã•れã¾ã—ãŸã€? ; break;
501 case HttpURLConnection.HTTP_MOVED_TEMP : msg = "302: ä¸?™‚çš?«åˆ?‚Šæ›¿ãˆã¾ã™ã?" ; break;
502 case HttpURLConnection.HTTP_SEE_OTHER : msg = "303: ä»–ã‚’å‚ç?ã—ã¦ãã ã•ã„ã€? ; break;
503 case HttpURLConnection.HTTP_NOT_MODIFIED : msg = "304: ä¿®æ£ã•れã¾ã›ã‚“ã§ã—ãŸã€? ; break;
504 case HttpURLConnection.HTTP_USE_PROXY : msg = "305: プãƒã‚シを使用ã—ã¦ãã ã•ã„ã€? ; break;
505 case 306 : msg = "306: 仕様ã?拡張案ã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27)
506 case 307 : msg = "307: ä¸?™‚çš?ªãƒªãƒ?‚¤ãƒ¬ã‚¯ãƒˆã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27)
507 case HttpURLConnection.HTTP_BAD_REQUEST : msg = "400: ä¸å½“ãªè¦æ±‚ã§ã™ã?" ; break;
508 case HttpURLConnection.HTTP_UNAUTHORIZED : msg = "401: èªè¨¼ã•れã¾ã›ã‚“ã§ã—ãŸã€? ; break;
509 case HttpURLConnection.HTTP_PAYMENT_REQUIRED : msg = "402: 支払ã„ãŒå¿?¦ã§ã™ã?" ; break;
510 case HttpURLConnection.HTTP_FORBIDDEN : msg = "403: ç¦æ¢ã•れã¦ã?¾ã™ã?" ; break;
511 case HttpURLConnection.HTTP_NOT_FOUND : msg = "404: 見ã¤ã‹ã‚Šã¾ã›ã‚“ã§ã—ãŸã€? ; break;
512 case HttpURLConnection.HTTP_BAD_METHOD : msg = "405: メソãƒ?ƒ‰ã¯è¨±å¯ã•れã¾ã›ã‚“ã€? ; break;
513 case HttpURLConnection.HTTP_NOT_ACCEPTABLE : msg = "406: 許容ã•れã¾ã›ã‚“ã€? ; break;
514 case HttpURLConnection.HTTP_PROXY_AUTH : msg = "407: プãƒã‚ã‚·ã®èªè¨¼ãŒå¿?¦ã§ã™ã?" ; break;
515 case HttpURLConnection.HTTP_CLIENT_TIMEOUT : msg = "408: è¦æ±‚ãŒæ™‚é–“åˆ?‚Œã§ã™ã?" ; break;
516 case HttpURLConnection.HTTP_CONFLICT : msg = "409: é‡è¤?—ã¦ã?¾ã™ã?" ; break;
517 case HttpURLConnection.HTTP_GONE : msg = "410: å˜åœ¨ã—ã¾ã›ã‚“ã€? ; break;
518 case HttpURLConnection.HTTP_LENGTH_REQUIRED : msg = "411: é•·ã•ãŒå¿?¦ã§ã™ã?" ; break;
519 case HttpURLConnection.HTTP_PRECON_FAILED : msg = "412: 剿æ¡ä»¶ãŒæ£ã—ãã‚りã¾ã›ã‚“ã€? ; break;
520 case HttpURLConnection.HTTP_ENTITY_TOO_LARGE : msg = "413: è¦æ±‚エンãƒ?‚£ãƒ?‚£ãŒé•·ã™ãŽã¾ã™ã?" ; break;
521 case HttpURLConnection.HTTP_REQ_TOO_LONG : msg = "414: è¦æ±?URL ãŒé•·ã™ãŽã¾ã™ã?" ; break;
522 case HttpURLConnection.HTTP_UNSUPPORTED_TYPE : msg = "415: サãƒã?トã•れãªã?ƒ¡ãƒ?‚£ã‚¢ã‚¿ã‚¤ãƒ—ã§ã™ã?" ; break;
523 case 416 : msg = "416: è¦æ±‚ã•れãŸç¯?›²ã¯ä¸ååˆ?§ã™ã?" ; break; // 5.6.7.0 (2013/07/27)
524 case 417 : msg = "417: è¦æ±‚ã©ãŠã‚Šã®å‡¦ç?Œä¸å¯èƒ½ã§ã™ã?" ; break; // 5.6.7.0 (2013/07/27)
525 case HttpURLConnection.HTTP_INTERNAL_ERROR : msg = "500: å†?ƒ¨ã‚µãƒ¼ãƒã‚¨ãƒ©ãƒ¼ã§ã™ã?" ; break;
526 case HttpURLConnection.HTTP_NOT_IMPLEMENTED : msg = "501: 実è£?•れã¦ã?¾ã›ã‚“ã€? ; break;
527 case HttpURLConnection.HTTP_BAD_GATEWAY : msg = "502: 誤ã£ãŸã‚²ãƒ¼ãƒˆã‚¦ã‚§ã‚¤ã§ã™ã?" ; break;
528 case HttpURLConnection.HTTP_UNAVAILABLE : msg = "503: サービスãŒåˆ©ç”¨ã§ãã¾ã›ã‚“ã€? ; break;
529 case HttpURLConnection.HTTP_GATEWAY_TIMEOUT : msg = "504: ã‚²ãƒ¼ãƒˆã‚¦ã‚§ã‚¤ãŒæ™‚é–“å?れã§ã™ã?" ; break;
530 case HttpURLConnection.HTTP_VERSION : msg = "505: HTTP ãƒã?ジョンãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€?; break;
531 // default : msg = "-1: 未定義" ;
532 default : msg = code + ": 未定義" ; // 5.6.7.0 (2013/07/27)
533 }
534 return msg ;
535 }
536
537 /**
538 * サンプル実行用ã®ãƒ¡ã‚¤ãƒ³ãƒ¡ã‚½ãƒ?ƒ‰
539 *
540 * Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]
541 *
542 * args[*] : [-info/-data] æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)ã€?
543 * args[*] : [-post=ファイルå] POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)ã€?
544 * args[*] : [-encode=UTF-8] エンコードをæŒ?®šã—ã¾ã?é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用)
545 * args[*] : [-out=ファイルå] çµæžœã‚’ファイルã«å‡ºåŠ›ã—ã¾ã™ã?ファイルエンコードもæŒ?®šã—ã¾ã™ã?
546 * args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)ã€?
547 * args[A] : url ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã™ã?
548 * args[B] : [user:passwd] BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã™ã?
549 *
550 * @og.rev 5.6.7.0 (2013/07/27) -errEx 追�
551 *
552 * @param args コマンド引数é…å?
553 * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
554 */
555 public static void main( final String[] args ) throws IOException {
556 if( args.length < 3 ) {
557 LogWriter.log( "Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]" );
558 LogWriter.log( " args[*] : [-info/-data] æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)" );
559 LogWriter.log( " args[*] : [-post=ファイルå] POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)" );
560 LogWriter.log( " args[*] : [-encode=UTF-8] エンコードをæŒ?®šã—ã¾ã™ã?(é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用)" );
561 LogWriter.log( " args[*] : [-out=ファイルå] çµæžœã‚’ファイルã«å‡ºåŠ›ã—ã¾ã™ã?ファイルエンコードもæŒ?®šã—ã¾ã? );
562 LogWriter.log( " args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)" );
563 LogWriter.log( " args[A] : url ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã? );
564 LogWriter.log( " args[B] : [user:passwd] BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã? );
565 return;
566 }
567
568 boolean isInfo = false ;
569 boolean isPost = false ;
570 String postKey = null ;
571 String postFile = null ;
572 String encode = null ;
573 String outFile = null ;
574 boolean isEx = false ; // 5.6.7.0 (2013/07/27) 追�
575 String[] vals = new String[2]; // url,userPass ã®é ?«å¼•æ•°è¨å®?
576
577 int adrs = 0;
578 for( int i=0; i<args.length; i++ ) {
579 String arg = args[i];
580 if( arg.equalsIgnoreCase( "-info" ) ) {
581 isInfo = true;
582 }
583 else if( arg.equalsIgnoreCase( "-data" ) ) {
584 isInfo = false;
585 }
586 else if( arg.startsWith( "-post=" ) ) {
587 isPost = true;
588 int sepAdrs = arg.indexOf( ':',6 );
589 postKey = arg.substring( 6,sepAdrs );
590 postFile = arg.substring( sepAdrs+1 );
591 }
592 else if( arg.startsWith( "-encode=" ) ) {
593 encode = arg.substring( 8 );
594 }
595 else if( arg.startsWith( "-out=" ) ) {
596 outFile = arg.substring( 5 );
597 }
598 else if( arg.startsWith( "-errEx=" ) ) { // 5.6.7.0 (2013/07/27) 追�
599 isEx = "true".equalsIgnoreCase( arg.substring( 7 ) );
600 }
601 else if( arg.startsWith( "-" ) ) {
602 System.out.println( "Error Argment:" + arg );
603 }
604 else {
605 vals[adrs++] = arg;
606 }
607 }
608
609 String urlStr = vals[0] ;
610 String userPass = vals[1] ;
611
612 URLConnect conn = new URLConnect( urlStr,userPass );
613
614 // POST ãƒ??ã‚¿ã¯ã€connect() ã™ã‚‹å‰ã«ã€è¨å®šã—ã¾ã™ã?
615 if( isPost ) {
616 FileString file = new FileString();
617 file.setFilename( postFile );
618 String postData = file.getValue();
619
620 conn.setPostData( XHTMLTag.urlEncode(postKey, postData) );
621 }
622
623 conn.connect();
624 if( encode != null ) {
625 conn.setCharset( encode ); // encode æŒ?®?
626 }
627 else {
628 encode = conn.getCharset(); // æŒ?®šãŒãªã‘れã°ã€æŽ¥ç¶šå?ã® charset を使用
629 }
630
631 final PrintWriter writer ;
632 if( outFile != null ) {
633 writer = FileUtil.getPrintWriter( new File( outFile ),encode );
634 }
635 else {
636 writer = FileUtil.getLogWriter( "System.out" );
637 }
638
639 int code = conn.getCode(); // 5.6.7.0 (2013/07/27) レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã?ã€å¸¸ã«æ‹¾ã£ã¦ãŠãã¾ã™ã?
640 if( isInfo ) {
641 writer.println( "URL :" + conn.getUrl() );
642 writer.println( "Type :" + conn.getType() );
643 // writer.println( "Code :" + conn.getCode() );
644 writer.println( "Code :" + code ); // 5.6.7.0 (2013/07/27) å–得済ã¿ã®å€¤ã‚’利用ã€?
645 writer.println( "Message:" + conn.getMessage() );
646 writer.println( "Charset:" + conn.getCharset() );
647 }
648 else {
649 writer.println( conn.readData() );
650 }
651
652 conn.disconnect();
653
654 Closer.ioClose( writer );
655
656 // 5.6.7.0 (2013/07/27) trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?
657 if( isEx && code >= 400 ) {
658 String errMsg = URLConnect.code2Message( code );
659 throw new RuntimeException( errMsg );
660 }
661 }
662 }