UserPropertiesImpl.java 6.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.granite.security.user.internal;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.internal.AuthorizableInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

public class UserPropertiesImpl
implements UserProperties {
    private static final Set<String> RESERVED_PREFIXES = new HashSet<String>();
    private final AuthorizableInfo info;
    private final Node node;
    private final ResourceResolver resourceResolver;
    private Resource resource;

    UserPropertiesImpl(AuthorizableInfo info, Node node, ResourceResolver resourceResolver) {
        if (info == null || node == null) {
            throw new IllegalArgumentException("AuthorizableId and user properties node may not be null.");
        }
        this.info = info;
        this.node = node;
        this.resourceResolver = resourceResolver;
    }

    @Override
    public String getAuthorizableID() {
        return this.info.getId();
    }

    @Override
    public Node getNode() {
        return this.node;
    }

    @Override
    public String[] getPropertyNames() throws RepositoryException {
        return this.getPropertyNames(null);
    }

    @Override
    public String[] getPropertyNames(String relPath) throws RepositoryException {
        ArrayList<String> propNames = new ArrayList<String>();
        Node n = relPath == null ? this.node : this.node.getNode(relPath);
        PropertyIterator it = n.getProperties();
        while (it.hasNext()) {
            String propName = it.nextProperty().getName();
            if (UserPropertiesImpl.isInternal(propName)) continue;
            propNames.add(propName);
        }
        return propNames.toArray(new String[propNames.size()]);
    }

    @Override
    public String getProperty(String relativePath) throws RepositoryException {
        return this.getProperty(relativePath, null, String.class);
    }

    @Override
    public <T> T getProperty(String relativePath, T defaultValue, Class<T> type) throws RepositoryException {
        UserPropertiesImpl.checkIsInternal(relativePath);
        ValueMap valueMap = this.getValueMap();
        Object v = valueMap.get(relativePath, type);
        return v == null ? defaultValue : v;
    }

    @Override
    public Resource getResource(String relativePath) throws RepositoryException {
        UserPropertiesImpl.checkIsInternal(relativePath);
        return this.resourceResolver.getResource(this.getResource(), relativePath);
    }

    @Override
    public Iterator<Resource> getResources(String relativePath) throws RepositoryException {
        UserPropertiesImpl.checkIsInternal(relativePath);
        Resource resource = this.getResource(relativePath);
        if (resource == null) {
            return Collections.EMPTY_SET.iterator();
        }
        return this.resourceResolver.listChildren(resource);
    }

    @Override
    public String getResourcePath(String relativePath, String suffix, String defaultPath) throws RepositoryException {
        UserPropertiesImpl.checkIsInternal(relativePath);
        Resource resource = this.getResource(relativePath);
        if (resource == null) {
            return defaultPath;
        }
        String path = resource.getPath();
        return suffix != null ? path + suffix : path;
    }

    @Override
    public String getDisplayName() throws RepositoryException {
        return this.getDisplayName(null);
    }

    @Override
    public String getDisplayName(String nameDisplayOrder) throws RepositoryException {
        String formattedName = this.getProperty("displayName");
        if (formattedName == null) {
            StringBuilder bld = new StringBuilder();
            if (nameDisplayOrder != null) {
                String[] nameTypes;
                for (String nameType : nameTypes = nameDisplayOrder.split(" ")) {
                    UserPropertiesImpl.append(bld, this.getProperty(nameType));
                }
            } else {
                UserPropertiesImpl.append(bld, this.getProperty("givenName"));
                UserPropertiesImpl.append(bld, this.getProperty("middleName"));
                UserPropertiesImpl.append(bld, this.getProperty("familyName"));
            }
            formattedName = bld.toString();
        }
        return formattedName.length() > 0 ? formattedName : this.getAuthorizableID();
    }

    private Resource getResource() throws RepositoryException {
        if (this.resource == null) {
            this.resource = this.resourceResolver.getResource(this.node.getPath());
        }
        return this.resource;
    }

    private ValueMap getValueMap() throws RepositoryException {
        Resource resource = this.getResource();
        return resource == null ? ValueMap.EMPTY : ResourceUtil.getValueMap((Resource)resource);
    }

    private static void append(StringBuilder formatted, String prop) {
        if (prop != null && prop.length() > 0) {
            if (formatted.length() > 0) {
                formatted.append(" ");
            }
            formatted.append(prop);
        }
    }

    private static boolean isInternal(String relPath) {
        String propertyName = Text.getName((String)relPath);
        for (String reservedPrefix : RESERVED_PREFIXES) {
            if (!propertyName.startsWith(reservedPrefix)) continue;
            return true;
        }
        return false;
    }

    private static void checkIsInternal(String relPath) {
        if (UserPropertiesImpl.isInternal(relPath)) {
            throw new IllegalArgumentException("Internal property name " + relPath);
        }
    }

    @Override
    public boolean isGroupProperties() {
        return this.info.isGroup();
    }

    @Override
    public String getAuthorizablePath() {
        return this.info.getPath();
    }

    static {
        RESERVED_PREFIXES.add("jcr:");
        RESERVED_PREFIXES.add("rep:");
        RESERVED_PREFIXES.add("sling:");
    }
}