Example usage for java.net Authenticator setDefault

List of usage examples for java.net Authenticator setDefault

Introduction

In this page you can find the example usage for java.net Authenticator setDefault.

Prototype

public static synchronized void setDefault(Authenticator a) 

Source Link

Document

Sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authentication.

Usage

From source file:org.regenstrief.util.Util.java

/**
 * Initializes HTTP authentication and SSL
 **//*from   w  w w .j  a  v a 2s .  com*/
public final synchronized static void initializeSecurity() {
    if (myAuthenticator == null) {
        RegenPropertyResolver.loadProperties(); // Copy from our properties file to System properties (including SSL properties)

        myAuthenticator = new MyAuthenticator(); // Enable HTTP authentication
        Authenticator.setDefault(myAuthenticator);

        //System.setProperty(PROP_HANDLER, HANDLER_SUN_SSL); // This can't be mixed with setHostnameVerifier
        //System.setProperty("javax.net.debug", "ssl:handshake"); // This can be put in our properties file

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    }
}

From source file:fr.cls.atoll.motu.library.misc.intfce.Organizer.java

/**
 * Gets the url connection opts./*from w  ww  .  ja v a 2  s.co  m*/
 * 
 * @param scheme the scheme
 * @param host the host
 * @return the url connection opts
 * @throws MotuException the motu exception
 */
public static Proxy getUrlConnectionProxy(String scheme, String host) throws MotuException {

    if (LOG.isDebugEnabled()) {
        LOG.debug("getUrlConnectionProxy(String, String) - start - scheme=" + scheme + ", host=" + host);
    }

    Proxy proxy = null;
    String proxyHost = null;
    String proxyPort = null;
    String proxyLogin = null;
    String proxyPwd = null;

    Authenticator.setDefault(null);

    if (Organizer.isNullOrEmpty(scheme)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getUrlConnectionProxy(String, String) - end - scheme is null or empty");
        }
        return proxy;
    }

    MotuConfigFileSystemWrapper<Boolean> wrapperBoolean = new MotuConfigFileSystemWrapper<Boolean>();
    MotuConfigFileSystemWrapper<Period> wrapperPeriod = new MotuConfigFileSystemWrapper<Period>();
    MotuConfigFileSystemWrapper<String> wrapperString = new MotuConfigFileSystemWrapper<String>();

    if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
        Boolean isUseProxy = wrapperBoolean.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_USEHTTPPROXY);
        if ((isUseProxy != null) && (isUseProxy)) {
            proxyHost = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYHOST);
            proxyPort = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYPORT);
            proxyLogin = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYLOGIN);
            proxyPwd = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_HTTPPROXYPWD);
        }
    } else if (scheme.equalsIgnoreCase("ftp")) {
        Boolean isUseProxy = wrapperBoolean.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_USEFTPPROXY);
        if ((isUseProxy != null) && (isUseProxy)) {
            proxyHost = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYHOST);
            proxyPort = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYPORT);
            proxyLogin = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYLOGIN);
            proxyPwd = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_FTPPROXYPWD);
        }
    } else if (scheme.equalsIgnoreCase("sftp")) {
        Boolean isUseProxy = wrapperBoolean.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_USESFTPPROXY);
        if ((isUseProxy != null) && (isUseProxy)) {
            proxyHost = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYHOST);
            proxyPort = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYPORT);
            proxyLogin = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYLOGIN);
            proxyPwd = wrapperString.getFieldValue(host, MotuConfigFileSystemWrapper.PROP_SFTPPROXYPWD);
        }

    }

    if ((proxyHost != null) && (proxyPort != null)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getUrlConnectionProxy(String, String) - Create Proxy -  proxyHost=" + proxyHost
                    + ", proxyPort=" + proxyPort);
        }
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));

        if ((proxyLogin != null) && (proxyPwd != null)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("getUrlConnectionProxy(String, String) - set authentication - proxyLogin="
                        + proxyLogin + ", proxyPwd=" + proxyPwd);
            }
            Authenticator.setDefault(new SimpleAuthenticator(proxyLogin, proxyPwd));
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("getUrlConnectionProxy(String, String) - end");
    }
    return proxy;
}