This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from launchdarkly/drichelson/ch2310/java-sdk-do…
…es-not-allow-configuration-for Remove apache http in favor of okhttp
- Loading branch information
Showing
13 changed files
with
172 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ | |
build/ | ||
bin/ | ||
gradle.properties | ||
|
||
classes/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 30 additions & 98 deletions
128
src/main/java/com/launchdarkly/client/FeatureRequestor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,58 @@ | ||
package com.launchdarkly.client; | ||
|
||
import org.apache.http.client.cache.CacheResponseStatus; | ||
import org.apache.http.client.cache.HttpCacheContext; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.cache.CacheConfig; | ||
import org.apache.http.impl.client.cache.CachingHttpClients; | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
import org.apache.http.util.EntityUtils; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
class FeatureRequestor { | ||
|
||
public static final String GET_LATEST_FLAGS_PATH = "/sdk/latest-flags"; | ||
private static final Logger logger = LoggerFactory.getLogger(FeatureRequestor.class); | ||
private static final String GET_LATEST_FLAGS_PATH = "/sdk/latest-flags"; | ||
private final String sdkKey; | ||
private final LDConfig config; | ||
private final CloseableHttpClient client; | ||
private static final Logger logger = LoggerFactory.getLogger(FeatureRequestor.class); | ||
|
||
FeatureRequestor(String sdkKey, LDConfig config) { | ||
this.sdkKey = sdkKey; | ||
this.config = config; | ||
this.client = createClient(); | ||
} | ||
|
||
protected CloseableHttpClient createClient() { | ||
CloseableHttpClient client; | ||
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); | ||
manager.setMaxTotal(100); | ||
manager.setDefaultMaxPerRoute(20); | ||
|
||
CacheConfig cacheConfig = CacheConfig.custom() | ||
.setMaxCacheEntries(1000) | ||
.setMaxObjectSize(131072) | ||
.setSharedCache(false) | ||
.build(); | ||
|
||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setConnectTimeout(config.connectTimeout) | ||
.setSocketTimeout(config.socketTimeout) | ||
.setProxy(config.proxyHost) | ||
.build(); | ||
client = CachingHttpClients.custom() | ||
.setCacheConfig(cacheConfig) | ||
.setConnectionManager(manager) | ||
.setDefaultRequestConfig(requestConfig) | ||
.build(); | ||
return client; | ||
} | ||
|
||
Map<String, FeatureFlag> getAllFlags() throws IOException { | ||
HttpCacheContext context = HttpCacheContext.create(); | ||
|
||
HttpGet request = config.getRequest(sdkKey, GET_LATEST_FLAGS_PATH); | ||
|
||
CloseableHttpResponse response = null; | ||
try { | ||
logger.debug("Making request: " + request); | ||
response = client.execute(request, context); | ||
|
||
logCacheResponse(context.getCacheResponseStatus()); | ||
if (!Util.handleResponse(logger, request, response)) { | ||
throw new IOException("Failed to fetch flags"); | ||
} | ||
|
||
String json = EntityUtils.toString(response.getEntity()); | ||
logger.debug("Got response: " + response.toString()); | ||
return FeatureFlag.fromJsonMap(json); | ||
} | ||
finally { | ||
try { | ||
if (response != null) response.close(); | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
String body = get(GET_LATEST_FLAGS_PATH); | ||
return FeatureFlag.fromJsonMap(body); | ||
} | ||
|
||
void logCacheResponse(CacheResponseStatus status) { | ||
switch (status) { | ||
case CACHE_HIT: | ||
logger.debug("A response was generated from the cache with " + | ||
"no requests sent upstream"); | ||
break; | ||
case CACHE_MODULE_RESPONSE: | ||
logger.debug("The response was generated directly by the " + | ||
"caching module"); | ||
break; | ||
case CACHE_MISS: | ||
logger.debug("The response came from an upstream server"); | ||
break; | ||
case VALIDATED: | ||
logger.debug("The response was generated from the cache " + | ||
"after validating the entry with the origin server"); | ||
break; | ||
} | ||
FeatureFlag getFlag(String featureKey) throws IOException { | ||
String body = get(GET_LATEST_FLAGS_PATH + "/" + featureKey); | ||
return FeatureFlag.fromJson(body); | ||
} | ||
|
||
FeatureFlag getFlag(String featureKey) throws IOException { | ||
HttpCacheContext context = HttpCacheContext.create(); | ||
HttpGet request = config.getRequest(sdkKey, GET_LATEST_FLAGS_PATH + "/" + featureKey); | ||
CloseableHttpResponse response = null; | ||
try { | ||
response = client.execute(request, context); | ||
private String get(String path) throws IOException { | ||
Request request = config.getRequestBuilder(sdkKey) | ||
.url(config.baseURI.toString() + path) | ||
.get() | ||
.build(); | ||
|
||
logCacheResponse(context.getCacheResponseStatus()); | ||
logger.debug("Making request: " + request); | ||
|
||
if (!Util.handleResponse(logger, request, response)) { | ||
throw new IOException("Failed to fetch flag"); | ||
} | ||
return FeatureFlag.fromJson(EntityUtils.toString(response.getEntity())); | ||
} | ||
finally { | ||
try { | ||
if (response != null) response.close(); | ||
} catch (IOException ignored) { | ||
try (Response response = config.httpClient.newCall(request).execute()) { | ||
String body = response.body().string(); | ||
|
||
if (!response.isSuccessful()) { | ||
if (response.code() == 401) { | ||
logger.error("[401] Invalid SDK key when accessing URI: " + request.url()); | ||
} | ||
throw new IOException("Unexpected response when retrieving Feature Flag(s): " + response + " using url: " | ||
+ request.url() + " with body: " + body); | ||
} | ||
logger.debug("Get flag(s) response: " + response.toString() + " with body: " + body); | ||
logger.debug("Cache hit count: " + config.httpClient.cache().hitCount() + " Cache network Count: " + config.httpClient.cache().networkCount()); | ||
logger.debug("Cache response: " + response.cacheResponse()); | ||
logger.debug("Network response: " + response.networkResponse()); | ||
|
||
return body; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.