1515 */
1616package 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 ;
1822import java .util .Arrays ;
23+ import java .util .LinkedList ;
1924import java .util .List ;
2025import java .util .Properties ;
2126
27+ import org .telosys .tools .commons .PropertiesManager ;
28+
2229public 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}
0 commit comments