001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.aci;
021
022
023import java.util.Collection;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.model.constants.AuthenticationLevel;
029
030
031/**
032 * An abstract class that provides common properties and operations for
033 * {@link ItemFirstACIItem} and {@link UserFirstACIItem} as specified X.501
034 * specification.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public abstract class ACIItem
039{
040    /** The ACIItemComponet identifier */
041    private String identificationTag;
042
043    /** The precedence : a number in [0 - 255] */
044    private int precedence = 0;
045
046    /** The authentication level. One of 'none', 'simple' and 'strong' */
047    private AuthenticationLevel authenticationLevel;
048
049
050    /**
051     * Creates a new instance
052     * 
053     * @param identificationTag the id string of this item
054     * @param precedence the precedence of this item
055     * @param authenticationLevel the level of authentication required to this item
056     */
057    protected ACIItem( String identificationTag, int precedence, AuthenticationLevel authenticationLevel )
058    {
059        if ( identificationTag == null )
060        {
061            throw new IllegalArgumentException( I18n.err( I18n.ERR_04001_NULL_IDENTIFICATION_TAG ) );
062        }
063
064        if ( ( precedence < 0 ) || ( precedence > 255 ) )
065        {
066            throw new IllegalArgumentException( I18n.err( I18n.ERR_04002_BAD_PRECENDENCE, precedence ) );
067        }
068
069        if ( authenticationLevel == null )
070        {
071            throw new IllegalArgumentException( I18n.err( I18n.ERR_04003_NULL_AUTHENTICATION_LEVEL ) );
072        }
073
074        this.identificationTag = identificationTag;
075        this.precedence = precedence;
076        this.authenticationLevel = authenticationLevel;
077    }
078
079
080    /**
081     * Gets the id string of this item.
082     *
083     * @return the identification tag
084     */
085    public String getIdentificationTag()
086    {
087        return identificationTag;
088    }
089
090
091    /**
092     * Gets the precedence of this item.
093     *
094     * @return the precedence
095     */
096    public int getPrecedence()
097    {
098        return precedence;
099    }
100
101
102    /**
103     * Gets the level of authentication required to this item.
104     *
105     * @return the authentication level
106     */
107    public AuthenticationLevel getAuthenticationLevel()
108    {
109        return authenticationLevel;
110    }
111
112
113    /**
114     * Converts this item into a collection of {@link ACITuple}s.
115     *
116     * @return the converted collection of {@link ACITuple}
117     */
118    public abstract Collection<ACITuple> toTuples();
119
120
121    /**
122     * Converts a collection of {@link GrantAndDenial}s into a collection of {@link MicroOperation}s.
123     *
124     * @param grantsAndDenials the grants and denials
125     * @return the collection of {@link MicroOperation}s
126     */
127    protected static Collection<MicroOperation> toMicroOperations( Collection<GrantAndDenial> grantsAndDenials )
128    {
129        Set<MicroOperation> microOps = new HashSet<>();
130
131        for ( GrantAndDenial grantAndDenial : grantsAndDenials )
132        {
133            microOps.add( grantAndDenial.getMicroOperation() );
134        }
135
136        return microOps;
137    }
138
139
140    /**
141     * {@inheritDoc}
142     */
143    @Override
144    public String toString()
145    {
146        StringBuilder buf = new StringBuilder();
147
148        // identificationTag
149        buf.append( "identificationTag \"" );
150        buf.append( getIdentificationTag() );
151
152        // precedence
153        buf.append( "\", precedence " );
154        buf.append( getPrecedence() );
155
156        // authenticationLevel
157        buf.append( ", authenticationLevel " );
158        buf.append( getAuthenticationLevel().getName() );
159
160        return buf.toString();
161    }
162}