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.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                            return;
332                    }
333                    
334                    propKeys = StringUtil.csv2Array( keys );
335                    propVals = StringUtil.csv2Array( vals );
336            }
337            
338            /**
339             * POSTã™ã‚‹ãƒ??タを設定ã—ã¾ã™ã?
340             *
341             * POSTã™ã‚‹å ´åˆã?ã€connect() 処ç?‚’行ã†å‰ã«ã€ãƒ‡ãƒ¼ã‚¿ã‚’設定ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
342             *
343             * @og.rev 4.1.0.0 (2007/12/22) キーã¨å€¤ã®ã‚»ãƒ?ƒˆã‚’å–å¾—ã™ã‚‹ã‚ˆã?¤‰æ›´
344             * @param       data    POST�?タ
345             */
346            public void setPostData( final String data ) {
347                    postData = data;
348                    if( postData != null && "?".indexOf( postData ) == 0 ) { // å…ˆé?ã®?を抜ã?
349                            postData = postData.substring(1);
350                    }
351                    if( postData != null ) { isPost = true; }
352            }
353    
354            /**
355             * タイãƒ?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
356             *
357             * @return      タイ��?
358             */
359            public String getType() { return type; }
360    
361            /**
362             * ãƒ??ã‚¿é‡?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
363             *
364             * @return      �?タ��?
365             */
366            public long getLength() { return length; }
367    
368            /**
369             * 作æ?æ—¥æ™?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
370             *
371             * @return      作æ?æ—¥æ™?
372             */
373            public long getDate() { return date; }
374    
375            /**
376             * æ›´æ–°æ—¥æ™?æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
377             *
378             * @return      更新日�
379             */
380            public long getModified() { return modified; }
381    
382            /**
383             * çµæžœã‚³ãƒ¼ãƒ?æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã?
384             *
385             * @return      çµæžœã‚³ãƒ¼ãƒ?æƒ??
386             */
387            public int getCode() { return rpsCode; }
388    
389            /**
390             * メソãƒ?ƒ‰ æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã?
391             *
392             * @return      メソãƒ?ƒ‰ æƒ??
393             */
394            public String getMethod() { return rpsMethod; }
395    
396            /**
397             * メãƒ?‚»ãƒ¼ã‚¸ æƒ??(HttpURLConnection)ã‚’å–å¾—ã—ã¾ã™ã?
398             *
399             * @return      メãƒ?‚»ãƒ¼ã‚¸ æƒ??
400             */
401            public String getMessage() { return rpsMessage; }
402    
403            /**
404             * キャラクタ æƒ??ã‚’å–å¾—ã—ã¾ã™ã?
405             *
406             * @return      キャラクタ �?
407             */
408            public String getCharset() { return charset; }
409    
410            /**
411             * キャラクタ æƒ??を設定ã—ã¾ã™ã?
412             *
413             * @param  chset キャラクタ �?
414             */
415            public void setCharset( final String chset ) { charset = chset; }
416    
417            /**
418             * 接続å?ã®ãƒ??ã‚¿ã®ãƒªãƒ¼ãƒ??ã‚’å–å¾—ã—ã¾ã™ã?
419             *
420             * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
421             * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã?
422             * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€?
423             * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã?
424             * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã®
425             * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€?
426             * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€?
427             *
428             * @return      æŽ¥ç¶šçµæžœã®ãƒªãƒ¼ãƒ??
429             * @throws  IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
430             */
431            public BufferedReader getReader() throws IOException {
432                    InputStream in = conn.getInputStream();
433    
434                    final BufferedReader reader ;
435                    if( charset != null ) {
436                            reader = new BufferedReader( new InputStreamReader( in,charset ) );
437                    }
438                    else {
439    //                      reader = new BufferedReader( new InputStreamReader( in ) );
440                            reader = new BufferedReader( new InputStreamReader( in,StringUtil.DEFAULT_CHARSET ) );          // 5.5.2.6 (2012/05/25) findbugs対�
441                    }
442    
443                    return reader;
444            }
445    
446            /**
447             * 接続å?ã®ãƒ??ã‚¿ã®å…¥åŠ›ã‚¹ãƒˆãƒªãƒ¼ãƒ?‚’å–å¾—ã—ã¾ã™ã?
448             *
449             * ã“ã?処ç??å‰ã«ã€connect() 処ç?‚’実行ã—ã¦ãŠãå¿?¦ãŒã‚りã¾ã™ã?
450             * å–å¾—ã—ãŸãƒ‡ãƒ¼ã‚¿ã¯ã€æŒ‡å®šã?URL ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã®ã¿ã§ã™ã?
451             * 通常ã®Webブラウザã¯ã€ã‚¤ãƒ¡ãƒ¼ã‚¸ã‚??JavaScriptファイルã€CSSファイルãªã©ã€?
452             * å?¨®ãƒ•ァイル毎ã«HTTP接続を行ã„ã€å–å¾—ã—ã¦ã€ãƒ¬ãƒ³ãƒ?ƒªãƒ³ã‚°ã—ã¾ã™ã?
453             * ã“ã?メソãƒ?ƒ‰ã§ã®å‡¦ç?§ã¯ã€ãれらã®ãƒ•ァイルå†?«æŒ?®šã•れã¦ã?‚‹URLã®
454             * å†å¸°çš?ªå–å¾—ã?行ã„ã¾ã›ã‚“ã€?
455             * よã£ã¦ã€ãƒ•レーãƒ??ç?ªã©ã‚‚行ã„ã¾ã›ã‚“ã€?
456             *
457             * @og.rev 5.4.2.0 (2011/12/01) æ–°è¦è¿½åŠ?
458             *
459             * @return      æŽ¥ç¶šçµæžœã®å…¥åŠ›ã‚’å‡ºåŠ›ã—ã¾ã™ã?
460             * @throws  IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
461             */
462            public InputStream getInputStream() throws IOException {
463    //              InputStream in = new BufferedInputStream( conn.getInputStream() );
464    //              return in;
465                    return new BufferedInputStream( conn.getInputStream() );                // 5.5.2.4 (2012/05/16)
466            }
467    
468            /**
469             * HttpURLConnection ã®ãƒ¬ã‚¹ãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã«å¯¾å¿œã™ã‚‹ãƒ¡ãƒ?‚»ãƒ¼ã‚¸æ–?­—å?ã‚’è¿”ã—ã¾ã™ã?
470             *
471             * HttpURLConnection ã® getResponseCode() メソãƒ?ƒ‰ã«ã‚ˆã‚Šå–å¾—ã•れãŸã€HTTPレスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?
472             * ã«å¯¾å¿œã™ã‚‹æ–‡å­—å?ã‚’è¿”ã—ã¾ã™ã?ã“ã?æ–?­—å?ã¯ã€HttpURLConnection ã§å®šç¾©ã•れã?
473             * static 定数ã®ã‚³ãƒ¡ãƒ³ãƒˆã‚’ã€å®šç¾©ã—ã¦ã?¾ã™ã?
474             *
475             * @og.rev 5.6.7.0 (2013/07/27) レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ä¾?追åŠ?
476             *
477             * @param       code    HTTPレスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ?
478             *
479             * @return      レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã«å¯¾å¿œã™ã‚‹æ–‡å­—å?
480             * @see HttpURLConnection#HTTP_ACCEPTED
481             */
482            public static String code2Message( final int code ) {
483                    final String msg ;
484                    switch( code ) {
485                            case 100                                                                                : msg = "100: è¦æ±‚ã?続行å¯èƒ½ã§ã™ã?"                                                ;       break;  // 5.6.7.0 (2013/07/27)
486                            case 101                                                                                : msg = "101: プロトコルをå?り替ãˆã¾ã™ã?"                             ;       break;  // 5.6.7.0 (2013/07/27)
487                            case HttpURLConnection.HTTP_OK                                  : msg = "200: OK ã§ã™ã?"                                                         ;       break;
488                            case HttpURLConnection.HTTP_CREATED                     : msg = "201: 作æ?ã•れã¾ã—ãŸã€?                                                     ;       break;
489                            case HttpURLConnection.HTTP_ACCEPTED                    : msg = "202: 許å¯ã•れã¾ã—ãŸã€?                                                    ;       break;
490                            case HttpURLConnection.HTTP_NOT_AUTHORITATIVE   : msg = "203: ä¸å½“ãªæƒ??ã§ã™ã?"                                                    ;       break;
491                            case HttpURLConnection.HTTP_NO_CONTENT                  : msg = "204: コンãƒ?ƒ³ãƒ?Œã‚りã¾ã›ã‚“ã€?                                      ;       break;
492                            case HttpURLConnection.HTTP_RESET                               : msg = "205: コンãƒ?ƒ³ãƒ?‚’リセãƒ?ƒˆã—ã¾ã™ã?"                          ;       break;
493                            case HttpURLConnection.HTTP_PARTIAL                     : msg = "206: 部åˆ?š„ãªã‚³ãƒ³ãƒ?ƒ³ãƒ?§ã™ã?"                                     ;       break;
494                            case HttpURLConnection.HTTP_MULT_CHOICE                 : msg = "300: è¤?•°é¸æŠžã•れã¦ã?¾ã™ã?"                                        ;       break;
495                            case HttpURLConnection.HTTP_MOVED_PERM                  : msg = "301: 永続的ã«ç§»å‹•ã•れã¾ã—ãŸã€?                                      ;       break;
496                            case HttpURLConnection.HTTP_MOVED_TEMP                  : msg = "302: ä¸?™‚çš?«åˆ?‚Šæ›¿ãˆã¾ã™ã?"                                        ;       break;
497                            case HttpURLConnection.HTTP_SEE_OTHER                   : msg = "303: ä»–ã‚’å‚ç?ã—ã¦ãã ã•ã„ã€?                                 ;       break;
498                            case HttpURLConnection.HTTP_NOT_MODIFIED                : msg = "304: 修正ã•れã¾ã›ã‚“ã§ã—ãŸã€?                                       ;       break;
499                            case HttpURLConnection.HTTP_USE_PROXY                   : msg = "305: プロキシを使用ã—ã¦ãã ã•ã„ã€?                          ;       break;
500                            case 306                                                                                : msg = "306: 仕様ã?拡張案ã§ã™ã?"                                          ;       break;  // 5.6.7.0 (2013/07/27)
501                            case 307                                                                                : msg = "307: ä¸?™‚çš?ªãƒªãƒ?‚¤ãƒ¬ã‚¯ãƒˆã§ã™ã?"                            ;       break;  // 5.6.7.0 (2013/07/27)
502                            case HttpURLConnection.HTTP_BAD_REQUEST                 : msg = "400: ä¸å½“ãªè¦æ±‚ã§ã™ã?"                                                    ;       break;
503                            case HttpURLConnection.HTTP_UNAUTHORIZED                : msg = "401: èªè¨¼ã•れã¾ã›ã‚“ã§ã—ãŸã€?                                       ;       break;
504                            case HttpURLConnection.HTTP_PAYMENT_REQUIRED    : msg = "402: 支払ã„ãŒå¿?¦ã§ã™ã?"                                          ;       break;
505                            case HttpURLConnection.HTTP_FORBIDDEN                   : msg = "403: ç¦æ­¢ã•れã¦ã?¾ã™ã?"                                          ;       break;
506                            case HttpURLConnection.HTTP_NOT_FOUND                   : msg = "404: 見ã¤ã‹ã‚Šã¾ã›ã‚“ã§ã—ãŸã€?                                        ;       break;
507                            case HttpURLConnection.HTTP_BAD_METHOD                  : msg = "405: メソãƒ?ƒ‰ã¯è¨±å¯ã•れã¾ã›ã‚“ã€?                           ;       break;
508                            case HttpURLConnection.HTTP_NOT_ACCEPTABLE              : msg = "406: 許容ã•れã¾ã›ã‚“ã€?                                                    ;       break;
509                            case HttpURLConnection.HTTP_PROXY_AUTH                  : msg = "407: プロキシã®èªè¨¼ãŒå¿?¦ã§ã™ã?"                          ;       break;
510                            case HttpURLConnection.HTTP_CLIENT_TIMEOUT              : msg = "408: è¦æ±‚ãŒæ™‚é–“åˆ?‚Œã§ã™ã?"                                         ;       break;
511                            case HttpURLConnection.HTTP_CONFLICT                    : msg = "409: é‡è¤?—ã¦ã?¾ã™ã?"                                                    ;       break;
512                            case HttpURLConnection.HTTP_GONE                                : msg = "410: 存在ã—ã¾ã›ã‚“ã€?                                                       ;       break;
513                            case HttpURLConnection.HTTP_LENGTH_REQUIRED     : msg = "411: é•·ã•ãŒå¿?¦ã§ã™ã?"                                                    ;       break;
514                            case HttpURLConnection.HTTP_PRECON_FAILED               : msg = "412: 剿æ¡ä»¶ãŒæ­£ã—ãã‚りã¾ã›ã‚“ã€?                          ;       break;
515                            case HttpURLConnection.HTTP_ENTITY_TOO_LARGE    : msg = "413: è¦æ±‚エンãƒ?‚£ãƒ?‚£ãŒé•·ã™ãŽã¾ã™ã?"                 ;       break;
516                            case HttpURLConnection.HTTP_REQ_TOO_LONG                : msg = "414: è¦æ±?URL ãŒé•·ã™ãŽã¾ã™ã?"                                      ;       break;
517                            case HttpURLConnection.HTTP_UNSUPPORTED_TYPE    : msg = "415: サãƒã?トã•れãªã?ƒ¡ãƒ?‚£ã‚¢ã‚¿ã‚¤ãƒ—ã§ã™ã?"            ;       break;
518                            case 416                                                                                : msg = "416: è¦æ±‚ã•れãŸç¯?›²ã¯ä¸ååˆ?§ã™ã?"                           ;       break;  // 5.6.7.0 (2013/07/27)
519                            case 417                                                                                : msg = "417: è¦æ±‚ã©ãŠã‚Šã®å‡¦ç?Œä¸å¯èƒ½ã§ã™ã?"                        ;       break;  // 5.6.7.0 (2013/07/27)
520                            case HttpURLConnection.HTTP_INTERNAL_ERROR              : msg = "500: å†?ƒ¨ã‚µãƒ¼ãƒã‚¨ãƒ©ãƒ¼ã§ã™ã?"                                      ;       break;
521                            case HttpURLConnection.HTTP_NOT_IMPLEMENTED     : msg = "501: 実è£?•れã¦ã?¾ã›ã‚“ã€?                                          ;       break;
522                            case HttpURLConnection.HTTP_BAD_GATEWAY                 : msg = "502: 誤ã£ãŸã‚²ãƒ¼ãƒˆã‚¦ã‚§ã‚¤ã§ã™ã?"                                    ;       break;
523                            case HttpURLConnection.HTTP_UNAVAILABLE                 : msg = "503: サービスãŒåˆ©ç”¨ã§ãã¾ã›ã‚“ã€?                            ;       break;
524                            case HttpURLConnection.HTTP_GATEWAY_TIMEOUT     : msg = "504: ã‚²ãƒ¼ãƒˆã‚¦ã‚§ã‚¤ãŒæ™‚é–“å?れã§ã™ã?"                          ;       break;
525                            case HttpURLConnection.HTTP_VERSION                     : msg = "505: HTTP ãƒã?ジョンãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€?;       break;
526    //                      default : msg = "-1: 未定義" ;
527                            default : msg = code + ": 未定義" ;             // 5.6.7.0 (2013/07/27)
528                    }
529                    return msg ;
530            }
531    
532            /**
533             * サンプル実行用ã®ãƒ¡ã‚¤ãƒ³ãƒ¡ã‚½ãƒ?ƒ‰
534             *
535             * Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]
536             *
537             *   args[*] : [-info/-data]       æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)ã€?
538             *   args[*] : [-post=ファイルå]  POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)ã€?
539             *   args[*] : [-encode=UTF-8]     エンコードをæŒ?®šã—ã¾ã?é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用)
540             *   args[*] : [-out=ファイルå]   çµæžœã‚’ファイルã«å‡ºåŠ›ã—ã¾ã™ã?ファイルエンコードもæŒ?®šã—ã¾ã™ã?
541             *   args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)ã€?
542             *   args[A] : url                 ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã™ã?
543             *   args[B] : [user:passwd]       BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã™ã?
544             *
545             * @og.rev 5.6.7.0 (2013/07/27) -errEx 追�
546             *
547             * @param       args    コマンド引数é…å?
548             * @throws IOException 入出力エラーãŒç™ºç”Ÿã—ãŸã¨ã?
549             */
550            public static void main( final String[] args ) throws IOException {
551                    if( args.length < 3 ) {
552                            LogWriter.log( "Usage: java org.opengion.fukurou.util.URLConnect [-info/-data] … url [user:passwd]"                            );
553                            LogWriter.log( "   args[*] : [-info/-data]       æƒ??ã®å–å¾—ã‹ã€ãƒ‡ãƒ¼ã‚¿ã®å–å¾—ã‹ã‚’指定ã—ã¾ã?åˆæœŸå€¤:-data)"                        );
554                            LogWriter.log( "   args[*] : [-post=ファイルå]  POSTメソãƒ?ƒ‰ã‚’指定ã—ã¦ã€ãƒ•ァイルãƒ??ã‚¿ã‚’é?ä¿¡ã—ã¾ã?åˆæœŸå€¤:-get)"  );
555                            LogWriter.log( "   args[*] : [-encode=UTF-8]     エンコードをæŒ?®šã—ã¾ã™ã?(é€šå¸¸ã¯æŽ¥ç¶šå?ã®encodeを使用)"                              );
556                            LogWriter.log( "   args[*] : [-out=ファイルå]   çµæžœã‚’ファイルã«å‡ºåŠ›ã—ã¾ã™ã?ファイルエンコードもæŒ?®šã—ã¾ã?              );
557                            LogWriter.log( "   args[*] : [-errEx=true/false] trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?åˆæœŸå€¤:false)" );
558                            LogWriter.log( "   args[A] : url                 ?µ?²?¬ã‚’指定ã—ã¾ã™ã?GETã®å ´åˆã?パラメータ㯠?KEY=VALã§ã?                    );
559                            LogWriter.log( "   args[B] : [user:passwd]       BASICèªè¨¼ã®ã‚¨ãƒªã‚¢ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ™‚ã«æŒ?®šã—ã¾ã?                                              );
560                            return;
561                    }
562    
563                    boolean isInfo  = false ;
564                    boolean isPost  = false ;
565                    String postKey  = null ;
566                    String postFile = null ;
567                    String encode   = null ;
568                    String outFile  = null ;
569                    boolean isEx    = false ;                               // 5.6.7.0 (2013/07/27) 追�
570                    String[] vals   = new String[2];                // url,userPass ã®é ?«å¼•数設å®?
571    
572                    int adrs = 0;
573                    for( int i=0; i<args.length; i++ ) {
574                            String arg = args[i];
575                            if( arg.equalsIgnoreCase( "-info" ) ) {
576                                    isInfo = true;
577                            }
578                            else if( arg.equalsIgnoreCase( "-data" ) ) {
579                                    isInfo = false;
580                            }
581                            else if( arg.startsWith( "-post=" ) ) {
582                                    isPost = true;
583                                    int sepAdrs = arg.indexOf( ':',6 );
584                                    postKey  = arg.substring( 6,sepAdrs );
585                                    postFile = arg.substring( sepAdrs+1 );
586                            }
587                            else if( arg.startsWith( "-encode=" ) ) {
588                                    encode = arg.substring( 8 );
589                            }
590                            else if( arg.startsWith( "-out=" ) ) {
591                                    outFile = arg.substring( 5 );
592                            }
593                            else if( arg.startsWith( "-errEx=" ) ) {                                                        // 5.6.7.0 (2013/07/27) 追�
594                                    isEx = "true".equalsIgnoreCase( arg.substring( 7 ) );
595                            }
596                            else if( arg.startsWith( "-" ) ) {
597                                    System.out.println( "Error Argment:" + arg );
598                            }
599                            else {
600                                    vals[adrs++] = arg;
601                            }
602                    }
603    
604                    String urlStr   = vals[0] ;
605                    String userPass = vals[1] ;
606    
607                    URLConnect conn = new URLConnect( urlStr,userPass );
608    
609                    // POST ãƒ??ã‚¿ã¯ã€connect() ã™ã‚‹å‰ã«ã€è¨­å®šã—ã¾ã™ã?
610                    if( isPost ) {
611                            FileString file = new FileString();
612                            file.setFilename( postFile );
613                            String postData = file.getValue();
614    
615                            conn.setPostData( XHTMLTag.urlEncode(postKey, postData) );
616                    }
617    
618                    conn.connect();
619                    if( encode != null ) {
620                            conn.setCharset( encode );              // encode æŒ?®?
621                    }
622                    else {
623                            encode = conn.getCharset();             // æŒ?®šãŒãªã‘れã°ã€æŽ¥ç¶šå?ã® charset を使用
624                    }
625    
626                    final PrintWriter writer ;
627                    if( outFile != null ) {
628                            writer = FileUtil.getPrintWriter( new File( outFile ),encode );
629                    }
630                    else {
631                            writer = FileUtil.getLogWriter( "System.out" );
632                    }
633    
634                    int code = conn.getCode();              // 5.6.7.0 (2013/07/27) レスãƒãƒ³ã‚¹ã‚³ãƒ¼ãƒ‰ã?ã€å¸¸ã«æ‹¾ã£ã¦ãŠãã¾ã™ã?
635                    if( isInfo ) {
636                            writer.println( "URL    :" + conn.getUrl() );
637                            writer.println( "Type   :" + conn.getType() );
638    //                      writer.println( "Code   :" + conn.getCode() );
639                            writer.println( "Code   :" + code );                                    // 5.6.7.0 (2013/07/27) å–得済ã¿ã®å€¤ã‚’利用ã€?
640                            writer.println( "Message:" + conn.getMessage() );
641                            writer.println( "Charset:" + conn.getCharset() );
642                    }
643                    else {
644                            writer.println( conn.readData() );
645                    }
646    
647                    conn.disconnect();
648    
649                    Closer.ioClose( writer );
650    
651                    // 5.6.7.0 (2013/07/27) trueã®å ´åˆã??šï½½?Žï¾Ÿï¾ï½½?º?°??¾žãŒã€?XX,5XX ã®æ™‚ã« RuntimeException を投ã’ã¾ã?
652                    if( isEx && code >= 400 ) {
653                            String errMsg = URLConnect.code2Message( code );
654                            throw new RuntimeException( errMsg );
655                    }
656            }
657    }