All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javawebstack.httpserver.util.DirectoryFileProvider Maven / Gradle / Ivy

Go to download

This library provides a routing and request mapping stack on top of the well known and industry proven eclipse jetty http server. It also supports websockets.

The newest version!
package org.javawebstack.httpserver.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class DirectoryFileProvider implements FileProvider {

    private final File directory;

    public DirectoryFileProvider(File directory) {
        this.directory = directory;
    }

    public InputStream getFile(String path) {
        File file = new File(directory, path);
        if (!file.exists() || !file.isFile() || !isInside(directory, file))
            return null;
        try {
            return new FileInputStream(file);
        } catch (FileNotFoundException ignored) {
        }
        return null;
    }

    private static boolean isInside(File directory, File file) {
        if (file.getParentFile().equals(directory))
            return true;
        if (directory.getParentFile() == null)
            return false;
        return isInside(directory.getParentFile(), file);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy