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