Skip to content

Commit 584922e

Browse files
committed
Http and proxy config changes
1 parent 2b18f34 commit 584922e

File tree

12 files changed

+261
-139
lines changed

12 files changed

+261
-139
lines changed

src/main/java/org/telosys/tools/commons/github/GitHubClient.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
*/
1616
package org.telosys.tools.commons.github;
1717

18+
import java.io.File;
1819
import java.util.HashMap;
1920
import java.util.LinkedList;
2021
import java.util.List;
2122
import java.util.Map;
22-
import java.util.Properties;
2323

2424
import org.json.simple.JSONArray;
2525
import org.json.simple.JSONObject;
2626
import org.json.simple.parser.JSONParser;
2727
import org.json.simple.parser.ParseException;
28-
import org.telosys.tools.commons.PropertiesManager;
29-
import org.telosys.tools.commons.cfg.TelosysToolsCfg;
3028
import org.telosys.tools.commons.http.Base64;
3129
import org.telosys.tools.commons.http.HttpClient;
3230
import org.telosys.tools.commons.http.HttpResponse;
@@ -40,7 +38,7 @@
4038
public class GitHubClient {
4139

4240
// public static final String VERSION = "2.1" ; // GitHub Client Version (for checking in CLI and Download)
43-
public static final String VERSION = "3 (2024-01-05)" ; // GitHub Client Version (for checking in CLI and Download)
41+
public static final String VERSION = "3.2 (2024-01-08)" ; // GitHub Client Version (for checking in CLI and Download)
4442

4543
public static final String GIT_HUB_HOST_URL = "https://api.github.com" ;
4644

@@ -67,17 +65,21 @@ public GitHubClient(String propertiesFileAbsolutePath) {
6765
}
6866

6967
private HttpClient buildHttpClient() {
70-
// TODO : load properties
71-
PropertiesManager propertiesManager = new PropertiesManager(propertiesFileAbsolutePath) ;
72-
Properties properties = propertiesManager.load(); // Ret NULL if file not found
73-
if ( properties != null ) {
74-
return new HttpClient(properties);
68+
// PropertiesManager propertiesManager = new PropertiesManager(propertiesFileAbsolutePath) ;
69+
// Properties properties = propertiesManager.load(); // Ret NULL if file not found
70+
// if ( properties != null ) {
71+
// return new HttpClient(properties);
72+
// }
73+
// else {
74+
// // Properties file not found, no properties loaded : use default values
75+
// return new HttpClient();
76+
// }
77+
if ( propertiesFileAbsolutePath != null ) {
78+
return new HttpClient(new File(propertiesFileAbsolutePath));
7579
}
7680
else {
77-
// Properties file not found, no properties loaded : use default values
7881
return new HttpClient();
7982
}
80-
8183
}
8284

8385
private Map<String, String> buildRequestHeaders() {

src/main/java/org/telosys/tools/commons/http/HttpClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
public class HttpClient {
2828

29-
public static final String VERSION = "5 (2024-01-05)" ;
29+
public static final String VERSION = "5.3 (2024-01-08)" ;
3030

3131
// Default "User-Agent" value
3232
private static final String USER_AGENT = "User-Agent" ;
@@ -45,10 +45,10 @@ public HttpClient() {
4545
* Constructor with http configuration defined by properties
4646
* @param properties proxy properties or null if none
4747
*/
48-
public HttpClient(Properties properties) {
48+
public HttpClient(File propertiesFile) {
4949
super();
50-
if ( properties != null ) {
51-
HttpSystemConfig.init(properties);
50+
if ( propertiesFile != null ) {
51+
HttpSystemConfig.init(propertiesFile);
5252
}
5353
else {
5454
HttpSystemConfig.init();

src/main/java/org/telosys/tools/commons/http/HttpSystemConfig.java

Lines changed: 142 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515
*/
1616
package org.telosys.tools.commons.http;
1717

18+
import java.io.File;
19+
import java.net.Proxy;
20+
import java.net.ProxySelector;
21+
import java.net.URI;
1822
import java.util.Arrays;
23+
import java.util.LinkedList;
1924
import java.util.List;
2025
import java.util.Properties;
2126

27+
import org.telosys.tools.commons.PropertiesManager;
28+
2229
public class HttpSystemConfig {
2330

2431
/**
@@ -35,32 +42,38 @@ private HttpSystemConfig() {
3542
private static final String HTTPS_PROTOCOLS = "https.protocols" ;
3643
private static final String TLS_VER_1_2 = "TLSv1.2" ;
3744

38-
private static final void configTLSv2() {
45+
private static final boolean isTLSv12AlreadySet() {
3946
String httpsProtocols = System.getProperty(HTTPS_PROTOCOLS);
40-
if ( httpsProtocols != null && httpsProtocols.contains(TLS_VER_1_2) ) {
41-
// TLS v 1.2 is already set in the system property
42-
return ; // Nothing to do
43-
}
44-
else {
47+
return ( httpsProtocols != null && httpsProtocols.contains(TLS_VER_1_2) );
48+
}
49+
50+
private static final void setTLSv12() {
51+
if ( ! isTLSv12AlreadySet() ) {
4552
System.setProperty(HTTPS_PROTOCOLS, TLS_VER_1_2);
4653
}
4754
}
4855

49-
private static final String JAVA_NET_USE_SYSTEM_PROXIES = "java.net.useSystemProxies" ;
50-
5156
private static final List<String> HTTP_PROP_KEYS = Arrays.asList(
57+
// see https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html
58+
// Proxy config for HTTP
5259
"http.proxySet", "http.proxyHost", "http.proxyPort", "http.proxyUser", "http.proxyPassword", "http.nonProxyHosts",
60+
// Proxy config for HTTPS
5361
"https.proxySet", "https.proxyHost", "https.proxyPort", "https.proxyUser", "https.proxyPassword", "https.nonProxyHosts",
54-
JAVA_NET_USE_SYSTEM_PROXIES,
62+
// Proxy config for SOCKETS (config for all 'sockets', can be overriden by HTTP/HTTPS/FTP configuration)
63+
"socksProxyVersion", "socksProxyHost", "socksProxyPort", "java.net.socks.username", "java.net.socks.password",
64+
// Proxy config for FTP
65+
// FTP is useless for Telosys
66+
// Proxy config based on system properties
67+
"java.net.useSystemProxies", // just for "getProperties" (property check only once at JVM startup)
68+
// TLS version (for Java 7)
5569
HTTPS_PROTOCOLS
5670
);
5771

5872
/**
5973
* Init HTTP System Properties with default values
6074
*/
6175
protected static final void init() {
62-
System.setProperty(JAVA_NET_USE_SYSTEM_PROXIES, "true");
63-
configTLSv2();
76+
setTLSv12();
6477
}
6578

6679
/**
@@ -72,10 +85,36 @@ protected static final void init(Properties properties) {
7285
for (String key : properties.stringPropertyNames()) {
7386
// only known properties are set
7487
if ( HTTP_PROP_KEYS.contains(key) ) {
75-
System.setProperty(key, properties.getProperty(key));
88+
String value = properties.getProperty(key);
89+
if ( value != null ) {
90+
String trimedValue = value.trim();
91+
if ( trimedValue.isEmpty() ) {
92+
System.clearProperty(key); // No value (explicit 'void' in properties file)
93+
}
94+
else {
95+
System.setProperty(key, trimedValue.trim()); // Value to use
96+
}
97+
}
7698
}
7799
}
78-
configTLSv2();
100+
setTLSv12();
101+
}
102+
103+
/**
104+
* Init HTTP System Properties from the given properties file
105+
* @param propertiesFile
106+
*/
107+
public static final void init(File propertiesFile) {
108+
PropertiesManager propertiesManager = new PropertiesManager(propertiesFile) ;
109+
Properties properties = propertiesManager.load(); // Return NULL if file not found
110+
if ( properties != null ) {
111+
// Properties loaded
112+
init(properties);
113+
}
114+
else {
115+
// Properties file not found, no properties loaded : use default values
116+
init();
117+
}
79118
}
80119

81120
/**
@@ -94,4 +133,94 @@ public static final Properties getHttpSystemProperties() {
94133
}
95134
return result;
96135
}
136+
137+
138+
/**
139+
* Returns the current http configuration after reloading the given properties file.
140+
* @param propertiesFile
141+
* @return
142+
*/
143+
public static List<String> getCurrentHttpConfig(File propertiesFile) {
144+
init(propertiesFile);
145+
return getCurrentHttpConfig();
146+
}
147+
148+
/**
149+
* Returns current http configuration (as is at current time)
150+
* @return
151+
*/
152+
public static List<String> getCurrentHttpConfig() {
153+
List<String> lines = new LinkedList<>();
154+
lines.add("Http configuration (system properties) :");
155+
Properties systemProperties = System.getProperties();
156+
for (String k : HTTP_PROP_KEYS) {
157+
String v = systemProperties.getProperty(k);
158+
String v2 = "";
159+
if ( v != null ) {
160+
v2 = "'" + v + "'" ;
161+
}
162+
else {
163+
v2 = "(undefined)";
164+
}
165+
lines.add(" . '" + k + "' = " + v2);
166+
}
167+
return lines;
168+
}
169+
170+
/**
171+
* Get current proxy configuration (depends on current System Properties)
172+
* @param protocol
173+
*/
174+
private static void getCurrentProxyConfig(String protocol, List<String> lines) {
175+
lines.add("Proxies for '" + protocol + "' :");
176+
URI uri;
177+
try {
178+
uri = new URI(protocol + "://foo");
179+
List<Proxy> proxies = ProxySelector.getDefault().select(uri);
180+
if ( proxies != null ) {
181+
for ( Proxy p : proxies ) {
182+
String address = "" ;
183+
if ( p.address() != null ) {
184+
address = "address '" + p.address() + "'";
185+
}
186+
else {
187+
address = "(no address)";
188+
}
189+
// proxy type : enum : DIRECT, HTTP, SOCKS
190+
// "DIRECT" : no proxy
191+
// "SOCKS" : low level proxy type used by default for all protocols => must be overriden for high level protocols
192+
// "HTTP" : proxy type used for high level protocols : "http", "https" and "ftp"
193+
lines.add(" - type '" + p.type() + "' : " + address);
194+
}
195+
}
196+
else {
197+
lines.add(" - no proxy");
198+
}
199+
} catch (Exception e) {
200+
lines.add("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage());
201+
}
202+
}
203+
204+
/**
205+
* Returns the current proxy configuration after reloading the given properties file.
206+
* @param propertiesFile
207+
* @return
208+
*/
209+
public static List<String> getCurrentProxyConfig(File propertiesFile) {
210+
init(propertiesFile);
211+
return getCurrentProxyConfig();
212+
}
213+
214+
/**
215+
* Returns current proxy configuration (as is at current time)
216+
* @return
217+
*/
218+
public static List<String> getCurrentProxyConfig() {
219+
List<String> lines = new LinkedList<>();
220+
getCurrentProxyConfig("http", lines);
221+
getCurrentProxyConfig("https", lines);
222+
getCurrentProxyConfig("socket", lines);
223+
getCurrentProxyConfig("ftp", lines);
224+
return lines;
225+
}
97226
}

src/main/java/org/telosys/tools/commons/http/HttpUtil.java

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,17 @@
1515
*/
1616
package org.telosys.tools.commons.http;
1717

18-
import java.io.PrintStream;
19-
import java.net.InetSocketAddress;
20-
import java.net.Proxy;
21-
import java.net.ProxySelector;
22-
import java.net.URI;
23-
import java.net.URISyntaxException;
24-
import java.security.GeneralSecurityException;
18+
import java.security.KeyManagementException;
19+
import java.security.NoSuchAlgorithmException;
2520
import java.security.cert.X509Certificate;
26-
import java.util.List;
27-
import java.util.Properties;
2821

2922
import javax.net.ssl.HttpsURLConnection;
3023
import javax.net.ssl.SSLContext;
3124
import javax.net.ssl.TrustManager;
3225
import javax.net.ssl.X509TrustManager;
3326

27+
import org.telosys.tools.commons.exception.TelosysRuntimeException;
28+
3429
public class HttpUtil {
3530

3631
/**
@@ -57,55 +52,21 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
5752
return new X509Certificate[0];
5853
}
5954
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
55+
// Empty method (nothing to do)
6056
}
6157
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
58+
// Empty method (nothing to do)
6259
}
6360
}
6461
};
6562
// Install the all-trusting trust manager
66-
try {
67-
SSLContext sc = SSLContext.getInstance("SSL");
68-
sc.init(null, trustAllCerts, new java.security.SecureRandom());
69-
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
70-
} catch (GeneralSecurityException e) {
71-
}
72-
}
73-
74-
//------------------------------------------------------------------------------------------
75-
public static final void showSystemProxies(PrintStream out) {
76-
// Show system proxies on Windows :
77-
// > netsh winhttp show proxy"
78-
// The proxy settings for WinHTTP are not the proxy settings for Microsoft Internet Explorer
79-
80-
out.println("Current system proxies : ");
81-
//System.setProperty("java.net.useSystemProxies", "true");
82-
83-
List<Proxy> list = null;
84-
try {
85-
list = ProxySelector.getDefault().select(new URI("http://foo/bar"));
86-
}
87-
catch (URISyntaxException e) {
88-
e.printStackTrace();
89-
}
90-
if (list != null) {
91-
int n = 0 ;
92-
for ( Proxy proxy : list ) {
93-
n++;
94-
out.println("Proxy #" + n + " : " );
95-
// Type type = proxy.type();
96-
// . DIRECT : Represents a direct connection, or the absence of a proxy.
97-
// . HTTP : Represents proxy for high level protocols such as HTTP or FTP.
98-
// . SOCKS : Represents a SOCKS (V4 or V5) proxy.
99-
out.println(" proxy type (DIRECT|HTTP|SOCKS) : " + proxy.type());
100-
101-
InetSocketAddress addr = (InetSocketAddress) proxy.address();
102-
if (addr == null) {
103-
out.println(" no address (InetSocketAddress)");
104-
} else {
105-
out.println(" proxy host : " + addr.getHostName());
106-
out.println(" proxy port : " + addr.getPort());
107-
}
108-
}
63+
try {
64+
SSLContext sc = SSLContext.getInstance("SSL");
65+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
66+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
67+
} catch (KeyManagementException | NoSuchAlgorithmException e) {
68+
throw new TelosysRuntimeException("Cannot disable SSL certificate validation", e);
10969
}
11070
}
71+
11172
}

0 commit comments

Comments
 (0)