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();
}
}
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();
}
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();
}
}
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;
}
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();
}
}
Aggregations