001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.util.ldap; 017 018import java.util.LinkedHashMap; 019import java.util.Map; 020 021import javax.naming.directory.SearchControls; 022 023import org.ametys.runtime.i18n.I18nizableText; 024import org.ametys.runtime.model.Enumerator; 025 026/** 027 * {@link Enumerator} for listing scopes supported in a LDAP query. 028 */ 029public class ScopeEnumerator implements Enumerator<String> 030{ 031 private static final String __CORE_IMPL_CATALOGUE = "plugin.core-impl"; 032 private static final Map<String, I18nizableText> __SCOPES; 033 034 static 035 { 036 __SCOPES = new LinkedHashMap<>(3); 037 __SCOPES.put("object", new I18nizableText(__CORE_IMPL_CATALOGUE, "PLUGINS_CORE_USERS_LDAPUSER_CONFIG_SEARCH_SCOPE_ENUM_OBJECT")); 038 __SCOPES.put("one", new I18nizableText(__CORE_IMPL_CATALOGUE, "PLUGINS_CORE_USERS_LDAPUSER_CONFIG_SEARCH_SCOPE_ENUM_ONE")); 039 __SCOPES.put("sub", new I18nizableText(__CORE_IMPL_CATALOGUE, "PLUGINS_CORE_USERS_LDAPUSER_CONFIG_SEARCH_SCOPE_ENUM_SUB")); 040 } 041 042 /** 043 * Parses a scope config parameter into a <code>int</code> for using it as 044 * {@link SearchControls}. 045 * @param scope the scope string representation. 046 * @return the scope as a <code>SearchControls.*_SCOPE</code>. 047 * @throws IllegalArgumentException if the given scope is not valid. 048 */ 049 public static int parseScope(String scope) throws IllegalArgumentException 050 { 051 if ("one".equals(scope)) 052 { 053 return SearchControls.ONELEVEL_SCOPE; 054 } 055 else if ("sub".equals(scope)) 056 { 057 return SearchControls.SUBTREE_SCOPE; 058 } 059 else if ("object".equals(scope)) 060 { 061 return SearchControls.OBJECT_SCOPE; 062 } 063 else 064 { 065 throw new IllegalArgumentException("Invalid scope: " + scope); 066 } 067 } 068 069 public I18nizableText getEntry(String value) 070 { 071 return __SCOPES.get(value); 072 } 073 074 public Map<String, I18nizableText> getTypedEntries() 075 { 076 return __SCOPES; 077 } 078}