Skip to content

Add support for custom CloseableHttpClient for Proxy Support #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.optimizely.ab.event.EventHandler;
import com.optimizely.ab.internal.PropertyUtils;
import com.optimizely.ab.notification.NotificationCenter;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -247,11 +248,26 @@ public static Optimizely newDefaultInstance(String sdkKey, String fallback) {
* @return A new Optimizely instance
*/
public static Optimizely newDefaultInstance(String sdkKey, String fallback, String datafileAccessToken) {
return newDefaultInstance(sdkKey, fallback, datafileAccessToken, null);
}

/**
* Returns a new Optimizely instance with authenticated datafile support.
*
* @param sdkKey SDK key used to build the ProjectConfigManager.
* @param fallback Fallback datafile string used by the ProjectConfigManager to be immediately available.
* @param datafileAccessToken Token for authenticated datafile access.
* @param customHttpClient Customizable CloseableHttpClient to build OptimizelyHttpClient.
* @return A new Optimizely instance
*/
public static Optimizely newDefaultInstance(String sdkKey, String fallback, String datafileAccessToken, CloseableHttpClient customHttpClient) {
NotificationCenter notificationCenter = new NotificationCenter();

HttpProjectConfigManager.Builder builder = HttpProjectConfigManager.builder()
OptimizelyHttpClient optimizelyHttpClient = new OptimizelyHttpClient(customHttpClient);
HttpProjectConfigManager.Builder builder;
builder = HttpProjectConfigManager.builder()
.withDatafile(fallback)
.withNotificationCenter(notificationCenter)
.withOptimizelyHttpClient(customHttpClient == null ? null : optimizelyHttpClient)
.withSdkKey(sdkKey);

if (datafileAccessToken != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
import com.optimizely.ab.event.BatchEventProcessor;
import com.optimizely.ab.internal.PropertyUtils;
import com.optimizely.ab.notification.NotificationCenter;
import org.apache.http.HttpHost;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -237,6 +243,24 @@ public void newDefaultInstanceWithDatafileAccessToken() throws Exception {
assertTrue(optimizely.isValid());
}

@Test
public void newDefaultInstanceWithDatafileAccessTokenAndCustomHttpClient() throws Exception {
// Add custom Proxy and Port here
int port = 443;
String proxyHostName = "someProxy.com";
HttpHost proxyHost = new HttpHost(proxyHostName, port);

HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);

HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

CloseableHttpClient httpClient = clientBuilder.build();
String datafileString = Resources.toString(Resources.getResource("valid-project-config-v4.json"), Charsets.UTF_8);
optimizely = OptimizelyFactory.newDefaultInstance("sdk-key", datafileString, "auth-token", httpClient);
assertTrue(optimizely.isValid());
}

@Test
public void newDefaultInstanceWithProjectConfig() throws Exception {
optimizely = OptimizelyFactory.newDefaultInstance(() -> null);
Expand Down