package com.zhibao.crm.common.config.render;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.jfinal.render.RenderManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
public class ErrorRender extends Render {
private static final String HTML_CONTENT_TYPE = "text/html; charset=" + getEncoding();
private static final String JSON_CONTENT_TYPE = "application/json; charset=" + getEncoding();
private static String HTML_CONTENT = "
TITLECONTENT
智保CRM";
private static final String HTML404 = HTML_CONTENT.replace("TITLE", "404 Not Found").replace("CONTENT", "404 Not Found");
private static final String HTML500 = HTML_CONTENT.replace("TITLE", "500 Internal Server Error").replace("CONTENT", "500 Internal Server Error");
private static final String HTML401 = HTML_CONTENT.replace("TITLE", "401 Unauthorized").replace("CONTENT", "401 Unauthorized");
private static final String HTML403 = HTML_CONTENT.replace("TITLE", "403 Forbidden").replace("CONTENT", "403 Forbidden");
private static final String JSON404 = "{\"code\": 404, \"msg\": \"404 请求失败\"}";
private static final String JSON500 = "{\"code\": 500, \"msg\": \"500 操作失败\"}";
private static final String JSON401 = "{\"code\": 401, \"msg\": \"401 未授权\"}";
private static final String JSON403 = "{\"code\": 403, \"msg\": \"403 权限不足\"}";
private int errorCode;
protected ErrorRender(int errorCode, String view) {
this.errorCode = errorCode;
this.view = view;
}
public void render() {
String view = this.getView();
if(view != null) {
this.response.setStatus(this.getErrorCode());
RenderManager.me().getRenderFactory().getRender(view).setContext(this.request, this.response).render();
} else {
try {
String e;
if(this.isAjax(this.request)) {
e = this.getErrorJson();
this.response.setStatus(this.getErrorCode());
this.response.setContentType(JSON_CONTENT_TYPE);
} else {
e = this.getErrorHtml();
this.response.setStatus(this.getErrorCode());
this.response.setContentType(HTML_CONTENT_TYPE);
}
PrintWriter writer = this.response.getWriter();
writer.write(e);
writer.flush();
} catch (IOException var4) {
throw new RenderException(var4);
}
}
}
private String getErrorHtml() {
int errorCode = this.getErrorCode();
return errorCode == 404?HTML404:(errorCode == 500?HTML500:(errorCode == 401?HTML401:(errorCode == 403?HTML403:"" + errorCode + " Error" + errorCode + " Error
CRM软件")));
}
private String getErrorJson() {
int errorCode = this.getErrorCode();
return errorCode == 404?"{\"code\": 404, \"msg\": \"404 请求失败\"}":(errorCode == 500?"{\"code\": 500, \"msg\": \"500 操作失败\"}":(errorCode == 401?"{\"code\": 401, \"msg\": \"401 未授权\"}":(errorCode == 403?"{\"code\": 403, \"msg\": \"403 权限不足\"}":String.format("{code: %d, msg: \'%d操作失败\'}", new Object[]{Integer.valueOf(errorCode), Integer.valueOf(errorCode)}))));
}
private int getErrorCode() {
return this.errorCode != 0?this.errorCode:100;
}
private boolean isAjax(HttpServletRequest request) {
return request.getHeader("Admin-Token") != null;
}
}