| <?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 :: Core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.io</a> > <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 |
| * "License"); 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 |
| * "AS IS" 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 <b>{@code classpath:}</b> |
| */ |
| public static final String CLASSPATH_PREFIX = "classpath:"; |
| /** |
| * Resource path prefix that specifies to load from a url location, value is <b>{@code url:}</b> |
| */ |
| public static final String URL_PREFIX = "url:"; |
| /** |
| * Resource path prefix that specifies to load from a file location, value is <b>{@code file:}</b> |
| */ |
| public static final String FILE_PREFIX = "file:"; |
| |
| /** |
| * Private internal log instance. |
| */ |
| <span class="fc" id="L54"> private static final Logger log = LoggerFactory.getLogger(ResourceUtils.class);</span> |
| |
| /** |
| * Prevent instantiation. |
| */ |
| <span class="nc" id="L59"> private ResourceUtils() {</span> |
| <span class="nc" id="L60"> }</span> |
| |
| /** |
| * 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({"UnusedDeclaration"}) |
| public static boolean hasResourcePrefix(String resourcePath) { |
| <span class="nc bnc" id="L74" title="All 2 branches missed."> return resourcePath != null &&</span> |
| <span class="nc bnc" id="L75" title="All 2 branches missed."> (resourcePath.startsWith(CLASSPATH_PREFIX) ||</span> |
| <span class="nc bnc" id="L76" title="All 2 branches missed."> resourcePath.startsWith(URL_PREFIX) ||</span> |
| <span class="nc bnc" id="L77" title="All 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="5 of 6 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 InputStraem 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="pc bpc" id="L125" title="1 of 2 branches missed."> if (resourcePath.startsWith(CLASSPATH_PREFIX)) {</span> |
| <span class="fc" id="L126"> is = loadFromClassPath(stripPrefix(resourcePath));</span> |
| |
| <span class="nc bnc" id="L128" title="All 2 branches missed."> } else if (resourcePath.startsWith(URL_PREFIX)) {</span> |
| <span class="nc" 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("Resource [" + resourcePath + "] could not be found.");</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("Opening file [" + path + "]...");</span> |
| } |
| <span class="nc" id="L149"> return new FileInputStream(path);</span> |
| } |
| |
| private static InputStream loadFromUrl(String urlPath) throws IOException { |
| <span class="nc" id="L153"> log.debug("Opening url {}", urlPath);</span> |
| <span class="nc" id="L154"> URL url = new URL(urlPath);</span> |
| <span class="nc" id="L155"> return url.openStream();</span> |
| } |
| |
| private static InputStream loadFromClassPath(String path) { |
| <span class="fc" id="L159"> log.debug("Opening resource from class path [{}]", 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(":") + 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("Error closing input stream.", 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.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |