Skip to content

Commit e0c6ba0

Browse files
committed
add auth-datafile support
1 parent 3eba4fc commit e0c6ba0

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

core-httpclient-impl/src/main/java/com/optimizely/ab/OptimizelyFactory.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ public static Optimizely newDefaultInstance(String sdkKey, String fallback) {
189189
return newDefaultInstance(builder.build(), notificationCenter);
190190
}
191191

192+
/**
193+
* Returns a new Optimizely instance with authenticated datafile support.
194+
*
195+
* @param sdkKey SDK key used to build the ProjectConfigManager.
196+
* @param authDatafileToken Token for authenticated datafile access.
197+
* @param fallback Fallback datafile string used by the ProjectConfigManager to be immediately available.
198+
*/
199+
public static Optimizely newDefaultInstance(String sdkKey, String authDatafileToken, String fallback) {
200+
NotificationCenter notificationCenter = new NotificationCenter();
201+
202+
HttpProjectConfigManager.Builder builder = HttpProjectConfigManager.builder()
203+
.withDatafile(fallback)
204+
.withNotificationCenter(notificationCenter)
205+
.withSdkKey(sdkKey)
206+
.withAuthDatafileToken(authDatafileToken);
207+
208+
return newDefaultInstance(builder.build(), notificationCenter);
209+
}
210+
192211
/**
193212
* Returns a new Optimizely instance based on preset configuration.
194213
* EventHandler - {@link AsyncEventHandler}

core-httpclient-impl/src/main/java/com/optimizely/ab/config/HttpProjectConfigManager.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
5454

5555
private final OptimizelyHttpClient httpClient;
5656
private final URI uri;
57+
private final String authDatafileToken;
5758
private String datafileLastModified;
5859

59-
private HttpProjectConfigManager(long period, TimeUnit timeUnit, OptimizelyHttpClient httpClient, String url, long blockingTimeoutPeriod, TimeUnit blockingTimeoutUnit, NotificationCenter notificationCenter) {
60+
private HttpProjectConfigManager(long period,
61+
TimeUnit timeUnit,
62+
OptimizelyHttpClient httpClient,
63+
String url,
64+
String authDatafileToken,
65+
long blockingTimeoutPeriod,
66+
TimeUnit blockingTimeoutUnit,
67+
NotificationCenter notificationCenter) {
6068
super(period, timeUnit, blockingTimeoutPeriod, blockingTimeoutUnit, notificationCenter);
6169
this.httpClient = httpClient;
6270
this.uri = URI.create(url);
71+
this.authDatafileToken = authDatafileToken;
6372
}
6473

6574
public URI getUri() {
@@ -106,6 +115,10 @@ static ProjectConfig parseProjectConfig(String datafile) throws ConfigParseExcep
106115
protected ProjectConfig poll() {
107116
HttpGet httpGet = new HttpGet(uri);
108117

118+
if (authDatafileToken != null) {
119+
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + authDatafileToken);
120+
}
121+
109122
if (datafileLastModified != null) {
110123
httpGet.setHeader(HttpHeaders.IF_MODIFIED_SINCE, datafileLastModified);
111124
}
@@ -132,7 +145,9 @@ public static Builder builder() {
132145
public static class Builder {
133146
private String datafile;
134147
private String url;
148+
private String authDatafileToken = null;
135149
private String format = "https://cdn.optimizely.com/datafiles/%s.json";
150+
private String authFormat = "https://www.optimizely-cdn.com/datafiles/auth/%s.json";
136151
private OptimizelyHttpClient httpClient;
137152
private NotificationCenter notificationCenter;
138153

@@ -153,6 +168,11 @@ public Builder withSdkKey(String sdkKey) {
153168
return this;
154169
}
155170

171+
public Builder withAuthDatafileToken(String token) {
172+
this.authDatafileToken = token;
173+
return this;
174+
}
175+
156176
public Builder withUrl(String url) {
157177
this.url = url;
158178
return this;
@@ -261,14 +281,26 @@ public HttpProjectConfigManager build(boolean defer) {
261281
throw new NullPointerException("sdkKey cannot be null");
262282
}
263283

264-
url = String.format(format, sdkKey);
284+
if (authDatafileToken == null) {
285+
url = String.format(format, sdkKey);
286+
} else {
287+
url = String.format(authFormat, sdkKey);
288+
}
265289
}
266290

267291
if (notificationCenter == null) {
268292
notificationCenter = new NotificationCenter();
269293
}
270294

271-
HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager(period, timeUnit, httpClient, url, blockingTimeoutPeriod, blockingTimeoutUnit, notificationCenter);
295+
HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager(
296+
period,
297+
timeUnit,
298+
httpClient,
299+
url,
300+
authDatafileToken,
301+
blockingTimeoutPeriod,
302+
blockingTimeoutUnit,
303+
notificationCenter);
272304

273305
if (datafile != null) {
274306
try {

0 commit comments

Comments
 (0)