001: package net.sourceforge.squirrel_sql.fw.util;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.net.Authenticator;
022: import java.net.PasswordAuthentication;
023: import java.util.Properties;
024:
025: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
026: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
027:
028: /**
029: * This class will apply the settings from a <TT>ProxySettings</TT>
030: * object to a <TT>Properties</TT> object.
031: *
032: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
033: */
034: public class ProxyHandler {
035: /** Logger for this class. */
036: private final static ILogger s_log = LoggerController
037: .createLogger(ProxyHandler.class);
038:
039: public ProxyHandler() {
040: super ();
041: }
042:
043: public void apply(ProxySettings proxy) {
044: apply(proxy, System.getProperties());
045: }
046:
047: public void apply(ProxySettings proxy, Properties props) {
048: if (proxy == null) {
049: throw new IllegalArgumentException("ProxySettings == null");
050: }
051:
052: final boolean http = proxy.getHttpUseProxy();
053: if (http) {
054: applySetting(props, "proxySet", "true");
055: applySetting(props, "http.proxyHost", proxy
056: .getHttpProxyServer());
057: applySetting(props, "http.proxyPort", proxy
058: .getHttpProxyPort());
059: applySetting(props, "http.nonProxyHosts", proxy
060: .getHttpNonProxyHosts());
061: final String user = proxy.getHttpProxyUser();
062: String password = proxy.getHttpProxyPassword();
063: if (password == null) {
064: password = "";
065: }
066: if (user != null && user.length() > 0) {
067: s_log.debug("Using HTTP proxy with security");
068: Authenticator.setDefault(new MyAuthenticator(user,
069: password));
070: } else {
071: s_log.debug("Using HTTP proxy without security");
072: Authenticator.setDefault(null);
073: }
074: } else {
075: s_log.debug("Not using HTTP proxy");
076: props.remove("proxySet");
077: props.remove("http.proxyHost");
078: props.remove("http.proxyPort");
079: props.remove("http.nonProxyHosts");
080: Authenticator.setDefault(null);
081: }
082:
083: final boolean socks = proxy.getSocksUseProxy();
084: if (socks) {
085: applySetting(props, "socksProxyHost", proxy
086: .getSocksProxyServer());
087: applySetting(props, "socksProxyPort", proxy
088: .getSocksProxyPort());
089: } else {
090: props.remove("socksProxyHost");
091: props.remove("socksProxyPort");
092: }
093: }
094:
095: private void applySetting(Properties props, String key, String value) {
096: if (value != null && value.length() > 0) {
097: props.put(key, value);
098: } else {
099: props.remove(key);
100: }
101: }
102:
103: private final static class MyAuthenticator extends Authenticator {
104: private final PasswordAuthentication _password;
105:
106: public MyAuthenticator(String user, String password) {
107: super ();
108: _password = new PasswordAuthentication(user, password
109: .toCharArray());
110:
111: }
112:
113: protected PasswordAuthentication getPasswordAuthentication() {
114: return _password;
115: }
116: }
117: }
|