org.elasticsearch.hadoop.rest.HeaderProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch-hadoop-cascading Show documentation
Show all versions of elasticsearch-hadoop-cascading Show documentation
Elasticsearch Hadoop Cascading
The newest version!
package org.elasticsearch.hadoop.rest;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.hadoop.EsHadoopIllegalArgumentException;
import org.elasticsearch.hadoop.cfg.Settings;
import org.elasticsearch.hadoop.util.Assert;
import org.elasticsearch.hadoop.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.hadoop.cfg.ConfigurationOptions.ES_NET_HTTP_HEADER_PREFIX;
/**
* Pulls HTTP header information from Configurations, validates them, joins them with connector defaults, and
* applies them to HTTP Operations.
*/
public final class HeaderProcessor {
private static final Log LOG = LogFactory.getLog(HeaderProcessor.class);
/**
* Headers that are reserved and should not be specified from outside of the connector code.
*/
private enum ReservedHeaders {
/**
* Content-Type HTTP Header should not be set by user as all requests made to Elasticsearch are done
* in JSON format, or the _bulk api compliant line delimited JSON.
*/
CONTENT_TYPE("Content-Type", "application/json", "ES-Hadoop communicates in JSON format only"),
/*
* Elasticsearch also supports line-delimited JSON for '_bulk' operations which isn't
* official and has a few variations:
* * http://specs.okfnlabs.org/ndjson/
* * https://github.com/ndjson/ndjson-spec/blob/48ea03cea6796b614cfbff4d4eb921f0b1d35c26/specification.md
*
* The '_bulk' operation will still work with "application/json", but as a note, the suggested MIME types
* for line delimited json are:
* * "application/x-ldjson"
* * "application/x-ndjson"
*/
/**
* Accept HTTP Header should not be set by user as all responses from Elasticsearch are expected to be
* in JSON format.
*/
ACCEPT("Accept", "application/json", "ES-Hadoop communicates in JSON format only");
private static final Map NAME_MAP = new HashMap();
static {
for (ReservedHeaders reservedHeaders : ReservedHeaders.values()) {
NAME_MAP.put(reservedHeaders.name, reservedHeaders);
}
}
/**
* @return Map of ReservedHeaders, keyed by header name.
*/
public static Map byName() {
return NAME_MAP;
}
private final String name;
private final String defaultValue;
private final String reasonReserved;
ReservedHeaders(String name, String defaultValue, String reasonReserved) {
this.name = name;
this.defaultValue = defaultValue;
this.reasonReserved = reasonReserved;
}
public String getName() {
return name;
}
public String getDefaultValue() {
return defaultValue;
}
public String getReasonReserved() {
return reasonReserved;
}
@Override
public String toString() {
return "ReservedHeaders{" +
"name='" + name + '\'' +
'}';
}
}
private final List headers;
public HeaderProcessor(Settings settings) {
Map workingHeaders = new HashMap();
for (Map.Entry