View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.aci;
21  
22  
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.model.constants.AuthenticationLevel;
29  
30  
31  /**
32   * An abstract class that provides common properties and operations for
33   * {@link ItemFirstACIItem} and {@link UserFirstACIItem} as specified X.501
34   * specification.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public abstract class ACIItem
39  {
40      /** The ACIItemComponet identifier */
41      private String identificationTag;
42  
43      /** The precedence : a number in [0 - 255] */
44      private int precedence = 0;
45  
46      /** The authentication level. One of 'none', 'simple' and 'strong' */
47      private AuthenticationLevel authenticationLevel;
48  
49  
50      /**
51       * Creates a new instance
52       * 
53       * @param identificationTag the id string of this item
54       * @param precedence the precedence of this item
55       * @param authenticationLevel the level of authentication required to this item
56       */
57      protected ACIItem( String identificationTag, int precedence, AuthenticationLevel authenticationLevel )
58      {
59          if ( identificationTag == null )
60          {
61              throw new IllegalArgumentException( I18n.err( I18n.ERR_04001_NULL_IDENTIFICATION_TAG ) );
62          }
63  
64          if ( ( precedence < 0 ) || ( precedence > 255 ) )
65          {
66              throw new IllegalArgumentException( I18n.err( I18n.ERR_04002_BAD_PRECENDENCE, precedence ) );
67          }
68  
69          if ( authenticationLevel == null )
70          {
71              throw new IllegalArgumentException( I18n.err( I18n.ERR_04003_NULL_AUTHENTICATION_LEVEL ) );
72          }
73  
74          this.identificationTag = identificationTag;
75          this.precedence = precedence;
76          this.authenticationLevel = authenticationLevel;
77      }
78  
79  
80      /**
81       * Gets the id string of this item.
82       *
83       * @return the identification tag
84       */
85      public String getIdentificationTag()
86      {
87          return identificationTag;
88      }
89  
90  
91      /**
92       * Gets the precedence of this item.
93       *
94       * @return the precedence
95       */
96      public int getPrecedence()
97      {
98          return precedence;
99      }
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 }