blob: d5c364d2b20d3e6e9f2ef283e053cb2a38d731db [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>LdapUtils.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.realm.ldap</a> &gt; <span class="el_source">LdapUtils.java</span></div><h1>LdapUtils.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.realm.ldap;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.ldap.LdapContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class providing static methods to make working with LDAP
* easier.
*
* @since 0.2
*/
<span class="nc" id="L38">public final class LdapUtils {</span>
/**
* Private internal log instance.
*/
<span class="fc" id="L43"> private static final Logger log = LoggerFactory.getLogger(LdapUtils.class);</span>
/**
* Closes an LDAP context, logging any errors, but not throwing
* an exception if there is a failure.
*
* @param ctx the LDAP context to close.
*/
public static void closeContext(LdapContext ctx) {
try {
<span class="fc bfc" id="L53" title="All 2 branches covered."> if (ctx != null) {</span>
<span class="fc" id="L54"> ctx.close();</span>
}
<span class="nc" id="L56"> } catch (NamingException e) {</span>
<span class="nc" id="L57"> log.error(&quot;Exception while closing LDAP context. &quot;, e);</span>
<span class="fc" id="L58"> }</span>
<span class="fc" id="L59"> }</span>
/**
* Helper method used to retrieve all attribute values from a particular context attribute.
*
* @param attr the LDAP attribute.
* @return the values of the attribute.
* @throws javax.naming.NamingException if there is an LDAP error while reading the values.
*/
public static Collection&lt;String&gt; getAllAttributeValues(Attribute attr) throws NamingException {
<span class="nc" id="L69"> Set&lt;String&gt; values = new HashSet&lt;String&gt;();</span>
<span class="nc" id="L70"> NamingEnumeration ne = null;</span>
try {
<span class="nc" id="L72"> ne = attr.getAll();</span>
<span class="nc bnc" id="L73" title="All 2 branches missed."> while (ne.hasMore()) {</span>
<span class="nc" id="L74"> String value = (String) ne.next();</span>
<span class="nc" id="L75"> values.add(value);</span>
<span class="nc" id="L76"> }</span>
} finally {
<span class="nc" id="L78"> closeEnumeration(ne);</span>
<span class="nc" id="L79"> }</span>
<span class="nc" id="L81"> return values;</span>
}
//added based on SHIRO-127, per Emmanuel's comment [1]
// [1] https://issues.apache.org/jira/browse/SHIRO-127?focusedCommentId=12891380&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12891380
public static void closeEnumeration(NamingEnumeration ne) {
try {
<span class="nc bnc" id="L89" title="All 2 branches missed."> if (ne != null) {</span>
<span class="nc" id="L90"> ne.close();</span>
}
<span class="nc" id="L92"> } catch (NamingException e) {</span>
<span class="nc" id="L93"> log.error(&quot;Exception while closing NamingEnumeration: &quot;, e);</span>
<span class="nc" id="L94"> }</span>
<span class="nc" id="L95"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>