2626
2727public class HttpClient {
2828
29- public static final String VERSION = "4 (2018-04-09 )" ;
29+ public static final String VERSION = "5 (2024-01-05 )" ;
3030
3131 // Default "User-Agent" value
3232 private static final String USER_AGENT = "User-Agent" ;
3333 private static final String USER_AGENT_VALUE = "Telosys-HttpClient" ;
34-
35- //------------------------------------------------------------------------------
36- // TLS ver 1.2 configuration ( TLS 1.2 not defined by default with Java 7 )
37- // TLS 1.2 is required for GitHub https URLs
38- //------------------------------------------------------------------------------
39- private static final String HTTPS_PROTOCOLS = "https.protocols" ;
40- private static final String TLS_VER_1_2 = "TLSv1.2" ;
41-
42- private static final void configTLSv2 () {
43- String httpsProtocols = System .getProperty (HTTPS_PROTOCOLS );
44- if ( httpsProtocols != null && httpsProtocols .contains (TLS_VER_1_2 ) ) {
45- // TLS v 1.2 is already set in the system property
46- return ; // Nothing to do
47- }
48- else {
49- System .setProperty (HTTPS_PROTOCOLS , TLS_VER_1_2 );
50- }
51- }
52- //------------------------------------------------------------------------------
53-
54- private final HttpClientConfig configuration ;
55- private boolean isConfigured = false ;
5634
5735 /**
58- * Constructor without http configuration (no proxy)
36+ * Constructor without http configuration properties <br>
37+ * Default properties will be used (java.net.useSystemProxies=true)
5938 */
6039 public HttpClient () {
6140 super ();
62- this .configuration = null ;
63- init ();
64- }
65-
66- /**
67- * Constructor with http configuration (proxy)
68- * @param configuration
69- */
70- public HttpClient (HttpClientConfig configuration ) {
71- super ();
72- this .configuration = configuration ;
73- init ();
41+ HttpSystemConfig .init ();
7442 }
7543
7644 /**
7745 * Constructor with http configuration defined by properties
78- * @param proxyProperties proxy properties or null if none
46+ * @param properties proxy properties or null if none
7947 */
80- public HttpClient (Properties proxyProperties ) {
48+ public HttpClient (Properties properties ) {
8149 super ();
82- if ( proxyProperties != null ) {
83- this . configuration = new HttpClientConfig ( proxyProperties );
50+ if ( properties != null ) {
51+ HttpSystemConfig . init ( properties );
8452 }
8553 else {
86- this . configuration = null ;
54+ HttpSystemConfig . init () ;
8755 }
88- init ();
8956 }
9057
91- /**
92- * Post construction initialization
93- */
94- private final void init () {
95- if ( ! isConfigured ) {
96- if ( configuration != null ) {
97- if ( configuration .isUseSystemProxies () ) {
98- useSystemProxies () ;
99- }
100- else {
101- proxyConfig ( configuration .getHttpProxy () );
102- proxyConfig ( configuration .getHttpsProxy () );
103- }
104- }
105- else {
106- useSystemProxies () ;
107- }
108- isConfigured = true ;
109- }
110- configTLSv2 ();
111- }
112-
113- private void proxyConfig (HttpProxy proxy ) {
114- if ( proxy != null ) {
115- Properties systemProperties = System .getProperties ();
116- systemProperties .setProperty ( proxy .getProtocol () + ".proxySet" , "true" );
117- systemProperties .setProperty ( proxy .getProtocol () + ".proxyHost" , proxy .getHost () );
118- systemProperties .setProperty ( proxy .getProtocol () + ".proxyPort" , String .valueOf (proxy .getPort ()) );
119- if ( proxy .getUser () != null ) {
120- systemProperties .setProperty ( proxy .getProtocol () + ".proxyUser" , String .valueOf (proxy .getUser ()) );
121- if ( proxy .getPassword () != null ) {
122- systemProperties .setProperty ( proxy .getProtocol () + ".proxyPassword" , String .valueOf (proxy .getPassword ()) );
123- }
124- }
125- }
126- }
127-
128- private void useSystemProxies () {
129- Properties systemProperties = System .getProperties ();
130- systemProperties .setProperty ( "java.net.useSystemProxies" , "true" );
131- }
132-
13358 //---------------------------------------------------------------------
13459 // GET
13560 //---------------------------------------------------------------------
@@ -275,7 +200,7 @@ private HttpURLConnection connect(URL url, String method, Map<String, String> he
275200 connection .connect ();
276201 return connection ;
277202 } catch (Exception e ) {
278- throw new Exception ("Connection failed" );
203+ throw new Exception ("Connection failed" , e );
279204 }
280205 }
281206
@@ -346,8 +271,6 @@ private long downloadFileV2(URL url, String destFileName) throws Exception {
346271 throw new Exception ("Unexpected HTTP Response Code " + responseCode );
347272 }
348273
349- // String contentType = connection.getContentType();
350-
351274 long totalBytesRead = 0L ;
352275 try ( InputStream inputStream = connection .getInputStream ();
353276 FileOutputStream outputStream = new FileOutputStream (destFileName ) ) {
@@ -362,35 +285,6 @@ private long downloadFileV2(URL url, String destFileName) throws Exception {
362285 return totalBytesRead ;
363286 }
364287
365- // private long downloadFileViaURL(URL url, String destFileName ) throws Exception {
366- //
367- // checkDestination(destFileName);
368- //
369- // long totalBytesRead = 0L;
370- // try {
371- // url.openConnection();
372- // InputStream reader = url.openStream();
373- //
374- // FileOutputStream writer = new FileOutputStream(destFileName);
375- // byte[] buffer = new byte[BUFFER_SIZE];
376- // int bytesRead = 0;
377- //
378- // while ((bytesRead = reader.read(buffer)) > 0)
379- // {
380- // writer.write(buffer, 0, bytesRead);
381- // buffer = new byte[BUFFER_SIZE];
382- // totalBytesRead += bytesRead;
383- // }
384- //
385- // writer.close();
386- // reader.close();
387- //
388- // } catch (IOException e) {
389- // throw new Exception ("IOException", e);
390- // }
391- // return totalBytesRead ;
392- // }
393-
394288 private void checkDestination (String destFileName ) throws Exception {
395289 File file = new File (destFileName ) ;
396290 File parent = file .getParentFile ();
0 commit comments