blob: 77d7709772328fa33d4c05d7ca37451f7416b446 [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>PrincipalTag.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-web</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.tags</a> &gt; <span class="el_source">PrincipalTag.java</span></div><h1>PrincipalTag.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.web.tags;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* &lt;p&gt;Tag used to print out the String value of a user's default principal,
* or a specific principal as specified by the tag's attributes.&lt;/p&gt;
*
* &lt;p&gt; If no attributes are specified, the tag prints out the &lt;tt&gt;toString()&lt;/tt&gt;
* value of the user's default principal. If the &lt;tt&gt;type&lt;/tt&gt; attribute
* is specified, the tag looks for a principal with the given type. If the
* &lt;tt&gt;property&lt;/tt&gt; attribute is specified, the tag prints the string value of
* the specified property of the principal. If no principal is found or the user
* is not authenticated, the tag displays nothing unless a &lt;tt&gt;defaultValue&lt;/tt&gt;
* is specified.&lt;/p&gt;
*
* @since 0.2
*/
<span class="nc" id="L45">public class PrincipalTag extends SecureTag {</span>
//TODO - complete JavaDoc
/*--------------------------------------------
| C O N S T A N T S |
============================================*/
/*--------------------------------------------
| I N S T A N C E V A R I A B L E S |
============================================*/
<span class="nc" id="L56"> private static final Logger log = LoggerFactory.getLogger(PrincipalTag.class);</span>
/**
* The type of principal to be retrieved, or null if the default principal should be used.
*/
private String type;
/**
* The property name to retrieve of the principal, or null if the &lt;tt&gt;toString()&lt;/tt&gt; value should be used.
*/
private String property;
/**
* The default value that should be displayed if the user is not authenticated, or no principal is found.
*/
private String defaultValue;
/*--------------------------------------------
| C O N S T R U C T O R S |
============================================*/
/*--------------------------------------------
| A C C E S S O R S / M O D I F I E R S |
============================================*/
public String getType() {
<span class="nc" id="L83"> return type;</span>
}
public void setType(String type) {
<span class="nc" id="L88"> this.type = type;</span>
<span class="nc" id="L89"> }</span>
public String getProperty() {
<span class="nc" id="L93"> return property;</span>
}
public void setProperty(String property) {
<span class="nc" id="L98"> this.property = property;</span>
<span class="nc" id="L99"> }</span>
public String getDefaultValue() {
<span class="nc" id="L103"> return defaultValue;</span>
}
public void setDefaultValue(String defaultValue) {
<span class="nc" id="L108"> this.defaultValue = defaultValue;</span>
<span class="nc" id="L109"> }</span>
/*--------------------------------------------
| M E T H O D S |
============================================*/
@SuppressWarnings({&quot;unchecked&quot;})
public int onDoStartTag() throws JspException {
<span class="nc" id="L118"> String strValue = null;</span>
<span class="nc bnc" id="L120" title="All 2 branches missed."> if (getSubject() != null) {</span>
// Get the principal to print out
Object principal;
<span class="nc bnc" id="L125" title="All 2 branches missed."> if (type == null) {</span>
<span class="nc" id="L126"> principal = getSubject().getPrincipal();</span>
} else {
<span class="nc" id="L128"> principal = getPrincipalFromClassName();</span>
}
// Get the string value of the principal
<span class="nc bnc" id="L132" title="All 2 branches missed."> if (principal != null) {</span>
<span class="nc bnc" id="L133" title="All 2 branches missed."> if (property == null) {</span>
<span class="nc" id="L134"> strValue = principal.toString();</span>
} else {
<span class="nc" id="L136"> strValue = getPrincipalProperty(principal, property);</span>
}
}
}
// Print out the principal value if not null
<span class="nc bnc" id="L143" title="All 2 branches missed."> if (strValue != null) {</span>
try {
<span class="nc" id="L145"> pageContext.getOut().write(strValue);</span>
<span class="nc" id="L146"> } catch (IOException e) {</span>
<span class="nc" id="L147"> throw new JspTagException(&quot;Error writing [&quot; + strValue + &quot;] to JSP.&quot;, e);</span>
<span class="nc" id="L148"> }</span>
}
<span class="nc" id="L151"> return SKIP_BODY;</span>
}
@SuppressWarnings({&quot;unchecked&quot;})
private Object getPrincipalFromClassName() {
<span class="nc" id="L156"> Object principal = null;</span>
try {
<span class="nc" id="L159"> Class cls = Class.forName(type);</span>
<span class="nc" id="L160"> principal = getSubject().getPrincipals().oneByType(cls);</span>
<span class="nc" id="L161"> } catch (ClassNotFoundException e) {</span>
<span class="nc bnc" id="L162" title="All 2 branches missed."> if (log.isErrorEnabled()) {</span>
<span class="nc" id="L163"> log.error(&quot;Unable to find class for name [&quot; + type + &quot;]&quot;);</span>
}
<span class="nc" id="L165"> }</span>
<span class="nc" id="L166"> return principal;</span>
}
private String getPrincipalProperty(Object principal, String property) throws JspTagException {
<span class="nc" id="L171"> String strValue = null;</span>
try {
<span class="nc" id="L174"> BeanInfo bi = Introspector.getBeanInfo(principal.getClass());</span>
// Loop through the properties to get the string value of the specified property
<span class="nc" id="L177"> boolean foundProperty = false;</span>
<span class="nc bnc" id="L178" title="All 2 branches missed."> for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {</span>
<span class="nc bnc" id="L179" title="All 2 branches missed."> if (pd.getName().equals(property)) {</span>
<span class="nc" id="L180"> Object value = pd.getReadMethod().invoke(principal, (Object[]) null);</span>
<span class="nc" id="L181"> strValue = String.valueOf(value);</span>
<span class="nc" id="L182"> foundProperty = true;</span>
<span class="nc" id="L183"> break;</span>
}
}
<span class="nc bnc" id="L187" title="All 2 branches missed."> if (!foundProperty) {</span>
<span class="nc" id="L188"> final String message = &quot;Property [&quot; + property + &quot;] not found in principal of type [&quot; + principal.getClass().getName() + &quot;]&quot;;</span>
<span class="nc bnc" id="L189" title="All 2 branches missed."> if (log.isErrorEnabled()) {</span>
<span class="nc" id="L190"> log.error(message);</span>
}
<span class="nc" id="L192"> throw new JspTagException(message);</span>
}
<span class="nc" id="L195"> } catch (Exception e) {</span>
<span class="nc" id="L196"> final String message = &quot;Error reading property [&quot; + property + &quot;] from principal of type [&quot; + principal.getClass().getName() + &quot;]&quot;;</span>
<span class="nc bnc" id="L197" title="All 2 branches missed."> if (log.isErrorEnabled()) {</span>
<span class="nc" id="L198"> log.error(message, e);</span>
}
<span class="nc" id="L200"> throw new JspTagException(message, e);</span>
<span class="nc" id="L201"> }</span>
<span class="nc" id="L203"> return strValue;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>