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    package org.apache.shiro.realm.ldap;
020    
021    import java.util.Collection;
022    import java.util.HashSet;
023    import java.util.Set;
024    import javax.naming.NamingEnumeration;
025    import javax.naming.NamingException;
026    import javax.naming.directory.Attribute;
027    import javax.naming.ldap.LdapContext;
028    
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     * Utility class providing static methods to make working with LDAP
034     * easier.
035     *
036     * @since 0.2
037     */
038    public final class LdapUtils {
039    
040        /**
041         * Private internal log instance.
042         */
043        private static final Logger log = LoggerFactory.getLogger(LdapUtils.class);
044    
045        /**
046         * Closes an LDAP context, logging any errors, but not throwing
047         * an exception if there is a failure.
048         *
049         * @param ctx the LDAP context to close.
050         */
051        public static void closeContext(LdapContext ctx) {
052            try {
053                if (ctx != null) {
054                    ctx.close();
055                }
056            } catch (NamingException e) {
057                log.error("Exception while closing LDAP context. ", e);
058            }
059        }
060    
061        /**
062         * Helper method used to retrieve all attribute values from a particular context attribute.
063         *
064         * @param attr the LDAP attribute.
065         * @return the values of the attribute.
066         * @throws javax.naming.NamingException if there is an LDAP error while reading the values.
067         */
068        public static Collection<String> getAllAttributeValues(Attribute attr) throws NamingException {
069            Set<String> values = new HashSet<String>();
070            NamingEnumeration ne = null;
071            try {
072                ne = attr.getAll();
073                while (ne.hasMore()) {
074                    String value = (String) ne.next();
075                    values.add(value);
076                }
077            } finally {
078                closeEnumeration(ne);
079            }
080    
081            return values;
082        }
083    
084        //added based on SHIRO-127, per Emmanuel's comment [1]
085        // [1] https://issues.apache.org/jira/browse/SHIRO-127?focusedCommentId=12891380&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12891380
086    
087        public static void closeEnumeration(NamingEnumeration ne) {
088            try {
089                if (ne != null) {
090                    ne.close();
091                }
092            } catch (NamingException e) {
093                log.error("Exception while closing NamingEnumeration: ", e);
094            }
095        }
096    
097    }