Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
#2 Reworked ProxyClient to use OkHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
gkunze committed Dec 8, 2020
1 parent d20ec84 commit 8b46658
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
14 changes: 6 additions & 8 deletions src/main/java/xltutil/proxy/ProxyHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@

import java.net.URL;

import org.apache.http.client.HttpClient;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpClient.Builder;
import org.openqa.selenium.remote.internal.OkHttpClient;

public class ProxyHttpClient implements org.openqa.selenium.remote.http.HttpClient.Factory
{
private final HttpClient httpClient;
private final OkHttpClient okHttpClient;

public ProxyHttpClient(HttpClient httpClient)
public ProxyHttpClient(OkHttpClient okHttpClient)
{
this.httpClient = httpClient;
this.okHttpClient = okHttpClient;
}

@Override
public org.openqa.selenium.remote.http.HttpClient createClient(URL url)
{
return new OkHttpClient((okhttp3.OkHttpClient) httpClient, url);
return (HttpClient) okHttpClient;
}

@Override
public Builder builder()
{
// TODO Auto-generated method stub
return null;
throw new UnsupportedOperationException();
}

@Override
public void cleanupIdleClients()
{
// TODO Auto-generated method stub

}
}
38 changes: 30 additions & 8 deletions src/main/java/xltutil/runner/helper/AnnotationRunnerHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xltutil.runner.helper;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand All @@ -10,15 +12,13 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.UnsupportedCommandException;
Expand All @@ -40,6 +40,7 @@
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.internal.OkHttpClient;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;

Expand All @@ -48,6 +49,10 @@
import com.xceptance.xlt.api.webdriver.XltFirefoxDriver;
import com.xceptance.xlt.engine.SessionImpl;

import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import xltutil.annotation.TestTargets;
import xltutil.dto.BrowserConfigurationDto;
import xltutil.dto.ProxyConfigurationDto;
Expand Down Expand Up @@ -136,17 +141,34 @@ public static HttpCommandExecutor createGridExecutor(final ProxyConfigurationDto
basicCredentialsProvider.setCredentials(gridAuth, gridCredentials);
}

Authenticator proxyAuthenticator = new Authenticator()
{
@Override
public Request authenticate(Route route, Response response) throws IOException
{
AuthScope scope = new AuthScope(route.socketAddress().getHostName(), route.socketAddress().getPort());
return response.request().newBuilder()
.header("Proxy-Authorization", basicCredentialsProvider.getCredentials(scope).toString())
.build();
}
};

// now create a http client, set the custom proxy and inject the credentials
final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
final okhttp3.OkHttpClient.Builder clientBuilder = new okhttp3.OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS);
if (proxyConfig != null)
clientBuilder.setProxy(new HttpHost(proxyConfig.getHost(), Integer.valueOf(proxyConfig.getPort())));
final CloseableHttpClient httpClient = clientBuilder.build();
{
clientBuilder.proxy(new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxyConfig.getHost(), Integer.valueOf(proxyConfig.getPort()))))
.proxyAuthenticator(proxyAuthenticator);

}

final Map<String, CommandInfo> additionalCommands = new HashMap<String, CommandInfo>(); // just a dummy

// this command executor will do the credential magic for us. both proxy and target site credentials
return new HttpCommandExecutor(additionalCommands, gridUrl, new ProxyHttpClient(httpClient));
return new HttpCommandExecutor(additionalCommands, gridUrl, new ProxyHttpClient(new OkHttpClient(clientBuilder.build(), gridUrl)));

}

Expand Down

0 comments on commit 8b46658

Please sign in to comment.