Skip to content

Commit

Permalink
Fix simple client
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Oct 26, 2015
1 parent 027edd6 commit fe6c8ce
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
116 changes: 68 additions & 48 deletions client/src/test/java/org/asynchttpclient/test/TestUtils.java
@@ -1,32 +1,8 @@
package org.asynchttpclient.test;

import static java.nio.charset.StandardCharsets.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;

import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.security.authentication.DigestAuthenticator;
import org.eclipse.jetty.security.authentication.LoginAuthenticator;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.reactivestreams.Publisher;

import rx.Observable;
import rx.RxReactiveStreams;

import javax.net.ssl.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand All @@ -38,7 +14,9 @@
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.security.*;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
Expand All @@ -50,6 +28,35 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.security.authentication.DigestAuthenticator;
import org.eclipse.jetty.security.authentication.LoginAuthenticator;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.reactivestreams.Publisher;

import rx.Observable;
import rx.RxReactiveStreams;

public class TestUtils {

public static final String USER = "user";
Expand All @@ -69,10 +76,10 @@ public class TestUtils {
try {
TMP_DIR.mkdirs();
TMP_DIR.deleteOnExit();
LARGE_IMAGE_FILE = new File(TestUtils.class.getClassLoader().getResource("300k.png").toURI());
LARGE_IMAGE_FILE = resourceAsFile("300k.png");
LARGE_IMAGE_BYTES = FileUtils.readFileToByteArray(LARGE_IMAGE_FILE);
LARGE_IMAGE_PUBLISHER = createPublisher(LARGE_IMAGE_BYTES, /*chunkSize*/ 1000);
SIMPLE_TEXT_FILE = new File(TestUtils.class.getClassLoader().getResource("SimpleTextFile.txt").toURI());
LARGE_IMAGE_PUBLISHER = createPublisher(LARGE_IMAGE_BYTES, /* chunkSize */1000);
SIMPLE_TEXT_FILE = resourceAsFile("SimpleTextFile.txt");
SIMPLE_TEXT_FILE_STRING = FileUtils.readFileToString(SIMPLE_TEXT_FILE, UTF_8);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
Expand All @@ -85,6 +92,21 @@ public static synchronized int findFreePort() throws IOException {
}
}

private static File resourceAsFile(String path) throws URISyntaxException, IOException {
ClassLoader cl = TestUtils.class.getClassLoader();
URI uri = cl.getResource(path).toURI();
if (uri.isAbsolute() && !uri.isOpaque()) {
return new File(uri);
} else {
File tmpFile = File.createTempFile("tmpfile-", ".data", TMP_DIR);
tmpFile.deleteOnExit();
try (InputStream is = cl.getResourceAsStream(path)) {
FileUtils.copyInputStreamToFile(is, tmpFile);
return tmpFile;
}
}
}

public static File createTempFile(int approxSize) throws IOException {
long repeats = approxSize / TestUtils.PATTERN_BYTES.length + 1;
File tmpFile = File.createTempFile("tmpfile-", ".data", TMP_DIR);
Expand Down Expand Up @@ -115,11 +137,11 @@ public ByteBufferIterable(byte[] payload, int chunkSize) {
this.chunkSize = chunkSize;
}


@Override
public Iterator<ByteBuffer> iterator() {
return new Iterator<ByteBuffer>() {
private int currentIndex = 0;

@Override
public boolean hasNext() {
return currentIndex != payload.length;
Expand Down Expand Up @@ -154,21 +176,19 @@ public static void addHttpConnector(Server server, int port) {
server.addConnector(connector);
}

public static Server newJettyHttpsServer(int port) throws URISyntaxException {
public static Server newJettyHttpsServer(int port) throws IOException, URISyntaxException {
Server server = new Server();
addHttpsConnector(server, port);
return server;
}

public static void addHttpsConnector(Server server, int port) throws URISyntaxException {
ClassLoader cl = TestUtils.class.getClassLoader();
public static void addHttpsConnector(Server server, int port) throws IOException, URISyntaxException {

URL keystoreUrl = cl.getResource("ssltest-keystore.jks");
String keyStoreFile = new File(keystoreUrl.toURI()).getAbsolutePath();
String keyStoreFile = resourceAsFile("ssltest-keystore.jks").getAbsolutePath();
SslContextFactory sslContextFactory = new SslContextFactory(keyStoreFile);
sslContextFactory.setKeyStorePassword("changeit");

String trustStoreFile = new File(cl.getResource("ssltest-cacerts.jks").toURI()).getAbsolutePath();
String trustStoreFile = resourceAsFile("ssltest-cacerts.jks").getAbsolutePath();
sslContextFactory.setTrustStorePath(trustStoreFile);
sslContextFactory.setTrustStorePassword("changeit");

Expand Down Expand Up @@ -220,11 +240,12 @@ private static void addAuthHandler(Server server, String auth, LoginAuthenticato
}

private static KeyManager[] createKeyManagers() throws GeneralSecurityException, IOException {
InputStream keyStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("ssltest-cacerts.jks");
char[] keyStorePassword = "changeit".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(keyStoreStream, keyStorePassword);
assert(ks.size() > 0);
try (InputStream keyStoreStream = TestUtils.class.getClassLoader().getResourceAsStream("ssltest-cacerts.jks")) {
char[] keyStorePassword = "changeit".toCharArray();
ks.load(keyStoreStream, keyStorePassword);
}
assert (ks.size() > 0);

// Set up key manager factory to use our key store
char[] certificatePassword = "changeit".toCharArray();
Expand All @@ -236,11 +257,12 @@ private static KeyManager[] createKeyManagers() throws GeneralSecurityException,
}

private static TrustManager[] createTrustManagers() throws GeneralSecurityException, IOException {
InputStream keyStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("ssltest-keystore.jks");
char[] keyStorePassword = "changeit".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(keyStoreStream, keyStorePassword);
assert(ks.size() > 0);
try (InputStream keyStoreStream = TestUtils.class.getClassLoader().getResourceAsStream("ssltest-keystore.jks")) {
char[] keyStorePassword = "changeit".toCharArray();
ks.load(keyStoreStream, keyStorePassword);
}
assert (ks.size() > 0);

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
Expand Down Expand Up @@ -273,14 +295,12 @@ public DummyTrustManager(final AtomicBoolean trust, final X509TrustManager tm) {
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
tm.checkClientTrusted(chain, authType);
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (!trust.get()) {
throw new CertificateException("Server certificate not trusted.");
}
Expand Down
Expand Up @@ -305,7 +305,7 @@ private Future<Response> execute(RequestBuilder rb, BodyConsumer bodyConsumer, T
handler = new ResumableBodyConsumerAsyncHandler(length, handler);
}

return asyncHttpClient().executeRequest(request, handler);
return getAsyncHttpClient().executeRequest(request, handler);
}

private AsyncHttpClient getAsyncHttpClient() {
Expand Down

0 comments on commit fe6c8ce

Please sign in to comment.