1
1
package cc .arduino .utils .network ;
2
2
3
3
import cc .arduino .net .CustomProxySelector ;
4
+ import com .sun .istack .internal .NotNull ;
4
5
import org .apache .commons .codec .binary .Base64 ;
6
+ import org .apache .commons .httpclient .CircularRedirectException ;
7
+ import org .apache .commons .lang3 .StringUtils ;
8
+ import org .slf4j .Logger ;
9
+ import org .slf4j .LoggerFactory ;
5
10
import processing .app .BaseNoGui ;
6
11
import processing .app .PreferencesData ;
7
12
12
17
import java .net .URISyntaxException ;
13
18
import java .net .URL ;
14
19
import java .util .function .Consumer ;
15
- import java .util .logging .Level ;
16
- import java .util .logging .Logger ;
17
20
18
21
public class HttpConnectionManager {
19
- private static Logger log = Logger
20
- .getLogger (HttpConnectionManager .class .getName ());
22
+ private static Logger log = LoggerFactory .getLogger (HttpConnectionManager .class );
21
23
private final URL requestURL ;
22
24
private final String userAgent ;
23
25
private int connectTimeout ;
@@ -41,38 +43,35 @@ public HttpConnectionManager(URL requestURL) {
41
43
Integer .parseInt (
42
44
PreferencesData .get ("http.connection_timeout" , "5000" ));
43
45
} catch (NumberFormatException e ) {
44
- log .log ( Level . WARNING ,
46
+ log .warn (
45
47
"Cannot parse the http.connection_timeout configuration switch to default 5000 milliseconds" , e .getCause ());
46
48
this .connectTimeout = 5000 ;
47
49
}
48
50
49
51
}
50
52
51
- public HttpURLConnection makeConnection (Consumer <HttpURLConnection > beforeConnection )
52
- throws URISyntaxException , NoSuchMethodException , IOException ,
53
- ScriptException {
53
+ public HttpURLConnection makeConnection (@ NotNull Consumer <HttpURLConnection > beforeConnection )
54
+ throws IOException , NoSuchMethodException , ScriptException , URISyntaxException {
54
55
return makeConnection (this .requestURL , 0 , beforeConnection );
55
56
}
56
57
57
- private HttpURLConnection makeConnection (URL requestURL , int movedTimes ,
58
- Consumer <HttpURLConnection > beforeConnection )
59
- throws NoSuchMethodException , ScriptException , IOException ,
60
- URISyntaxException {
58
+
59
+ public HttpURLConnection makeConnection ()
60
+ throws IOException , NoSuchMethodException , ScriptException , URISyntaxException {
61
+ return makeConnection (this .requestURL , 0 , (c ) -> {});
62
+ }
63
+
64
+ private HttpURLConnection makeConnection (@ NotNull URL requestURL , int movedTimes ,
65
+ @ NotNull Consumer <HttpURLConnection > beforeConnection ) throws IOException , URISyntaxException , ScriptException , NoSuchMethodException {
61
66
log .info ("Prepare http request to " + requestURL );
62
- if (requestURL == null ) {
63
- log .warning ("Invalid request url is null" );
64
- throw new RuntimeException ("Invalid request url is null" );
65
- }
66
67
if (movedTimes > 3 ) {
67
- log .warning ("Too many redirect " + requestURL );
68
- throw new RuntimeException ("Too many redirect " + requestURL );
68
+ log .warn ("Too many redirect " + requestURL );
69
+ throw new CircularRedirectException ("Too many redirect " + requestURL );
69
70
}
70
71
71
72
Proxy proxy = new CustomProxySelector (PreferencesData .getMap ())
72
73
.getProxyFor (requestURL .toURI ());
73
- if ("true" .equals (System .getProperty ("DEBUG" ))) {
74
- System .err .println ("Using proxy " + proxy );
75
- }
74
+ log .debug ("Using proxy {}" , proxy );
76
75
77
76
HttpURLConnection connection = (HttpURLConnection ) requestURL
78
77
.openConnection (proxy );
@@ -89,7 +88,7 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
89
88
beforeConnection .accept (connection );
90
89
91
90
// Connect
92
- log .info ("Connect to " + requestURL );
91
+ log .info ("Connect to {} with method {}" , requestURL , connection . getRequestMethod () );
93
92
94
93
connection .connect ();
95
94
int resp = connection .getResponseCode ();
@@ -102,7 +101,7 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
102
101
103
102
return this .makeConnection (newUrl , movedTimes + 1 , beforeConnection );
104
103
}
105
- log .info ("The response code was" + resp );
104
+ log .info ("The response code {}, headers {}" , resp , StringUtils . join ( connection . getHeaderFields ()) );
106
105
107
106
return connection ;
108
107
}
0 commit comments