try {
String vmFileName = target.replace(".jhtml", ".vm").replace("/", "\\").substring(1);
final File file = new File(this.docRoot, target.replace(".jhtml", ".vm"));
if(!file.exists()) {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
NStringEntity entity = new NStringEntity(
"<html><body><h1>File" + file.getPath() +
" not found</h1></body></html>",
ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
System.out.println("File " + file.getPath() + " not found");
} else {
template = Velocity.getTemplate(vmFileName, templateEncode);
//
templateCache.putIfAbsent(target, template);
}
} catch (Exception e) {
logger.error("Exception: get template", e);
}
}
// 渲染VM
VelocityContext cxt = new VelocityContext();
monitor.getData(cxt);
Writer writer = new StringWriter();
template.merge(cxt, writer);
response.setStatusCode(HttpStatus.SC_OK);
NStringEntity entity = new NStringEntity(writer.toString(), ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
} else {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
NStringEntity entity = new NStringEntity("Velocity not init succeed",
ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
System.out.println("Velocity not init succeed");
}
} else {
final File file = new File(this.docRoot, target);
if (!file.exists()) {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
NStringEntity entity = new NStringEntity(
"<html><body><h1>File" + file.getPath() +
" not found</h1></body></html>",
ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
System.out.println("File " + file.getPath() + " not found");
} else if (!file.canRead() || file.isDirectory()) {
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
NStringEntity entity = new NStringEntity(
"<html><body><h1>Access denied</h1></body></html>",
ContentType.create("text/html", "UTF-8"));
response.setEntity(entity);
System.out.println("Cannot read file " + file.getPath());