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.codec.api;
021
022
023import java.util.Set;
024
025import org.apache.directory.api.util.Strings;
026import org.apache.mina.util.ConcurrentHashSet;
027
028
029/**
030 * An implementation of the BinaryAttributeDetector interface. It's used
031 * on the client side to detect if an Attribute is HumanRedable.<br>
032 * One can inject some new attributes, replace the existing list,
033 * remove some attributes. <br>
034 * We provide a list of Attributes which are known to be binary :
035 * <ul>
036 * <li>entryACI</li>
037 * <li>prescriptiveACI</li>
038 * <li>subentryACI</li>
039 * <li>audio</li>
040 * <li>javaByteCode</li>
041 * <li>javaClassByteCode</li>
042 * <li>krb5key</li>
043 * <li>m-byteCode</li>
044 * <li>privateKey</li>
045 * <li>publicKey</li>
046 * <li>userPKCS12</li>
047 * <li>userSMIMECertificate</li>
048 * <li>cACertificate</li>
049 * <li>userCertificate</li>
050 * <li>authorityRevocationList</li>
051 * <li>certificateRevocationList</li>
052 * <li>deltaRevocationList</li>
053 * <li>crossCertificatePair</li>
054 * <li>personalSignature</li>
055 * <li>photo</li>
056 * <li>jpegPhoto</li>
057 * <li>supportedAlgorithms</li>
058 * </ul>
059 * <br>
060 * In order to reset the detector to get back to those default value, it's enough
061 * to call the setBinaryAttributes() with null as a parameter.
062 * 
063 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
064 */
065public class DefaultConfigurableBinaryAttributeDetector extends SchemaBinaryAttributeDetector
066    implements ConfigurableBinaryAttributeDetector
067{
068    /** A set of binary Attribute ID */
069    private Set<String> binaryAttributes = new ConcurrentHashSet<>();
070
071    /** A list of all the known binary attributes */
072    public static final String[] DEFAULT_BINARY_ATTRIBUTES = new String[]
073        {
074            // Syntax : ACI Item
075            "entryACI",
076            "prescriptiveACI",
077            "subentryACI",
078
079            // Syntax : AUDIO
080            "audio",
081
082            // Syntax : Binary
083            "javaByteCode",
084            "javaClassByteCode",
085            "krb5key",
086            "m-byteCode",
087            "privateKey",
088            "publicKey",
089            "userPKCS12",
090            "userSMIMECertificate",
091
092            // Syntax : Certificate
093            "cACertificate",
094            "userCertificate",
095
096            // Syntax : Certificate List
097            "authorityRevocationList",
098            "certificateRevocationList",
099            "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}