com.cognifide.cq.cqsm.foundation.permissions.Restrictions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apm-bundle Show documentation
Show all versions of apm-bundle Show documentation
AEM Permission Management is an AEM based tool focused on streamlining the permission configuration
The newest version!
/*
* ========================LICENSE_START=================================
* AEM Permission Management
* %%
* Copyright (C) 2013 Cognifide Limited
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package com.cognifide.cq.cqsm.foundation.permissions;
import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.PropertyType;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.ValueFormatException;
import lombok.Getter;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;
@Getter
@ToString
public class Restrictions {
private static final String STRICT = "STRICT";
private final String glob;
private final List ntNames;
private final List itemNames;
public Restrictions(String glob, List ntNames, List itemNames) {
this.glob = glob;
this.ntNames = notNullCopy(ntNames);
this.itemNames = notNullCopy(itemNames);
}
private List notNullCopy(List strings) {
return strings != null ? ImmutableList.copyOf(strings) : Collections.emptyList();
}
public Map getSingleValueRestrictions(ValueFactory valueFactory) {
Map result = new HashMap<>();
if (StringUtils.isNotBlank(glob)) {
result.put("rep:glob", normalizeGlob(valueFactory));
}
return result;
}
private Value normalizeGlob(ValueFactory valueFactory) {
if (STRICT.equalsIgnoreCase(glob)) {
return valueFactory.createValue(StringUtils.EMPTY);
}
return valueFactory.createValue(glob);
}
public Map getMultiValueRestrictions(ValueFactory valueFactory)
throws ValueFormatException {
Map result = new HashMap<>();
addRestrictions(valueFactory, result, "rep:ntNames", ntNames);
addRestrictions(valueFactory, result, "rep:itemNames", itemNames);
return result;
}
private void addRestrictions(ValueFactory valueFactory, Map result, String key, List names)
throws ValueFormatException {
if (names != null && !names.isEmpty()) {
result.put(key, createRestrictions(valueFactory, names));
}
}
private Value[] createRestrictions(ValueFactory valueFactory, List names)
throws ValueFormatException {
Value[] values = new Value[names.size()];
for (int index = 0; index < names.size(); index++) {
values[index] = valueFactory.createValue(names.get(index), PropertyType.NAME);
}
return values;
}
}