blob: ef3371040117001e2bb9a454559e643a198a9574 [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>SimplePrincipalCollection.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 :: Test Coverage</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.subject</a> &gt; <span class="el_source">SimplePrincipalCollection.java</span></div><h1>SimplePrincipalCollection.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.subject;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.util.StringUtils;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
/**
* A simple implementation of the {@link MutablePrincipalCollection} interface that tracks principals internally
* by storing them in a {@link LinkedHashMap}.
*
* @since 0.9
*/
@SuppressWarnings({&quot;unchecked&quot;})
public class SimplePrincipalCollection implements MutablePrincipalCollection {
// Serialization reminder:
// You _MUST_ change this number if you introduce a change to this class
// that is NOT serialization backwards compatible. Serialization-compatible
// changes do not require a change to this number. If you need to generate
// a new number in this case, use the JDK's 'serialver' program to generate it.
private static final long serialVersionUID = -6305224034025797558L;
//TODO - complete JavaDoc
private Map&lt;String, Set&gt; realmPrincipals;
private transient String cachedToString; //cached toString() result, as this can be printed many times in logging
<span class="fc" id="L52"> public SimplePrincipalCollection() {</span>
<span class="fc" id="L53"> }</span>
<span class="fc" id="L55"> public SimplePrincipalCollection(Object principal, String realmName) {</span>
<span class="fc bfc" id="L56" title="All 2 branches covered."> if (principal instanceof Collection) {</span>
<span class="fc" id="L57"> addAll((Collection) principal, realmName);</span>
} else {
<span class="fc" id="L59"> add(principal, realmName);</span>
}
<span class="fc" id="L61"> }</span>
<span class="fc" id="L63"> public SimplePrincipalCollection(Collection principals, String realmName) {</span>
<span class="fc" id="L64"> addAll(principals, realmName);</span>
<span class="fc" id="L65"> }</span>
<span class="fc" id="L67"> public SimplePrincipalCollection(PrincipalCollection principals) {</span>
<span class="fc" id="L68"> addAll(principals);</span>
<span class="fc" id="L69"> }</span>
protected Collection getPrincipalsLazy(String realmName) {
<span class="fc bfc" id="L72" title="All 2 branches covered."> if (realmPrincipals == null) {</span>
<span class="fc" id="L73"> realmPrincipals = new LinkedHashMap&lt;String, Set&gt;();</span>
}
<span class="fc" id="L75"> Set principals = realmPrincipals.get(realmName);</span>
<span class="fc bfc" id="L76" title="All 2 branches covered."> if (principals == null) {</span>
<span class="fc" id="L77"> principals = new LinkedHashSet();</span>
<span class="fc" id="L78"> realmPrincipals.put(realmName, principals);</span>
}
<span class="fc" id="L80"> return principals;</span>
}
/**
* Returns the first available principal from any of the {@code Realm} principals, or {@code null} if there are
* no principals yet.
* &lt;p/&gt;
* The 'first available principal' is interpreted as the principal that would be returned by
* &lt;code&gt;{@link #iterator() iterator()}.{@link java.util.Iterator#next() next()}.&lt;/code&gt;
*
* @inheritDoc
*/
public Object getPrimaryPrincipal() {
<span class="pc bpc" id="L93" title="1 of 2 branches missed."> if (isEmpty()) {</span>
<span class="nc" id="L94"> return null;</span>
}
<span class="fc" id="L96"> return iterator().next();</span>
}
public void add(Object principal, String realmName) {
<span class="pc bpc" id="L100" title="1 of 2 branches missed."> if (realmName == null) {</span>
<span class="nc" id="L101"> throw new IllegalArgumentException(&quot;realmName argument cannot be null.&quot;);</span>
}
<span class="pc bpc" id="L103" title="1 of 2 branches missed."> if (principal == null) {</span>
<span class="nc" id="L104"> throw new IllegalArgumentException(&quot;principal argument cannot be null.&quot;);</span>
}
<span class="fc" id="L106"> this.cachedToString = null;</span>
<span class="fc" id="L107"> getPrincipalsLazy(realmName).add(principal);</span>
<span class="fc" id="L108"> }</span>
public void addAll(Collection principals, String realmName) {
<span class="pc bpc" id="L111" title="1 of 2 branches missed."> if (realmName == null) {</span>
<span class="nc" id="L112"> throw new IllegalArgumentException(&quot;realmName argument cannot be null.&quot;);</span>
}
<span class="pc bpc" id="L114" title="1 of 2 branches missed."> if (principals == null) {</span>
<span class="nc" id="L115"> throw new IllegalArgumentException(&quot;principals argument cannot be null.&quot;);</span>
}
<span class="pc bpc" id="L117" title="1 of 2 branches missed."> if (principals.isEmpty()) {</span>
<span class="nc" id="L118"> throw new IllegalArgumentException(&quot;principals argument cannot be an empty collection.&quot;);</span>
}
<span class="fc" id="L120"> this.cachedToString = null;</span>
<span class="fc" id="L121"> getPrincipalsLazy(realmName).addAll(principals);</span>
<span class="fc" id="L122"> }</span>
public void addAll(PrincipalCollection principals) {
<span class="pc bpc" id="L125" title="1 of 2 branches missed."> if (principals.getRealmNames() != null) {</span>
<span class="fc bfc" id="L126" title="All 2 branches covered."> for (String realmName : principals.getRealmNames()) {</span>
<span class="fc bfc" id="L127" title="All 2 branches covered."> for (Object principal : principals.fromRealm(realmName)) {</span>
<span class="fc" id="L128"> add(principal, realmName);</span>
<span class="fc" id="L129"> }</span>
<span class="fc" id="L130"> }</span>
}
<span class="fc" id="L132"> }</span>
public &lt;T&gt; T oneByType(Class&lt;T&gt; type) {
<span class="pc bpc" id="L135" title="2 of 4 branches missed."> if (realmPrincipals == null || realmPrincipals.isEmpty()) {</span>
<span class="nc" id="L136"> return null;</span>
}
<span class="fc" id="L138"> Collection&lt;Set&gt; values = realmPrincipals.values();</span>
<span class="fc bfc" id="L139" title="All 2 branches covered."> for (Set set : values) {</span>
<span class="fc bfc" id="L140" title="All 2 branches covered."> for (Object o : set) {</span>
<span class="fc bfc" id="L141" title="All 2 branches covered."> if (type.isAssignableFrom(o.getClass())) {</span>
<span class="fc" id="L142"> return (T) o;</span>
}
<span class="fc" id="L144"> }</span>
<span class="fc" id="L145"> }</span>
<span class="fc" id="L146"> return null;</span>
}
public &lt;T&gt; Collection&lt;T&gt; byType(Class&lt;T&gt; type) {
<span class="nc bnc" id="L150" title="All 4 branches missed."> if (realmPrincipals == null || realmPrincipals.isEmpty()) {</span>
<span class="nc" id="L151"> return Collections.EMPTY_SET;</span>
}
<span class="nc" id="L153"> Set&lt;T&gt; typed = new LinkedHashSet&lt;T&gt;();</span>
<span class="nc" id="L154"> Collection&lt;Set&gt; values = realmPrincipals.values();</span>
<span class="nc bnc" id="L155" title="All 2 branches missed."> for (Set set : values) {</span>
<span class="nc bnc" id="L156" title="All 2 branches missed."> for (Object o : set) {</span>
<span class="nc bnc" id="L157" title="All 2 branches missed."> if (type.isAssignableFrom(o.getClass())) {</span>
<span class="nc" id="L158"> typed.add((T) o);</span>
}
<span class="nc" id="L160"> }</span>
<span class="nc" id="L161"> }</span>
<span class="nc bnc" id="L162" title="All 2 branches missed."> if (typed.isEmpty()) {</span>
<span class="nc" id="L163"> return Collections.EMPTY_SET;</span>
}
<span class="nc" id="L165"> return Collections.unmodifiableSet(typed);</span>
}
public List asList() {
<span class="fc" id="L169"> Set all = asSet();</span>
<span class="pc bpc" id="L170" title="1 of 2 branches missed."> if (all.isEmpty()) {</span>
<span class="nc" id="L171"> return Collections.EMPTY_LIST;</span>
}
<span class="fc" id="L173"> return Collections.unmodifiableList(new ArrayList(all));</span>
}
public Set asSet() {
<span class="pc bpc" id="L177" title="2 of 4 branches missed."> if (realmPrincipals == null || realmPrincipals.isEmpty()) {</span>
<span class="nc" id="L178"> return Collections.EMPTY_SET;</span>
}
<span class="fc" id="L180"> Set aggregated = new LinkedHashSet();</span>
<span class="fc" id="L181"> Collection&lt;Set&gt; values = realmPrincipals.values();</span>
<span class="fc bfc" id="L182" title="All 2 branches covered."> for (Set set : values) {</span>
<span class="fc" id="L183"> aggregated.addAll(set);</span>
<span class="fc" id="L184"> }</span>
<span class="pc bpc" id="L185" title="1 of 2 branches missed."> if (aggregated.isEmpty()) {</span>
<span class="nc" id="L186"> return Collections.EMPTY_SET;</span>
}
<span class="fc" id="L188"> return Collections.unmodifiableSet(aggregated);</span>
}
public Collection fromRealm(String realmName) {
<span class="pc bpc" id="L192" title="2 of 4 branches missed."> if (realmPrincipals == null || realmPrincipals.isEmpty()) {</span>
<span class="nc" id="L193"> return Collections.EMPTY_SET;</span>
}
<span class="fc" id="L195"> Set principals = realmPrincipals.get(realmName);</span>
<span class="pc bpc" id="L196" title="1 of 4 branches missed."> if (principals == null || principals.isEmpty()) {</span>
<span class="fc" id="L197"> principals = Collections.EMPTY_SET;</span>
}
<span class="fc" id="L199"> return Collections.unmodifiableSet(principals);</span>
}
public Set&lt;String&gt; getRealmNames() {
<span class="pc bpc" id="L203" title="1 of 2 branches missed."> if (realmPrincipals == null) {</span>
<span class="nc" id="L204"> return null;</span>
} else {
<span class="fc" id="L206"> return realmPrincipals.keySet();</span>
}
}
public boolean isEmpty() {
<span class="pc bpc" id="L211" title="2 of 4 branches missed."> return realmPrincipals == null || realmPrincipals.isEmpty();</span>
}
public void clear() {
<span class="nc" id="L215"> this.cachedToString = null;</span>
<span class="nc bnc" id="L216" title="All 2 branches missed."> if (realmPrincipals != null) {</span>
<span class="nc" id="L217"> realmPrincipals.clear();</span>
<span class="nc" id="L218"> realmPrincipals = null;</span>
}
<span class="nc" id="L220"> }</span>
public Iterator iterator() {
<span class="fc" id="L223"> return asSet().iterator();</span>
}
public boolean equals(Object o) {
<span class="fc bfc" id="L227" title="All 2 branches covered."> if (o == this) {</span>
<span class="fc" id="L228"> return true;</span>
}
<span class="pc bpc" id="L230" title="1 of 2 branches missed."> if (o instanceof SimplePrincipalCollection) {</span>
<span class="nc" id="L231"> SimplePrincipalCollection other = (SimplePrincipalCollection) o;</span>
<span class="nc bnc" id="L232" title="All 4 branches missed."> return this.realmPrincipals != null ? this.realmPrincipals.equals(other.realmPrincipals) : other.realmPrincipals == null;</span>
}
<span class="fc" id="L234"> return false;</span>
}
public int hashCode() {
<span class="pc bpc" id="L238" title="2 of 4 branches missed."> if (this.realmPrincipals != null &amp;&amp; !realmPrincipals.isEmpty()) {</span>
<span class="fc" id="L239"> return realmPrincipals.hashCode();</span>
}
<span class="nc" id="L241"> return super.hashCode();</span>
}
/**
* Returns a simple string representation suitable for printing.
*
* @return a simple string representation suitable for printing.
* @since 1.0
*/
public String toString() {
<span class="fc bfc" id="L251" title="All 2 branches covered."> if (this.cachedToString == null) {</span>
<span class="fc" id="L252"> Set&lt;Object&gt; principals = asSet();</span>
<span class="pc bpc" id="L253" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(principals)) {</span>
<span class="fc" id="L254"> this.cachedToString = StringUtils.toString(principals.toArray());</span>
} else {
<span class="nc" id="L256"> this.cachedToString = &quot;empty&quot;;</span>
}
}
<span class="fc" id="L259"> return this.cachedToString;</span>
}
/**
* Serialization write support.
* &lt;p/&gt;
* NOTE: Don't forget to change the serialVersionUID constant at the top of this class
* if you make any backwards-incompatible serialization changes!!!
* (use the JDK 'serialver' program for this)
*
* @param out output stream provided by Java serialization
* @throws IOException if there is a stream error
*/
private void writeObject(ObjectOutputStream out) throws IOException {
<span class="fc" id="L274"> out.defaultWriteObject();</span>
<span class="pc bpc" id="L275" title="1 of 2 branches missed."> boolean principalsExist = !CollectionUtils.isEmpty(realmPrincipals);</span>
<span class="fc" id="L276"> out.writeBoolean(principalsExist);</span>
<span class="pc bpc" id="L277" title="1 of 2 branches missed."> if (principalsExist) {</span>
<span class="fc" id="L278"> out.writeObject(realmPrincipals);</span>
}
<span class="fc" id="L280"> }</span>
/**
* Serialization read support - reads in the Map principals collection if it exists in the
* input stream.
* &lt;p/&gt;
* NOTE: Don't forget to change the serialVersionUID constant at the top of this class
* if you make any backwards-incompatible serialization changes!!!
* (use the JDK 'serialver' program for this)
*
* @param in input stream provided by
* @throws IOException if there is an input/output problem
* @throws ClassNotFoundException if the underlying Map implementation class is not available to the classloader.
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
<span class="fc" id="L295"> in.defaultReadObject();</span>
<span class="fc" id="L296"> boolean principalsExist = in.readBoolean();</span>
<span class="pc bpc" id="L297" title="1 of 2 branches missed."> if (principalsExist) {</span>
<span class="fc" id="L298"> this.realmPrincipals = (Map&lt;String, Set&gt;) in.readObject();</span>
}
<span class="fc" id="L300"> }</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>