Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.sap.cloud.security.ams.dcl.client.pdp.AttributesImpl Maven / Gradle / Ivy
/************************************************************************
* © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cloud.security.ams.dcl.client.pdp;
import static com.sap.cloud.security.ams.dcl.client.language.DataControlLanguageTools.normalizeQualifiedPolicy;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.checkKeys;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.checkValue;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.copyNN;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.deepCopy;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.doGet;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.doSet;
import static com.sap.cloud.security.ams.dcl.client.pdp.AttributesTools.scope;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import com.sap.cloud.security.ams.dcl.client.el.AttributeName;
import com.sap.cloud.security.ams.dcl.client.el.QualifiedNamesBuilder;
final class AttributesImpl implements Attributes {
private static final String NULL_NAME_NOT_ALLOWED = "null name not allowed";
private final class StructureImpl implements Structure {
private final Deque> curserStack = new ArrayDeque<>();
private final List curserNamesStack = new ArrayList<>();
private final Map root;
private Map current;
public StructureImpl(Map root) {
this.root = root;
this.current = root;
}
public Structure reset() {
curserStack.clear();
curserNamesStack.clear();
this.current = root;
return this;
}
@Override
public Structure value(String key, Object value) {
if (value instanceof SpecialValue) {
List segments = new ArrayList<>(curserNamesStack);
segments.add(key);
addSpecialValue(qualifiedNamesBuilder().createAttributeName(segments), (SpecialValue) value);
} else {
current.put(key, checkValue(value));
}
return this;
}
@Override
public Structure structure(String key) {
current = scope(current, key, curserStack);
curserNamesStack.add(key);
return this;
}
// @Override
// public Structure structure(String... keys) {
// current = scope(current, keys, curserStack);
// return this;
// }
@Override
public Structure end() {
current = curserStack.removeLast();
curserNamesStack.remove(curserNamesStack.size() - 1);
return this;
}
// @Override
// public Structure end(int n) {
// while (n > 0) {
// current = curserStack.removeLast();
// n--;
// }
// return this;
// }
@Override
public Attributes attributes() {
return AttributesImpl.this;
}
}
private transient Set ignores;
private transient Set unknowns;
private transient final Map map;
private transient final StructureImpl curser;
private QualifiedNamesBuilder nameBuilder;
private transient Map dcl;
public AttributesImpl() {
this(AttributesTools.createMap());
}
public AttributesImpl(Map attributes) {
this(attributes, false);
}
public AttributesImpl(Map attributes, boolean copyMapDeep) {
this(copyMapDeep ? deepCopy(attributes) : attributes, null, null);
}
private AttributesImpl(Map attributes, Set ignores, Set unknowns) {
this.map = Objects.requireNonNull(attributes, "Map must not be null.");
this.ignores = ignores; // NOSONAR
this.unknowns = unknowns; // NOSONAR
curser = new StructureImpl(attributes);
}
@SuppressWarnings("unchecked")
public AttributesImpl(AttributesImpl other) {
this(deepCopy(other.getBackingMap()), copyNN(other.ignores), copyNN(other.unknowns));
}
private Map dcl() {
if (dcl == null) {
dcl = scope(map, Names.DCL_SECTION_KEY, null);
}
return dcl;
}
private QualifiedNamesBuilder qualifiedNamesBuilder() {
if (nameBuilder == null) {
nameBuilder = QualifiedNamesBuilder.create();
}
return nameBuilder;
}
@Override
public Attributes setAction(String action) {
dcl().put(Names.DCL_ACTION_KEY, checkValue(action));
return this;
}
@Override
public Attributes setResource(String resource) {
dcl().put(Names.DCL_RESOURCE_KEY, checkValue(resource));
return this;
}
@Override
public Attributes setTenant(String tenant) {
dcl().put(Names.DCL_TENANT_KEY, checkValue(tenant));
return this;
}
@Override
public Attributes setPrincipalToPolicies(String... keys) {
Map dcl = dcl();
if (keys == null) {
dcl.remove(Names.DCL_PRINCIPAL_TO_POLICIES_KEY);
} else {
checkKeys(keys);
List content = new ArrayList<>();
Collections.addAll(content, keys);
dcl.put(Names.DCL_PRINCIPAL_TO_POLICIES_KEY, content);
}
dcl.remove(Names.DCL_POLICIES_KEY);
return this;
}
@Override
public Attributes setPrincipalToPolicies(List keys) {
Map dcl = dcl();
if (keys == null) {
dcl.remove(Names.DCL_PRINCIPAL_TO_POLICIES_KEY);
} else {
checkKeys(keys);
List content = new ArrayList<>(keys);
dcl.put(Names.DCL_PRINCIPAL_TO_POLICIES_KEY, content);
}
dcl.remove(Names.DCL_POLICIES_KEY);
return this;
}
@Override
public Attributes setPolicies(Collection policies) {
setDclPolicies(Names.DCL_POLICIES_KEY, policies).remove(Names.DCL_PRINCIPAL_TO_POLICIES_KEY);
return this;
}
@Override
public Attributes setPolicies(String... policies) {
setDclPolicies(Names.DCL_POLICIES_KEY, policies).remove(Names.DCL_PRINCIPAL_TO_POLICIES_KEY);
return this;
}
@Override
public Attributes addPolicy(String qualifiedPolicy) {
qualifiedPolicy = normalizeQualifiedPolicy(qualifiedPolicy);
Map dcl = dcl();
@SuppressWarnings("unchecked")
Set content = (Set) dcl.get(Names.DCL_POLICIES_KEY);
if (content == null) {
content = new HashSet<>();
dcl.put(Names.DCL_POLICIES_KEY, content);
}
content.add(qualifiedPolicy);
dcl.remove(Names.DCL_PRINCIPAL_TO_POLICIES_KEY);
return this;
}
@Override
public Attributes setScopeFilterPolicies(Collection policies) {
setDclPolicies(Names.DCL_SCOPE_FILTER_KEY, policies);
return this;
}
@Override
public Attributes setScopeFilterPolicies(String... policies) {
setDclPolicies(Names.DCL_SCOPE_FILTER_KEY, policies);
return this;
}
private Map setDclPolicies(String key, Collection policies) {
Map dcl = dcl();
if (policies == null) {
dcl.remove(key);
} else {
Set content = AttributesTools.createSet();
for (String policy : policies) {
content.add(normalizeQualifiedPolicy(policy));
}
dcl.put(key, content);
}
return dcl;
}
private Map setDclPolicies(String key, String[] policies) {
Map dcl = dcl();
if (policies == null) {
dcl.remove(key);
} else {
Set content = AttributesTools.createSet();
for (String policy : policies) {
content.add(normalizeQualifiedPolicy(policy));
}
dcl.put(key, content);
}
return dcl;
}
@Override
public Structure app() {
return curser.reset().structure(Names.APP_SECTION_KEY);
}
@Override
public Structure env() {
return curser.reset().structure(Names.ENV_SECTION_KEY);
}
@Override
public Attributes setValue(AttributeName path, Object value) {
if (value instanceof SpecialValue) {
addSpecialValue(path, (SpecialValue) value);
} else {
doSet(map, value, path);
}
return this;
}
@Override
public Object getValue(AttributeName path) {
Objects.nonNull(path);
if (path.isEmpty()) { // NOSONAR
return map;
}
if (AttributesTools.startsWith(path, ignores)) {
return SpecialValue.IGNORE;
}
if (AttributesTools.startsWith(path, unknowns)) {
return SpecialValue.UNKNOWN;
}
return doGet(map, path);
}
// @Override
// public Structure structure(String... keys) {
// return curser.reset().structure(keys);
// }
@Override
public Attributes copy() {
return new AttributesImpl(this);
}
@Override
public String toString() {
return "Attributes [map=" + map + ", ignores=" + getIgnores() + ", unknowns=" + getUnknowns() + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ignores == null) ? 0 : ignores.hashCode());
result = prime * result + ((unknowns == null) ? 0 : unknowns.hashCode());
result = prime * result + ((map == null) ? 0 : map.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AttributesImpl other = (AttributesImpl) obj;
if (ignores == null) {
if (other.ignores != null)
return false;
} else if (!ignores.equals(other.ignores))
return false;
if (unknowns == null) {
if (other.unknowns != null)
return false;
} else if (!unknowns.equals(other.unknowns))
return false;
return map.equals(other.map);
}
void addSpecialValue(AttributeName name, SpecialValue specialValue) {
Objects.requireNonNull(name, NULL_NAME_NOT_ALLOWED);
if (specialValue == SpecialValue.IGNORE) {
AttributesTools.checkIgnoreName(name);
grantIgnores(false).add(name);
} else if (specialValue == SpecialValue.UNKNOWN) {
grantUnknowns(false).add(name);
} else {
throw new IllegalArgumentException("Cannot set SpecialValue '" + specialValue + "'");
}
}
@Override
public Attributes setUnknowns(Collection unknowns) {
if (unknowns == null || unknowns.isEmpty()) {
return setUnknowns();
}
for (AttributeName name : unknowns) {
Objects.requireNonNull(name, NULL_NAME_NOT_ALLOWED);
}
grantUnknowns(true).addAll(unknowns);
return this;
}
@Override
public Attributes setUnknowns(AttributeName... unknowns) {
if (unknowns == null || unknowns.length == 0) {
return setUnknowns();
}
for (AttributeName name : unknowns) {
Objects.requireNonNull(name, NULL_NAME_NOT_ALLOWED);
}
Collections.addAll(grantUnknowns(true), unknowns);
return this;
}
@Override
public Attributes setUnknowns(String... unknowns) {
if (unknowns == null || unknowns.length == 0) {
return setUnknowns();
}
Set res = AttributesTools.createSet();
QualifiedNamesBuilder nb = qualifiedNamesBuilder();
for (String unknown : unknowns) {
AttributeName name = nb.parseAttributeName(unknown);
res.add(name);
}
this.unknowns = res;
return this;
}
@Override
public Attributes setUnknowns() {
this.unknowns = null;
return this;
}
@Override
public Attributes setIgnores(Collection ignores) {
if (ignores == null || ignores.isEmpty()) {
return setIgnores();
}
for (AttributeName name : ignores) {
AttributesTools.checkIgnoreName(name);
}
grantIgnores(true).addAll(ignores);
return this;
}
@Override
public Attributes setIgnores(AttributeName... ignores) {
if (ignores == null || ignores.length == 0) {
return setIgnores();
}
for (AttributeName name : ignores) {
AttributesTools.checkIgnoreName(name);
}
Collections.addAll(grantIgnores(true), ignores);
return this;
}
@Override
public Attributes setIgnores(String... ignores) {
if (ignores == null || ignores.length == 0) {
return setIgnores();
}
Set ig = AttributesTools.createSet();
QualifiedNamesBuilder nb = qualifiedNamesBuilder();
for (String ignore : ignores) {
AttributeName name = nb.parseAttributeName(ignore);
AttributesTools.checkIgnoreName(name);
ig.add(name);
}
this.ignores = ig;
return this;
}
@Override
public Attributes setIgnores() {
this.ignores = null;
return this;
}
private Set grantIgnores(boolean clear) {
if (ignores == null) {
ignores = AttributesTools.createSet();
} else if (clear) {
ignores.clear();
}
return ignores; // NOSONAR
}
@Override
public Set getIgnores() {
return ignores == null ? Collections.emptySet() : ignores;
}
private Set grantUnknowns(boolean clear) {
if (unknowns == null) {
unknowns = AttributesTools.createSet();
} else if (clear) {
unknowns.clear();
}
return unknowns; // NOSONAR
}
@Override
public Set getUnknowns() {
return unknowns == null ? Collections.emptySet() : unknowns;
}
@Override
public Map getBackingMap() {
return map;
}
@Override
public void iteratePath(BiConsumer, Object> consumer) {
AttributesTools.iterate(new ArrayList<>(), this.map, consumer);
}
}