001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.utils; 014 015import java.io.*; 016import java.util.*; 017 018/** 019 * Utility class for finding resources for a class. 020 * 021 * <p> 022 * Same as {@link ClasspathResourceFinderSimple}, but if the resource cannot be found in the classpath, then an attempt 023 * is made to look in the JVM working directory. 024 * <br>Path traversals outside the working directory are not allowed for security reasons. 025 */ 026public class ClasspathResourceFinderBasic extends ClasspathResourceFinderSimple { 027 028 @Override /* ClasspathResourceFinder */ 029 public InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException { 030 InputStream is = findClasspathResource(baseClass, name, locale); 031 if (is != null) 032 return is; 033 return findFileSystemResource(name, locale); 034 } 035 036 /** 037 * Workhorse method for retrieving a resource from the file system. 038 * 039 * <p> 040 * This method can be overridden by subclasses to provide customized handling of resource retrieval from file systems. 041 * 042 * @param name The resource name. 043 * @param locale 044 * The resource locale. 045 * <br>Can be <jk>null</jk>. 046 * @return The resource stream, or <jk>null</jk> if it couldn't be found. 047 * @throws IOException 048 */ 049 protected InputStream findFileSystemResource(String name, Locale locale) throws IOException { 050 if (name.indexOf("..") == -1) { 051 for (String n2 : getCandidateFileNames(name, locale)) { 052 File f = new File(n2); 053 if (f.exists() && f.canRead() && ! f.isAbsolute()) { 054 return new FileInputStream(f); 055 } 056 } 057 } 058 return null; 059 } 060}