001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 */
026
027package org.forgerock.opendj.config;
028
029import java.util.EnumSet;
030import java.util.regex.Pattern;
031
032import org.forgerock.util.Reject;
033
034/**
035 * ACI property definition.
036 */
037public final class ACIPropertyDefinition extends PropertyDefinition<String> {
038
039    /**
040     * An interface for incrementally constructing ACI property definitions.
041     */
042    public static final class Builder extends AbstractBuilder<String, ACIPropertyDefinition> {
043
044        /** Private constructor. */
045        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
046            super(d, propertyName);
047        }
048
049        /** {@inheritDoc} */
050        @Override
051        protected ACIPropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
052                String propertyName, EnumSet<PropertyOption> options,
053                AdministratorAction adminAction, DefaultBehaviorProvider<String> defaultBehavior) {
054            return new ACIPropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
055        }
056    }
057
058    /**
059     * Create a ACI property definition builder.
060     *
061     * @param d
062     *            The managed object definition associated with this property
063     *            definition.
064     * @param propertyName
065     *            The property name.
066     * @return Returns the new ACI property definition builder.
067     */
068    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
069        return new Builder(d, propertyName);
070    }
071
072    /**
073     * Pattern used for performing basic ACI syntax validation. Taken from the
074     * Aci class in the server.
075     */
076    private static final Pattern ACI_REGEX =
077            Pattern.compile("^\\s*(\\(\\s*(\\w+)\\s*(!?=)\\s*\"([^\"]+)\"\\s*\\)\\s*)*\\s*\\"
078                    + "(\\s*(?i)version(?-i)\\s*(\\d\\.\\d)\\s*;\\s*(?i)acl(?-i)\\s*\"([^\"]*)"
079                    + "\"\\s*;\\s*\\s*(\\w+)\\s*\\(([^()]+)\\)\\s*(.+?\"[)]*)\\s*;\\s*\\s*\\)\\s*$");
080
081    /** Private constructor. */
082    private ACIPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
083            EnumSet<PropertyOption> options, AdministratorAction adminAction,
084            DefaultBehaviorProvider<String> defaultBehavior) {
085        super(d, String.class, propertyName, options, adminAction, defaultBehavior);
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public void validateValue(String value) {
091        Reject.ifNull(value);
092
093        // No additional validation required.
094    }
095
096    /** {@inheritDoc} */
097    @Override
098    public String decodeValue(String value) {
099        Reject.ifNull(value);
100
101        /*
102         * We don't have access to the ACI class from the server so do
103         * best-effort using regular expressions. TODO: is it worth improving on
104         * this? We could use reflection to get the appropriate parser which
105         * would allow us to use full validation in OpenDJ whilst remaining
106         * decoupled in other applications.
107         */
108        if (ACI_REGEX.matcher(value).matches()) {
109            return value;
110        }
111        throw PropertyException.illegalPropertyValueException(this, value);
112    }
113
114    /** {@inheritDoc} */
115    @Override
116    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
117        return v.visitACI(this, p);
118    }
119
120    /** {@inheritDoc} */
121    @Override
122    public <R, P> R accept(PropertyValueVisitor<R, P> v, String value, P p) {
123        return v.visitACI(this, value, p);
124    }
125
126    /** {@inheritDoc} */
127    @Override
128    public int compare(String o1, String o2) {
129        return o1.compareTo(o2);
130    }
131}