|
12 | 12 | import com.maxmind.minfraud.response.InsightsResponse; |
13 | 13 | import com.maxmind.minfraud.response.ScoreResponse; |
14 | 14 | import org.apache.http.HttpEntity; |
| 15 | +import org.apache.http.HttpHost; |
15 | 16 | import org.apache.http.HttpResponse; |
16 | 17 | import org.apache.http.auth.Credentials; |
17 | 18 | import org.apache.http.auth.UsernamePasswordCredentials; |
|
25 | 26 | import org.apache.http.impl.client.HttpClientBuilder; |
26 | 27 | import org.apache.http.util.EntityUtils; |
27 | 28 |
|
| 29 | +import java.io.Closeable; |
28 | 30 | import java.io.IOException; |
29 | | -import java.net.MalformedURLException; |
30 | | -import java.net.URISyntaxException; |
31 | | -import java.net.URL; |
| 31 | +import java.net.*; |
32 | 32 | import java.util.*; |
33 | 33 |
|
34 | 34 | /** |
35 | 35 | * Client for MaxMind minFraud Score, Insights, and Factors |
36 | 36 | */ |
37 | | -public final class WebServiceClient { |
| 37 | +public final class WebServiceClient implements Closeable { |
38 | 38 | private static final String pathBase = "/minfraud/v2.0/"; |
39 | 39 |
|
40 | 40 | private final String host; |
41 | 41 | private final int port; |
42 | 42 | private final boolean useHttps; |
43 | 43 | private final List<String> locales; |
44 | 44 | private final String licenseKey; |
45 | | - private final int connectTimeout; |
46 | | - private final int readTimeout; |
47 | 45 | private final int userId; |
48 | 46 |
|
| 47 | + |
| 48 | + private final ObjectMapper mapper; |
| 49 | + private final CloseableHttpClient httpClient; |
| 50 | + |
49 | 51 | private WebServiceClient(WebServiceClient.Builder builder) { |
50 | 52 | host = builder.host; |
51 | 53 | port = builder.port; |
52 | 54 | useHttps = builder.useHttps; |
53 | 55 | locales = builder.locales; |
54 | 56 | licenseKey = builder.licenseKey; |
55 | | - connectTimeout = builder.connectTimeout; |
56 | | - readTimeout = builder.readTimeout; |
57 | 57 | userId = builder.userId; |
| 58 | + |
| 59 | + mapper = new ObjectMapper(); |
| 60 | + mapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); |
| 61 | + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 62 | + |
| 63 | + RequestConfig.Builder configBuilder = RequestConfig.custom() |
| 64 | + .setConnectTimeout(builder.connectTimeout) |
| 65 | + .setSocketTimeout(builder.readTimeout); |
| 66 | + |
| 67 | + if (builder.proxy != null) { |
| 68 | + InetSocketAddress address = (InetSocketAddress) builder.proxy.address(); |
| 69 | + HttpHost proxyHost = new HttpHost(address.getHostName(), address.getPort()); |
| 70 | + configBuilder.setProxy(proxyHost); |
| 71 | + } |
| 72 | + |
| 73 | + RequestConfig config = configBuilder.build(); |
| 74 | + httpClient = |
| 75 | + HttpClientBuilder.create() |
| 76 | + .setUserAgent(userAgent()) |
| 77 | + .setDefaultRequestConfig(config).build(); |
58 | 78 | } |
59 | 79 |
|
60 | 80 | /** |
@@ -87,6 +107,7 @@ public static final class Builder { |
87 | 107 | int readTimeout = -1; |
88 | 108 |
|
89 | 109 | List<String> locales = Collections.singletonList("en"); |
| 110 | + private Proxy proxy; |
90 | 111 |
|
91 | 112 | /** |
92 | 113 | * @param userId Your MaxMind user ID. |
@@ -162,6 +183,15 @@ public WebServiceClient.Builder readTimeout(int val) { |
162 | 183 | return this; |
163 | 184 | } |
164 | 185 |
|
| 186 | + /** |
| 187 | + * @param val the proxy to use when making this request. |
| 188 | + * @return Builder object |
| 189 | + */ |
| 190 | + public Builder proxy(Proxy val) { |
| 191 | + this.proxy = val; |
| 192 | + return this; |
| 193 | + } |
| 194 | + |
165 | 195 | /** |
166 | 196 | * @return an instance of {@code WebServiceClient} created from the |
167 | 197 | * fields set on this builder. |
@@ -254,16 +284,7 @@ private <T> T responseFor(String service, Transaction transaction, Class<T> cls) |
254 | 284 | URL url = createUrl(WebServiceClient.pathBase + service); |
255 | 285 | HttpPost request = requestFor(transaction, url); |
256 | 286 |
|
257 | | - RequestConfig config = RequestConfig.custom() |
258 | | - .setConnectTimeout(this.connectTimeout) |
259 | | - .setSocketTimeout(this.readTimeout) |
260 | | - .build(); |
261 | | - |
262 | | - try ( |
263 | | - CloseableHttpClient httpClient = |
264 | | - HttpClientBuilder.create().setDefaultRequestConfig(config).build(); |
265 | | - CloseableHttpResponse response = httpClient.execute(request) |
266 | | - ) { |
| 287 | + try (CloseableHttpResponse response = httpClient.execute(request)) { |
267 | 288 | return handleResponse(response, url, cls); |
268 | 289 | } |
269 | 290 | } |
@@ -315,9 +336,6 @@ private <T> T handleResponse(CloseableHttpResponse response, URL url, Class<T> c |
315 | 336 | + " but there was no message body.", 200, url); |
316 | 337 | } |
317 | 338 |
|
318 | | - ObjectMapper mapper = new ObjectMapper(); |
319 | | - mapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); |
320 | | - mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
321 | 339 | InjectableValues inject = new Std().addValue( |
322 | 340 | "locales", locales); |
323 | 341 |
|
@@ -348,8 +366,6 @@ private void handle4xxStatus(HttpResponse response, URL url) |
348 | 366 |
|
349 | 367 | Map<String, String> content; |
350 | 368 | try { |
351 | | - ObjectMapper mapper = new ObjectMapper(); |
352 | | - mapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); |
353 | 369 | content = mapper.readValue(body, |
354 | 370 | new TypeReference<HashMap<String, String>>() { |
355 | 371 | }); |
@@ -410,16 +426,26 @@ private String userAgent() { |
410 | 426 | + " Java/" + System.getProperty("java.version"); |
411 | 427 | } |
412 | 428 |
|
| 429 | + |
| 430 | + /** |
| 431 | + * Close any open connections and return resources to the system. |
| 432 | + */ |
| 433 | + @Override |
| 434 | + public void close() throws IOException { |
| 435 | + httpClient.close(); |
| 436 | + } |
| 437 | + |
413 | 438 | @Override |
414 | 439 | public String toString() { |
415 | | - return "WebServiceClient{" + "host='" + this.host + '\'' + |
416 | | - ", port=" + this.port + |
417 | | - ", useHttps=" + this.useHttps + |
418 | | - ", locales=" + this.locales + |
419 | | - ", licenseKey='" + this.licenseKey + '\'' + |
420 | | - ", connectTimeout=" + this.connectTimeout + |
421 | | - ", readTimeout=" + this.readTimeout + |
422 | | - ", userId=" + this.userId + |
| 440 | + return "WebServiceClient{" + |
| 441 | + "host='" + host + '\'' + |
| 442 | + ", port=" + port + |
| 443 | + ", useHttps=" + useHttps + |
| 444 | + ", locales=" + locales + |
| 445 | + ", licenseKey='" + licenseKey + '\'' + |
| 446 | + ", userId=" + userId + |
| 447 | + ", mapper=" + mapper + |
| 448 | + ", httpClient=" + httpClient + |
423 | 449 | '}'; |
424 | 450 | } |
425 | 451 | } |
0 commit comments