
cn.taketoday.core.io.FileSystemResourceLoader Maven / Gradle / Ivy
/*
* Original Author -> Harry Yang ([email protected]) https://taketoday.cn
* Copyright © TODAY & 2017 - 2022 All Rights Reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/]
*/
package cn.taketoday.core.io;
/**
* {@link ResourceLoader} implementation that resolves plain paths as
* file system resources rather than as class path resources
* (the latter is {@link DefaultResourceLoader}'s default strategy).
*
* NOTE: Plain paths will always be interpreted as relative
* to the current VM working directory, even if they start with a slash.
* (This is consistent with the semantics in a Servlet container.)
* Use an explicit "file:" prefix to enforce an absolute file path.
*
* @author Juergen Hoeller
* @author Harry Yang
* @see DefaultResourceLoader
* @since 4.0 2022/2/20 16:39
*/
public class FileSystemResourceLoader extends DefaultResourceLoader {
/**
* Resolve resource paths as file system paths.
*
Note: Even if a given path starts with a slash, it will get
* interpreted as relative to the current VM working directory.
*
* @param path the path to the resource
* @return the corresponding Resource handle
* @see FileSystemResource
* @see cn.taketoday.web.context.support.ServletContextResourceLoader#getResourceByPath
*/
@Override
protected Resource getResourceByPath(String path) {
if (path.startsWith("/")) {
path = path.substring(1);
}
return new FileSystemContextResource(path);
}
/**
* FileSystemResource that explicitly expresses a context-relative path
* through implementing the ContextResource interface.
*/
private static class FileSystemContextResource extends FileSystemResource implements ContextResource {
public FileSystemContextResource(String path) {
super(path);
}
@Override
public String getPathWithinContext() {
return getPath();
}
}
}