Skip to content

Commit 755591f

Browse files
committed
Updates the android target to use a version of HttpClient that is closer
to the version shipped with Android. Also adds support for skipping SSL certificate verifcation. This allows the client to be used with self signed certificates. Conflicts: src/main/resources/android-java/apiInvoker.mustache
1 parent cf95d4e commit 755591f

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

src/main/resources/android-java/apiInvoker.mustache

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
99
import org.apache.http.*;
1010
import org.apache.http.client.*;
1111
import org.apache.http.client.methods.*;
12+
import org.apache.http.conn.*;
13+
import org.apache.http.conn.scheme.*;
14+
import org.apache.http.conn.ssl.*;
1215
import org.apache.http.entity.StringEntity;
1316
import org.apache.http.impl.client.*;
17+
import org.apache.http.impl.conn.*;
18+
import org.apache.http.params.*;
1419
import org.apache.http.util.EntityUtils;
1520

1621
import java.io.File;
22+
import java.net.Socket;
23+
import java.net.UnknownHostException;
1724
import java.net.URLEncoder;
1825

1926
import java.util.Map;
@@ -22,15 +29,50 @@ import java.util.List;
2229
import java.io.IOException;
2330
import java.io.UnsupportedEncodingException;
2431

32+
import java.security.GeneralSecurityException;
33+
import java.security.KeyManagementException;
34+
import java.security.KeyStore;
35+
import java.security.NoSuchAlgorithmException;
36+
import java.security.SecureRandom;
37+
import java.security.cert.*;
38+
39+
import java.text.DateFormat;
40+
import java.text.SimpleDateFormat;
41+
42+
import java.util.Date;
43+
import java.util.Random;
44+
import java.util.logging.Level;
45+
import java.util.logging.Logger;
46+
47+
import javax.crypto.Mac;
48+
import javax.crypto.spec.SecretKeySpec;
49+
50+
import javax.net.ssl.SSLContext;
51+
import javax.net.ssl.TrustManager;
52+
import javax.net.ssl.X509TrustManager;
53+
2554
public class ApiInvoker {
2655
private static ApiInvoker INSTANCE = new ApiInvoker();
2756
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
2857
2958
private HttpClient client = null;
59+
60+
private boolean ignoreSSLCertificates = false;
61+
62+
private ClientConnectionManager ignoreSSLConnectionManager;
63+
64+
public ApiInvoker() {
65+
initConnectionManager();
66+
}
67+
3068
public static ApiInvoker getInstance() {
3169
return INSTANCE;
3270
}
3371

72+
public void ignoreSSLCertificates(boolean ignoreSSLCertificates) {
73+
this.ignoreSSLCertificates = ignoreSSLCertificates;
74+
}
75+
3476
public void addDefaultHeader(String key, String value) {
3577
defaultHeaderMap.put(key, value);
3678
}
@@ -167,8 +209,58 @@ public class ApiInvoker {
167209
}
168210

169211
private HttpClient getClient(String host) {
170-
if(client == null)
171-
client = new DefaultHttpClient();
212+
if(client == null) {
213+
if (ignoreSSLCertificates && ignoreSSLConnectionManager != null) {
214+
// Trust self signed certificates
215+
client = new DefaultHttpClient(ignoreSSLConnectionManager, new BasicHttpParams());
216+
} else {
217+
client = new DefaultHttpClient();
218+
}
219+
}
172220
return client;
173221
}
174-
}
222+
223+
private void initConnectionManager() {
224+
try {
225+
final SSLContext sslContext = SSLContext.getInstance("SSL");
226+
227+
// set up a TrustManager that trusts everything
228+
TrustManager[] trustManagers = new TrustManager[] {
229+
new X509TrustManager() {
230+
public X509Certificate[] getAcceptedIssuers() {
231+
return null;
232+
}
233+
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
234+
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
235+
}};
236+
237+
sslContext.init(null, trustManagers, new SecureRandom());
238+
239+
SSLSocketFactory sf = new SSLSocketFactory((KeyStore)null) {
240+
private javax.net.ssl.SSLSocketFactory sslFactory = sslContext.getSocketFactory();
241+
242+
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
243+
throws IOException, UnknownHostException {
244+
return sslFactory.createSocket(socket, host, port, autoClose);
245+
}
246+
247+
public Socket createSocket() throws IOException {
248+
return sslFactory.createSocket();
249+
}
250+
};
251+
252+
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
253+
Scheme httpsScheme = new Scheme("https", sf, 443);
254+
SchemeRegistry schemeRegistry = new SchemeRegistry();
255+
schemeRegistry.register(httpsScheme);
256+
257+
ignoreSSLConnectionManager = new SingleClientConnManager(new BasicHttpParams(), schemeRegistry);
258+
} catch (NoSuchAlgorithmException e) {
259+
// This will only be thrown if SSL isn't available for some reason.
260+
} catch (KeyManagementException e) {
261+
// This might be thrown when passing a key into init(), but no key is being passed.
262+
} catch (GeneralSecurityException e) {
263+
throw new RuntimeException("Couldn't Init SSLSocketFactory: " + e.getMessage(), e);
264+
}
265+
}
266+
}

src/main/resources/android-java/pom.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
<maven-plugin-version>1.0.0</maven-plugin-version>
208208
<junit-version>4.8.1</junit-version>
209209
<scala-test-version>1.6.1</scala-test-version>
210-
<httpclient-version>4.2.3</httpclient-version>
210+
<httpclient-version>4.0</httpclient-version>
211211
<scala-maven-plugin-version>3.1.5</scala-maven-plugin-version>
212212
</properties>
213213
</project>

0 commit comments

Comments
 (0)