fm.liu.engine.Handler Maven / Gradle / Ivy
package fm.liu.engine;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class Handler {
protected Request request;
protected Response response;
public void init(Session session) {
this.request = session.getRequest();
this.response = session.getResponse();
}
public void construct() {}
public String getArgument(String key) {
return this.request.getArgument().get(key);
}
public void response(String content) {
response.setContent(content);
}
public Template render(String fileName) {
Template template = null;
try {
if (Config.DEBUG) {
fileName = fileName.replace(".", "/");
DynamicCompiler compiler = new DynamicCompiler();
template = compiler.run(fileName + ".html");
if (template == null) {
ErrorUtil.set(response, 500, fileName + "模板不存在!");
}
} else {
Class> clazz = Class.forName("www." + fileName + "Template");
template = (Template) clazz.newInstance();
return template;
}
} catch (ClassNotFoundException e) {
if (!Config.DEBUG) {
fileName = fileName.replace(".", "/");
DynamicCompiler compiler = new DynamicCompiler();
template = compiler.run(fileName + ".html");
if (template == null) {
ErrorUtil.set(response, 500, fileName + "模板不存在!");
}
}
} catch (InstantiationException | IllegalAccessException e) {
Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, e);
}
return template;
}
public void initTemplate(String fileName) {
fileName = fileName.replace(".", "/");
DynamicCompiler compiler = new DynamicCompiler();
compiler.run(fileName + ".html");
}
public void redirect(String url) {
response.setStatus(303);
if (url.toLowerCase().indexOf("http://") < 0) {
url = "http://" + this.request.getHeader().get("Host") + url;
}
response.addHeader("Location: " + url);
}
public void jump(String message, String url, int time, boolean status) {
if (message == null) {
message = "Message!";
}
if (time < 1) {
time = 3;
}
if ("XMLHttpRequest".equals(this.request.getHeader().get("X-Requested-With"))) {
HashMap map = new HashMap<>();
map.put("status", status);
map.put("data", message);
response.setContentType(response.findContentType("json"));
this.response(Json.encode(map));
} else {
Template jump = this.render("jump");
jump.put("status", status);
jump.put("message", message);
jump.put("isSetUrl", url != null);
jump.put("url", url);
jump.put("wait", time);
this.response(jump.display());
}
}
public void success(String message, String url, int time) {
this.jump(message == null ? "Success!" : message, url, time, true);
}
public void success(String message, String url) {
this.success(message, url, 0);
}
public void success(String message) {
this.success(message, null, 0);
}
public void error(String message, String url, int time) {
this.jump(message == null ? "Error!" : message, url, time, false);
}
public void error(String message, String url) {
this.success(message, url, 0);
}
public void error(String message) {
this.success(message, null, 0);
}
public abstract void init();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy