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<String>();
70  
71      /** A list of all the known binary attributes */
72      public static final String[] DEFAULT_BINARY_ATTRIBUTES = new String[]
73          {
74              "entryACI", // Syntax : ACI Item
75              "prescriptiveACI", // Syntax : ACI Item
76              "subentryACI", // Syntax : ACI Item
77              "audio", // Syntax : Audio
78              "javaByteCode", // Syntax : Binary
79              "javaClassByteCode", // Syntax : Binary
80              "krb5key", // Syntax : Binary
81              "m-byteCode", // Syntax : Binary
82              "privateKey", // Syntax : Binary
83              "publicKey", // Syntax : Binary
84              "userPKCS12", // Syntax : Binary
85              "userSMIMECertificate", // Syntax : Binary
86              "cACertificate", // Syntax : Certificate
87              "userCertificate", // Syntax : Certificate
88              "authorityRevocationList", // Syntax : Certificate List
89              "certificateRevocationList",// Syntax : Certificate List
90              "deltaRevocationList", // Syntax : Certificate List
91              "crossCertificatePair", // Syntax : Certificate Pair
92              "personalSignature", // Syntax : Fax
93              "photo", // Syntax : Fax
94              "jpegPhoto", // Syntax : JPEG
95              "supportedAlgorithms", // Syntax : Supported Algorithm
96              "javaSerializedData", // Syntax : Octet String
97              "userPassword", // Syntax : Octet String
98              
99              // Active Directory specific attributes see DIRAPI-177
100             "objectSid",
101             "objectGUID",
102             "thumbnailLogo",
103             "thumbnailPhoto",
104             "x500uniqueIdentifier"
105         };
106 
107 
108     /**
109      * Creates a new instance of a ConfigurableBinaryAttributeDetector. This will
110      * load a set of default attribute ID that are known to be binary.
111      */
112     public DefaultConfigurableBinaryAttributeDetector()
113     {
114         setBinaryAttributes( DEFAULT_BINARY_ATTRIBUTES );
115     }
116 
117 
118     /**
119      * {@inheritDoc}
120      */
121     public boolean isBinary( String attributeId )
122     {
123         boolean isBinary = super.isBinary( attributeId );
124 
125         if ( isBinary )
126         {
127             return true;
128         }
129 
130         String attrId = Strings.toLowerCase( attributeId );
131 
132         return binaryAttributes.contains( attrId );
133     }
134 
135 
136     /**
137      * {@inheritDoc}
138      */
139     public void addBinaryAttribute( String... binaryAttributes )
140     {
141         if ( binaryAttributes != null )
142         {
143             for ( String binaryAttribute : binaryAttributes )
144             {
145                 String attrId = Strings.toLowerCase( binaryAttribute );
146                 this.binaryAttributes.add( attrId );
147             }
148         }
149     }
150 
151 
152     /**
153      * {@inheritDoc}
154      */
155     public void removeBinaryAttribute( String... binaryAttributes )
156     {
157         if ( binaryAttributes != null )
158         {
159             for ( String binaryAttribute : binaryAttributes )
160             {
161                 String attrId = Strings.toLowerCase( binaryAttribute );
162                 this.binaryAttributes.remove( attrId );
163             }
164         }
165     }
166 
167 
168     /**
169      * {@inheritDoc}
170      */
171     public void setBinaryAttributes( String... binaryAttributes )
172     {
173         this.binaryAttributes.clear();
174 
175         // Special case for 'null'
176         if ( binaryAttributes == null )
177         {
178             // Reseting to the default list of binary attributes
179             binaryAttributes = DEFAULT_BINARY_ATTRIBUTES;
180         }
181 
182         addBinaryAttribute( binaryAttributes );
183     }
184 }