Search in sources :

Example 71 with Authenticator

use of java.net.Authenticator in project goodies by sonatype.

the class JettyProxyProviderTest method testAuthAfterProxyAuthGetFail401.

@Test
public void testAuthAfterProxyAuthGetFail401() throws Exception {
    JettyProxyProvider proxy = new JettyProxyProvider("u", "p");
    proxy.addBehaviour("/*", new Debug(), new Content());
    proxy.addAuthentication("/*", "BASIC");
    proxy.addUser("user", "password");
    proxy.start();
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestingHost().equals("localhost")) {
                String password = "p";
                return new PasswordAuthentication("u", password.toCharArray());
            }
            return super.getPasswordAuthentication();
        }
    });
    URL url = new URL("http://speutel.invalid/foo");
    SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort());
    Proxy p = new Proxy(Type.HTTP, sa);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(p);
    try {
        conn.setDoInput(true);
        conn.connect();
        assertEquals(401, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 72 with Authenticator

use of java.net.Authenticator in project goodies by sonatype.

the class JettyProxyProviderTest method testProxyAuthGet.

@Test
public void testProxyAuthGet() throws Exception {
    JettyProxyProvider proxy = new JettyProxyProvider("u", "p");
    proxy.addBehaviour("/*", new Debug(), new Content());
    proxy.start();
    URL url = new URL("http://speutel.invalid/foo");
    SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort());
    Proxy p = new Proxy(Type.HTTP, sa);
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestingHost().equals("localhost")) {
                String password = "p";
                return new PasswordAuthentication("u", password.toCharArray());
            }
            return super.getPasswordAuthentication();
        }
    });
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(p);
    conn.setDoInput(true);
    conn.connect();
    assertEquals(200, conn.getResponseCode());
    assertEquals("foo", read(conn.getContent()).trim());
    conn.disconnect();
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) URL(java.net.URL) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 73 with Authenticator

use of java.net.Authenticator in project goodies by sonatype.

the class JettyProxyProviderTest method testAuthAfterProxyAuthGet.

@Test
public void testAuthAfterProxyAuthGet() throws Exception {
    JettyProxyProvider proxy = new JettyProxyProvider("u", "p");
    proxy.addBehaviour("/*", new Debug(), new Content());
    proxy.addAuthentication("/*", "BASIC");
    proxy.addUser("user", "password");
    proxy.start();
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            if (getRequestingHost().equals("localhost")) {
                String password = "p";
                return new PasswordAuthentication("u", password.toCharArray());
            } else {
                String password = "password";
                return new PasswordAuthentication("user", password.toCharArray());
            }
        }
    });
    URL url = new URL("http://speutel.invalid/foo");
    SocketAddress sa = new InetSocketAddress("localhost", proxy.getPort());
    Proxy p = new Proxy(Type.HTTP, sa);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(p);
    try {
        conn.setDoInput(true);
        conn.connect();
        assertEquals(200, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Content(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Debug(org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 74 with Authenticator

use of java.net.Authenticator in project stripe-java by stripe.

the class LiveStripeResponseGetter method createStripeConnection.

private static java.net.HttpURLConnection createStripeConnection(String url, RequestOptions options) throws IOException {
    URL stripeURL;
    String customURLStreamHandlerClassName = System.getProperty(CUSTOM_URL_STREAM_HANDLER_PROPERTY_NAME, null);
    if (customURLStreamHandlerClassName != null) {
        // instantiate the custom handler provided
        try {
            Class<URLStreamHandler> clazz = (Class<URLStreamHandler>) Class.forName(customURLStreamHandlerClassName);
            Constructor<URLStreamHandler> constructor = clazz.getConstructor();
            URLStreamHandler customHandler = constructor.newInstance();
            stripeURL = new URL(null, url, customHandler);
        } catch (ClassNotFoundException e) {
            throw new IOException(e);
        } catch (SecurityException e) {
            throw new IOException(e);
        } catch (NoSuchMethodException e) {
            throw new IOException(e);
        } catch (IllegalArgumentException e) {
            throw new IOException(e);
        } catch (InstantiationException e) {
            throw new IOException(e);
        } catch (IllegalAccessException e) {
            throw new IOException(e);
        } catch (InvocationTargetException e) {
            throw new IOException(e);
        }
    } else {
        stripeURL = new URL(url);
    }
    HttpURLConnection conn;
    if (Stripe.getConnectionProxy() != null) {
        conn = (HttpURLConnection) stripeURL.openConnection(Stripe.getConnectionProxy());
        Authenticator.setDefault(new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return Stripe.getProxyCredential();
            }
        });
    } else {
        conn = (HttpURLConnection) stripeURL.openConnection();
    }
    conn.setConnectTimeout(options.getConnectTimeout());
    conn.setReadTimeout(options.getReadTimeout());
    conn.setUseCaches(false);
    for (Map.Entry<String, String> header : getHeaders(options).entrySet()) {
        conn.setRequestProperty(header.getKey(), header.getValue());
    }
    if (conn instanceof HttpsURLConnection) {
        ((HttpsURLConnection) conn).setSSLSocketFactory(socketFactory);
    }
    return conn;
}
Also used : IOException(java.io.IOException) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) URLStreamHandler(java.net.URLStreamHandler) HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) Map(java.util.Map) Authenticator(java.net.Authenticator) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 75 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);
        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)

Aggregations

Authenticator (java.net.Authenticator)80 PasswordAuthentication (java.net.PasswordAuthentication)50 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)9 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)3 JCommander (com.beust.jcommander.JCommander)2