17
17
import java .util .function .Consumer ;
18
18
19
19
public class HttpConnectionManager {
20
- private static Logger log = LoggerFactory .getLogger (HttpConnectionManager .class );
20
+ private static Logger log = LoggerFactory
21
+ .getLogger (HttpConnectionManager .class );
21
22
private final URL requestURL ;
22
23
private final String userAgent ;
23
24
private int connectTimeout ;
24
25
25
-
26
26
public HttpConnectionManager (URL requestURL ) {
27
27
this .requestURL = requestURL ;
28
- final String defaultUserAgent = String .format (
29
- "ArduinoIDE/%s (%s; %s; %s; %s) Java/%s (%s)" ,
30
- BaseNoGui .VERSION_NAME ,
31
- System .getProperty ("os.name" ),
32
- System .getProperty ("os.version" ),
33
- System .getProperty ("os.arch" ),
34
- System .getProperty ("user.language" ),
35
- System .getProperty ("java.version" ),
36
- System .getProperty ("java.vendor" )
37
- );
28
+ final String defaultUserAgent = String
29
+ .format ("ArduinoIDE/%s (%s; %s; %s; %s) Java/%s (%s)" ,
30
+ BaseNoGui .VERSION_NAME , System .getProperty ("os.name" ),
31
+ System .getProperty ("os.version" ), System .getProperty ("os.arch" ),
32
+ System .getProperty ("user.language" ),
33
+ System .getProperty ("java.version" ),
34
+ System .getProperty ("java.vendor" ));
38
35
this .userAgent = PreferencesData .get ("http.user_agent" , defaultUserAgent );
39
36
try {
40
- this .connectTimeout =
41
- Integer .parseInt (
42
- PreferencesData .get ("http.connection_timeout" , "5000" ));
37
+ this .connectTimeout = Integer
38
+ .parseInt (PreferencesData .get ("http.connection_timeout" , "5000" ));
43
39
} catch (NumberFormatException e ) {
44
40
log .warn (
45
- "Cannot parse the http.connection_timeout configuration switch to default 5000 milliseconds" , e .getCause ());
41
+ "Cannot parse the http.connection_timeout configuration switch to default 5000 milliseconds" ,
42
+ e .getCause ());
46
43
this .connectTimeout = 5000 ;
47
44
}
48
45
49
46
}
50
47
51
- public HttpURLConnection makeConnection (Consumer <HttpURLConnection > beforeConnection )
52
- throws IOException , NoSuchMethodException , ScriptException , URISyntaxException {
48
+ public HttpURLConnection makeConnection (
49
+ Consumer <HttpURLConnection > beforeConnection )
50
+ throws IOException , NoSuchMethodException , ScriptException ,
51
+ URISyntaxException {
53
52
return makeConnection (this .requestURL , 0 , beforeConnection );
54
53
}
55
54
56
-
57
55
public HttpURLConnection makeConnection ()
58
- throws IOException , NoSuchMethodException , ScriptException , URISyntaxException {
59
- return makeConnection (this .requestURL , 0 , (c ) -> {});
56
+ throws IOException , NoSuchMethodException , ScriptException ,
57
+ URISyntaxException {
58
+ return makeConnection (this .requestURL , 0 , (c ) -> {
59
+ });
60
60
}
61
61
62
62
private HttpURLConnection makeConnection (URL requestURL , int movedTimes ,
63
- Consumer <HttpURLConnection > beforeConnection ) throws IOException , URISyntaxException , ScriptException , NoSuchMethodException {
64
- log .info ("Prepare http request to " + requestURL );
63
+ Consumer <HttpURLConnection > beforeConnection )
64
+ throws IOException , URISyntaxException , ScriptException ,
65
+ NoSuchMethodException {
65
66
if (movedTimes > 3 ) {
66
67
log .warn ("Too many redirect " + requestURL );
67
68
throw new IOException ("Too many redirect " + requestURL );
@@ -85,21 +86,21 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
85
86
connection .setConnectTimeout (connectTimeout );
86
87
beforeConnection .accept (connection );
87
88
88
- // Connect
89
- log .info ("Connect to {} with method {}" , requestURL , connection .getRequestMethod ());
90
-
91
89
connection .connect ();
92
90
int resp = connection .getResponseCode ();
93
91
94
92
if (resp == HttpURLConnection .HTTP_MOVED_PERM
95
93
|| resp == HttpURLConnection .HTTP_MOVED_TEMP ) {
96
94
97
95
URL newUrl = new URL (connection .getHeaderField ("Location" ));
98
- log .info ("The response code was a 301,302 so try again with the new URL " + newUrl );
96
+ log .info ("The response code was a 301,302 so try again with the new URL "
97
+ + newUrl );
99
98
100
99
return this .makeConnection (newUrl , movedTimes + 1 , beforeConnection );
101
100
}
102
- log .info ("The response code {}, headers {}" , resp , StringUtils .join (connection .getHeaderFields ()));
101
+ log .info ("Executed {} successfully to {} code {}, headers {}" ,
102
+ connection .getRequestMethod (), requestURL , resp ,
103
+ StringUtils .join (connection .getHeaderFields ()));
103
104
104
105
return connection ;
105
106
}
0 commit comments