18
18
19
19
import com .optimizely .ab .HttpClientUtils ;
20
20
import com .optimizely .ab .OptimizelyHttpClient ;
21
+ import com .optimizely .ab .annotations .VisibleForTesting ;
21
22
import com .optimizely .ab .config .parser .ConfigParseException ;
22
23
import com .optimizely .ab .internal .PropertyUtils ;
23
24
import com .optimizely .ab .notification .NotificationCenter ;
@@ -44,6 +45,7 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
44
45
public static final String CONFIG_BLOCKING_DURATION = "http.project.config.manager.blocking.duration" ;
45
46
public static final String CONFIG_BLOCKING_UNIT = "http.project.config.manager.blocking.unit" ;
46
47
public static final String CONFIG_SDK_KEY = "http.project.config.manager.sdk.key" ;
48
+ public static final String CONFIG_DATAFILE_AUTH_TOKEN = "http.project.config.manager.datafile.auth.token" ;
47
49
48
50
public static final long DEFAULT_POLLING_DURATION = 5 ;
49
51
public static final TimeUnit DEFAULT_POLLING_UNIT = TimeUnit .MINUTES ;
@@ -54,12 +56,21 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
54
56
55
57
private final OptimizelyHttpClient httpClient ;
56
58
private final URI uri ;
59
+ private final String datafileAccessToken ;
57
60
private String datafileLastModified ;
58
61
59
- private HttpProjectConfigManager (long period , TimeUnit timeUnit , OptimizelyHttpClient httpClient , String url , long blockingTimeoutPeriod , TimeUnit blockingTimeoutUnit , NotificationCenter notificationCenter ) {
62
+ private HttpProjectConfigManager (long period ,
63
+ TimeUnit timeUnit ,
64
+ OptimizelyHttpClient httpClient ,
65
+ String url ,
66
+ String datafileAccessToken ,
67
+ long blockingTimeoutPeriod ,
68
+ TimeUnit blockingTimeoutUnit ,
69
+ NotificationCenter notificationCenter ) {
60
70
super (period , timeUnit , blockingTimeoutPeriod , blockingTimeoutUnit , notificationCenter );
61
71
this .httpClient = httpClient ;
62
72
this .uri = URI .create (url );
73
+ this .datafileAccessToken = datafileAccessToken ;
63
74
}
64
75
65
76
public URI getUri () {
@@ -104,11 +115,7 @@ static ProjectConfig parseProjectConfig(String datafile) throws ConfigParseExcep
104
115
105
116
@ Override
106
117
protected ProjectConfig poll () {
107
- HttpGet httpGet = new HttpGet (uri );
108
-
109
- if (datafileLastModified != null ) {
110
- httpGet .setHeader (HttpHeaders .IF_MODIFIED_SINCE , datafileLastModified );
111
- }
118
+ HttpGet httpGet = createHttpRequest ();
112
119
113
120
logger .debug ("Fetching datafile from: {}" , httpGet .getURI ());
114
121
try {
@@ -125,14 +132,31 @@ protected ProjectConfig poll() {
125
132
return null ;
126
133
}
127
134
135
+ @ VisibleForTesting
136
+ HttpGet createHttpRequest () {
137
+ HttpGet httpGet = new HttpGet (uri );
138
+
139
+ if (datafileAccessToken != null ) {
140
+ httpGet .setHeader (HttpHeaders .AUTHORIZATION , "Bearer " + datafileAccessToken );
141
+ }
142
+
143
+ if (datafileLastModified != null ) {
144
+ httpGet .setHeader (HttpHeaders .IF_MODIFIED_SINCE , datafileLastModified );
145
+ }
146
+
147
+ return httpGet ;
148
+ }
149
+
128
150
public static Builder builder () {
129
151
return new Builder ();
130
152
}
131
153
132
154
public static class Builder {
133
155
private String datafile ;
134
156
private String url ;
157
+ private String datafileAccessToken = null ;
135
158
private String format = "https://cdn.optimizely.com/datafiles/%s.json" ;
159
+ private String authFormat = "https://config.optimizely.com/datafiles/auth/%s.json" ;
136
160
private OptimizelyHttpClient httpClient ;
137
161
private NotificationCenter notificationCenter ;
138
162
@@ -153,6 +177,11 @@ public Builder withSdkKey(String sdkKey) {
153
177
return this ;
154
178
}
155
179
180
+ public Builder withDatafileAccessToken (String token ) {
181
+ this .datafileAccessToken = token ;
182
+ return this ;
183
+ }
184
+
156
185
public Builder withUrl (String url ) {
157
186
this .url = url ;
158
187
return this ;
@@ -261,14 +290,26 @@ public HttpProjectConfigManager build(boolean defer) {
261
290
throw new NullPointerException ("sdkKey cannot be null" );
262
291
}
263
292
264
- url = String .format (format , sdkKey );
293
+ if (datafileAccessToken == null ) {
294
+ url = String .format (format , sdkKey );
295
+ } else {
296
+ url = String .format (authFormat , sdkKey );
297
+ }
265
298
}
266
299
267
300
if (notificationCenter == null ) {
268
301
notificationCenter = new NotificationCenter ();
269
302
}
270
303
271
- HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager (period , timeUnit , httpClient , url , blockingTimeoutPeriod , blockingTimeoutUnit , notificationCenter );
304
+ HttpProjectConfigManager httpProjectManager = new HttpProjectConfigManager (
305
+ period ,
306
+ timeUnit ,
307
+ httpClient ,
308
+ url ,
309
+ datafileAccessToken ,
310
+ blockingTimeoutPeriod ,
311
+ blockingTimeoutUnit ,
312
+ notificationCenter );
272
313
273
314
if (datafile != null ) {
274
315
try {
0 commit comments