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 */ 064 private AliasDerefMode( int value, String jndiValue ) 065 { 066 this.value = value; 067 this.jndiValue = jndiValue; 068 } 069 070 071 /** 072 * @return The value associated with the current element. 073 */ 074 public int getValue() 075 { 076 return value; 077 } 078 079 080 /** 081 * Gets the enumeration from by extracting the value for the JNDI LDAP 082 * specific environment property, java.naming.ldap.derefAliases, from the 083 * environment. 084 * 085 * @param env 086 * the JNDI environment with a potential value for the 087 * java.naming.ldap.derefAliases property 088 * @return the enumeration for the environment 089 */ 090 public static AliasDerefMode getEnum( Map<String, Object> env ) 091 { 092 String property = ( String ) env.get( JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES ); 093 094 if ( null == property ) 095 { 096 return DEREF_ALWAYS; 097 } 098 else 099 { 100 if ( property.trim().equalsIgnoreCase( "always" ) ) 101 { 102 return DEREF_ALWAYS; 103 } 104 else if ( property.trim().equalsIgnoreCase( "never" ) ) 105 { 106 return NEVER_DEREF_ALIASES; 107 } 108 else if ( property.trim().equalsIgnoreCase( "finding" ) ) 109 { 110 return DEREF_FINDING_BASE_OBJ; 111 } 112 else if ( property.trim().equalsIgnoreCase( "searching" ) ) 113 { 114 return DEREF_IN_SEARCHING; 115 } 116 else 117 { 118 throw new IllegalArgumentException( I18n.err( I18n.ERR_04186, property, 119 JndiPropertyConstants.JNDI_LDAP_DAP_DEREF_ALIASES ) ); 120 } 121 } 122 } 123 124 125 /** 126 * Checks to see if we dereference while searching and finding the base. 127 * 128 * @return true if value is DEREF_ALWAYS, false otherwise 129 */ 130 public boolean isDerefAlways() 131 { 132 return this == DEREF_ALWAYS; 133 } 134 135 136 /** 137 * Checks to see if we never dereference aliases. 138 * 139 * @return true if value is NEVER_DEREF_ALIASES, false otherwise 140 */ 141 public boolean isNeverDeref() 142 { 143 return this == NEVER_DEREF_ALIASES; 144 } 145 146 147 /** 148 * Checks to see if we dereference while searching. 149 * 150 * @return true if value is DEREF_ALWAYS_VAL, or DEREF_IN_SEARCHING, and 151 * false otherwise. 152 */ 153 public boolean isDerefInSearching() 154 { 155 switch ( this ) 156 { 157 case DEREF_ALWAYS: 158 return true; 159 160 case DEREF_FINDING_BASE_OBJ: 161 return false; 162 163 case DEREF_IN_SEARCHING: 164 return true; 165 166 case NEVER_DEREF_ALIASES: 167 return false; 168 169 default: 170 throw new IllegalArgumentException( I18n.err( I18n.ERR_04187 ) ); 171 } 172 } 173 174 175 /** 176 * Checks to see if we dereference while finding the base. 177 * 178 * @return true if value is DEREF_ALWAYS, or DEREF_FINDING_BASE_OBJ, and 179 * false otherwise. 180 */ 181 public boolean isDerefFindingBase() 182 { 183 switch ( this ) 184 { 185 case DEREF_ALWAYS: 186 return true; 187 188 case DEREF_FINDING_BASE_OBJ: 189 return true; 190 191 case DEREF_IN_SEARCHING: 192 return false; 193 194 case NEVER_DEREF_ALIASES: 195 return false; 196 197 default: 198 throw new IllegalArgumentException( "Class has bug: check for valid enumeration values" ); 199 } 200 } 201 202 203 /** 204 * get the AliasDerefMode corresponding to the integer value passed 205 * 206 * @param val the AliasDerefMode's integer value 207 * @return the AliasDerefMode whose value is equivalent to the given integer value 208 */ 209 public static AliasDerefMode getDerefMode( int val ) 210 { 211 switch ( val ) 212 { 213 case 0: 214 return NEVER_DEREF_ALIASES; 215 216 case 1: 217 return DEREF_IN_SEARCHING; 218 219 case 2: 220 return DEREF_FINDING_BASE_OBJ; 221 222 case 3: 223 return DEREF_ALWAYS; 224 225 default: 226 throw new IllegalArgumentException( "Unknown derefmode " + val ); 227 } 228 } 229 230 231 /** 232 * get the AliasDerefMode corresponding to the string value {@link #jndiValue} passed 233 * 234 * @param val the AliasDerefMode's string value 235 * @return the AliasDerefMode whose value is equivalent to the given string value 236 */ 237 public static AliasDerefMode getDerefMode( String val ) 238 { 239 if ( val != null ) 240 { 241 if ( val.equals( NEVER_DEREF_ALIASES.jndiValue ) ) 242 { 243 return NEVER_DEREF_ALIASES; 244 } 245 246 if ( val.equals( DEREF_IN_SEARCHING.jndiValue ) ) 247 { 248 return DEREF_IN_SEARCHING; 249 } 250 251 if ( val.equals( DEREF_FINDING_BASE_OBJ.jndiValue ) ) 252 { 253 return DEREF_FINDING_BASE_OBJ; 254 } 255 256 if ( val.equals( DEREF_ALWAYS.jndiValue ) ) 257 { 258 return DEREF_ALWAYS; 259 } 260 } 261 262 throw new IllegalArgumentException( "Unknown derefmode " + val ); 263 } 264 265 266 public String getJndiValue() 267 { 268 return jndiValue; 269 } 270}