Example usage for org.springframework.core.io FileSystemResource exists

List of usage examples for org.springframework.core.io FileSystemResource exists

Introduction

In this page you can find the example usage for org.springframework.core.io FileSystemResource exists.

Prototype

@Override
public boolean exists() 

Source Link

Document

This implementation returns whether the underlying file exists.

Usage

From source file:org.springframework.boot.actuate.endpoint.mvc.LogFileMvcEndpoint.java

private Resource getLogFileResource() {
    LogFile logFile = LogFile.get(this.environment);
    if (logFile == null) {
        logger.debug("Missing 'logging.file' or 'logging.path' properties");
        return null;
    }/* w w w .  j av  a2 s  .c  o  m*/
    FileSystemResource resource = new FileSystemResource(logFile.toString());
    if (!resource.exists()) {
        if (logger.isWarnEnabled()) {
            logger.debug("Log file '" + resource + "' does not exist");
        }
        return null;
    }
    return resource;
}

From source file:piecework.content.concrete.FileSystemContentProvider.java

@Override
public ContentResource findByLocation(ContentProfileProvider modelProvider, String rawPath)
        throws PieceworkException {
    if (!rawPath.startsWith("file:")) {
        LOG.error("Should not be looking for a file resource without the file prefix");
        return null;
    }// w ww  .  ja  v  a  2 s  . c om

    String path = rawPath.substring("file:".length());

    ContentProfile contentProfile = modelProvider.contentProfile();
    // Show never use FileSystemContentProvider unless the content profile explicitly
    // specifies a base path
    if (contentProfile == null || StringUtils.isEmpty(contentProfile.getBaseDirectory()))
        return null;

    File file = new File(path);

    try {
        // Check that base directory is a descendent of Piecework's filesystem root
        File baseDirectory = new File(contentProfile.getBaseDirectory());
        boolean isBaseDirectoryDescendent = FileUtility.isAncestorOf(root, baseDirectory);

        // The base directory is not a descendent of root, then try to find it under the root
        if (!isBaseDirectoryDescendent) {
            baseDirectory = new File(root, contentProfile.getBaseDirectory());
            isBaseDirectoryDescendent = FileUtility.isAncestorOf(root, baseDirectory);
        }

        boolean isFileDescendent = FileUtility.isAncestorOf(baseDirectory, file);

        if (!isFileDescendent) {
            file = new File(baseDirectory, path);
            isFileDescendent = FileUtility.isAncestorOf(baseDirectory, file);
        }

        boolean isFileReadAllowed = isBaseDirectoryDescendent && isFileDescendent;
        if (!isFileReadAllowed) {
            accessTracker.alarm(AlarmSeverity.MINOR, "Attempt to access file " + file.getAbsolutePath()
                    + " outside of " + root.getAbsolutePath() + " forbidden", modelProvider.principal());
            throw new ForbiddenError();
        }
    } catch (IOException ioe) {
        LOG.error("Unable to determine if file is within approved limits of file system", ioe);
        throw new InternalServerError(Constants.ExceptionCodes.system_misconfigured, ioe.getMessage());
    }

    FileSystemResource resource = new FileSystemResource(file);

    if (resource.exists())
        return new FileSystemContentResource(resource);

    LOG.error("No file exists at this path: " + file.getAbsolutePath());
    return null;
}

From source file:piecework.util.UserInterfaceUtility.java

public static ContentResource template(File templatesDirectory, String templateName) throws NotFoundError {
    ContentResource resource = null;//from  w w w  .  j av a  2 s. c  om
    if (templatesDirectory != null) {
        File file = new File(templatesDirectory, templateName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);

        if (!fileSystemResource.exists())
            throw new NotFoundError();

        resource = new FileSystemContentResource(fileSystemResource);
    } else {
        ClassPathResource classPathResource = new ClassPathResource(TEMPLATES_CLASSPATH_PREFIX + templateName);

        if (!classPathResource.exists())
            throw new NotFoundError();

        resource = new ClasspathContentResource(classPathResource);
    }

    return resource;
}