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);
}
}
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());
}
}
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);
}
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);
}
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());
}
Aggregations