1818
1919import com .optimizely .ab .HttpClientUtils ;
2020import com .optimizely .ab .OptimizelyHttpClient ;
21+ import com .optimizely .ab .annotations .VisibleForTesting ;
2122import com .optimizely .ab .config .parser .ConfigParseException ;
2223import com .optimizely .ab .internal .PropertyUtils ;
2324import com .optimizely .ab .notification .NotificationCenter ;
@@ -44,6 +45,7 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
4445 public static final String CONFIG_BLOCKING_DURATION = "http.project.config.manager.blocking.duration" ;
4546 public static final String CONFIG_BLOCKING_UNIT = "http.project.config.manager.blocking.unit" ;
4647 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" ;
4749
4850 public static final long DEFAULT_POLLING_DURATION = 5 ;
4951 public static final TimeUnit DEFAULT_POLLING_UNIT = TimeUnit .MINUTES ;
@@ -54,12 +56,21 @@ public class HttpProjectConfigManager extends PollingProjectConfigManager {
5456
5557 private final OptimizelyHttpClient httpClient ;
5658 private final URI uri ;
59+ private final String datafileAccessToken ;
5760 private String datafileLastModified ;
5861
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 ) {
6070 super (period , timeUnit , blockingTimeoutPeriod , blockingTimeoutUnit , notificationCenter );
6171 this .httpClient = httpClient ;
6272 this .uri = URI .create (url );
73+ this .datafileAccessToken = datafileAccessToken ;
6374 }
6475
6576 public URI getUri () {
@@ -104,11 +115,7 @@ static ProjectConfig parseProjectConfig(String datafile) throws ConfigParseExcep
104115
105116 @ Override
106117 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 ();
112119
113120 logger .debug ("Fetching datafile from: {}" , httpGet .getURI ());
114121 try {
@@ -125,14 +132,31 @@ protected ProjectConfig poll() {
125132 return null ;
126133 }
127134
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+
128150 public static Builder builder () {
129151 return new Builder ();
130152 }
131153
132154 public static class Builder {
133155 private String datafile ;
134156 private String url ;
157+ private String datafileAccessToken = null ;
135158 private String format = "https://cdn.optimizely.com/datafiles/%s.json" ;
159+ private String authFormat = "https://config.optimizely.com/datafiles/auth/%s.json" ;
136160 private OptimizelyHttpClient httpClient ;
137161 private NotificationCenter notificationCenter ;
138162
@@ -153,6 +177,11 @@ public Builder withSdkKey(String sdkKey) {
153177 return this ;
154178 }
155179
180+ public Builder withDatafileAccessToken (String token ) {
181+ this .datafileAccessToken = token ;
182+ return this ;
183+ }
184+
156185 public Builder withUrl (String url ) {
157186 this .url = url ;
158187 return this ;
@@ -261,14 +290,26 @@ public HttpProjectConfigManager build(boolean defer) {
261290 throw new NullPointerException ("sdkKey cannot be null" );
262291 }
263292
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+ }
265298 }
266299
267300 if (notificationCenter == null ) {
268301 notificationCenter = new NotificationCenter ();
269302 }
270303
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 );
272313
273314 if (datafile != null ) {
274315 try {
0 commit comments