Support http auth in couchdb river plugin, closes #390.

This commit is contained in:
kimchy 2010-09-30 20:58:04 +02:00
parent 735ad0d43d
commit 1af53bce0d
1 changed files with 13 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.action.bulk.BulkRequestBuilder; import org.elasticsearch.client.action.bulk.BulkRequestBuilder;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.EsExecutors; 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 couchDb;
private final String couchFilter; private final String couchFilter;
private final String couchFilterParamsUrl; private final String couchFilterParamsUrl;
private final String basicAuth;
private final String indexName; private final String indexName;
private final String typeName; private final String typeName;
@ -97,12 +99,20 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
} else { } else {
couchFilterParamsUrl = null; 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 { } else {
couchHost = "localhost"; couchHost = "localhost";
couchPort = 5984; couchPort = 5984;
couchDb = "db"; couchDb = "db";
couchFilter = null; couchFilter = null;
couchFilterParamsUrl = null; couchFilterParamsUrl = null;
basicAuth = null;
} }
if (settings.settings().containsKey("index")) { if (settings.settings().containsKey("index")) {
@ -293,6 +303,9 @@ public class CouchdbRiver extends AbstractRiverComponent implements River {
try { try {
URL url = new URL("http", couchHost, couchPort, file); URL url = new URL("http", couchHost, couchPort, file);
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
if (basicAuth != null) {
connection.addRequestProperty("Authorization", basicAuth);
}
connection.setDoInput(true); connection.setDoInput(true);
connection.setUseCaches(false); connection.setUseCaches(false);
is = connection.getInputStream(); is = connection.getInputStream();