Search in sources :

Example 11 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class ReferencingAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    Authenticator cxfauth = auth.get();
    if (cxfauth == null) {
        remove();
    }
    PasswordAuthentication pauth = null;
    if (wrapped != null) {
        try {
            pauth = tryWith(wrapped);
            if (pauth != null) {
                return pauth;
            }
        } catch (Exception e) {
            pauth = null;
        }
    }
    if (cxfauth != null) {
        try {
            pauth = tryWith(cxfauth);
        } catch (Exception e1) {
            pauth = null;
        }
    }
    return pauth;
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 12 with Authenticator

use of java.net.Authenticator in project apjp by jvansteirteghem.

the class Main method onCreate.

public void onCreate() {
    try {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        APJP.APJP_KEY = sharedPreferences.getString("APJP_KEY", "");
        APJP.APJP_LOGGER_ID = "APJP";
        APJP.APJP_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_PROXY_SERVER";
        APJP.APJP_LOCAL_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTP_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTP_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTP_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTP_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_HTTP_PROXY_SERVER";
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTP_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTP_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTP_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTP_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTP_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTP_SERVER_LOGGER_ID = "APJP_LOCAL_HTTP_SERVER";
        APJP.APJP_LOCAL_HTTP_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL = new String[10];
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY = new String[10][5];
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE = new String[10][5];
        for (int i = 0; i < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY.length; i = i + 1) {
            APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL[i] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_URL", "");
            for (int j = 0; j < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
                APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_KEY", "");
                APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_VALUE", "");
            }
        }
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTPS_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTPS_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_HTTPS_PROXY_SERVER";
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTPS_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTPS_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTPS_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTPS_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTPS_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_ID = "APJP_LOCAL_HTTPS_SERVER";
        APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL = new String[10];
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY = new String[10][5];
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE = new String[10][5];
        for (int i = 0; i < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY.length; i = i + 1) {
            APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_URL", "");
            for (int j = 0; j < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
                APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_KEY", "");
                APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_VALUE", "");
            }
        }
        APJP.APJP_HTTP_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_HTTP_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_HTTP_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_HTTP_PROXY_SERVER_USERNAME = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_USERNAME", "");
        APJP.APJP_HTTP_PROXY_SERVER_PASSWORD = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_PASSWORD", "");
        APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_HTTPS_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_HTTPS_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_HTTPS_PROXY_SERVER_USERNAME = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_USERNAME", "");
        APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_PASSWORD", "");
        APJP.APJP_APPLICATION = getApplication();
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication passwordAuthentication = null;
                if (this.getRequestorType() == Authenticator.RequestorType.PROXY) {
                    if (this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTP") == true) {
                        passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTP_PROXY_SERVER_USERNAME, APJP.APJP_HTTP_PROXY_SERVER_PASSWORD.toCharArray());
                    } else {
                        if (this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTPS") == true) {
                            passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTPS_PROXY_SERVER_USERNAME, APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD.toCharArray());
                        }
                    }
                }
                return passwordAuthentication;
            }
        });
        logger = Logger.getLogger(APJP.APJP_LOGGER_ID);
        proxyServer = new ProxyServer();
    } catch (Exception e) {
        logger.log(1, "EXCEPTION", e);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 13 with Authenticator

use of java.net.Authenticator in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method configure_http_proxy_credentials.

@Test
public void configure_http_proxy_credentials() {
    DefaultHttpDownloader.AuthenticatorFacade system = mock(DefaultHttpDownloader.AuthenticatorFacade.class);
    Settings settings = new MapSettings();
    settings.setProperty("https.proxyHost", "1.2.3.4");
    settings.setProperty("http.proxyUser", "the_login");
    settings.setProperty("http.proxyPassword", "the_passwd");
    new DefaultHttpDownloader.BaseHttpDownloader(system, settings, null);
    verify(system).setDefaultAuthenticator(argThat(new TypeSafeMatcher<Authenticator>() {

        @Override
        protected boolean matchesSafely(Authenticator authenticator) {
            DefaultHttpDownloader.ProxyAuthenticator a = (DefaultHttpDownloader.ProxyAuthenticator) authenticator;
            PasswordAuthentication authentication = a.getPasswordAuthentication();
            return authentication.getUserName().equals("the_login") && new String(authentication.getPassword()).equals("the_passwd");
        }

        @Override
        public void describeTo(Description description) {
        }
    }));
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) MapSettings(org.sonar.api.config.MapSettings) Settings(org.sonar.api.config.Settings) MapSettings(org.sonar.api.config.MapSettings) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 14 with Authenticator

use of java.net.Authenticator in project twitter4j by yusuke.

the class AlternativeHttpClientImpl method prepareOkHttpClient.

private void prepareOkHttpClient() {
    if (okHttpClient == null) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //set protocols
        List<Protocol> protocols = new ArrayList<Protocol>();
        protocols.add(Protocol.HTTP_1_1);
        if (sPreferHttp2)
            protocols.add(Protocol.HTTP_2);
        if (sPreferSpdy)
            protocols.add(Protocol.SPDY_3);
        builder.protocols(protocols);
        //connectionPool setup
        builder.connectionPool(new ConnectionPool(MAX_CONNECTIONS, KEEP_ALIVE_DURATION_MS, TimeUnit.MILLISECONDS));
        //redirect disable
        builder.followSslRedirects(false);
        //for proxy
        if (isProxyConfigured()) {
            if (CONF.getHttpProxyUser() != null && !CONF.getHttpProxyUser().equals("")) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Proxy AuthUser: " + CONF.getHttpProxyUser());
                    logger.debug("Proxy AuthPassword: " + CONF.getHttpProxyPassword().replaceAll(".", "*"));
                }
                Authenticator.setDefault(new Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType().equals(RequestorType.PROXY)) {
                            return new PasswordAuthentication(CONF.getHttpProxyUser(), CONF.getHttpProxyPassword().toCharArray());
                        } else {
                            return null;
                        }
                    }
                });
            }
            final Proxy proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(CONF.getHttpProxyHost(), CONF.getHttpProxyPort()));
            if (logger.isDebugEnabled()) {
                logger.debug("Opening proxied connection(" + CONF.getHttpProxyHost() + ":" + CONF.getHttpProxyPort() + ")");
            }
            builder.proxy(proxy);
        }
        //connection timeout
        if (CONF.getHttpConnectionTimeout() > 0) {
            builder.connectTimeout(CONF.getHttpConnectionTimeout(), TimeUnit.MILLISECONDS);
        }
        //read timeout
        if (CONF.getHttpReadTimeout() > 0) {
            builder.readTimeout(CONF.getHttpReadTimeout(), TimeUnit.MILLISECONDS);
        }
        okHttpClient = builder.build();
    }
}
Also used : ArrayList(java.util.ArrayList) Authenticator(java.net.Authenticator)

Example 15 with Authenticator

use of java.net.Authenticator in project jdk8u_jdk by JetBrains.

the class HttpsProxyStackOverflow method startServer.

static BadAuthProxyServer startServer() throws IOException {
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xyz", "xyz".toCharArray());
        }
    });
    BadAuthProxyServer server = new BadAuthProxyServer(new ServerSocket(0));
    Thread serverThread = new Thread(server);
    serverThread.start();
    return server;
}
Also used : ServerSocket(java.net.ServerSocket) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Authenticator (java.net.Authenticator)24 PasswordAuthentication (java.net.PasswordAuthentication)18 URL (java.net.URL)6 InetSocketAddress (java.net.InetSocketAddress)5 Proxy (java.net.Proxy)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 Field (java.lang.reflect.Field)3 ServerSocket (java.net.ServerSocket)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Test (org.junit.Test)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Method (java.lang.reflect.Method)2 InetAddress (java.net.InetAddress)2 URI (java.net.URI)2 List (java.util.List)2 Properties (java.util.Properties)2 SSLServerSocket (javax.net.ssl.SSLServerSocket)2 SSLSocket (javax.net.ssl.SSLSocket)2