Class BasicFileFinder

java.lang.Object
org.apache.juneau.cp.BasicFileFinder
All Implemented Interfaces:
FileFinder

public class BasicFileFinder extends Object implements FileFinder
Basic implementation of a FileFinder.

Specialized behavior can be implemented by overridding the find(String, Locale) method.

Example:

public class MyFileFinder extends BasicFileFinder { @Override protected Optional<InputStream> find(String name, Locale locale) throws IOException { // Do special handling or just call super.find(). return super.find(name, locale); } }

See Also:
  • Constructor Details

  • Method Details

    • getStream

      public final Optional<InputStream> getStream(String name, Locale locale) throws IOException
      Description copied from interface: FileFinder
      Returns the contents of the resource with the specified name.
      Specified by:
      getStream in interface FileFinder
      Parameters:
      name - The resource name. See Class.getResource(String) for format.
      locale - The locale of the resource to retrieve.
      If null, won't look for localized file names.
      Returns:
      The resolved resource contents, or null if the resource was not found.
      Throws:
      IOException - Thrown by underlying stream.
    • getString

      public Optional<String> getString(String name, Locale locale) throws IOException
      Description copied from interface: FileFinder
      Returns the file with the specified name as a string.
      Specified by:
      getString in interface FileFinder
      Parameters:
      name - The file name.
      locale - The locale of the resource to retrieve.
      If null, won't look for localized file names.
      Returns:
      The contents of the file as a string. Assumes UTF-8 encoding.
      Throws:
      IOException - If file could not be read.
    • find

      protected Optional<InputStream> find(String name, Locale locale) throws IOException
      The main implementation method for finding files.

      Subclasses can override this method to provide their own handling.

      Parameters:
      name - The resource name. See Class.getResource(String) for format.
      locale - The locale of the resource to retrieve.
      If null, won't look for localized file names.
      Returns:
      The resolved resource contents, or null if the resource was not found.
      Throws:
      IOException - Thrown by underlying stream.
    • getCandidateFileNames

      protected List<String> getCandidateFileNames(String fileName, Locale locale)
      Returns the candidate file names for the specified file name in the specified locale.

      For example, if looking for the "MyResource.txt" file in the Japanese locale, the iterator will return names in the following order:

      1. "MyResource_ja_JP.txt"
      2. "MyResource_ja.txt"
      3. "MyResource.txt"

      If the locale is null, then it will only return "MyResource.txt".

      Parameters:
      fileName - The name of the file to get candidate file names on.
      locale - The locale.
      If null, won't look for localized file names.
      Returns:
      An iterator of file names to look at.
    • getCandidateLocales

      protected List<Locale> getCandidateLocales(Locale locale)
      Returns the candidate locales for the specified locale.

      For example, if locale is "ja_JP", then this method will return:

      1. "ja_JP"
      2. "ja"
      3. ""
      Parameters:
      locale - The locale to get the list of candidate locales for.
      Returns:
      The list of candidate locales.
    • isInvalidPath

      protected boolean isInvalidPath(String path)
      Checks for path malformations such as use of ".." which can be used to open up security holes.

      Default implementation returns true if the path is any of the following:

      • Is blank or null.
      • Contains ".." (to prevent traversing out of working directory).
      • Contains "%" (to prevent URI trickery).
      Parameters:
      path - The path to check.
      Returns:
      true if the path is invalid.
    • isIgnoredFile

      protected boolean isIgnoredFile(String name)
      Returns true if the file should be ignored based on file name.
      Parameters:
      name - The name to check.
      Returns:
      true if the file should be ignored.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object