This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new f5a071fbbd Verify tool proxy setting before adjusting or asking.
     new f2d7d00e3a Merge pull request #6764 from 
sdedic/sdedic/network-proxy-probe
f5a071fbbd is described below

commit f5a071fbbd2cd274eb247e8780b89092cbe6bf08
Author: Svata Dedic <[email protected]>
AuthorDate: Wed Nov 29 17:45:26 2023 +0100

    Verify tool proxy setting before adjusting or asking.
---
 .../gradle/execute/GradleNetworkProxySupport.java  | 51 ++++++++++++++++++++++
 .../modules/maven/execute/MavenProxySupport.java   | 51 ++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git 
a/extide/gradle/src/org/netbeans/modules/gradle/execute/GradleNetworkProxySupport.java
 
b/extide/gradle/src/org/netbeans/modules/gradle/execute/GradleNetworkProxySupport.java
index e1acb94718..9113b61d50 100644
--- 
a/extide/gradle/src/org/netbeans/modules/gradle/execute/GradleNetworkProxySupport.java
+++ 
b/extide/gradle/src/org/netbeans/modules/gradle/execute/GradleNetworkProxySupport.java
@@ -22,12 +22,15 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
 import java.net.Proxy;
 import java.net.ProxySelector;
 import java.net.SocketAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URL;
 import java.text.DateFormat;
 import java.util.Date;
 import java.util.HashMap;
@@ -93,6 +96,12 @@ public class GradleNetworkProxySupport {
     private static final int PORT_DEFAULT_HTTPS = 1080;
     private static final int PORT_DEFAULT_HTTP = 80;
     
+    /**
+     * Timeout for the network probe. The probe is done in case project 
settings mismatch with the autodetected ones.
+     * If set to 0 or negative number, the project proxy configuration will 
not be probed.
+     */
+    private static final int PROXY_PROBE_TIMEOUT = 
Integer.getInteger("netbeans.networkProxy.timeout", 1000);
+    
     private final Project project;
     
     /**
@@ -295,6 +304,48 @@ public class GradleNetworkProxySupport {
                     action = NetworkProxySettings.NOTICE;
                 }
             }
+            if (action != NetworkProxySettings.IGNORE && PROXY_PROBE_TIMEOUT > 
0) {
+                // last check: make an outbound connection to a public site
+                URL probeUrl;
+                P: try {
+                    Proxy probeProxy;
+                    
+                    if (proxyHost != null) {
+                        LOG.log(Level.FINE, "Trying to probe with proxy {0}", 
proxyAuthority);
+                        InetSocketAddress sa = new 
InetSocketAddress(proxyHost, proxyPort);
+                        if (!sa.isUnresolved()) {
+                            probeProxy = new Proxy(Proxy.Type.HTTP, sa);
+                        } else {
+                            LOG.log(Level.FINE, "Tool proxy {0} probe not 
resolvable", proxyAuthority);
+                            break P;
+                        }
+                    } else {
+                        probeProxy = Proxy.NO_PROXY;
+                    }
+                    probeUrl = new URL(PROBE_URI_STRING);
+                    HttpURLConnection c = null;
+                    try {
+                        c = 
(HttpURLConnection)probeUrl.openConnection(probeProxy);
+                        c.setReadTimeout(PROXY_PROBE_TIMEOUT);
+                        c.setConnectTimeout(PROXY_PROBE_TIMEOUT);
+                        c.setRequestMethod("HEAD");
+                        c.connect();
+                        // force something through
+                        c.getLastModified();
+                        return CompletableFuture.completedFuture(new 
ProxyResult(Status.CONTINUE, probeProxy, proxyAuthority, publicProxySpec, 
publicProxyHost, publicProxyPort));
+                    } catch (IOException ex) {
+                        // the probe has failed
+                        LOG.log(Level.FINE, "Tool proxy {0} probe failed", 
proxyAuthority);
+                    } finally {
+                        if (c != null) {
+                            c.disconnect();
+                        }
+                    }
+                } catch (MalformedURLException ex) {
+                    // this is competely unexpected
+                    Exceptions.printStackTrace(ex);
+                }
+            }
             switch (action) {
                 case IGNORE:
                     return 
CompletableFuture.completedFuture(createResult(Status.CONTINUE));
diff --git 
a/java/maven/src/org/netbeans/modules/maven/execute/MavenProxySupport.java 
b/java/maven/src/org/netbeans/modules/maven/execute/MavenProxySupport.java
index baf3b024e6..c3947d2102 100644
--- a/java/maven/src/org/netbeans/modules/maven/execute/MavenProxySupport.java
+++ b/java/maven/src/org/netbeans/modules/maven/execute/MavenProxySupport.java
@@ -25,12 +25,15 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.lang.reflect.Field;
+import java.net.HttpURLConnection;
 import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
 import java.net.Proxy;
 import java.net.ProxySelector;
 import java.net.SocketAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
@@ -165,6 +168,12 @@ public class MavenProxySupport {
     private static final int PORT_DEFAULT_HTTPS = 1080;
     private static final int PORT_DEFAULT_HTTP = 80;
     
+    /**
+     * Timeout for the network probe. The probe is done in case project 
settings mismatch with the autodetected ones.
+     * If set to 0 or negative number, the project proxy configuration will 
not be probed.
+     */
+    private static final int PROXY_PROBE_TIMEOUT = 
Integer.getInteger("netbeans.networkProxy.timeout", 1000);
+
     /**
      * Past decisions made by the user during this session. The Map is used so 
the user si not bothered that often with questions.
      * If the user chooses 'override' or 'continue' (no action), the Map 
receives the public proxy spec and the result. If the same
@@ -689,6 +698,48 @@ public class MavenProxySupport {
                 }
             }
 
+            if (action != NetworkProxySettings.IGNORE && PROXY_PROBE_TIMEOUT > 
0) {
+                // last check: make an outbound connection to a public site
+                URL probeUrl;
+                P: try {
+                    Proxy probeProxy;
+                    
+                    if (proxyHost != null) {
+                        LOG.log(Level.FINE, "Trying to probe with proxy {0}", 
proxyAuthority);
+                        InetSocketAddress sa = new 
InetSocketAddress(proxyHost, proxyPort);
+                        if (!sa.isUnresolved()) {
+                            probeProxy = new Proxy(Proxy.Type.HTTP, sa);
+                        } else {
+                            LOG.log(Level.FINE, "Tool proxy {0} probe not 
resolvable", proxyAuthority);
+                            break P;
+                        }
+                    } else {
+                        probeProxy = Proxy.NO_PROXY;
+                    }
+                    probeUrl = new URL(PROBE_URI_STRING);
+                    HttpURLConnection c = null;
+                    try {
+                        c = 
(HttpURLConnection)probeUrl.openConnection(probeProxy);
+                        c.setReadTimeout(PROXY_PROBE_TIMEOUT);
+                        c.setConnectTimeout(PROXY_PROBE_TIMEOUT);
+                        c.setRequestMethod("HEAD");
+                        c.connect();
+                        // force something through
+                        c.getLastModified();
+                        return CompletableFuture.completedFuture(new 
ProxyResult(Status.CONTINUE, probeProxy, proxyAuthority, publicProxySpec, 
publicProxyHost, publicProxyPort, publicProxyNonDefaultPort > 0, 
mavenSettings));
+                    } catch (IOException ex) {
+                        // the probe has failed
+                        LOG.log(Level.FINE, "Tool proxy {0} probe failed", 
proxyAuthority);
+                    } finally {
+                        if (c != null) {
+                            c.disconnect();
+                        }
+                    }
+                } catch (MalformedURLException ex) {
+                    // this is competely unexpected
+                    Exceptions.printStackTrace(ex);
+                }
+            }
             switch (action) {
                 case IGNORE:
                     return 
CompletableFuture.completedFuture(createResult(Status.CONTINUE));


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to