fix rendering templates and rendering the index file if present
This commit is contained in:
parent
bef8762346
commit
cf259ee8fb
4 changed files with 45 additions and 36 deletions
|
@ -58,8 +58,15 @@ public abstract class AbstractResourceHandler implements HttpHandler {
|
||||||
logger.log(Level.FINE, "handle: before creating resource " + this.getClass().getName());
|
logger.log(Level.FINE, "handle: before creating resource " + this.getClass().getName());
|
||||||
Resource resource = createResource(context);
|
Resource resource = createResource(context);
|
||||||
logger.log(Level.FINE, "handle: resource = " + (resource != null ? resource.getClass().getName() + " " + resource : null));
|
logger.log(Level.FINE, "handle: resource = " + (resource != null ? resource.getClass().getName() + " " + resource : null));
|
||||||
if (resource == null || !resource.isExists()) {
|
if (resource instanceof HtmlTemplateResource) {
|
||||||
logger.log(Level.FINER, "resource does not exist: " + resource);
|
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);
|
throw new HttpException("resource not found", context, HttpResponseStatus.NOT_FOUND);
|
||||||
} else if (resource.isDirectory()) {
|
} else if (resource.isDirectory()) {
|
||||||
logger.log(Level.FINER, "we have a directory request");
|
logger.log(Level.FINER, "we have a directory request");
|
||||||
|
@ -77,13 +84,9 @@ public abstract class AbstractResourceHandler implements HttpHandler {
|
||||||
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT) // 307
|
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT) // 307
|
||||||
.build().flush(); // flush is important
|
.build().flush(); // flush is important
|
||||||
} else if (resource.isExistsIndexFile()) {
|
} else if (resource.isExistsIndexFile()) {
|
||||||
// external redirect to default index file in this directory
|
// internal redirect to default index file in this directory
|
||||||
logger.log(Level.FINER, "external redirect to default index file in this directory: " + resource.getIndexFileName());
|
logger.log(Level.FINER, "internal redirect to default index file in this directory: " + resource.getIndexFileName());
|
||||||
context.response()
|
generateCacheableResource(context, resource);
|
||||||
.addHeader(HttpHeaderNames.LOCATION, resource.getIndexFileName())
|
|
||||||
.setResponseStatus(HttpResponseStatus.TEMPORARY_REDIRECT) // 307
|
|
||||||
.build()
|
|
||||||
.flush(); // write headers
|
|
||||||
} else {
|
} else {
|
||||||
// send forbidden, we do not allow directory access
|
// send forbidden, we do not allow directory access
|
||||||
context.response()
|
context.response()
|
||||||
|
|
|
@ -80,7 +80,7 @@ public class FileResourceHandler extends AbstractResourceHandler {
|
||||||
|
|
||||||
protected class FileResource implements Resource {
|
protected class FileResource implements Resource {
|
||||||
|
|
||||||
private final Path path;
|
private Path path;
|
||||||
|
|
||||||
private final String resourcePath;
|
private final String resourcePath;
|
||||||
|
|
||||||
|
@ -123,17 +123,19 @@ public class FileResourceHandler extends AbstractResourceHandler {
|
||||||
this.path = httpServerContext.resolve(webRoot).resolve(normalizedPath);
|
this.path = httpServerContext.resolve(webRoot).resolve(normalizedPath);
|
||||||
}
|
}
|
||||||
this.mimeType = mimeTypeService.getContentType(resourcePath);
|
this.mimeType = mimeTypeService.getContentType(resourcePath);
|
||||||
this.url = URL.create(path.toUri().toString());
|
if (Files.isDirectory(path) && getIndexFileName() != null) {
|
||||||
this.baseName = basename(name);
|
// internal redirect to indexFileName
|
||||||
this.suffix = suffix(name);
|
this.path = path.resolve(indexFileName);
|
||||||
this.isExists = Files.exists(path);
|
this.isExistsIndexFile = Files.exists(path);
|
||||||
this.isDirectory = Files.isDirectory(path);
|
|
||||||
if (isDirectory && getIndexFileName() != null) {
|
|
||||||
this.isExistsIndexFile = Files.exists(path.resolve(indexFileName));
|
|
||||||
httpServerContext.done();
|
|
||||||
} else {
|
} else {
|
||||||
this.isExistsIndexFile = false;
|
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) {
|
if (isExists) {
|
||||||
this.lastModified = Files.getLastModifiedTime(path).toInstant();
|
this.lastModified = Files.getLastModifiedTime(path).toInstant();
|
||||||
this.length = Files.size(path);
|
this.length = Files.size(path);
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class HtmlTemplateResource implements HttpServerResource {
|
||||||
|
|
||||||
private final HtmlTemplateResourceHandler templateResourceHandler;
|
private final HtmlTemplateResourceHandler templateResourceHandler;
|
||||||
|
|
||||||
private final Path path;
|
private Path path;
|
||||||
|
|
||||||
private final String resourcePath;
|
private final String resourcePath;
|
||||||
|
|
||||||
|
@ -54,6 +54,7 @@ public class HtmlTemplateResource implements HttpServerResource {
|
||||||
logger.log(Level.FINE, "resource path = " + resourcePath);
|
logger.log(Level.FINE, "resource path = " + resourcePath);
|
||||||
this.path = resourcePath.length() > 0 ? root.resolve(resourcePath) : root;
|
this.path = resourcePath.length() > 0 ? root.resolve(resourcePath) : root;
|
||||||
logger.log(Level.FINE, "path = " + path);
|
logger.log(Level.FINE, "path = " + path);
|
||||||
|
logger.log(Level.FINE, "index file name = " + indexFileName);
|
||||||
this.url = URL.create(path.toUri().toString());
|
this.url = URL.create(path.toUri().toString());
|
||||||
logger.log(Level.FINE, "uri = " + url);
|
logger.log(Level.FINE, "uri = " + url);
|
||||||
this.name = path.getFileName().toString();
|
this.name = path.getFileName().toString();
|
||||||
|
@ -61,9 +62,11 @@ public class HtmlTemplateResource implements HttpServerResource {
|
||||||
this.suffix = AbstractResourceHandler.suffix(name);
|
this.suffix = AbstractResourceHandler.suffix(name);
|
||||||
this.isExists = Files.exists(path);
|
this.isExists = Files.exists(path);
|
||||||
this.isDirectory = Files.isDirectory(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) {
|
if (isDirectory && getIndexFileName() != null) {
|
||||||
this.isExistsIndexFile = Files.exists(path.resolve(indexFileName));
|
this.path = path.resolve(indexFileName);
|
||||||
|
this.isExistsIndexFile = Files.exists(path);
|
||||||
httpServerContext.done();
|
httpServerContext.done();
|
||||||
} else {
|
} else {
|
||||||
this.isExistsIndexFile = false;
|
this.isExistsIndexFile = false;
|
||||||
|
|
|
@ -50,17 +50,7 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
|
||||||
logger.log(Level.WARNING, "template engine is null");
|
logger.log(Level.WARNING, "template engine is null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
Path templatePath = getPath();
|
Path templatePath = getPath();
|
||||||
logger.log(Level.FINE, "templatePath = " + getPath());
|
|
||||||
GroovyHttpResonseStatusTemplateResource resource = httpServerContext.attributes().get(GroovyHttpResonseStatusTemplateResource.class, "_resource");
|
|
||||||
if (templatePath == null && isExists() && 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");
|
HttpService service = httpServerContext.attributes().get(HttpService.class, "service");
|
||||||
GroovyTemplateService groovyTemplateService = (GroovyTemplateService) service;
|
GroovyTemplateService groovyTemplateService = (GroovyTemplateService) service;
|
||||||
if (groovyTemplateService.getTemplateName() != null) {
|
if (groovyTemplateService.getTemplateName() != null) {
|
||||||
|
@ -69,6 +59,13 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
|
||||||
} else {
|
} else {
|
||||||
logger.log(Level.FINE, "the GroovyTemplateService does not have a template name set");
|
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 (resource != null) {
|
||||||
|
logger.log(Level.FINE, "Groovy HTTP status response rendering");
|
||||||
|
String indexFileName = resource.getIndexFileName();
|
||||||
|
if (indexFileName != null) {
|
||||||
|
templatePath = application.resolve(indexFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// override if 'templatePath' attribute is set
|
// override if 'templatePath' attribute is set
|
||||||
|
@ -84,9 +81,13 @@ public class GroovyTemplateResource extends HtmlTemplateResource {
|
||||||
templatePath = application.resolve(getIndexFileName());
|
templatePath = application.resolve(getIndexFileName());
|
||||||
}
|
}
|
||||||
if (isDirectory()) {
|
if (isDirectory()) {
|
||||||
logger.log(Level.WARNING, "unable to render a directory, this is 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);
|
throw new HttpException("forbidden", httpServerContext, HttpResponseStatus.FORBIDDEN);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
logger.log(Level.FINE, "rendering groovy template " + templatePath);
|
logger.log(Level.FINE, "rendering groovy template " + templatePath);
|
||||||
templates.computeIfAbsent(templatePath, path -> {
|
templates.computeIfAbsent(templatePath, path -> {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue