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<String>();
070
071    /** A list of all the known binary attributes */
072    public static final String[] DEFAULT_BINARY_ATTRIBUTES = new String[]
073        {
074            "entryACI", // Syntax : ACI Item
075            "prescriptiveACI", // Syntax : ACI Item
076            "subentryACI", // Syntax : ACI Item
077            "audio", // Syntax : Audio
078            "javaByteCode", // Syntax : Binary
079            "javaClassByteCode", // Syntax : Binary
080            "krb5key", // Syntax : Binary
081            "m-byteCode", // Syntax : Binary
082            "privateKey", // Syntax : Binary
083            "publicKey", // Syntax : Binary
084            "userPKCS12", // Syntax : Binary
085            "userSMIMECertificate", // Syntax : Binary
086            "cACertificate", // Syntax : Certificate
087            "userCertificate", // Syntax : Certificate
088            "authorityRevocationList", // Syntax : Certificate List
089            "certificateRevocationList",// Syntax : Certificate List
090            "deltaRevocationList", // Syntax : Certificate List
091            "crossCertificatePair", // Syntax : Certificate Pair
092            "personalSignature", // Syntax : Fax
093            "photo", // Syntax : Fax
094            "jpegPhoto", // Syntax : JPEG
095            "supportedAlgorithms", // Syntax : Supported Algorithm
096            "javaSerializedData", // Syntax : Octet String
097            "userPassword", // Syntax : Octet String
098            
099            // 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}