From 1af53bce0d18df485300ca4e1c16c1efefe09c65 Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 30 Sep 2010 20:58:04 +0200 Subject: [PATCH] Support http auth in couchdb river plugin, closes #390. --- .../elasticsearch/river/couchdb/CouchdbRiver.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/river/couchdb/src/main/java/org/elasticsearch/river/couchdb/CouchdbRiver.java b/plugins/river/couchdb/src/main/java/org/elasticsearch/river/couchdb/CouchdbRiver.java index 2773edc77ce..e2d0494a7d6 100644 --- a/plugins/river/couchdb/src/main/java/org/elasticsearch/river/couchdb/CouchdbRiver.java +++ b/plugins/river/couchdb/src/main/java/org/elasticsearch/river/couchdb/CouchdbRiver.java @@ -25,6 +25,7 @@ import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.action.bulk.BulkRequestBuilder; import org.elasticsearch.cluster.block.ClusterBlockException; +import org.elasticsearch.common.Base64; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; @@ -60,6 +61,7 @@ public class CouchdbRiver extends AbstractRiverComponent implements River { private final String couchDb; private final String couchFilter; private final String couchFilterParamsUrl; + private final String basicAuth; private final String indexName; private final String typeName; @@ -97,12 +99,20 @@ public class CouchdbRiver extends AbstractRiverComponent implements River { } else { couchFilterParamsUrl = null; } + if (couchSettings.containsKey("user") && couchSettings.containsKey("password")) { + String user = couchSettings.get("user").toString(); + String password = couchSettings.get("password").toString(); + basicAuth = "Basic " + Base64.encodeBytes((user + ":" + password).getBytes()); + } else { + basicAuth = null; + } } else { couchHost = "localhost"; couchPort = 5984; couchDb = "db"; couchFilter = null; couchFilterParamsUrl = null; + basicAuth = null; } if (settings.settings().containsKey("index")) { @@ -293,6 +303,9 @@ public class CouchdbRiver extends AbstractRiverComponent implements River { try { URL url = new URL("http", couchHost, couchPort, file); connection = (HttpURLConnection) url.openConnection(); + if (basicAuth != null) { + connection.addRequestProperty("Authorization", basicAuth); + } connection.setDoInput(true); connection.setUseCaches(false); is = connection.getInputStream();