|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.security; |
| 21 | + |
| 22 | +import org.elasticsearch.client.security.user.privileges.Role; |
| 23 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 24 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +/** |
| 33 | + * Response when requesting one or more roles. |
| 34 | + * Returns a List of {@link Role} objects |
| 35 | + */ |
| 36 | +public final class GetRolesResponse { |
| 37 | + |
| 38 | + private final List<Role> roles; |
| 39 | + |
| 40 | + public GetRolesResponse(List<Role> roles) { |
| 41 | + this.roles = Collections.unmodifiableList(roles); |
| 42 | + } |
| 43 | + |
| 44 | + public List<Role> getRoles() { |
| 45 | + return roles; |
| 46 | + } |
| 47 | + |
| 48 | + public static GetRolesResponse fromXContent(XContentParser parser) throws IOException { |
| 49 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); |
| 50 | + final List<Role> roles = new ArrayList<>(); |
| 51 | + XContentParser.Token token; |
| 52 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 53 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation); |
| 54 | + roles.add(Role.PARSER.parse(parser, parser.currentName())); |
| 55 | + } |
| 56 | + return new GetRolesResponse(roles); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean equals(Object o) { |
| 61 | + if (this == o) return true; |
| 62 | + if (o == null || getClass() != o.getClass()) return false; |
| 63 | + GetRolesResponse response = (GetRolesResponse) o; |
| 64 | + return Objects.equals(roles, response.roles); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int hashCode() { |
| 69 | + return Objects.hash(roles); |
| 70 | + } |
| 71 | +} |
0 commit comments