private static String convertToYaml(String type, BytesReference bytes, boolean prettyPrint) throws IOException { try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, bytes.streamInput())) { parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { return null; } parser.nextToken(); XContentBuilder builder = XContentFactory.yamlBuilder(); if (prettyPrint) { builder.prettyPrint(); } builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML); return Strings.toString(builder); } }
/** * Makes sure that current token is of type {@link Token#FIELD_NAME} and the field name is equal to the provided one * @throws ParsingException if the token is not of type {@link Token#FIELD_NAME} or is not equal to the given field name */ public static void ensureFieldName(XContentParser parser, Token token, String fieldName) throws IOException { ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation); String currentName = parser.currentName(); if (currentName.equals(fieldName) == false) { String message = "Failed to parse object: expecting field with name [%s] but found [%s]"; throw new ParsingException(parser.getTokenLocation(), String.format(Locale.ROOT, message, fieldName, currentName)); } }
public static NestedAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { String path = null; XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.VALUE_STRING) { if (NestedAggregator.PATH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { path = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); } } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); } } if (path == null) { // "field" doesn't exist, so we fall back to the context of the ancestors throw new ParsingException(parser.getTokenLocation(), "Missing [path] field for nested aggregation [" + aggregationName + "]"); } return new NestedAggregationBuilder(aggregationName, path); }
private static Map<String, Set<String>> parseContexts(XContentParser parser) throws IOException { Map<String, Set<String>> contexts = new HashMap<>(); while((parser.nextToken()) != XContentParser.Token.END_OBJECT) { ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); String key = parser.currentName(); ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser::getTokenLocation); Set<String> values = new HashSet<>(); while((parser.nextToken()) != XContentParser.Token.END_ARRAY) { ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.currentToken(), parser::getTokenLocation); values.add(parser.text()); } contexts.put(key, values); } return contexts; }
@Override public SignificanceHeuristic parse(XContentParser parser) throws IOException, QueryShardException { String givenName = parser.currentName(); boolean backgroundIsSuperset = true; XContentParser.Token token = parser.nextToken(); while (!token.equals(XContentParser.Token.END_OBJECT)) { if (BACKGROUND_IS_SUPERSET.match(parser.currentName(), parser.getDeprecationHandler())) { parser.nextToken(); backgroundIsSuperset = parser.booleanValue(); } else { throw new ElasticsearchParseException("failed to parse [{}] significance heuristic. unknown field [{}]", givenName, parser.currentName()); } token = parser.nextToken(); } return newHeuristic(true, backgroundIsSuperset); } };
public static BulkResponse fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.nextToken(); ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation); List<BulkItemResponse> items = new ArrayList<>(); String currentFieldName = parser.currentName(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (TOOK.equals(currentFieldName)) { took = parser.longValue(); ingestTook = parser.longValue(); } else if (ERRORS.equals(currentFieldName) == false) { throwUnknownField(currentFieldName, parser.getTokenLocation()); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { items.add(BulkItemResponse.fromXContent(parser, items.size())); throwUnknownField(currentFieldName, parser.getTokenLocation()); throwUnknownToken(token, parser.getTokenLocation());
public static WrapperQueryBuilder fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed"); } String fieldName = parser.currentName(); if (! QUERY_FIELD.match(fieldName, parser.getDeprecationHandler())) { throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed, expected `query` but was " + fieldName); } parser.nextToken(); byte[] source = parser.binaryValue(); parser.nextToken(); if (source == null) { throw new ParsingException(parser.getTokenLocation(), "wrapper query has no [query] specified"); } return new WrapperQueryBuilder(source); }
public static SnapshotId fromXContent(XContentParser parser) throws IOException { // the new format from 5.0 which contains the snapshot name and uuid if (parser.currentToken() == XContentParser.Token.START_OBJECT) { String name = null; String uuid = null; while (parser.nextToken() != XContentParser.Token.END_OBJECT) { String currentFieldName = parser.currentName(); parser.nextToken(); if (NAME.equals(currentFieldName)) { name = parser.text(); } else if (UUID.equals(currentFieldName)) { uuid = parser.text(); } } return new SnapshotId(name, uuid); } else { // the old format pre 5.0 that only contains the snapshot name, use the name as the uuid too final String name = parser.text(); return new SnapshotId(name, name); } }
parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { log.error("Cannot parse config for type {} because {}!={}", type, type, parser.currentName()); return null; parser.nextToken();
@Override public SignificanceHeuristic parse(XContentParser parser) throws IOException, QueryShardException { String givenName = parser.currentName(); boolean includeNegatives = false; boolean backgroundIsSuperset = true; XContentParser.Token token = parser.nextToken(); while (!token.equals(XContentParser.Token.END_OBJECT)) { if (INCLUDE_NEGATIVES_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) { parser.nextToken(); includeNegatives = parser.booleanValue(); } else if (BACKGROUND_IS_SUPERSET.match(parser.currentName(), parser.getDeprecationHandler())) { parser.nextToken(); backgroundIsSuperset = parser.booleanValue(); } else { throw new ElasticsearchParseException("failed to parse [{}] significance heuristic. unknown field [{}]", givenName, parser.currentName()); } token = parser.nextToken(); } return newHeuristic(includeNegatives, backgroundIsSuperset); }
public static SpanGapQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; int width = 0; String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; } else if (token.isValue()) { width = parser.intValue(); } } SpanGapQueryBuilder result = new SpanGapQueryBuilder(fieldName, width); return result; }
public static ReverseNestedAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { String path = null; XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.VALUE_STRING) { if ("path".equals(currentFieldName)) { path = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); } } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); } } ReverseNestedAggregationBuilder factory = new ReverseNestedAggregationBuilder( aggregationName); if (path != null) { factory.path(path); } return factory; }
private static void parseCompoundSortField(XContentParser parser, List<SortBuilder<?>> sortFields) throws IOException { XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { String fieldName = parser.currentName(); token = parser.nextToken(); if (token == XContentParser.Token.VALUE_STRING) { SortOrder order = SortOrder.fromString(parser.text()); sortFields.add(fieldOrScoreSort(fieldName).order(order)); } else { if (PARSERS.containsKey(fieldName)) { sortFields.add(PARSERS.get(fieldName).fromXContent(parser, fieldName)); } else { sortFields.add(FieldSortBuilder.fromXContent(parser, fieldName)); } } } } }
parser.nextToken(); parser.nextToken(); if(!id.equals((parser.currentName()))) { log.error("Cannot parse config for type {} because {}!={}", id, id, parser.currentName()); return null; parser.nextToken();
public static SmoothingModel fromXContent(XContentParser parser) throws IOException { XContentParser.Token token; String fieldName = null; double alpha = DEFAULT_LAPLACE_ALPHA; while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } if (token.isValue() && ALPHA_FIELD.match(fieldName, parser.getDeprecationHandler())) { alpha = parser.doubleValue(); } } return new Laplace(alpha); }
IndexBoost(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_OBJECT) { token = parser.nextToken(); if (token == XContentParser.Token.FIELD_NAME) { index = parser.currentName(); } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] in [" + INDICES_BOOST_FIELD + "] but found [" + token + "]", parser.getTokenLocation()); } token = parser.nextToken(); if (token == XContentParser.Token.VALUE_NUMBER) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_NUMBER + "] in [" + INDICES_BOOST_FIELD + "] but found [" + token + "]", parser.getTokenLocation()); } token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.END_OBJECT + "] in [" + INDICES_BOOST_FIELD + "] but found [" + token + "]", parser.getTokenLocation()); } } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] in [" + parser.currentName() + "] but found [" + token + "]", parser.getTokenLocation()); } }
/** * Parse a search scroll request from a request body provided through the REST layer. * Values that are already be set and are also found while parsing will be overridden. */ public void fromXContent(XContentParser parser) throws IOException { if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new IllegalArgumentException("Malformed content, must start with an object"); } else { XContentParser.Token token; String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) { scrollId(parser.text()); } else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) { scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll"))); } else { throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] "); } } } } }
private static String skipTemplateName(XContentParser parser) throws IOException { XContentParser.Token token = parser.nextToken(); if (token != null && token == XContentParser.Token.START_OBJECT) { token = parser.nextToken(); if (token == XContentParser.Token.FIELD_NAME) { String currentFieldName = parser.currentName(); if (VALID_FIELDS.contains(currentFieldName)) { return currentFieldName; } else { // we just hit the template name, which should be ignored and we move on parser.nextToken(); } } } return null; } }
public static SmoothingModel fromXContent(XContentParser parser) throws IOException { XContentParser.Token token; String fieldName = null; double discount = DEFAULT_BACKOFF_DISCOUNT; while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } if (token.isValue() && DISCOUNT_FIELD.match(fieldName, parser.getDeprecationHandler())) { discount = parser.doubleValue(); } } return new StupidBackoff(discount); }
public static HighlightField fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); String fieldName = parser.currentName(); Text[] fragments = null; XContentParser.Token token = parser.nextToken(); if (token == XContentParser.Token.START_ARRAY) { List<Text> values = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { values.add(new Text(parser.text())); } fragments = values.toArray(new Text[values.size()]); } else if (token == XContentParser.Token.VALUE_NULL) { fragments = null; } else { throw new ParsingException(parser.getTokenLocation(), "unexpected token type [" + token + "]"); } return new HighlightField(fieldName, fragments); }