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  package org.apache.shiro.realm.ldap;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Set;
24  import javax.naming.NamingEnumeration;
25  import javax.naming.NamingException;
26  import javax.naming.directory.Attribute;
27  import javax.naming.ldap.LdapContext;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Utility class providing static methods to make working with LDAP
34   * easier.
35   *
36   * @since 0.2
37   */
38  public final class LdapUtils {
39  
40      /**
41       * Private internal log instance.
42       */
43      private static final Logger log = LoggerFactory.getLogger(LdapUtils.class);
44  
45      /**
46       * Closes an LDAP context, logging any errors, but not throwing
47       * an exception if there is a failure.
48       *
49       * @param ctx the LDAP context to close.
50       */
51      public static void closeContext(LdapContext ctx) {
52          try {
53              if (ctx != null) {
54                  ctx.close();
55              }
56          } catch (NamingException e) {
57              log.error("Exception while closing LDAP context. ", e);
58          }
59      }
60  
61      /**
62       * Helper method used to retrieve all attribute values from a particular context attribute.
63       *
64       * @param attr the LDAP attribute.
65       * @return the values of the attribute.
66       * @throws javax.naming.NamingException if there is an LDAP error while reading the values.
67       */
68      public static Collection<String> getAllAttributeValues(Attribute attr) throws NamingException {
69          Set<String> values = new HashSet<String>();
70          NamingEnumeration ne = null;
71          try {
72              ne = attr.getAll();
73              while (ne.hasMore()) {
74                  String value = (String) ne.next();
75                  values.add(value);
76              }
77          } finally {
78              closeEnumeration(ne);
79          }
80  
81          return values;
82      }
83  
84      //added based on SHIRO-127, per Emmanuel's comment [1]
85      // [1] https://issues.apache.org/jira/browse/SHIRO-127?focusedCommentId=12891380&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12891380
86  
87      public static void closeEnumeration(NamingEnumeration ne) {
88          try {
89              if (ne != null) {
90                  ne.close();
91              }
92          } catch (NamingException e) {
93              log.error("Exception while closing NamingEnumeration: ", e);
94          }
95      }
96  
97  }