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   *    https://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       * @param jndiValue the JNDI value
64       */
65      AliasDerefMode( int value, String jndiValue )
66      {
67          this.value = value;
68          this.jndiValue = jndiValue;
69      }
70  
71  
72      /**
73       * @return The value associated with the current element.
74       */
75      public int getValue()
76      {
77          return value;
78      }
79  
80  
81      /**
82       * Gets the enumeration from by extracting the value for the JNDI LDAP
83       * specific environment property, java.naming.ldap.derefAliases, from the
84       * environment.
85       * 
86       * @param env
87       *            the JNDI environment with a potential value for the
88       *            java.naming.ldap.derefAliases property
89       * @return the enumeration for the environment
90       */
91      public static AliasDerefMode getEnum( Map<String, Object> env )
92      {
93          String property = ( String ) env.get( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES );
94  
95          if ( null == property )
96          {
97              return DEREF_ALWAYS;
98          }
99          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 }