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.model.message;
21  
22  
23  import java.util.Map;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.constants.JndiPropertyConstants;
27  
28  
29  /**
30   * Type-safe derefAliases search parameter enumeration which determines the mode
31   * of alias handling. Note that the jndi values of these ValuedEnums correspond
32   * to the string value for the java.naming.ldap.derefAliases JNDI LDAP specific
33   * property.  The integer value represents the values used in the LDAP ASN.1 for
34   * different settings.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public enum AliasDerefMode
39  {
40      /** Alias handling mode value that treats aliases like entries */
41      NEVER_DEREF_ALIASES(0, "never"),
42  
43      /** Alias handling mode value that dereferences only when searching */
44      DEREF_IN_SEARCHING(1, "searching"),
45  
46      /** Alias handling mode value that dereferences only in finding the base */
47      DEREF_FINDING_BASE_OBJ(2, "finding"),
48  
49      /** Alias handling mode value that dereferences always */
50      DEREF_ALWAYS(3, "always");
51  
52      /** Stores the integer value of each element of the enumeration */
53      private int value;
54      /** Stores the integer value of each element of the enumeration */
55      private String jndiValue;
56  
57  
58      /**
59       * Private constructor so no other instances can be created other than the
60       * public static constants in this class.
61       * 
62       * @param value the integer value of the enumeration.
63       */
64      AliasDerefMode( int value, String jndiValue )
65      {
66          this.value = value;
67          this.jndiValue = jndiValue;
68      }
69  
70  
71      /**
72       * @return The value associated with the current element.
73       */
74      public int getValue()
75      {
76          return value;
77      }
78  
79  
80      /**
81       * Gets the enumeration from by extracting the value for the JNDI LDAP
82       * specific environment property, java.naming.ldap.derefAliases, from the
83       * environment.
84       * 
85       * @param env
86       *            the JNDI environment with a potential value for the
87       *            java.naming.ldap.derefAliases property
88       * @return the enumeration for the environment
89       */
90      public static AliasDerefMode getEnum( Map<String, Object> env )
91      {
92          String property = ( String ) env.get( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES );
93  
94          if ( null == property )
95          {
96              return DEREF_ALWAYS;
97          }
98          else
99          {
100             String trimmedProperty = property.trim();
101             
102             if ( "always".equalsIgnoreCase( trimmedProperty ) )
103             {
104                 return DEREF_ALWAYS;
105             }
106             else if ( "never".equalsIgnoreCase( trimmedProperty ) )
107             {
108                 return NEVER_DEREF_ALIASES;
109             }
110             else if ( "finding".equalsIgnoreCase( trimmedProperty ) )
111             {
112                 return DEREF_FINDING_BASE_OBJ;
113             }
114             else if ( "searching".equalsIgnoreCase( trimmedProperty ) )
115             {
116                 return DEREF_IN_SEARCHING;
117             }
118             else
119             {
120                 throw new IllegalArgumentException( I18n.err( I18n.ERR_04186, property,
121                     JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES ) );
122             }
123         }
124     }
125 
126 
127     /**
128      * Checks to see if we dereference while searching and finding the base.
129      * 
130      * @return true if value is DEREF_ALWAYS, false otherwise
131      */
132     public boolean isDerefAlways()
133     {
134         return this == DEREF_ALWAYS;
135     }
136 
137 
138     /**
139      * Checks to see if we never dereference aliases.
140      * 
141      * @return true if value is NEVER_DEREF_ALIASES, false otherwise
142      */
143     public boolean isNeverDeref()
144     {
145         return this == NEVER_DEREF_ALIASES;
146     }
147 
148 
149     /**
150      * Checks to see if we dereference while searching.
151      * 
152      * @return true if value is DEREF_ALWAYS_VAL, or DEREF_IN_SEARCHING, and
153      *         false otherwise.
154      */
155     public boolean isDerefInSearching()
156     {
157         switch ( this )
158         {
159             case DEREF_ALWAYS:
160                 return true;
161 
162             case DEREF_FINDING_BASE_OBJ:
163                 return false;
164 
165             case DEREF_IN_SEARCHING:
166                 return true;
167 
168             case NEVER_DEREF_ALIASES:
169                 return false;
170 
171             default:
172                 throw new IllegalArgumentException( I18n.err( I18n.ERR_04187 ) );
173         }
174     }
175 
176 
177     /**
178      * Checks to see if we dereference while finding the base.
179      * 
180      * @return true if value is DEREF_ALWAYS, or DEREF_FINDING_BASE_OBJ, and
181      *         false otherwise.
182      */
183     public boolean isDerefFindingBase()
184     {
185         switch ( this )
186         {
187             case DEREF_ALWAYS:
188                 return true;
189 
190             case DEREF_FINDING_BASE_OBJ:
191                 return true;
192 
193             case DEREF_IN_SEARCHING:
194                 return false;
195 
196             case NEVER_DEREF_ALIASES:
197                 return false;
198 
199             default:
200                 throw new IllegalArgumentException( "Class has bug: check for valid enumeration values" );
201         }
202     }
203 
204 
205     /**
206      * get the AliasDerefMode corresponding to the integer value passed
207      *
208      * @param val the AliasDerefMode's integer value
209      * @return the AliasDerefMode whose value is equivalent to the given integer value
210      */
211     public static AliasDerefMode getDerefMode( int val )
212     {
213         switch ( val )
214         {
215             case 0:
216                 return NEVER_DEREF_ALIASES;
217 
218             case 1:
219                 return DEREF_IN_SEARCHING;
220 
221             case 2:
222                 return DEREF_FINDING_BASE_OBJ;
223 
224             case 3:
225                 return DEREF_ALWAYS;
226 
227             default:
228                 throw new IllegalArgumentException( "Unknown derefmode " + val );
229         }
230     }
231 
232 
233     /**
234      * get the AliasDerefMode corresponding to the string value jndiValue passed
235      *
236      * @param val the AliasDerefMode's string value
237      * @return the AliasDerefMode whose value is equivalent to the given string value
238      */
239     public static AliasDerefMode getDerefMode( String val )
240     {
241         if ( val != null )
242         {
243             if ( val.equals( NEVER_DEREF_ALIASES.jndiValue ) )
244             {
245                 return NEVER_DEREF_ALIASES;
246             }
247 
248             if ( val.equals( DEREF_IN_SEARCHING.jndiValue ) )
249             {
250                 return DEREF_IN_SEARCHING;
251             }
252 
253             if ( val.equals( DEREF_FINDING_BASE_OBJ.jndiValue ) )
254             {
255                 return DEREF_FINDING_BASE_OBJ;
256             }
257 
258             if ( val.equals( DEREF_ALWAYS.jndiValue ) )
259             {
260                 return DEREF_ALWAYS;
261             }
262         }
263 
264         throw new IllegalArgumentException( "Unknown derefmode " + val );
265     }
266 
267 
268     /**
269      * @return The JNDI value
270      */
271     public String getJndiValue()
272     {
273         return jndiValue;
274     }
275 }