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