Coverage Report - org.apache.shiro.realm.ldap.LdapUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LdapUtils
20%
5/24
33%
2/6
2.667
 
 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  0
 public final class LdapUtils {
 39  
 
 40  
     /**
 41  
      * Private internal log instance.
 42  
      */
 43  1
     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  4
             if (ctx != null) {
 54  2
                 ctx.close();
 55  
             }
 56  0
         } catch (NamingException e) {
 57  0
             log.error("Exception while closing LDAP context. ", e);
 58  4
         }
 59  4
     }
 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  0
         Set<String> values = new HashSet<String>();
 70  0
         NamingEnumeration ne = null;
 71  
         try {
 72  0
             ne = attr.getAll();
 73  0
             while (ne.hasMore()) {
 74  0
                 String value = (String) ne.next();
 75  0
                 values.add(value);
 76  0
             }
 77  
         } finally {
 78  0
             closeEnumeration(ne);
 79  0
         }
 80  
 
 81  0
         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  0
             if (ne != null) {
 90  0
                 ne.close();
 91  
             }
 92  0
         } catch (NamingException e) {
 93  0
             log.error("Exception while closing NamingEnumeration: ", e);
 94  0
         }
 95  0
     }
 96  
 
 97  
 }