All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.client.security.user.privileges.ApplicationPrivilege Maven / Gradle / Ivy

There is a newer version: 8.0.0-alpha2
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.client.security.user.privileges;

import org.elasticsearch.common.Strings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;

/**
 * Represents an application specific privilege. The application name, privilege name,
 * actions and metadata are completely managed by the client and can contain arbitrary
 * string values.
 */
public final class ApplicationPrivilege implements ToXContentObject {

    private static final ParseField APPLICATION = new ParseField("application");
    private static final ParseField NAME = new ParseField("name");
    private static final ParseField ACTIONS = new ParseField("actions");
    private static final ParseField METADATA = new ParseField("metadata");

    private final String application;
    private final String name;
    private final Set actions;
    private final Map metadata;

    public ApplicationPrivilege(String application, String name, Collection actions, @Nullable Map metadata) {
        if (Strings.isNullOrEmpty(application)) {
            throw new IllegalArgumentException("application name must be provided");
        } else {
            this.application = application;
        }
        if (Strings.isNullOrEmpty(name)) {
            throw new IllegalArgumentException("privilege name must be provided");
        } else {
            this.name = name;
        }
        if (actions == null || actions.isEmpty()) {
            throw new IllegalArgumentException("actions must be provided");
        } else {
            this.actions = Collections.unmodifiableSet(new HashSet<>(actions));
        }
        if (metadata == null || metadata.isEmpty()) {
            this.metadata = Collections.emptyMap();
        } else {
            this.metadata = Collections.unmodifiableMap(metadata);
        }
    }

    public String getApplication() {
        return application;
    }

    public String getName() {
        return name;
    }

    public Set getActions() {
        return actions;
    }

    public Map getMetadata() {
        return metadata;
    }

    @SuppressWarnings("unchecked")
    public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(
        "application_privilege",
        true,
        args -> new ApplicationPrivilege((String) args[0], (String) args[1], (Collection) args[2], (Map) args[3])
    );

    static {
        PARSER.declareString(constructorArg(), APPLICATION);
        PARSER.declareString(constructorArg(), NAME);
        PARSER.declareStringArray(constructorArg(), ACTIONS);
        PARSER.declareField(optionalConstructorArg(), XContentParser::map, ApplicationPrivilege.METADATA, ObjectParser.ValueType.OBJECT);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ApplicationPrivilege that = (ApplicationPrivilege) o;
        return Objects.equals(application, that.application)
            && Objects.equals(name, that.name)
            && Objects.equals(actions, that.actions)
            && Objects.equals(metadata, that.metadata);
    }

    @Override
    public int hashCode() {
        return Objects.hash(application, name, actions, metadata);
    }

    static ApplicationPrivilege fromXContent(XContentParser parser) throws IOException {
        return PARSER.parse(parser, null);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static final class Builder {
        private String applicationName = null;
        private String privilegeName = null;
        private Collection actions = null;
        private Map metadata = null;

        private Builder() {}

        public Builder application(String name) {
            this.applicationName = Objects.requireNonNull(name, "application name must be provided");
            return this;
        }

        public Builder privilege(String name) {
            this.privilegeName = Objects.requireNonNull(name, "privilege name must be provided");
            return this;
        }

        public Builder actions(String... actions) {
            this.actions = Arrays.asList(Objects.requireNonNull(actions));
            return this;
        }

        public Builder actions(Collection actions) {
            this.actions = Objects.requireNonNull(actions);
            return this;
        }

        public Builder metadata(Map metadata) {
            this.metadata = metadata;
            return this;
        }

        public ApplicationPrivilege build() {
            return new ApplicationPrivilege(applicationName, privilegeName, actions, metadata);
        }
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject()
            .field(APPLICATION.getPreferredName(), application)
            .field(NAME.getPreferredName(), name)
            .field(ACTIONS.getPreferredName(), actions);
        if (metadata != null && metadata.isEmpty() == false) {
            builder.field(METADATA.getPreferredName(), metadata);
        }
        return builder.endObject();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy