JAL-3633 Added Proxy Authentication credentials in Preferences and set via java.net...
[jalview.git] / src / jalview / gui / Preferences.java
index c4dcc6c..827c9a0 100755 (executable)
@@ -127,6 +127,8 @@ public class Preferences extends GPreferences
 
   private static final int MAX_FONT_SIZE = 30;
 
+  private String previousProxyType;
+
   /**
    * Holds name and link separated with | character. Sequence ID must be
    * $SEQUENCE_ID$ or $SEQUENCE_ID=/.possible | chars ./=$
@@ -450,7 +452,7 @@ public class Preferences extends GPreferences
     doReset.addActionListener(onReset);
 
     // filter to display only custom urls
-    final RowFilter<TableModel, Object> customUrlFilter = new RowFilter<>()
+    final RowFilter<TableModel, Object> customUrlFilter = new RowFilter<TableModel, Object>()
     {
       @Override
       public boolean include(
@@ -536,10 +538,34 @@ public class Preferences extends GPreferences
       }
     }
 
-    useProxy.setSelected(Cache.getDefault("USE_PROXY", false));
-    useProxy_actionPerformed(); // make sure useProxy is correctly initialised
-    proxyServerTB.setText(Cache.getDefault("PROXY_SERVER", ""));
-    proxyPortTB.setText(Cache.getDefault("PROXY_PORT", ""));
+    String proxyTypeString = Cache.getDefault("USE_PROXY", "false");
+    previousProxyType = proxyTypeString;
+    switch (proxyTypeString)
+    {
+    case Cache.PROXYTYPE_NONE:
+      proxyType.setSelected(noProxy.getModel(), true);
+      break;
+    case Cache.PROXYTYPE_SYSTEM:
+      proxyType.setSelected(systemProxy.getModel(), true);
+      break;
+    case Cache.PROXYTYPE_CUSTOM:
+      proxyType.setSelected(customProxy.getModel(), true);
+      break;
+    default:
+      String message = "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
+              + proxyTypeString;
+      Cache.log.warn(message);
+    }
+    proxyServerHttpTB.setText(Cache.getDefault("PROXY_SERVER", ""));
+    proxyPortHttpTB.setText(Cache.getDefault("PROXY_PORT", ""));
+    proxyServerHttpsTB.setText(Cache.getDefault("PROXY_SERVER_HTTPS", ""));
+    proxyPortHttpsTB.setText(Cache.getDefault("PROXY_PORT_HTTPS", ""));
+    proxyAuth.setSelected(Cache.getDefault("PROXY_AUTH", false));
+    proxyAuthUsernameTB
+            .setText(Cache.getDefault("PROXY_AUTH_USERNAME", ""));
+    proxyAuthPasswordTB
+            .setText(Cache.getDefault("PROXY_AUTH_PASSWORD", ""));
+    setCustomProxyEnabled();
 
     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
 
@@ -811,23 +837,21 @@ public class Preferences extends GPreferences
     Cache.applicationProperties.setProperty("DEFAULT_URL",
             sequenceUrlLinks.getPrimaryUrlId());
 
+    // Proxy settings
     Cache.applicationProperties.setProperty("USE_PROXY",
-            Boolean.toString(useProxy.isSelected()));
-
-    Cache.setOrRemove("PROXY_SERVER", proxyServerTB.getText());
-
-    Cache.setOrRemove("PROXY_PORT", proxyPortTB.getText());
+            customProxy.isSelected() ? Cache.PROXYTYPE_CUSTOM
+                    : noProxy.isSelected() ? Cache.PROXYTYPE_NONE
+                            : Cache.PROXYTYPE_SYSTEM);
+    Cache.setOrRemove("PROXY_SERVER", proxyServerHttpTB.getText());
+    Cache.setOrRemove("PROXY_PORT", proxyPortHttpTB.getText());
+    Cache.setOrRemove("PROXY_SERVER_HTTPS", proxyServerHttpsTB.getText());
+    Cache.setOrRemove("PROXY_PORT_HTTPS", proxyPortHttpsTB.getText());
+    Cache.setOrRemove("PROXY_AUTH",
+            Boolean.toString(proxyAuth.isSelected()));
+    Cache.setOrRemove("PROXY_AUTH_USERNAME", proxyAuthUsernameTB.getText());
+    Cache.setOrRemove("PROXY_AUTH_PASSWORD", proxyAuthPasswordTB.getText());
+    setProxyFromSettings();
 
-    if (useProxy.isSelected())
-    {
-      System.setProperty("http.proxyHost", proxyServerTB.getText());
-      System.setProperty("http.proxyPort", proxyPortTB.getText());
-    }
-    else
-    {
-      System.setProperty("http.proxyHost", "");
-      System.setProperty("http.proxyPort", "");
-    }
     Cache.setProperty("VERSION_CHECK",
             Boolean.toString(versioncheck.isSelected()));
     if (Cache.getProperty("USAGESTATS") != null || usagestats.isSelected())
@@ -924,6 +948,39 @@ public class Preferences extends GPreferences
     }
   }
 
+  public void setProxyFromSettings()
+  {
+    String proxyType = Cache.getDefault("USE_PROXY",
+            Cache.PROXYTYPE_SYSTEM);
+    if (proxyType.equals(Cache.PROXYTYPE_NONE))
+    {
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting no proxy settings");
+      Cache.setProxyProperties(null, null, null, null, null, null, null,
+              null);
+    }
+    else if (proxyType.equals(Cache.PROXYTYPE_CUSTOM))
+    {
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting custom proxy settings");
+      boolean proxyAuthSet = Cache.getDefault("PROXY_AUTH", false);
+      Cache.setProxyProperties(Cache.getDefault("PROXY_SERVER", null),
+              Cache.getDefault("PROXY_PORT", null),
+              Cache.getDefault("PROXY_SERVER_HTTPS", null),
+              Cache.getDefault("PROXY_PORT_HTTPS", null),
+              proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+                      : null,
+              proxyAuthSet ? Cache.getDefault("PROXY_AUTH_PASSWORD", "")
+                      : null);
+    }
+    else // systemProxy should be selected and is sensible default anyway
+    {
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting system proxy settings");
+      Cache.resetProxyProperties();
+    }
+  }
+
   /**
    * Do any necessary validation before saving settings. Return focus to the
    * first tab which fails validation.