Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Support for a http(s) proxy #2

Merged
merged 3 commits into from
Sep 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ This creates a new CreditCardFraudDetection object and set isSecure to s if
isSecure is false then it uses regular HTTP. if isSecure is true then it uses
secure HTTPS.

```java
ccfb = new CreditCardFraudDetection.Builder()
```

Additionally you can use the Builder object to create a `CreditCardFraudDetection`
instance that either uses
the [JVM settings for proxies](https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html)
(`useSystemProxies()`) or a custom proxy (specified by hostname and port, e.g. `useProxy("host", 3128)`).

Here is a full example:

```java
ccfs = new CreditCardFraudDetection.Builder().
useProxy("proxy-host", 6568).
build();
```


```java
ccfs.input(HashMap h);
```
Expand Down
49 changes: 42 additions & 7 deletions src/main/java/com/maxmind/ws/CreditCardFraudDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,32 @@ public class CreditCardFraudDetection extends HTTPBase {
'b', 'c', 'd', 'e', 'f' };

public CreditCardFraudDetection() {
url = "app/ccv2r";
check_field = "countryMatch";
allowed_fields = new HashMap<String, Integer>();
for (final String allowedfield : allowedfields) {
allowed_fields.put(allowedfield, new Integer(1));
}
initFields();
setIsSecure(true);
}

public CreditCardFraudDetection(boolean s) {
private void initFields() {
url = "app/ccv2r";
check_field = "countryMatch";
allowed_fields = new HashMap<String, Integer>();
for (final String allowedfield : allowedfields) {
allowed_fields.put(allowedfield, new Integer(1));
}
}

public CreditCardFraudDetection(boolean s) {
initFields();
setIsSecure(s);
}

private CreditCardFraudDetection(Builder builder) {
initFields();
setIsSecure(builder.isSecure);
this.proxyHost = builder.proxyHost;
this.proxyPort = builder.proxyPort;
this.useSystemProxies = builder.systemProxies;
}

@Override
protected String filter_field(String key, String value) {
if (("emailMD5".equals(key) && value.indexOf('@') != -1)
Expand All @@ -76,4 +83,32 @@ protected String filter_field(String key, String value) {
}
return value;
}

public static class Builder {
private boolean systemProxies = false;
private String proxyHost = null;
private int proxyPort = -1;
private boolean isSecure = true;

public Builder() { }

public Builder useSystemProxies() {
systemProxies = true;
return this;
}
public Builder proxy(String proxyHost, int proxyPort) {
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
return this;
}

public Builder withoutSecurity() {
this.isSecure = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should get rid of the separate buildUnsecure() method and just make a disableSecure() method (or something similar) on the builder.

return this;
}
public CreditCardFraudDetection build() {
return new CreditCardFraudDetection(this);
}

}
}
44 changes: 39 additions & 5 deletions src/main/java/com/maxmind/ws/HTTPBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@

package com.maxmind.ws;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ProxySelector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
Expand Down Expand Up @@ -58,6 +68,11 @@ public class HTTPBase {
public long wsIpaddrRefreshTimeout = 18000;
public String wsIpaddrCacheFile = "/tmp/maxmind.ws.cache";

boolean useSystemProxies = false;

String proxyHost = null;
int proxyPort = -1;

HTTPBase() {
queries = new HashMap<String, String>();
allowed_fields = new HashMap<String, Integer>();
Expand Down Expand Up @@ -154,16 +169,27 @@ boolean querySingleServer(String hostname) {
if (debug) {
System.out.println("url2 = " + url2);
}
CloseableHttpClient client = null;
try {
final DefaultHttpClient client = new DefaultHttpClient();
final HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params,
(int) timeout * 1000);
HttpConnectionParams.setSoTimeout(params, (int) timeout * 1000);
RequestConfig timeoutConfig = RequestConfig.custom().
setConnectionRequestTimeout((int) timeout * 1000).
setSocketTimeout((int) timeout * 1000).build();

HttpRoutePlanner proxyPlaner = null;
if (useSystemProxies) {
proxyPlaner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
}
if (proxyHost != null && proxyPort > -1) {
proxyPlaner = new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort));
}
client = HttpClients.custom().
setDefaultRequestConfig(timeoutConfig).
setRoutePlanner(proxyPlaner).build();

// connect the server
final HttpPost method = new HttpPost(url2);
method.setEntity(new UrlEncodedFormEntity(parameters));

final HttpResponse response = client.execute(method);

final String content = EntityUtils.toString(response.getEntity());
Expand Down Expand Up @@ -217,6 +243,14 @@ boolean querySingleServer(String hostname) {
}
System.out.println("error = " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
//ignore
}
}
return false;
}
Expand Down