use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.
the class JarResourceManagerTests method rootPathIsHandledCorrectly.
@Test
public void rootPathIsHandledCorrectly() throws IOException {
Resource resource = this.resourceManager.getResource("/");
assertThat(resource).isNotNull();
assertThat(resource.isDirectory()).isTrue();
}
use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.
the class JarResourceManagerTests method resourceIsFoundInJarFile.
@Test
public void resourceIsFoundInJarFile() throws IOException {
Resource resource = this.resourceManager.getResource("/hello.txt");
assertThat(resource).isNotNull();
assertThat(resource.isDirectory()).isFalse();
assertThat(resource.getContentLength()).isEqualTo(5);
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class DefaultServlet method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
String path = getPath(req);
if (!isAllowed(path, req.getDispatcherType())) {
resp.sendError(StatusCodes.NOT_FOUND);
return;
}
if (File.separatorChar != '/') {
//if the separator char is not / we want to replace it with a / and canonicalise
path = CanonicalPathUtils.canonicalize(path.replace(File.separatorChar, '/'));
}
final Resource resource;
//we want to disallow windows characters in the path
if (File.separatorChar == '/' || !path.contains(File.separator)) {
resource = resourceManager.getResource(path);
} else {
resource = null;
}
if (resource == null) {
if (req.getDispatcherType() == DispatcherType.INCLUDE) {
//servlet 9.3
throw new FileNotFoundException(path);
} else {
resp.sendError(StatusCodes.NOT_FOUND);
}
return;
} else if (resource.isDirectory()) {
if ("css".equals(req.getQueryString())) {
resp.setContentType("text/css");
resp.getWriter().write(DirectoryUtils.Blobs.FILE_CSS);
return;
} else if ("js".equals(req.getQueryString())) {
resp.setContentType("application/javascript");
resp.getWriter().write(DirectoryUtils.Blobs.FILE_JS);
return;
}
if (directoryListingEnabled) {
StringBuilder output = DirectoryUtils.renderDirectoryListing(req.getRequestURI(), resource);
resp.getWriter().write(output.toString());
} else {
resp.sendError(StatusCodes.FORBIDDEN);
}
} else {
if (path.endsWith("/")) {
//UNDERTOW-432
resp.sendError(StatusCodes.NOT_FOUND);
return;
}
serveFileBlocking(req, resp, resource);
}
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class ServletContextImpl method getResourcePaths.
@Override
public Set<String> getResourcePaths(final String path) {
final Resource resource;
try {
resource = deploymentInfo.getResourceManager().getResource(path);
} catch (IOException e) {
return null;
}
if (resource == null || !resource.isDirectory()) {
return null;
}
final Set<String> resources = new HashSet<>();
for (Resource res : resource.list()) {
Path file = res.getFilePath();
if (file != null) {
Path base = res.getResourceManagerRootPath();
if (base == null) {
//not much else we can do here
resources.add(file.toString());
} else {
String filePath = file.toAbsolutePath().toString().substring(base.toAbsolutePath().toString().length());
//for windows systems
filePath = filePath.replace('\\', '/');
if (Files.isDirectory(file)) {
filePath = filePath + "/";
}
resources.add(filePath);
}
}
}
return resources;
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class GetResourceTestCase method testSpecialCharacterInFileURL.
@Test
public void testSpecialCharacterInFileURL() throws IOException {
String tmp = System.getProperty("java.io.tmpdir");
PathResourceManager pathResourceManager = new PathResourceManager(Paths.get(tmp), 1);
Path file = Paths.get(tmp, "1#2.txt");
Files.write(file, "Hi".getBytes());
Resource res = pathResourceManager.getResource("1#2.txt");
try (InputStream in = res.getUrl().openStream()) {
Assert.assertEquals("Hi", FileUtils.readFile(in));
}
}
Aggregations