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 }