Search in sources :

Example 1 with State

use of io.crate.user.Privilege.State in project crate by crate.

the class UsersPrivilegesMetadata method privilegeFromXContent.

private static void privilegeFromXContent(XContentParser parser, Set<Privilege> privileges) throws IOException {
    XContentParser.Token currentToken;
    State state = null;
    Privilege.Type type = null;
    Privilege.Clazz clazz = null;
    String ident = null;
    String grantor = null;
    while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (currentToken == XContentParser.Token.FIELD_NAME) {
            String currentFieldName = parser.currentName();
            currentToken = parser.nextToken();
            switch(currentFieldName) {
                case "state":
                    if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'state' value is not a number [{}]", currentToken);
                    }
                    state = State.values()[parser.intValue()];
                    break;
                case "type":
                    if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'type' value is not a number [{}]", currentToken);
                    }
                    type = Privilege.Type.values()[parser.intValue()];
                    break;
                case "class":
                    if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'class' value is not a number [{}]", currentToken);
                    }
                    clazz = Privilege.Clazz.values()[parser.intValue()];
                    break;
                case "ident":
                    if (currentToken != XContentParser.Token.VALUE_STRING && currentToken != XContentParser.Token.VALUE_NULL) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'ident' value is not a string or null [{}]", currentToken);
                    }
                    ident = parser.textOrNull();
                    break;
                case "grantor":
                    if (currentToken != XContentParser.Token.VALUE_STRING) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'grantor' value is not a string [{}]", currentToken);
                    }
                    grantor = parser.text();
                    break;
                default:
                    throw new ElasticsearchParseException("failed to parse privilege");
            }
        } else if (currentToken == XContentParser.Token.END_ARRAY) {
            // empty privileges set
            return;
        }
    }
    privileges.add(new Privilege(state, type, clazz, ident, grantor));
}
Also used : State(io.crate.user.Privilege.State) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Privilege(io.crate.user.Privilege) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

Privilege (io.crate.user.Privilege)1 State (io.crate.user.Privilege.State)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1