[REST] Add more utilitis for source/body handling in RestAction

This commit is contained in:
Simon Willnauer 2015-04-22 17:41:46 +02:00
parent d7eba0df59
commit b3ca94460c
13 changed files with 73 additions and 127 deletions

View File

@ -19,13 +19,19 @@
package org.elasticsearch.rest;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.*;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.FilterClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.action.support.RestActions;
import java.util.Set;

View File

@ -32,6 +32,7 @@ import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestToXContentListener;
import java.io.IOException;
@ -69,16 +70,16 @@ public class RestAnalyzeAction extends BaseRestHandler {
analyzeRequest.tokenFilters(request.paramAsStringArray("token_filters", request.paramAsStringArray("filters", analyzeRequest.tokenFilters())));
analyzeRequest.charFilters(request.paramAsStringArray("char_filters", analyzeRequest.charFilters()));
if (request.hasContent() || request.hasParam("source")) {
XContentType type = contentType(request);
if (RestActions.hasBodyContent(request)) {
XContentType type = RestActions.guessBodyContentType(request);
if (type == null) {
if (text == null) {
text = bodyContent(request).toUtf8();
text = RestActions.getRestContent(request).toUtf8();
analyzeRequest.text(text);
}
} else {
// NOTE: if rest request with xcontent body has request parameters, the parameters does not override xcontent values
buildFromContent(bodyContent(request), analyzeRequest);
buildFromContent(RestActions.getRestContent(request), analyzeRequest);
}
}
@ -132,27 +133,4 @@ public class RestAnalyzeAction extends BaseRestHandler {
throw new ElasticsearchIllegalArgumentException("Failed to parse request body", e);
}
}
private XContentType contentType(final RestRequest request) {
if (request.hasContent()) {
return XContentFactory.xContentType(request.content());
} else if (request.hasParam("source")) {
return XContentFactory.xContentType(request.param("source"));
}
throw new ElasticsearchIllegalArgumentException("Can't guess contentType neither source nor content available");
}
private BytesReference bodyContent(final RestRequest request) {
if (request.hasContent()) {
return request.content();
} else if (request.hasParam("source")) {
return new BytesArray(request.param("source"));
}
throw new ElasticsearchIllegalArgumentException("Can't guess contentType neither source nor content available");
}
}

View File

@ -59,17 +59,12 @@ public class RestValidateQueryAction extends BaseRestHandler {
ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
validateQueryRequest.listenerThreaded(false);
validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
if (request.hasContent()) {
validateQueryRequest.source(request.content());
if (RestActions.hasBodyContent(request)) {
validateQueryRequest.source(RestActions.getRestContent(request));
} else {
String source = request.param("source");
if (source != null) {
validateQueryRequest.source(source);
} else {
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
validateQueryRequest.source(querySourceBuilder);
}
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
validateQueryRequest.source(querySourceBuilder);
}
}
validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));

View File

@ -60,17 +60,12 @@ public class RestCountAction extends BaseRestHandler {
CountRequest countRequest = new CountRequest(Strings.splitStringByCommaToArray(request.param("index")));
countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
countRequest.listenerThreaded(false);
if (request.hasContent()) {
countRequest.source(request.content());
if (RestActions.hasBodyContent(request)) {
countRequest.source(RestActions.getRestContent(request));
} else {
String source = request.param("source");
if (source != null) {
countRequest.source(source);
} else {
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
countRequest.source(querySourceBuilder);
}
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
countRequest.source(querySourceBuilder);
}
}
countRequest.routing(request.param("routing"));

View File

@ -54,17 +54,12 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
deleteByQueryRequest.listenerThreaded(false);
if (request.hasContent()) {
deleteByQueryRequest.source(request.content());
if (RestActions.hasBodyContent(request)) {
deleteByQueryRequest.source(RestActions.getRestContent(request));
} else {
String source = request.param("source");
if (source != null) {
deleteByQueryRequest.source(source);
} else {
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
deleteByQueryRequest.source(querySourceBuilder);
}
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
deleteByQueryRequest.source(querySourceBuilder);
}
}
deleteByQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));

View File

@ -49,17 +49,12 @@ public class RestExistsAction extends BaseRestHandler {
final ExistsRequest existsRequest = new ExistsRequest(Strings.splitStringByCommaToArray(request.param("index")));
existsRequest.indicesOptions(IndicesOptions.fromRequest(request, existsRequest.indicesOptions()));
existsRequest.listenerThreaded(false);
if (request.hasContent()) {
existsRequest.source(request.content());
if (RestActions.hasBodyContent(request)) {
existsRequest.source(RestActions.getRestContent(request));
} else {
String source = request.param("source");
if (source != null) {
existsRequest.source(source);
} else {
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
existsRequest.source(querySourceBuilder);
}
QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
if (querySourceBuilder != null) {
existsRequest.source(querySourceBuilder);
}
}
existsRequest.routing(request.param("routing"));

View File

@ -35,6 +35,7 @@ import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import org.elasticsearch.search.fetch.source.FetchSourceContext;
@ -63,12 +64,9 @@ public class RestExplainAction extends BaseRestHandler {
explainRequest.parent(request.param("parent"));
explainRequest.routing(request.param("routing"));
explainRequest.preference(request.param("preference"));
String sourceString = request.param("source");
String queryString = request.param("q");
if (request.hasContent()) {
explainRequest.source(request.content());
} else if (sourceString != null) {
explainRequest.source(new BytesArray(request.param("source")));
if (RestActions.hasBodyContent(request)) {
explainRequest.source(RestActions.getRestContent(request));
} else if (queryString != null) {
QueryStringQueryBuilder queryStringBuilder = QueryBuilders.queryStringQuery(queryString);
queryStringBuilder.defaultField(request.param("df"));

View File

@ -57,15 +57,15 @@ public class RestClearScrollAction extends BaseRestHandler {
String scrollIds = request.param("scroll_id");
ClearScrollRequest clearRequest = new ClearScrollRequest();
clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds)));
if (request.hasContent()) {
XContentType type = XContentFactory.xContentType(request.content());
if (RestActions.hasBodyContent(request)) {
XContentType type = RestActions.guessBodyContentType(request);
if (type == null) {
scrollIds = RestActions.getRestContent(request).toUtf8();
clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds)));
} else {
// NOTE: if rest request with xcontent body has request parameters, these parameters does not override xcontent value
clearRequest.setScrollIds(null);
buildFromContent(request.content(), clearRequest);
buildFromContent(RestActions.getRestContent(request), clearRequest);
}
}

View File

@ -31,6 +31,7 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.exists.RestExistsAction;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestStatusToXContentListener;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
@ -87,20 +88,11 @@ public class RestSearchAction extends BaseRestHandler {
// get the content, and put it in the body
// add content/source as template if template flag is set
boolean isTemplateRequest = request.path().endsWith("/template");
if (request.hasContent()) {
if (RestActions.hasBodyContent(request)) {
if (isTemplateRequest) {
searchRequest.templateSource(request.content());
searchRequest.templateSource(RestActions.getRestContent(request));
} else {
searchRequest.source(request.content());
}
} else {
String source = request.param("source");
if (source != null) {
if (isTemplateRequest) {
searchRequest.templateSource(source);
} else {
searchRequest.source(source);
}
searchRequest.source(RestActions.getRestContent(request));
}
}

View File

@ -69,16 +69,16 @@ public class RestSearchScrollAction extends BaseRestHandler {
searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null)));
}
if (request.hasContent() || request.hasParam("source")) {
XContentType type = contentType(request);
if (RestActions.hasBodyContent(request)) {
XContentType type = XContentFactory.xContentType(RestActions.getRestContent(request));
if (type == null) {
if (scrollId == null) {
scrollId = bodyContent(request).toUtf8();
scrollId = RestActions.getRestContent(request).toUtf8();
searchScrollRequest.scrollId(scrollId);
}
} else {
// NOTE: if rest request with xcontent body has request parameters, these parameters override xcontent values
buildFromContent(bodyContent(request), searchScrollRequest);
buildFromContent(RestActions.getRestContent(request), searchScrollRequest);
}
}
client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<SearchResponse>(channel));
@ -107,24 +107,4 @@ public class RestSearchScrollAction extends BaseRestHandler {
throw new ElasticsearchIllegalArgumentException("Failed to parse request body", e);
}
}
private XContentType contentType(final RestRequest request) {
if (request.hasContent()) {
return XContentFactory.xContentType(request.content());
} else if (request.hasParam("source")) {
return XContentFactory.xContentType(request.param("source"));
}
throw new ElasticsearchIllegalArgumentException("Can't guess contentType neither source nor content available");
}
private BytesReference bodyContent(final RestRequest request) {
if (request.hasContent()) {
return request.content();
} else if (request.hasParam("source")) {
return new BytesArray(request.param("source"));
}
throw new ElasticsearchIllegalArgumentException("Can't guess contentType neither source nor content available");
}
}

View File

@ -38,6 +38,7 @@ import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import org.elasticsearch.search.suggest.Suggest;
@ -60,15 +61,10 @@ public class RestSuggestAction extends BaseRestHandler {
SuggestRequest suggestRequest = new SuggestRequest(Strings.splitStringByCommaToArray(request.param("index")));
suggestRequest.indicesOptions(IndicesOptions.fromRequest(request, suggestRequest.indicesOptions()));
suggestRequest.listenerThreaded(false);
if (request.hasContent()) {
suggestRequest.suggest(request.content());
if (RestActions.hasBodyContent(request)) {
suggestRequest.suggest(RestActions.getRestContent(request));
} else {
String source = request.param("source");
if (source != null) {
suggestRequest.suggest(source);
} else {
throw new ElasticsearchIllegalArgumentException("no content or source provided to execute suggestion");
}
throw new ElasticsearchIllegalArgumentException("no content or source provided to execute suggestion");
}
suggestRequest.routing(request.param("routing"));
suggestRequest.preference(request.param("preference"));

View File

@ -28,6 +28,8 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.rest.RestRequest;
@ -128,4 +130,24 @@ public class RestActions {
return content;
}
/**
* guesses the content type from either payload or source parameter
* @param request Rest request
* @return rest content type or <code>null</code> if not applicable.
*/
public static XContentType guessBodyContentType(final RestRequest request) {
final BytesReference restContent = RestActions.getRestContent(request);
if (restContent == null) {
return null;
}
return XContentFactory.xContentType(restContent);
}
/**
* Returns <code>true</code> if either payload or source parameter is present. Otherwise <code>false</code>
*/
public static boolean hasBodyContent(final RestRequest request) {
return request.hasContent() || request.hasParam("source");
}
}

View File

@ -65,15 +65,9 @@ public class RestTermVectorsAction extends BaseRestHandler {
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
TermVectorsRequest termVectorsRequest = new TermVectorsRequest(request.param("index"), request.param("type"), request.param("id"));
XContentParser parser = null;
if (request.hasContent()) {
try {
parser = XContentFactory.xContent(request.content()).createParser(request.content());
if (RestActions.hasBodyContent(request)) {
try (XContentParser parser = XContentFactory.xContent(RestActions.guessBodyContentType(request)).createParser(RestActions.getRestContent(request))){
TermVectorsRequest.parseRequest(termVectorsRequest, parser);
} finally {
if (parser != null) {
parser.close();
}
}
}
readURIParameters(termVectorsRequest, request);