private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } }
scriptedUpsert = parser.booleanValue(); } else if ("upsert".equals(currentFieldName)) { XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()); builder.copyCurrentStructure(parser); safeUpsertRequest().source(builder); } else if ("doc".equals(currentFieldName)) { XContentBuilder docBuilder = XContentFactory.contentBuilder(parser.contentType()); docBuilder.copyCurrentStructure(parser); safeDoc().source(docBuilder);
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
@Override public XContentType contentType() { return parser.contentType(); }
/** * * @param parser the parser for the XContent that contains the serialized GetPipelineResponse. * @return an instance of GetPipelineResponse read from the parser * @throws IOException If the parsing fails */ public static GetPipelineResponse fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); List<PipelineConfiguration> pipelines = new ArrayList<>(); while(parser.nextToken().equals(Token.FIELD_NAME)) { String pipelineId = parser.currentName(); parser.nextToken(); XContentBuilder contentBuilder = XContentBuilder.builder(parser.contentType().xContent()); contentBuilder.generator().copyCurrentStructure(parser); PipelineConfiguration pipeline = new PipelineConfiguration( pipelineId, BytesReference.bytes(contentBuilder), contentBuilder.contentType() ); pipelines.add(pipeline); } ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation); return new GetPipelineResponse(pipelines); }
static ContextSetup parse(XContentParser parser, Void context) throws IOException { ContextSetup contextSetup = PARSER.parse(parser, null); contextSetup.setXContentType(parser.contentType()); return contextSetup; }
private static XContentParser transform(Mapping mapping, XContentParser parser) throws IOException { Map<String, Object> transformed; try (XContentParser ignored = parser) { transformed = transformSourceAsMap(mapping, parser.mapOrdered()); } XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()).value(transformed); return parser.contentType().xContent().createParser(builder.bytes()); }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return builder.bytes(); } }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } }
/** * We can't send parseSearchRequest REST content that it doesn't support * so we will have to remove the content that is valid in addition to * what it supports from the content first. This is a temporary hack and * should get better when SearchRequest has full ObjectParser support * then we can delegate and stuff. */ private XContentParser extractRequestSpecificFields(RestRequest restRequest, Map<String, Consumer<Object>> bodyConsumers) throws IOException { if (restRequest.hasContentOrSourceParam() == false) { return null; // body is optional } try (XContentParser parser = restRequest.contentOrSourceParamParser(); XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType())) { Map<String, Object> body = parser.map(); for (Map.Entry<String, Consumer<Object>> consumer : bodyConsumers.entrySet()) { Object value = body.remove(consumer.getKey()); if (value != null) { consumer.getValue().accept(value); } } return parser.contentType().xContent().createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), BytesReference.bytes(builder.map(body)).streamInput()); } } }
/** * Randomly shuffles the fields inside objects parsed using the {@link XContentParser} passed in. * Recursively goes through inner objects and also shuffles them. Exceptions for this * recursive shuffling behavior can be made by passing in the names of fields which * internally should stay untouched. */ public XContentBuilder shuffleXContent(XContentParser parser, boolean prettyPrint, String... exceptFieldNames) throws IOException { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()); if (prettyPrint) { xContentBuilder.prettyPrint(); } Token token = parser.currentToken() == null ? parser.nextToken() : parser.currentToken(); if (token == Token.START_ARRAY) { List<Object> shuffledList = shuffleList(parser.listOrderedMap(), new HashSet<>(Arrays.asList(exceptFieldNames))); return xContentBuilder.value(shuffledList); } //we need a sorted map for reproducibility, as we are going to shuffle its keys and write XContent back Map<String, Object> shuffledMap = shuffleMap((LinkedHashMap<String, Object>)parser.mapOrdered(), new HashSet<>(Arrays.asList(exceptFieldNames))); return xContentBuilder.map(shuffledMap); }
@Override protected String parseInlineScript(XContentParser parser) throws IOException { if (parser.currentToken() == XContentParser.Token.START_OBJECT) { contentType = parser.contentType(); XContentBuilder builder = XContentFactory.contentBuilder(contentType); return builder.copyCurrentStructure(parser).bytes().toUtf8(); } else { return parser.text(); } }
scriptedUpsert = parser.booleanValue(); } else if ("upsert".equals(currentFieldName)) { XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()); builder.copyCurrentStructure(parser); safeUpsertRequest().source(builder); } else if ("doc".equals(currentFieldName)) { XContentBuilder docBuilder = XContentFactory.contentBuilder(parser.contentType()); docBuilder.copyCurrentStructure(parser); safeDoc().source(docBuilder);
private void parseSource(XContentParser parser, SearchRequest search) throws IOException { /* * Extract the parameters that we need from the parser. We could do * away with this hack when search source has an ObjectParser. */ Map<String, Object> source = parser.map(); String[] indices = extractStringArray(source, "index"); if (indices != null) { search.indices(indices); } String[] types = extractStringArray(source, "type"); if (types != null) { search.types(types); } XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()); builder.map(source); search.extraSource(builder); }
private void parseSource(XContentParser parser, SearchRequest search) throws IOException { /* * Extract the parameters that we need from the parser. We could do * away with this hack when search source has an ObjectParser. */ Map<String, Object> source = parser.map(); String[] indices = extractStringArray(source, "index"); if (indices != null) { search.indices(indices); } String[] types = extractStringArray(source, "type"); if (types != null) { search.types(types); } XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()); builder.map(source); search.extraSource(builder); }
/** * * @param parser the parser for the XContent that contains the serialized GetPipelineResponse. * @return an instance of GetPipelineResponse read from the parser * @throws IOException If the parsing fails */ public static GetPipelineResponse fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); List<PipelineConfiguration> pipelines = new ArrayList<>(); while(parser.nextToken().equals(Token.FIELD_NAME)) { String pipelineId = parser.currentName(); parser.nextToken(); XContentBuilder contentBuilder = XContentBuilder.builder(parser.contentType().xContent()); contentBuilder.generator().copyCurrentStructure(parser); PipelineConfiguration pipeline = new PipelineConfiguration( pipelineId, BytesReference.bytes(contentBuilder), contentBuilder.contentType() ); pipelines.add(pipeline); } ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation); return new GetPipelineResponse(pipelines); }
public static SearchTemplateResponse fromXContent(XContentParser parser) throws IOException { SearchTemplateResponse searchTemplateResponse = new SearchTemplateResponse(); Map<String, Object> contentAsMap = parser.map(); if (contentAsMap.containsKey(TEMPLATE_OUTPUT_FIELD.getPreferredName())) { Object source = contentAsMap.get(TEMPLATE_OUTPUT_FIELD.getPreferredName()); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON) .value(source); searchTemplateResponse.setSource(BytesReference.bytes(builder)); } else { XContentType contentType = parser.contentType(); XContentBuilder builder = XContentFactory.contentBuilder(contentType) .map(contentAsMap); XContentParser searchResponseParser = contentType.xContent().createParser( parser.getXContentRegistry(), parser.getDeprecationHandler(), BytesReference.bytes(builder).streamInput()); SearchResponse searchResponse = SearchResponse.fromXContent(searchResponseParser); searchTemplateResponse.setResponse(searchResponse); } return searchTemplateResponse; }
/** * * @param parser the parser for the XContent that contains the serialized GetPipelineResponse. * @return an instance of GetPipelineResponse read from the parser * @throws IOException If the parsing fails */ public static GetPipelineResponse fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); List<PipelineConfiguration> pipelines = new ArrayList<>(); while(parser.nextToken().equals(Token.FIELD_NAME)) { String pipelineId = parser.currentName(); parser.nextToken(); XContentBuilder contentBuilder = XContentBuilder.builder(parser.contentType().xContent()); contentBuilder.generator().copyCurrentStructure(parser); PipelineConfiguration pipeline = new PipelineConfiguration( pipelineId, BytesReference.bytes(contentBuilder), contentBuilder.contentType() ); pipelines.add(pipeline); } ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation); return new GetPipelineResponse(pipelines); }
public static SearchTemplateResponse fromXContent(XContentParser parser) throws IOException { SearchTemplateResponse searchTemplateResponse = new SearchTemplateResponse(); Map<String, Object> contentAsMap = parser.map(); if (contentAsMap.containsKey(TEMPLATE_OUTPUT_FIELD.getPreferredName())) { Object source = contentAsMap.get(TEMPLATE_OUTPUT_FIELD.getPreferredName()); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON) .value(source); searchTemplateResponse.setSource(BytesReference.bytes(builder)); } else { XContentType contentType = parser.contentType(); XContentBuilder builder = XContentFactory.contentBuilder(contentType) .map(contentAsMap); XContentParser searchResponseParser = contentType.xContent().createParser( parser.getXContentRegistry(), parser.getDeprecationHandler(), BytesReference.bytes(builder).streamInput()); SearchResponse searchResponse = SearchResponse.fromXContent(searchResponseParser); searchTemplateResponse.setResponse(searchResponse); } return searchTemplateResponse; }