@Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(Fields.SNAPSHOT); builder.field(Fields.NAME, name); builder.field(Fields.VERSION_ID, version.id); builder.startArray(Fields.INDICES); for (String index : indices) { builder.value(index); } builder.endArray(); builder.field(Fields.STATE, state); if (reason != null) { builder.field(Fields.REASON, reason); } builder.field(Fields.START_TIME, startTime); builder.field(Fields.END_TIME, endTime); builder.field(Fields.TOTAL_SHARDS, totalShard); builder.field(Fields.SUCCESSFUL_SHARDS, successfulShards); builder.startArray(Fields.FAILURES); for (SnapshotShardFailure shardFailure : shardFailures) { builder.startObject(); shardFailure.toXContent(builder, params); builder.endObject(); } builder.endArray(); builder.endObject(); return builder; }
public static Snapshot fromXContent(XContentParser parser) throws IOException { String name = null; Version version = Version.CURRENT; SnapshotState state = SnapshotState.IN_PROGRESS; String reason = null; List<String> indices = Collections.emptyList(); long startTime = 0; long endTime = 0; int totalShard = 0; int successfulShards = 0; List<SnapshotShardFailure> shardFailures = NO_FAILURES; if (parser.currentToken() == null) { // fresh parser? move to the first token parser.nextToken(); } if (parser.currentToken() == XContentParser.Token.START_OBJECT) { // on a start object move to next token parser.nextToken(); } XContentParser.Token token; if ((token = parser.nextToken()) == XContentParser.Token.START_OBJECT) { String currentFieldName = parser.currentName(); if ("snapshot".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); token = parser.nextToken(); if (token.isValue()) { if ("name".equals(currentFieldName)) { name = parser.text(); } else if ("state".equals(currentFieldName)) { state = SnapshotState.valueOf(parser.text()); } else if ("reason".equals(currentFieldName)) { reason = parser.text(); } else if ("start_time".equals(currentFieldName)) { startTime = parser.longValue(); } else if ("end_time".equals(currentFieldName)) { endTime = parser.longValue(); } else if ("total_shards".equals(currentFieldName)) { totalShard = parser.intValue(); } else if ("successful_shards".equals(currentFieldName)) { successfulShards = parser.intValue(); } else if ("version_id".equals(currentFieldName)) { version = Version.fromId(parser.intValue()); } } else if (token == XContentParser.Token.START_ARRAY) { if ("indices".equals(currentFieldName)) { ArrayList<String> indicesArray = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { indicesArray.add(parser.text()); } indices = Collections.unmodifiableList(indicesArray); } else if ("failures".equals(currentFieldName)) { ArrayList<SnapshotShardFailure> shardFailureArrayList = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { shardFailureArrayList.add(SnapshotShardFailure.fromXContent(parser)); } shardFailures = Collections.unmodifiableList(shardFailureArrayList); } else { // It was probably created by newer version - ignoring parser.skipChildren(); } } else if (token == XContentParser.Token.START_OBJECT) { // It was probably created by newer version - ignoring parser.skipChildren(); } } } } } else { throw new ElasticsearchParseException("unexpected token [" + token + "]"); } return new Snapshot( name, indices, state, reason, version, startTime, endTime, totalShard, successfulShards, shardFailures); }