goja.mvc.error.GojaErrorRender Maven / Gradle / Ivy
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2013-2014 sagyf Yang. The Four Group.
*/
package goja.mvc.error;
import goja.core.StringPool;
import goja.mvc.Freemarkers;
import com.jfinal.core.Const;
import com.jfinal.core.JFinal;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.jfinal.render.RenderFactory;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
/**
* .
*
* @author sagyf yang
* @version 1.0 2014-11-08 20:54
* @since JDK 1.6
*/
public class GojaErrorRender extends Render {
public static final String ERROR_500_FTL =
" Application 500 Eroor Execution exception
${title!}
Exception information in detail
<#list errors as er>error#if>\">${er_index + 1}:${er}
#list>";
public static final String NOTFUND_FTL =
"Not found ${requestURI!} Not found
These routes have been tried, in this order :
<#list routes as r>- ${r}
#list>
";
protected static final String contentType = "text/html; charset=" + getEncoding();
protected static final String version =
"Powered by JFinal#Goja "
+ Const.JFINAL_VERSION
+ " ";
protected int errorCode;
public GojaErrorRender(int errorCode, String view) {
this.errorCode = errorCode;
this.view = view;
}
public void render() {
response.setStatus(getErrorCode()); // HttpServletResponse.SC_XXX_XXX
// render with view
String view = getView();
if (view != null) {
RenderFactory.me().getRender(view).setContext(request, response).render();
return;
}
// render with html content
PrintWriter writer = null;
try {
response.setContentType(contentType);
writer = response.getWriter();
writer.write(getErrorHtml());
writer.flush();
} catch (IOException e) {
throw new RenderException(e);
} finally {
if (writer != null) {
writer.close();
}
}
}
public String getErrorHtml() {
int errorCode = getErrorCode();
Map pdata = Maps.newHashMap();
final String requestURI = request.getRequestURI();
pdata.put("requestURI", requestURI);
switch (errorCode) {
case SC_INTERNAL_SERVER_ERROR:
Throwable te = (Throwable) request.getAttribute("goja_error");
String error_msg = Throwables.getStackTraceAsString(te);
List error_lines = Splitter.on(StringPool.NEWLINE).splitToList(error_msg);
pdata.put("title", error_lines.get(0));
pdata.put("errors", error_lines);
return Freemarkers.renderStrTemplate(ERROR_500_FTL, pdata);
case SC_NOT_FOUND:
final List allActionKeys = JFinal.me().getAllActionKeys();
pdata.put("routes", allActionKeys);
return Freemarkers.renderStrTemplate(NOTFUND_FTL, pdata);
default:
return ""
+ errorCode
+ " Error "
+ errorCode
+ " Error
"
+ version
+ "";
}
}
public int getErrorCode() {
return errorCode;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy