Add HTTPS handling to CouchDB river

Introduces 2 new configuration options:

* couchProtocol: http/https
  - Describes the protocol in use.
* no_verify: true/false
  - Allows to disable hostname verification for hosts with problematic certs
This commit is contained in:
Florian Gilcher 2011-11-19 19:51:12 +01:00 committed by Shay Banon
parent 8e9f01a0f7
commit bf5d722748
1 changed files with 21 additions and 1 deletions

View File

@ -58,6 +58,9 @@ import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.HttpsURLConnection;
import static org.elasticsearch.client.Requests.*; import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.common.xcontent.XContentFactory.*; import static org.elasticsearch.common.xcontent.XContentFactory.*;
@ -72,12 +75,14 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
private final String riverIndexName; private final String riverIndexName;
private final String couchProtocol;
private final String couchHost; private final String couchHost;
private final int couchPort; private final int couchPort;
private final String couchDb; private final String couchDb;
private final String couchFilter; private final String couchFilter;
private final String couchFilterParamsUrl; private final String couchFilterParamsUrl;
private final String basicAuth; private final String basicAuth;
private final boolean noVerify;
private final boolean couchIgnoreAttachments; private final boolean couchIgnoreAttachments;
private final String indexName; private final String indexName;
@ -102,6 +107,8 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
if (settings.settings().containsKey("couchdb")) { if (settings.settings().containsKey("couchdb")) {
Map<String, Object> couchSettings = (Map<String, Object>) settings.settings().get("couchdb"); Map<String, Object> couchSettings = (Map<String, Object>) settings.settings().get("couchdb");
couchProtocol = XContentMapValues.nodeStringValue(couchSettings.get("protocol"), "http");
noVerify = XContentMapValues.nodeBooleanValue(couchSettings.get("no_verify"), false);
couchHost = XContentMapValues.nodeStringValue(couchSettings.get("host"), "localhost"); couchHost = XContentMapValues.nodeStringValue(couchSettings.get("host"), "localhost");
couchPort = XContentMapValues.nodeIntegerValue(couchSettings.get("port"), 5984); couchPort = XContentMapValues.nodeIntegerValue(couchSettings.get("port"), 5984);
couchDb = XContentMapValues.nodeStringValue(couchSettings.get("db"), riverName.name()); couchDb = XContentMapValues.nodeStringValue(couchSettings.get("db"), riverName.name());
@ -135,12 +142,14 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
script = null; script = null;
} }
} else { } else {
couchProtocol = "http";
couchHost = "localhost"; couchHost = "localhost";
couchPort = 5984; couchPort = 5984;
couchDb = "db"; couchDb = "db";
couchFilter = null; couchFilter = null;
couchFilterParamsUrl = null; couchFilterParamsUrl = null;
couchIgnoreAttachments = false; couchIgnoreAttachments = false;
noVerify = false;
basicAuth = null; basicAuth = null;
script = null; script = null;
} }
@ -438,13 +447,24 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
HttpURLConnection connection = null; HttpURLConnection connection = null;
InputStream is = null; InputStream is = null;
try { try {
URL url = new URL("http", couchHost, couchPort, file); URL url = new URL(couchProtocol, couchHost, couchPort, file);
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
if (basicAuth != null) { if (basicAuth != null) {
connection.addRequestProperty("Authorization", basicAuth); connection.addRequestProperty("Authorization", basicAuth);
} }
connection.setDoInput(true); connection.setDoInput(true);
connection.setUseCaches(false); connection.setUseCaches(false);
if (noVerify) {
((HttpsURLConnection)connection).setHostnameVerifier(
new HostnameVerifier(){
public boolean verify(String string, SSLSession ssls) {
return true;
}
}
);
}
is = connection.getInputStream(); is = connection.getInputStream();
final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));