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.codec.api;
21  
22  
23  import java.util.Set;
24  
25  import org.apache.directory.api.util.Strings;
26  import org.apache.mina.util.ConcurrentHashSet;
27  
28  
29  /**
30   * An implementation of the BinaryAttributeDetector interface. It's used
31   * on the client side to detect if an Attribute is HumanRedable.<br>
32   * One can inject some new attributes, replace the existing list,
33   * remove some attributes. <br>
34   * We provide a list of Attributes which are known to be binary :
35   * <ul>
36   * <li>entryACI</li>
37   * <li>prescriptiveACI</li>
38   * <li>subentryACI</li>
39   * <li>audio</li>
40   * <li>javaByteCode</li>
41   * <li>javaClassByteCode</li>
42   * <li>krb5key</li>
43   * <li>m-byteCode</li>
44   * <li>privateKey</li>
45   * <li>publicKey</li>
46   * <li>userPKCS12</li>
47   * <li>userSMIMECertificate</li>
48   * <li>cACertificate</li>
49   * <li>userCertificate</li>
50   * <li>authorityRevocationList</li>
51   * <li>certificateRevocationList</li>
52   * <li>deltaRevocationList</li>
53   * <li>crossCertificatePair</li>
54   * <li>personalSignature</li>
55   * <li>photo</li>
56   * <li>jpegPhoto</li>
57   * <li>supportedAlgorithms</li>
58   * </ul>
59   * <br>
60   * In order to reset the detector to get back to those default value, it's enough
61   * to call the setBinaryAttributes() with null as a parameter.
62   * 
63   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
64   */
65  public class DefaultConfigurableBinaryAttributeDetector extends SchemaBinaryAttributeDetector
66      implements ConfigurableBinaryAttributeDetector
67  {
68      /** A set of binary Attribute ID */
69      private Set<String> binaryAttributes = new ConcurrentHashSet<>();
70  
71      /** A list of all the known binary attributes */
72      public static final String[] DEFAULT_BINARY_ATTRIBUTES = new String[]
73          {
74              // Syntax : ACI Item
75              "entryACI",
76              "prescriptiveACI",
77              "subentryACI",
78  
79              // Syntax : AUDIO
80              "audio",
81  
82              // Syntax : Binary
83              "javaByteCode",
84              "javaClassByteCode",
85              "krb5key",
86              "m-byteCode",
87              "privateKey",
88              "publicKey",
89              "userPKCS12",
90              "userSMIMECertificate",
91  
92              // Syntax : Certificate
93              "cACertificate",
94              "userCertificate",
95  
96              // Syntax : Certificate List
97              "authorityRevocationList",
98              "certificateRevocationList",
99              "deltaRevocationList",
100 
101             // Syntax : Certificate Pair
102             "crossCertificatePair",
103 
104             // Syntax : Fax
105             "personalSignature",
106             "photo",
107 
108             // Syntax : JPEG
109             "jpegPhoto",
110 
111             // Syntax : Supported Algorithm
112             "supportedAlgorithms",
113 
114             // Syntax : Octet String
115             "javaSerializedData",
116             "userPassword",
117 
118             // Active Directory specific attributes see DIRAPI-177
119             "objectSid",
120             "objectGUID",
121             "thumbnailLogo",
122             "thumbnailPhoto",
123             "x500uniqueIdentifier"
124     };
125 
126 
127     /**
128      * Creates a new instance of a ConfigurableBinaryAttributeDetector. This will
129      * load a set of default attribute ID that are known to be binary.
130      */
131     public DefaultConfigurableBinaryAttributeDetector()
132     {
133         setBinaryAttributes( DEFAULT_BINARY_ATTRIBUTES );
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public boolean isBinary( String attributeId )
142     {
143         boolean isBinary = super.isBinary( attributeId );
144 
145         if ( isBinary )
146         {
147             return true;
148         }
149 
150         String attrId = Strings.toLowerCaseAscii( attributeId );
151 
152         return binaryAttributes.contains( attrId );
153     }
154 
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public void addBinaryAttribute( String... binaryAttributes )
161     {
162         if ( binaryAttributes != null )
163         {
164             for ( String binaryAttribute : binaryAttributes )
165             {
166                 String attrId = Strings.toLowerCaseAscii( binaryAttribute );
167                 this.binaryAttributes.add( attrId );
168             }
169         }
170     }
171 
172 
173     /**
174      * {@inheritDoc}
175      */
176     @Override
177     public void removeBinaryAttribute( String... binaryAttributes )
178     {
179         if ( binaryAttributes != null )
180         {
181             for ( String binaryAttribute : binaryAttributes )
182             {
183                 String attrId = Strings.toLowerCaseAscii( binaryAttribute );
184                 this.binaryAttributes.remove( attrId );
185             }
186         }
187     }
188 
189 
190     /**
191      * {@inheritDoc}
192      */
193     @Override
194     public void setBinaryAttributes( String... binaryAttributes )
195     {
196         this.binaryAttributes.clear();
197 
198         // Special case for 'null'
199         if ( binaryAttributes == null )
200         {
201             // Reseting to the default list of binary attributes
202             addBinaryAttribute( DEFAULT_BINARY_ATTRIBUTES );
203         }
204         else
205         {
206             addBinaryAttribute( binaryAttributes );
207         }
208     }
209 }