Search in sources :

Example 51 with XContentType

use of org.graylog.shaded.elasticsearch7.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.graylog.shaded.elasticsearch7.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)

Example 53 with XContentType

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

the class BulkItemResponseTests method testFailureToAndFromXContent.

public void testFailureToAndFromXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    int itemId = randomIntBetween(0, 100);
    String index = randomAsciiOfLength(5);
    String type = randomAsciiOfLength(5);
    String id = randomAsciiOfLength(5);
    DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values());
    final Tuple<Throwable, ElasticsearchException> exceptions = randomExceptions();
    Exception bulkItemCause = (Exception) exceptions.v1();
    Failure bulkItemFailure = new Failure(index, type, id, bulkItemCause);
    BulkItemResponse bulkItemResponse = new BulkItemResponse(itemId, opType, bulkItemFailure);
    Failure expectedBulkItemFailure = new Failure(index, type, id, exceptions.v2(), ExceptionsHelper.status(bulkItemCause));
    BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(itemId, opType, expectedBulkItemFailure);
    BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, randomBoolean());
    // Shuffle the XContent fields
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    BulkItemResponse parsedBulkItemResponse;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsedBulkItemResponse = BulkItemResponse.fromXContent(parser, itemId);
        assertNull(parser.nextToken());
    }
    assertBulkItemResponse(expectedBulkItemResponse, parsedBulkItemResponse);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 54 with XContentType

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

the class DeleteResponseTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    final Tuple<DeleteResponse, DeleteResponse> tuple = randomDeleteResponse();
    DeleteResponse deleteResponse = tuple.v1();
    DeleteResponse expectedDeleteResponse = tuple.v2();
    boolean humanReadable = randomBoolean();
    final XContentType xContentType = randomFrom(XContentType.values());
    BytesReference deleteResponseBytes = toXContent(deleteResponse, xContentType, humanReadable);
    // Shuffle the XContent fields
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), deleteResponseBytes)) {
            deleteResponseBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    // Parse the XContent bytes to obtain a parsed DeleteResponse
    DeleteResponse parsedDeleteResponse;
    try (XContentParser parser = createParser(xContentType.xContent(), deleteResponseBytes)) {
        parsedDeleteResponse = DeleteResponse.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    // We can't use equals() to compare the original and the parsed delete response
    // because the random delete response can contain shard failures with exceptions,
    // and those exceptions are not parsed back with the same types.
    assertDocWriteResponse(expectedDeleteResponse, parsedDeleteResponse);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 55 with XContentType

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

the class GetResponseTests method testToAndFromXContent.

public void testToAndFromXContent() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
    GetResponse getResponse = new GetResponse(tuple.v1());
    GetResponse expectedGetResponse = new GetResponse(tuple.v2());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(getResponse, xContentType, humanReadable);
    //test that we can parse what we print out
    GetResponse parsedGetResponse;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        parsedGetResponse = GetResponse.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    assertEquals(expectedGetResponse, parsedGetResponse);
    //print the parsed object out and test that the output is the same as the original output
    BytesReference finalBytes = toXContent(parsedGetResponse, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
    //check that the source stays unchanged, no shuffling of keys nor anything like that
    assertEquals(expectedGetResponse.getSourceAsString(), parsedGetResponse.getSourceAsString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) GetResultTests.mutateGetResult(org.elasticsearch.index.get.GetResultTests.mutateGetResult) GetResult(org.elasticsearch.index.get.GetResult) GetResultTests.randomGetResult(org.elasticsearch.index.get.GetResultTests.randomGetResult) GetResultTests.copyGetResult(org.elasticsearch.index.get.GetResultTests.copyGetResult) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)86 BytesReference (org.elasticsearch.common.bytes.BytesReference)50 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 Map (java.util.Map)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)14 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 ArrayList (java.util.ArrayList)6 Collections.emptyMap (java.util.Collections.emptyMap)6 List (java.util.List)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