fix rendering templates and rendering the index file if present

This commit is contained in:
Jörg Prante 2023-03-14 17:08:58 +01:00
parent bef8762346
commit cf259ee8fb
4 changed files with 45 additions and 36 deletions

View file

@ -58,8 +58,15 @@ public abstract class AbstractResourceHandler implements HttpHandler {
logger.log(Level.FINE, "handle: before creating resource " + this.getClass().getName());
Resource resource = createResource(context);
logger.log(Level.FINE, "handle: resource = " + (resource != null ? resource.getClass().getName() + " " + resource : null));
if (resource == null || !resource.isExists()) {
logger.log(Level.FINER, "resource does not exist: " + resource);
if (resource instanceof HtmlTemplateResource) {
logger.log(Level.FINE, "handle: HTML template resource, generate cacheable resource, parameter = " +
context.httpRequest().getParameter());
generateCacheableResource(context, resource);
logger.log(Level.FINE, "handle: done");
return;
}
if (resource == null) {
logger.log(Level.FINER, "resource is null: " + resource);
throw new HttpException("resource not found", context, HttpResponseStatus.NOT_FOUND);
} else if (resource.isDirectory()) {
logger.log(Level.FINER, "we have a directory request");
@ -77,13 +84,9 @@ public abstract class AbstractResourceHandler implements HttpHandler {
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT) // 307
.build().flush(); // flush is important
} else if (resource.isExistsIndexFile()) {
// external redirect to default index file in this directory
logger.log(Level.FINER, "external redirect to default index file in this directory: " + resource.getIndexFileName());
context.response()
.addHeader(HttpHeaderNames.LOCATION, resource.getIndexFileName())
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT) // 307
.build()
.flush(); // write headers
// internal redirect to default index file in this directory
logger.log(Level.FINER, "internal redirect to default index file in this directory: " + resource.getIndexFileName());
generateCacheableResource(context, resource);
} else {
// send forbidden, we do not allow directory access
context.response()

View file

@ -80,7 +80,7 @@ public class FileResourceHandler extends AbstractResourceHandler {
protected class FileResource implements Resource {
private final Path path;
private Path path;
private final String resourcePath;
@ -123,17 +123,19 @@ public class FileResourceHandler extends AbstractResourceHandler {
this.path = httpServerContext.resolve(webRoot).resolve(normalizedPath);
}
this.mimeType = mimeTypeService.getContentType(resourcePath);
this.url = URL.create(path.toUri().toString());
this.baseName = basename(name);
this.suffix = suffix(name);
this.isExists = Files.exists(path);
this.isDirectory = Files.isDirectory(path);
if (isDirectory && getIndexFileName() != null) {
this.isExistsIndexFile = Files.exists(path.resolve(indexFileName));
httpServerContext.done();
if (Files.isDirectory(path) && getIndexFileName() != null) {
// internal redirect to indexFileName
this.path = path.resolve(indexFileName);
this.isExistsIndexFile = Files.exists(path);
} else {
this.isExistsIndexFile = false;
}
this.url = URL.create(path.toUri().toString());
this.baseName = basename(name);
this.suffix = suffix(name);
this.isDirectory = Files.isDirectory(path);
logger.log(Level.INFO, "resource path =" + resourcePath + " path = " + path + " isDirectory = " + isDirectory);
this.isExists = Files.exists(path);
if (isExists) {
this.lastModified = Files.getLastModifiedTime(path).toInstant();
this.length = Files.size(path);

View file

@ -17,7 +17,7 @@ public class HtmlTemplateResource implements HttpServerResource {
private final HtmlTemplateResourceHandler templateResourceHandler;
private final Path path;
private Path path;
private final String resourcePath;
@ -54,6 +54,7 @@ public class HtmlTemplateResource implements HttpServerResource {
logger.log(Level.FINE, "resource path = " + resourcePath);
this.path = resourcePath.length() > 0 ? root.resolve(resourcePath) : root;
logger.log(Level.FINE, "path = " + path);
logger.log(Level.FINE, "index file name = " + indexFileName);
this.url = URL.create(path.toUri().toString());
logger.log(Level.FINE, "uri = " + url);
this.name = path.getFileName().toString();
@ -61,9 +62,11 @@ public class HtmlTemplateResource implements HttpServerResource {
this.suffix = AbstractResourceHandler.suffix(name);
this.isExists = Files.exists(path);
this.isDirectory = Files.isDirectory(path);
logger.log(Level.FINE, "exists = " + isExists + " isDirectory = " + isDirectory);
logger.log(Level.FINE, "exists = " + isExists);
logger.log(Level.FINE, "isDirectory = " + isDirectory);
if (isDirectory && getIndexFileName() != null) {
this.isExistsIndexFile = Files.exists(path.resolve(indexFileName));
this.path = path.resolve(indexFileName);
this.isExistsIndexFile = Files.exists(path);
httpServerContext.done();
} else {
this.isExistsIndexFile = false;

View file

@ -50,26 +50,23 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
logger.log(Level.WARNING, "template engine is null");
return;
}
//
Path templatePath = getPath();
logger.log(Level.FINE, "templatePath = " + getPath());
HttpService service = httpServerContext.attributes().get(HttpService.class, "service");
GroovyTemplateService groovyTemplateService = (GroovyTemplateService) service;
if (groovyTemplateService.getTemplateName() != null) {
templatePath = application.resolve(groovyTemplateService.getTemplateName());
logger.log(Level.FINE, "templatePath after application.resolve() = " + templatePath);
} else {
logger.log(Level.FINE, "the GroovyTemplateService does not have a template name set");
}
logger.log(Level.FINE, "templatePath = " + templatePath);
GroovyHttpResonseStatusTemplateResource resource = httpServerContext.attributes().get(GroovyHttpResonseStatusTemplateResource.class, "_resource");
if (templatePath == null && isExists() && resource != null) {
if (resource != null) {
logger.log(Level.FINE, "Groovy HTTP status response rendering");
String indexFileName = resource.getIndexFileName();
if (indexFileName != null) {
templatePath = application.resolve(indexFileName);
}
if (templatePath == null) {
HttpService service = httpServerContext.attributes().get(HttpService.class, "service");
GroovyTemplateService groovyTemplateService = (GroovyTemplateService) service;
if (groovyTemplateService.getTemplateName() != null) {
templatePath = application.resolve(groovyTemplateService.getTemplateName());
logger.log(Level.FINE, "templatePath after application.resolve() = " + templatePath);
} else {
logger.log(Level.FINE, "the GroovyTemplateService does not have a template name set");
}
}
}
// override if 'templatePath' attribute is set
String overridePath = httpServerContext.attributes().get(String.class, "templatePath");
@ -84,8 +81,12 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
templatePath = application.resolve(getIndexFileName());
}
if (isDirectory()) {
logger.log(Level.WARNING, "unable to render a directory, this is forbidden");
throw new HttpException("forbidden", httpServerContext, HttpResponseStatus.FORBIDDEN);
if (isExistsIndexFile()) {
templatePath = getPath().resolve(getIndexFileName());
} else {
logger.log(Level.WARNING, "unable to render a directory without index file name, this is forbidden");
throw new HttpException("forbidden", httpServerContext, HttpResponseStatus.FORBIDDEN);
}
}
logger.log(Level.FINE, "rendering groovy template " + templatePath);
templates.computeIfAbsent(templatePath, path -> {