Skip to content

Commit b7f2396

Browse files
committed
Merge pull request #5 from Gared/https
Add possibility to request https api urls
2 parents 0ac9cda + 4363fe0 commit b7f2396

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

org/etherpad_lite_client/EPLiteClient.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
import java.util.Map;
2323
import java.util.HashMap;
2424
import java.util.Iterator;
25+
26+
import javax.net.ssl.HostnameVerifier;
27+
import javax.net.ssl.HttpsURLConnection;
28+
import javax.net.ssl.SSLContext;
29+
import javax.net.ssl.SSLSession;
30+
import javax.net.ssl.TrustManager;
31+
import javax.net.ssl.X509TrustManager;
32+
2533
import org.json.*;
2634

2735
/**
@@ -713,6 +721,8 @@ private HashMap call(String apiMethod, HashMap apiArgs, String httpMethod) {
713721
}
714722
}
715723
}
724+
725+
trustServerAndCertificate();
716726

717727
// Execute the API call
718728
String path = this.uri.getPath() + "/" + API_VERSION + "/" + apiMethod;
@@ -741,6 +751,43 @@ else if (httpMethod == "POST") {
741751
throw new EPLiteException("Unable to connect to Etherpad Lite instance (" + e.getClass() + "): " + e.getMessage());
742752
}
743753
}
754+
755+
/**
756+
* Creates a trust manager to trust all certificates if you open a ssl connection
757+
*/
758+
private void trustServerAndCertificate() {
759+
// Create a trust manager that does not validate certificate chains
760+
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
761+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
762+
return null;
763+
}
764+
765+
public void checkClientTrusted(
766+
java.security.cert.X509Certificate[] certs, String authType) {
767+
}
768+
769+
public void checkServerTrusted(
770+
java.security.cert.X509Certificate[] certs, String authType) {
771+
}
772+
}
773+
};
774+
775+
// Install the all-trusting trust manager
776+
try {
777+
SSLContext sc = SSLContext.getInstance("SSL");
778+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
779+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
780+
} catch (Exception e) {
781+
}
782+
783+
HostnameVerifier hv = new HostnameVerifier() {
784+
@Override
785+
public boolean verify(String hostname, SSLSession session) {
786+
return true;
787+
}
788+
};
789+
HttpsURLConnection.setDefaultHostnameVerifier(hv);
790+
}
744791

745792
/**
746793
* Converts the API resonse's JSON string into a HashMap.

0 commit comments

Comments
 (0)