+ else // systemProxy should be selected and is sensible default anyway
+ {
+ if (!previousProxyType.equals(proxyType))
+ Cache.log.info("Setting system proxy settings");
+ Cache.resetProxyProperties();
+ }
+ }
+
+ public static void setProxyProperties(String httpHost, String httpPort,
+ String httpsHost, String httpsPort, String httpUser,
+ char[] httpPassword, String httpsUser, char[] httpsPassword,
+ String nonProxyHosts)
+ {
+ setOrClearSystemProperty("http.proxyHost", httpHost);
+ setOrClearSystemProperty("http.proxyPort", httpPort);
+ setOrClearSystemProperty("https.proxyHost", httpsHost);
+ setOrClearSystemProperty("https.proxyPort", httpsPort);
+ setOrClearSystemProperty("http.proxyUser", httpUser);
+ setOrClearSystemProperty("http.proxyPassword", httpPassword);
+ setOrClearSystemProperty("https.proxyUser", httpsUser);
+ setOrClearSystemProperty("https.proxyPassword", httpsPassword);
+ // are we using a custom proxy (password prompt might be required)?
+ boolean customProxySet = getDefault("USE_PROXY", PROXYTYPE_SYSTEM)
+ .equals(PROXYTYPE_CUSTOM);
+ if (httpUser != null || httpsUser != null)
+ {
+ try
+ {
+ Authenticator.setDefault(new Authenticator()
+ {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication()
+ {
+ Cache.debug(
+ "*** START PasswordAuthentication.getPasswordAuthentication()");
+ Cache.debug("*** getRequestorType()=" + getRequestorType());
+ if (getRequestorType() == RequestorType.PROXY)
+ {
+ String protocol = getRequestingProtocol();
+ boolean needProxyPasswordSet = false;
+ Cache.debug("*** customProxySet = " + customProxySet);
+ Cache.debug("*** protocol = " + protocol);
+ Cache.debug("*** httpUser = " + httpUser);
+ Cache.debug(
+ "*** httpPassword = \"" + (httpPassword == null ? null
+ : new String(httpPassword)) + "\"");
+ Cache.debug("*** httpsUser = " + httpsUser);
+ Cache.debug("*** httpsPassword = \""
+ + (httpsPassword == null ? null
+ : new String(httpsPassword))
+ + "\"");
+ if (customProxySet &&
+ // we have a username but no password for the scheme being
+ // requested
+ (protocol.equalsIgnoreCase("http")
+ && (httpUser != null && httpUser.length() > 0
+ && (httpPassword == null
+ || httpPassword.length == 0)))
+ || (protocol.equalsIgnoreCase("https")
+ && (httpsUser != null
+ && httpsUser.length() > 0
+ && (httpsPassword == null
+ || httpsPassword.length == 0))))
+ {
+ // open Preferences -> Connections
+ String message = MessageManager
+ .getString("label.proxy_password_required");
+ Cache.debug("***+ TRYING TO OPEN PREFERENCES");
+ openPreferencesConnectionsForProxyPassword(message);
+ Cache.debug("***+ AFTER TRYING TO OPEN PREFERENCES");
+ }
+ else
+ {
+ Cache.debug("***+ TRYING TO GET PASSWORDAUTHENTICATION");
+ try
+ {
+ if (protocol.equalsIgnoreCase("http")
+ && getRequestingHost().equalsIgnoreCase(httpHost)
+ && getRequestingPort() == Integer
+ .valueOf(httpPort))
+ {
+ Cache.debug("***+ RETURNING PasswordAuthentication(\""
+ + httpUser + "\", \"" + new String(httpPassword)
+ + "\"");
+ return new PasswordAuthentication(httpUser,
+ httpPassword);
+ }
+ if (protocol.equalsIgnoreCase("https")
+ && getRequestingHost().equalsIgnoreCase(httpsHost)
+ && getRequestingPort() == Integer
+ .valueOf(httpsPort))
+ {
+ Cache.debug("***+ RETURNING PasswordAuthentication(\""
+ + httpsUser + "\", \"" + httpsPassword + "\"");
+ return new PasswordAuthentication(httpsUser,
+ httpsPassword);
+ }
+ } catch (NumberFormatException e)
+ {
+ Cache.error("Problem with proxy port values [http:"
+ + httpPort + ", https:" + httpsPort + "]");
+ }
+ Cache.debug(
+ "***+ AFTER TRYING TO GET PASSWORDAUTHENTICATION");
+ }
+ }
+ // non proxy request
+ Cache.debug("***+ Returning null");
+ return null;
+ }
+ });
+ // required to re-enable basic authentication (should be okay for a
+ // local proxy)
+ Cache.debug(
+ "***+ Setting jdk.http.auth.tunneling.disabledSchemes to ''");
+ System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
+ } catch (SecurityException e)
+ {
+ Cache.error("Could not set default Authenticator");
+ Cache.debug(getStackTraceString(e));
+ }
+ }
+ else
+ {
+ // reset the Authenticator to protect http.proxyUser and
+ // http.proxyPassword Just In Case
+ Cache.debug("***+ Setting default Authenticator to null");
+ Authenticator.setDefault(null);
+ }
+
+ // nonProxyHosts not currently configurable in Preferences
+ Cache.debug("***+ Setting http.nonProxyHosts property to \""
+ + nonProxyHosts + "\"");
+ setOrClearSystemProperty("http.nonProxyHosts", nonProxyHosts);
+ }
+
+ private static void openPreferencesConnectionsForProxyPassword(
+ String message)
+ {
+ //
+ Cache.info("Opening Preferences for proxy password");
+ // Desktop.instance.preferences_actionPerformed(null);
+ Cache.debug("***+########## TRYING TO OPEN PREFERENCES: " + message);
+ Preferences p = new Preferences(GPreferences.CONNECTIONS_TAB, message);
+ p.grabFocus();
+ }
+
+ public static void setOrClearSystemProperty(String key, char[] value)
+ {
+ setOrClearSystemProperty(key,
+ (value == null) ? null : new String(value));
+ }
+
+ public static void setOrClearSystemProperty(String key, String value)
+ {
+ if (key == null)
+ {
+ return;
+ }
+ if (value == null)
+ {
+ System.clearProperty(key);
+ }
+ else
+ {
+ System.setProperty(key, value);
+ }
+ }
+
+ public final static int DEBUG = 10;
+
+ public final static int INFO = 20;
+
+ public final static int WARN = 30;
+
+ public final static int ERROR = 40;
+
+ public static boolean println(int level, String message)
+ {
+ if (Cache.log == null)
+ {
+ if (level >= ERROR)
+ System.err.println(message);
+ else
+ System.out.println(message);
+ return false;
+ }
+ if (level >= WARN)
+ {
+ Cache.log.warn(message);
+ }
+ else if (level >= INFO)
+ {
+ Cache.log.info(message);
+ }
+ else
+ {
+ Cache.log.debug(message);
+ }
+ return true;
+ }
+
+ public static void debug(String message)
+ {
+ println(DEBUG, message);
+ }
+
+ public static void info(String message)
+ {
+ println(INFO, message);
+ }
+
+ public static void warn(String message)
+ {
+ println(WARN, message);
+ }
+
+ public static void error(String message)
+ {
+ println(ERROR, message);