blob: c3f95ae94b8b67101dc918e5aa8a87f5712f4a80 [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>ResourceUtils.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-lang</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.io</a> &gt; <span class="el_source">ResourceUtils.java</span></div><h1>ResourceUtils.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.io;
import org.apache.shiro.util.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Static helper methods for loading {@code Stream}-backed resources.
*
* @see #getInputStreamForPath(String)
* @since 0.2
*/
public class ResourceUtils {
/**
* Resource path prefix that specifies to load from a classpath location, value is &lt;b&gt;{@code classpath:}&lt;/b&gt;
*/
public static final String CLASSPATH_PREFIX = &quot;classpath:&quot;;
/**
* Resource path prefix that specifies to load from a url location, value is &lt;b&gt;{@code url:}&lt;/b&gt;
*/
public static final String URL_PREFIX = &quot;url:&quot;;
/**
* Resource path prefix that specifies to load from a file location, value is &lt;b&gt;{@code file:}&lt;/b&gt;
*/
public static final String FILE_PREFIX = &quot;file:&quot;;
/**
* Private internal log instance.
*/
<span class="fc" id="L54"> private static final Logger log = LoggerFactory.getLogger(ResourceUtils.class);</span>
/**
* Prevent instantiation.
*/
private ResourceUtils() {
}
/**
* Returns {@code true} if the resource path is not null and starts with one of the recognized
* resource prefixes ({@link #CLASSPATH_PREFIX CLASSPATH_PREFIX},
* {@link #URL_PREFIX URL_PREFIX}, or {@link #FILE_PREFIX FILE_PREFIX}), {@code false} otherwise.
*
* @param resourcePath the resource path to check
* @return {@code true} if the resource path is not null and starts with one of the recognized
* resource prefixes, {@code false} otherwise.
* @since 0.9
*/
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
public static boolean hasResourcePrefix(String resourcePath) {
<span class="pc bpc" id="L74" title="1 of 2 branches missed."> return resourcePath != null &amp;&amp;</span>
<span class="fc bfc" id="L75" title="All 2 branches covered."> (resourcePath.startsWith(CLASSPATH_PREFIX) ||</span>
<span class="pc bpc" id="L76" title="1 of 2 branches missed."> resourcePath.startsWith(URL_PREFIX) ||</span>
<span class="pc bpc" id="L77" title="1 of 2 branches missed."> resourcePath.startsWith(FILE_PREFIX));</span>
}
/**
* Returns {@code true} if the resource at the specified path exists, {@code false} otherwise. This
* method supports scheme prefixes on the path as defined in {@link #getInputStreamForPath(String)}.
*
* @param resourcePath the path of the resource to check.
* @return {@code true} if the resource at the specified path exists, {@code false} otherwise.
* @since 0.9
*/
public static boolean resourceExists(String resourcePath) {
<span class="fc" id="L89"> InputStream stream = null;</span>
<span class="fc" id="L90"> boolean exists = false;</span>
try {
<span class="nc" id="L93"> stream = getInputStreamForPath(resourcePath);</span>
<span class="nc" id="L94"> exists = true;</span>
<span class="fc" id="L95"> } catch (IOException e) {</span>
<span class="fc" id="L96"> stream = null;</span>
} finally {
<span class="pc bpc" id="L98" title="1 of 2 branches missed."> if (stream != null) {</span>
try {
<span class="nc" id="L100"> stream.close();</span>
<span class="nc" id="L101"> } catch (IOException ignored) {</span>
<span class="nc" id="L102"> }</span>
}
}
<span class="fc" id="L106"> return exists;</span>
}
/**
* Returns the InputStream for the resource represented by the specified path, supporting scheme
* prefixes that direct how to acquire the input stream
* ({@link #CLASSPATH_PREFIX CLASSPATH_PREFIX},
* {@link #URL_PREFIX URL_PREFIX}, or {@link #FILE_PREFIX FILE_PREFIX}). If the path is not prefixed by one
* of these schemes, the path is assumed to be a file-based path that can be loaded with a
* {@link FileInputStream FileInputStream}.
*
* @param resourcePath the String path representing the resource to obtain.
* @return the InputStream for the specified resource.
* @throws IOException if there is a problem acquiring the resource at the specified path.
*/
public static InputStream getInputStreamForPath(String resourcePath) throws IOException {
InputStream is;
<span class="fc bfc" id="L125" title="All 2 branches covered."> if (resourcePath.startsWith(CLASSPATH_PREFIX)) {</span>
<span class="fc" id="L126"> is = loadFromClassPath(stripPrefix(resourcePath));</span>
<span class="pc bpc" id="L128" title="1 of 2 branches missed."> } else if (resourcePath.startsWith(URL_PREFIX)) {</span>
<span class="fc" id="L129"> is = loadFromUrl(stripPrefix(resourcePath));</span>
<span class="nc bnc" id="L131" title="All 2 branches missed."> } else if (resourcePath.startsWith(FILE_PREFIX)) {</span>
<span class="nc" id="L132"> is = loadFromFile(stripPrefix(resourcePath));</span>
} else {
<span class="nc" id="L135"> is = loadFromFile(resourcePath);</span>
}
<span class="fc bfc" id="L138" title="All 2 branches covered."> if (is == null) {</span>
<span class="fc" id="L139"> throw new IOException(&quot;Resource [&quot; + resourcePath + &quot;] could not be found.&quot;);</span>
}
<span class="fc" id="L142"> return is;</span>
}
private static InputStream loadFromFile(String path) throws IOException {
<span class="nc bnc" id="L146" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L147"> log.debug(&quot;Opening file [&quot; + path + &quot;]...&quot;);</span>
}
<span class="nc" id="L149"> return new FileInputStream(path);</span>
}
private static InputStream loadFromUrl(String urlPath) throws IOException {
<span class="fc" id="L153"> log.debug(&quot;Opening url {}&quot;, urlPath);</span>
<span class="fc" id="L154"> URL url = new URL(urlPath);</span>
<span class="fc" id="L155"> return url.openStream();</span>
}
private static InputStream loadFromClassPath(String path) {
<span class="fc" id="L159"> log.debug(&quot;Opening resource from class path [{}]&quot;, path);</span>
<span class="fc" id="L160"> return ClassUtils.getResourceAsStream(path);</span>
}
private static String stripPrefix(String resourcePath) {
<span class="fc" id="L164"> return resourcePath.substring(resourcePath.indexOf(&quot;:&quot;) + 1);</span>
}
/**
* Convenience method that closes the specified {@link InputStream InputStream}, logging any
* {@link IOException IOException} that might occur. If the {@code InputStream}
* argument is {@code null}, this method does nothing. It returns quietly in all cases.
*
* @param is the {@code InputStream} to close, logging any {@code IOException} that might occur.
*/
public static void close(InputStream is) {
<span class="pc bpc" id="L175" title="1 of 2 branches missed."> if (is != null) {</span>
try {
<span class="fc" id="L177"> is.close();</span>
<span class="nc" id="L178"> } catch (IOException e) {</span>
<span class="nc" id="L179"> log.warn(&quot;Error closing input stream.&quot;, e);</span>
<span class="fc" id="L180"> }</span>
}
<span class="fc" id="L182"> }</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>