Search in sources :

Example 51 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class RestRequest method withContentOrSourceParamParserOrNull.

/**
     * Call a consumer with the parser for the contents of this request if it has contents, otherwise with a parser for the {@code source}
     * parameter if there is one, otherwise with {@code null}. Use {@link #contentOrSourceParamParser()} if you should throw an exception
     * back to the user when there isn't request content.
     */
public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentParser, IOException> withParser) throws IOException {
    Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
    BytesReference content = tuple.v2();
    XContentType xContentType = tuple.v1();
    if (content.length() > 0) {
        try (XContentParser parser = xContentType.xContent().createParser(xContentRegistry, content)) {
            withParser.accept(parser);
        }
    } else {
        withParser.accept(null);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 52 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class JsonXContentGenerator method writeRawField.

@Override
public final void writeRawField(String name, BytesReference content) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    writeRawField(name, content, contentType);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 53 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class JsonXContentGenerator method writeRawValue.

@Override
public final void writeRawValue(BytesReference content) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    writeRawValue(content, contentType);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 54 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class RestPutPipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    Tuple<XContentType, BytesReference> sourceTuple = restRequest.contentOrSourceParam();
    PutPipelineRequest request = new PutPipelineRequest(restRequest.param("id"), sourceTuple.v2(), sourceTuple.v1());
    request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout()));
    request.timeout(restRequest.paramAsTime("timeout", request.timeout()));
    return channel -> client.admin().cluster().putPipeline(request, new AcknowledgedRestListener<>(channel));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Settings(org.elasticsearch.common.settings.Settings) XContentType(org.elasticsearch.common.xcontent.XContentType) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) BytesReference(org.elasticsearch.common.bytes.BytesReference) Tuple(org.elasticsearch.common.collect.Tuple) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) XContentType(org.elasticsearch.common.xcontent.XContentType) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest)

Example 55 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class RestRequestTests method testContentTypeParsing.

public void testContentTypeParsing() {
    for (XContentType xContentType : XContentType.values()) {
        Map<String, List<String>> map = new HashMap<>();
        map.put("Content-Type", Collections.singletonList(xContentType.mediaType()));
        ContentRestRequest restRequest = new ContentRestRequest("", Collections.emptyMap(), map);
        assertEquals(xContentType, restRequest.getXContentType());
        map = new HashMap<>();
        map.put("Content-Type", Collections.singletonList(xContentType.mediaTypeWithoutParameters()));
        restRequest = new ContentRestRequest("", Collections.emptyMap(), map);
        assertEquals(xContentType, restRequest.getXContentType());
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)82 BytesReference (org.elasticsearch.common.bytes.BytesReference)48 XContentParser (org.elasticsearch.common.xcontent.XContentParser)42 Map (java.util.Map)19 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)19 HashMap (java.util.HashMap)18 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 BytesRef (org.apache.lucene.util.BytesRef)8 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 Collections.emptyMap (java.util.Collections.emptyMap)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5