io.searchbox.action.AbstractAction Maven / Gradle / Ivy
package io.searchbox.action;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.searchbox.annotations.JestId;
import io.searchbox.client.JestResult;
import io.searchbox.params.Parameters;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @author Dogukan Sonmez
* @author cihat keser
*/
public abstract class AbstractAction implements Action {
public static String CHARSET = "utf-8";
protected final static Logger log = LoggerFactory.getLogger(AbstractAction.class);
protected String indexName;
protected String typeName;
protected String nodes;
protected Object payload;
private final ConcurrentMap headerMap = new ConcurrentHashMap();
private final Multimap parameterMap = LinkedHashMultimap.create();
private final Set cleanApiParameters = new LinkedHashSet();
private String URI;
public AbstractAction() {
}
@SuppressWarnings("unchecked")
public AbstractAction(Builder builder) {
parameterMap.putAll(builder.parameters);
headerMap.putAll(builder.headers);
cleanApiParameters.addAll(builder.cleanApiParameters);
if (builder instanceof AbstractMultiIndexActionBuilder) {
indexName = ((AbstractMultiIndexActionBuilder) builder).getJoinedIndices();
if (builder instanceof AbstractMultiTypeActionBuilder) {
indexName = ((AbstractMultiTypeActionBuilder) builder).getJoinedIndices();
typeName = ((AbstractMultiTypeActionBuilder) builder).getJoinedTypes();
}
} else if (builder instanceof AbstractMultiINodeActionBuilder) {
nodes = ((AbstractMultiINodeActionBuilder) builder).getJoinedNodes();
}
}
protected T createNewElasticSearchResult(T result, String responseBody, int statusCode, String reasonPhrase, Gson gson) {
JsonObject jsonMap = parseResponseBody(responseBody);
result.setResponseCode(statusCode);
result.setJsonString(responseBody);
result.setJsonObject(jsonMap);
result.setPathToResult(getPathToResult());
if (isHttpSuccessful(statusCode)) {
result.setSucceeded(true);
log.debug("Request and operation succeeded");
} else {
result.setSucceeded(false);
// provide the generic HTTP status code error, if one hasn't already come in via the JSON response...
// eg.
// IndicesExist will return 404 (with no content at all) for a missing index, but:
// Update will return 404 (with an error message for DocumentMissingException)
if (result.getErrorMessage() == null) {
result.setErrorMessage(statusCode + " " + (reasonPhrase == null ? "null" : reasonPhrase));
}
log.debug("Response is failed. errorMessage is " + result.getErrorMessage());
}
return result;
}
protected boolean isHttpSuccessful(int httpCode) {
return (httpCode / 100) == 2;
}
protected JsonObject parseResponseBody(String responseBody) {
if (responseBody != null && !responseBody.trim().isEmpty()) {
return new JsonParser().parse(responseBody).getAsJsonObject();
}
return new JsonObject();
}
public static String getIdFromSource(Object source) {
if (source == null) return null;
Field[] fields = source.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(JestId.class)) {
try {
field.setAccessible(true);
Object name = field.get(source);
return name == null ? null : name.toString();
} catch (IllegalAccessException e) {
log.error("Unhandled exception occurred while getting annotated id from source", e);
}
}
}
return null;
}
public Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy