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.model.message;
021
022
023import java.util.Map;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.constants.JndiPropertyConstants;
027
028
029/**
030 * Type-safe derefAliases search parameter enumeration which determines the mode
031 * of alias handling. Note that the jndi values of these ValuedEnums correspond
032 * to the string value for the java.naming.ldap.derefAliases JNDI LDAP specific
033 * property.  The integer value represents the values used in the LDAP ASN.1 for
034 * different settings.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public enum AliasDerefMode
039{
040    /** Alias handling mode value that treats aliases like entries */
041    NEVER_DEREF_ALIASES(0, "never"),
042
043    /** Alias handling mode value that dereferences only when searching */
044    DEREF_IN_SEARCHING(1, "searching"),
045
046    /** Alias handling mode value that dereferences only in finding the base */
047    DEREF_FINDING_BASE_OBJ(2, "finding"),
048
049    /** Alias handling mode value that dereferences always */
050    DEREF_ALWAYS(3, "always");
051
052    /** Stores the integer value of each element of the enumeration */
053    private int value;
054    /** Stores the integer value of each element of the enumeration */
055    private String jndiValue;
056
057
058    /**
059     * Private constructor so no other instances can be created other than the
060     * public static constants in this class.
061     * 
062     * @param value the integer value of the enumeration.
063     * @param jndiValue the JNDI value
064     */
065    AliasDerefMode( int value, String jndiValue )
066    {
067        this.value = value;
068        this.jndiValue = jndiValue;
069    }
070
071
072    /**
073     * @return The value associated with the current element.
074     */
075    public int getValue()
076    {
077        return value;
078    }
079
080
081    /**
082     * Gets the enumeration from by extracting the value for the JNDI LDAP
083     * specific environment property, java.naming.ldap.derefAliases, from the
084     * environment.
085     * 
086     * @param env
087     *            the JNDI environment with a potential value for the
088     *            java.naming.ldap.derefAliases property
089     * @return the enumeration for the environment
090     */
091    public static AliasDerefMode getEnum( Map<String, Object> env )
092    {
093        String property = ( String ) env.get( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES );
094
095        if ( null == property )
096        {
097            return DEREF_ALWAYS;
098        }
099        else
100        {
101            String trimmedProperty = property.trim();
102            
103            if ( "always".equalsIgnoreCase( trimmedProperty ) )
104            {
105                return DEREF_ALWAYS;
106            }
107            else if ( "never".equalsIgnoreCase( trimmedProperty ) )
108            {
109                return NEVER_DEREF_ALIASES;
110            }
111            else if ( "finding".equalsIgnoreCase( trimmedProperty ) )
112            {
113                return DEREF_FINDING_BASE_OBJ;
114            }
115            else if ( "searching".equalsIgnoreCase( trimmedProperty ) )
116            {
117                return DEREF_IN_SEARCHING;
118            }
119            else
120            {
121                throw new IllegalArgumentException( I18n.err( I18n.ERR_13507_UNRECOGNIZED_JNDI_PROPERTY_VALUE, property,
122                    JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES ) );
123            }
124        }
125    }
126
127
128    /**
129     * Checks to see if we dereference while searching and finding the base.
130     * 
131     * @return true if value is DEREF_ALWAYS, false otherwise
132     */
133    public boolean isDerefAlways()
134    {
135        return this == DEREF_ALWAYS;
136    }
137
138
139    /**
140     * Checks to see if we never dereference aliases.
141     * 
142     * @return true if value is NEVER_DEREF_ALIASES, false otherwise
143     */
144    public boolean isNeverDeref()
145    {
146        return this == NEVER_DEREF_ALIASES;
147    }
148
149
150    /**
151     * Checks to see if we dereference while searching.
152     * 
153     * @return true if value is DEREF_ALWAYS_VAL, or DEREF_IN_SEARCHING, and
154     *         false otherwise.
155     */
156    public boolean isDerefInSearching()
157    {
158        return ( this == AliasDerefMode.DEREF_ALWAYS ) || ( this == AliasDerefMode.DEREF_IN_SEARCHING );
159    }
160
161
162    /**
163     * Checks to see if we dereference while finding the base.
164     * 
165     * @return true if value is DEREF_ALWAYS, or DEREF_FINDING_BASE_OBJ, and
166     *         false otherwise.
167     */
168    public boolean isDerefFindingBase()
169    {
170        switch ( this )
171        {
172            case DEREF_ALWAYS:
173                return true;
174
175            case DEREF_FINDING_BASE_OBJ:
176                return true;
177
178            case DEREF_IN_SEARCHING:
179                return false;
180
181            case NEVER_DEREF_ALIASES:
182                return false;
183
184            default:
185                throw new IllegalArgumentException( I18n.err( I18n.ERR_13518_CLASS_INVALID_ENULM_VALUE ) );
186        }
187    }
188
189
190    /**
191     * get the AliasDerefMode corresponding to the integer value passed
192     *
193     * @param val the AliasDerefMode's integer value
194     * @return the AliasDerefMode whose value is equivalent to the given integer value
195     */
196    public static AliasDerefMode getDerefMode( int val )
197    {
198        switch ( val )
199        {
200            case 0:
201                return NEVER_DEREF_ALIASES;
202
203            case 1:
204                return DEREF_IN_SEARCHING;
205
206            case 2:
207                return DEREF_FINDING_BASE_OBJ;
208
209            case 3:
210                return DEREF_ALWAYS;
211
212            default:
213                throw new IllegalArgumentException( I18n.err( I18n.ERR_13510_UNKNOWN_DEREF_MODE, val ) );
214        }
215    }
216
217
218    /**
219     * get the AliasDerefMode corresponding to the string value jndiValue passed
220     *
221     * @param val the AliasDerefMode's string value
222     * @return the AliasDerefMode whose value is equivalent to the given string value
223     */
224    public static AliasDerefMode getDerefMode( String val )
225    {
226        if ( val != null )
227        {
228            if ( val.equals( NEVER_DEREF_ALIASES.jndiValue ) )
229            {
230                return NEVER_DEREF_ALIASES;
231            }
232
233            if ( val.equals( DEREF_IN_SEARCHING.jndiValue ) )
234            {
235                return DEREF_IN_SEARCHING;
236            }
237
238            if ( val.equals( DEREF_FINDING_BASE_OBJ.jndiValue ) )
239            {
240                return DEREF_FINDING_BASE_OBJ;
241            }
242
243            if ( val.equals( DEREF_ALWAYS.jndiValue ) )
244            {
245                return DEREF_ALWAYS;
246            }
247        }
248
249        throw new IllegalArgumentException( I18n.err( I18n.ERR_13510_UNKNOWN_DEREF_MODE, val ) );
250    }
251
252
253    /**
254     * @return The JNDI value
255     */
256    public String getJndiValue()
257    {
258        return jndiValue;
259    }
260}