use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.
the class OpenSearchTestCase method shuffleXContent.
/**
* 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 static 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);
}
use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.
the class GetAliasesResponse method fromXContent.
/**
* Parse the get aliases response
*/
public static GetAliasesResponse fromXContent(XContentParser parser) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
ensureExpectedToken(Token.START_OBJECT, parser.currentToken(), parser);
Map<String, Set<AliasMetadata>> aliases = new HashMap<>();
String currentFieldName;
Token token;
String error = null;
OpenSearchException exception = null;
RestStatus status = RestStatus.OK;
while (parser.nextToken() != Token.END_OBJECT) {
if (parser.currentToken() == Token.FIELD_NAME) {
currentFieldName = parser.currentName();
if ("status".equals(currentFieldName)) {
if ((token = parser.nextToken()) != Token.FIELD_NAME) {
ensureExpectedToken(Token.VALUE_NUMBER, token, parser);
status = RestStatus.fromCode(parser.intValue());
}
} else if ("error".equals(currentFieldName)) {
token = parser.nextToken();
if (token == Token.VALUE_STRING) {
error = parser.text();
} else if (token == Token.START_OBJECT) {
parser.nextToken();
exception = OpenSearchException.innerFromXContent(parser, true);
} else if (token == Token.START_ARRAY) {
parser.skipChildren();
}
} else {
String indexName = parser.currentName();
if (parser.nextToken() == Token.START_OBJECT) {
Set<AliasMetadata> parseInside = parseAliases(parser);
aliases.put(indexName, parseInside);
}
}
}
}
if (exception != null) {
assert error == null;
assert aliases.isEmpty();
return new GetAliasesResponse(status, exception);
}
return new GetAliasesResponse(status, error, aliases);
}
use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.
the class MultiGetRequest method add.
public MultiGetRequest add(@Nullable String defaultIndex, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, @Nullable String defaultRouting, XContentParser parser, boolean allowExplicitIndex) throws IOException {
Token token;
String currentFieldName = null;
if ((token = parser.nextToken()) != Token.START_OBJECT) {
final String message = String.format(Locale.ROOT, "unexpected token [%s], expected [%s]", token, Token.START_OBJECT);
throw new ParsingException(parser.getTokenLocation(), message);
}
while ((token = parser.nextToken()) != Token.END_OBJECT) {
if (token == Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == Token.START_ARRAY) {
if ("docs".equals(currentFieldName)) {
parseDocuments(parser, this.items, defaultIndex, defaultFields, defaultFetchSource, defaultRouting, allowExplicitIndex);
} else if ("ids".equals(currentFieldName)) {
parseIds(parser, this.items, defaultIndex, defaultFields, defaultFetchSource, defaultRouting);
} else {
final String message = String.format(Locale.ROOT, "unknown key [%s] for a %s, expected [docs] or [ids]", currentFieldName, token);
throw new ParsingException(parser.getTokenLocation(), message);
}
} else {
final String message = String.format(Locale.ROOT, "unexpected token [%s], expected [%s] or [%s]", token, Token.FIELD_NAME, Token.START_ARRAY);
throw new ParsingException(parser.getTokenLocation(), message);
}
}
return this;
}
use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.
the class ClusterAllocationExplainIT method verifyShardInfo.
private void verifyShardInfo(XContentParser parser, boolean primary, boolean includeDiskInfo, ShardRoutingState state) throws IOException {
parser.nextToken();
assertEquals(Token.START_OBJECT, parser.currentToken());
parser.nextToken();
assertEquals("index", parser.currentName());
parser.nextToken();
assertEquals("idx", parser.text());
parser.nextToken();
assertEquals("shard", parser.currentName());
parser.nextToken();
assertEquals(0, parser.intValue());
parser.nextToken();
assertEquals("primary", parser.currentName());
parser.nextToken();
assertEquals(primary, parser.booleanValue());
parser.nextToken();
assertEquals("current_state", parser.currentName());
parser.nextToken();
assertEquals(state.toString().toLowerCase(Locale.ROOT), parser.text());
if (state == ShardRoutingState.UNASSIGNED) {
parser.nextToken();
assertEquals("unassigned_info", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
Token token;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
// until we reach end of unassigned_info
if (token == XContentParser.Token.FIELD_NAME) {
// we should never display "delayed" from unassigned info
assertNotEquals("delayed", parser.currentName());
if (parser.currentName().equals("last_allocation_status")) {
parser.nextToken();
assertThat(parser.text(), is(oneOf(AllocationDecision.NO.toString(), AllocationDecision.NO_VALID_SHARD_COPY.toString(), AllocationDecision.AWAITING_INFO.toString(), AllocationDecision.NO_ATTEMPT.toString())));
}
}
}
} else {
assertEquals(ShardRoutingState.STARTED, state);
parser.nextToken();
assertEquals("current_node", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
Token token;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
// until we reach end of current_node
if (token == Token.FIELD_NAME) {
assertTrue(parser.currentName().equals("id") || parser.currentName().equals("name") || parser.currentName().equals("transport_address") || parser.currentName().equals("weight_ranking") || parser.currentName().equals("attributes"));
// Skip past attributes object
if (parser.currentName().equals("attributes")) {
while (!parser.nextToken().equals(Token.END_OBJECT)) {
parser.nextToken();
}
break;
}
} else {
assertTrue(token.isValue());
assertNotNull(parser.text());
}
}
}
if (includeDiskInfo) {
// disk info is included, just verify the object is there
parser.nextToken();
assertEquals("cluster_info", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
int numObjects = 1;
while (numObjects > 0) {
Token token = parser.nextToken();
if (token == Token.START_OBJECT) {
++numObjects;
} else if (token == Token.END_OBJECT) {
--numObjects;
}
}
}
}
use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.
the class IndicesOptions method fromXContent.
public static IndicesOptions fromXContent(XContentParser parser) throws IOException {
EnumSet<WildcardStates> wildcardStates = null;
Boolean allowNoIndices = null;
Boolean ignoreUnavailable = null;
boolean ignoreThrottled = false;
Token token = parser.currentToken() == Token.START_OBJECT ? parser.currentToken() : parser.nextToken();
String currentFieldName = null;
if (token != Token.START_OBJECT) {
throw new OpenSearchParseException("expected START_OBJECT as the token but was " + token);
}
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == Token.START_ARRAY) {
if (EXPAND_WILDCARDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
if (wildcardStates == null) {
wildcardStates = EnumSet.noneOf(WildcardStates.class);
while ((token = parser.nextToken()) != Token.END_ARRAY) {
if (token.isValue()) {
WildcardStates.updateSetForValue(wildcardStates, parser.text());
} else {
throw new OpenSearchParseException("expected values within array for " + EXPAND_WILDCARDS_FIELD.getPreferredName());
}
}
} else {
throw new OpenSearchParseException("already parsed expand_wildcards");
}
} else {
throw new OpenSearchParseException(EXPAND_WILDCARDS_FIELD.getPreferredName() + " is the only field that is an array in IndicesOptions");
}
} else if (token.isValue()) {
if (EXPAND_WILDCARDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
if (wildcardStates == null) {
wildcardStates = EnumSet.noneOf(WildcardStates.class);
WildcardStates.updateSetForValue(wildcardStates, parser.text());
} else {
throw new OpenSearchParseException("already parsed expand_wildcards");
}
} else if (IGNORE_UNAVAILABLE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
ignoreUnavailable = parser.booleanValue();
} else if (ALLOW_NO_INDICES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
allowNoIndices = parser.booleanValue();
} else if (IGNORE_THROTTLED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
ignoreThrottled = parser.booleanValue();
} else {
throw new OpenSearchParseException("could not read indices options. unexpected index option [" + currentFieldName + "]");
}
} else {
throw new OpenSearchParseException("could not read indices options. unexpected object field [" + currentFieldName + "]");
}
}
if (wildcardStates == null) {
throw new OpenSearchParseException("indices options xcontent did not contain " + EXPAND_WILDCARDS_FIELD.getPreferredName());
}
if (ignoreUnavailable == null) {
throw new OpenSearchParseException("indices options xcontent did not contain " + IGNORE_UNAVAILABLE_FIELD.getPreferredName());
}
if (allowNoIndices == null) {
throw new OpenSearchParseException("indices options xcontent did not contain " + ALLOW_NO_INDICES_FIELD.getPreferredName());
}
return IndicesOptions.fromOptions(ignoreUnavailable, allowNoIndices, wildcardStates.contains(WildcardStates.OPEN), wildcardStates.contains(WildcardStates.CLOSED), wildcardStates.contains(WildcardStates.HIDDEN), true, false, false, ignoreThrottled);
}
Aggregations