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
018import org.apache.juneau.cp.*;
019
020/**
021 * Utility class for finding resources for a class.
022 *
023 * <p>
024 * Same as {@link ClasspathResourceFinderSimple}, but first searches the working directory for the file before
025 * looking in the classpath.
026 * <br>Path traversals outside the working directory are not allowed for security reasons.
027 *
028 * @deprecated Use {@link SimpleResourceFinder}.
029 */
030@Deprecated
031public class ClasspathResourceFinderBasic extends ClasspathResourceFinderSimple {
032
033   /**
034    * Reusable instance.
035    */
036   public static final ClasspathResourceFinderBasic INSTANCE = new ClasspathResourceFinderBasic();
037
038   @Override /* ClasspathResourceFinder */
039   public InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException {
040      InputStream is = findFileSystemResource(name, locale);
041      if (is != null)
042         return is;
043      return findClasspathResource(baseClass, name, locale);
044   }
045
046   /**
047    * Workhorse method for retrieving a resource from the file system.
048    *
049    * <p>
050    * This method can be overridden by subclasses to provide customized handling of resource retrieval from file systems.
051    *
052    * @param name The resource name.
053    * @param locale
054    *    The resource locale.
055    *    <br>Can be <jk>null</jk>.
056    * @return The resource stream, or <jk>null</jk> if it couldn't be found.
057    * @throws IOException Thrown by underlying stream.
058    */
059   protected InputStream findFileSystemResource(String name, Locale locale) throws IOException {
060      if (name.indexOf("..") == -1) {
061         for (String n2 : getCandidateFileNames(name, locale)) {
062            File f = new File(n2);
063            if (f.exists() && f.canRead() && ! f.isAbsolute()) {
064               return new FileInputStream(f);
065            }
066         }
067      }
068      return null;
069   }
070}