|
1 | 1 | package com.launchdarkly.client; |
2 | 2 |
|
3 | | -import org.apache.http.client.cache.CacheResponseStatus; |
4 | | -import org.apache.http.client.cache.HttpCacheContext; |
5 | | -import org.apache.http.client.config.RequestConfig; |
6 | | -import org.apache.http.client.methods.CloseableHttpResponse; |
7 | | -import org.apache.http.client.methods.HttpGet; |
8 | | -import org.apache.http.impl.client.CloseableHttpClient; |
9 | | -import org.apache.http.impl.client.cache.CacheConfig; |
10 | | -import org.apache.http.impl.client.cache.CachingHttpClients; |
11 | | -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
12 | | -import org.apache.http.util.EntityUtils; |
| 3 | +import okhttp3.Request; |
| 4 | +import okhttp3.Response; |
13 | 5 | import org.slf4j.Logger; |
14 | 6 | import org.slf4j.LoggerFactory; |
15 | 7 |
|
16 | 8 | import java.io.IOException; |
17 | 9 | import java.util.Map; |
18 | 10 |
|
19 | 11 | class FeatureRequestor { |
20 | | - |
21 | | - public static final String GET_LATEST_FLAGS_PATH = "/sdk/latest-flags"; |
| 12 | + private static final Logger logger = LoggerFactory.getLogger(FeatureRequestor.class); |
| 13 | + private static final String GET_LATEST_FLAGS_PATH = "/sdk/latest-flags"; |
22 | 14 | private final String sdkKey; |
23 | 15 | private final LDConfig config; |
24 | | - private final CloseableHttpClient client; |
25 | | - private static final Logger logger = LoggerFactory.getLogger(FeatureRequestor.class); |
26 | 16 |
|
27 | 17 | FeatureRequestor(String sdkKey, LDConfig config) { |
28 | 18 | this.sdkKey = sdkKey; |
29 | 19 | this.config = config; |
30 | | - this.client = createClient(); |
31 | | - } |
32 | | - |
33 | | - protected CloseableHttpClient createClient() { |
34 | | - CloseableHttpClient client; |
35 | | - PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); |
36 | | - manager.setMaxTotal(100); |
37 | | - manager.setDefaultMaxPerRoute(20); |
38 | | - |
39 | | - CacheConfig cacheConfig = CacheConfig.custom() |
40 | | - .setMaxCacheEntries(1000) |
41 | | - .setMaxObjectSize(131072) |
42 | | - .setSharedCache(false) |
43 | | - .build(); |
44 | | - |
45 | | - RequestConfig requestConfig = RequestConfig.custom() |
46 | | - .setConnectTimeout(config.connectTimeout) |
47 | | - .setSocketTimeout(config.socketTimeout) |
48 | | - .setProxy(config.proxyHost) |
49 | | - .build(); |
50 | | - client = CachingHttpClients.custom() |
51 | | - .setCacheConfig(cacheConfig) |
52 | | - .setConnectionManager(manager) |
53 | | - .setDefaultRequestConfig(requestConfig) |
54 | | - .build(); |
55 | | - return client; |
56 | 20 | } |
57 | 21 |
|
58 | 22 | Map<String, FeatureFlag> getAllFlags() throws IOException { |
59 | | - HttpCacheContext context = HttpCacheContext.create(); |
60 | | - |
61 | | - HttpGet request = config.getRequest(sdkKey, GET_LATEST_FLAGS_PATH); |
62 | | - |
63 | | - CloseableHttpResponse response = null; |
64 | | - try { |
65 | | - logger.debug("Making request: " + request); |
66 | | - response = client.execute(request, context); |
67 | | - |
68 | | - logCacheResponse(context.getCacheResponseStatus()); |
69 | | - if (!Util.handleResponse(logger, request, response)) { |
70 | | - throw new IOException("Failed to fetch flags"); |
71 | | - } |
72 | | - |
73 | | - String json = EntityUtils.toString(response.getEntity()); |
74 | | - logger.debug("Got response: " + response.toString()); |
75 | | - return FeatureFlag.fromJsonMap(json); |
76 | | - } |
77 | | - finally { |
78 | | - try { |
79 | | - if (response != null) response.close(); |
80 | | - } catch (IOException ignored) { |
81 | | - } |
82 | | - } |
| 23 | + String body = get(GET_LATEST_FLAGS_PATH); |
| 24 | + return FeatureFlag.fromJsonMap(body); |
83 | 25 | } |
84 | 26 |
|
85 | | - void logCacheResponse(CacheResponseStatus status) { |
86 | | - switch (status) { |
87 | | - case CACHE_HIT: |
88 | | - logger.debug("A response was generated from the cache with " + |
89 | | - "no requests sent upstream"); |
90 | | - break; |
91 | | - case CACHE_MODULE_RESPONSE: |
92 | | - logger.debug("The response was generated directly by the " + |
93 | | - "caching module"); |
94 | | - break; |
95 | | - case CACHE_MISS: |
96 | | - logger.debug("The response came from an upstream server"); |
97 | | - break; |
98 | | - case VALIDATED: |
99 | | - logger.debug("The response was generated from the cache " + |
100 | | - "after validating the entry with the origin server"); |
101 | | - break; |
102 | | - } |
| 27 | + FeatureFlag getFlag(String featureKey) throws IOException { |
| 28 | + String body = get(GET_LATEST_FLAGS_PATH + "/" + featureKey); |
| 29 | + return FeatureFlag.fromJson(body); |
103 | 30 | } |
104 | 31 |
|
105 | | - FeatureFlag getFlag(String featureKey) throws IOException { |
106 | | - HttpCacheContext context = HttpCacheContext.create(); |
107 | | - HttpGet request = config.getRequest(sdkKey, GET_LATEST_FLAGS_PATH + "/" + featureKey); |
108 | | - CloseableHttpResponse response = null; |
109 | | - try { |
110 | | - response = client.execute(request, context); |
| 32 | + private String get(String path) throws IOException { |
| 33 | + Request request = config.getRequestBuilder(sdkKey) |
| 34 | + .url(config.baseURI.toString() + path) |
| 35 | + .get() |
| 36 | + .build(); |
111 | 37 |
|
112 | | - logCacheResponse(context.getCacheResponseStatus()); |
| 38 | + logger.debug("Making request: " + request); |
113 | 39 |
|
114 | | - if (!Util.handleResponse(logger, request, response)) { |
115 | | - throw new IOException("Failed to fetch flag"); |
116 | | - } |
117 | | - return FeatureFlag.fromJson(EntityUtils.toString(response.getEntity())); |
118 | | - } |
119 | | - finally { |
120 | | - try { |
121 | | - if (response != null) response.close(); |
122 | | - } catch (IOException ignored) { |
| 40 | + try (Response response = config.httpClient.newCall(request).execute()) { |
| 41 | + String body = response.body().string(); |
| 42 | + |
| 43 | + if (!response.isSuccessful()) { |
| 44 | + if (response.code() == 401) { |
| 45 | + logger.error("[401] Invalid SDK key when accessing URI: " + request.url()); |
| 46 | + } |
| 47 | + throw new IOException("Unexpected response when retrieving Feature Flag(s): " + response + " using url: " |
| 48 | + + request.url() + " with body: " + body); |
123 | 49 | } |
| 50 | + logger.debug("Get flag(s) response: " + response.toString() + " with body: " + body); |
| 51 | + logger.debug("Cache hit count: " + config.httpClient.cache().hitCount() + " Cache network Count: " + config.httpClient.cache().networkCount()); |
| 52 | + logger.debug("Cache response: " + response.cacheResponse()); |
| 53 | + logger.debug("Network response: " + response.networkResponse()); |
| 54 | + |
| 55 | + return body; |
124 | 56 | } |
125 | 57 | } |
126 | 58 | } |
0 commit comments