com.fastchar.out.FastOutError Maven / Gradle / Ivy
package com.fastchar.out;
import com.fastchar.core.FastAction;
import com.fastchar.core.FastChar;
import com.fastchar.core.FastConstant;
import com.fastchar.utils.FastStringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class FastOutError extends FastOut {
public FastOutError() {
this.contentType = "text/html";
}
private static final String PoweredBy = "FastChar";
public String toHtml() {
if (data == null) {
data = getDescription();
}
String description = data.toString()
.replace("\n", "
")
.replace("\t", " ");
String html = "";
if (getStatus() == 500) {
html = "500 HTTP Status: 500 Message: An error occurred on the server! Description: " + description+ " Powered By: " + PoweredBy + " Version: " + FastConstant.FastCharVersion + "
";
}else if (getStatus() == 404) {
html = "404 HTTP Status: 404 Message: Http url does not exist! Description: " + description+ " Powered By: " + PoweredBy + " Version: " + FastConstant.FastCharVersion + "
";
}else if (getStatus() == 502) {
html = "502 HTTP Status: 502 Message: The Http response is not received! Description: " + description + " Powered By: " + PoweredBy + " Version: " + FastConstant.FastCharVersion + "
";
}
return html;
}
public String getErrorPage() {
if (getStatus() == 500) {
return FastChar.getConstant().getErrorPage500();
}else if (getStatus() == 502) {
return FastChar.getConstant().getErrorPage502();
}else if (getStatus() == 404) {
return FastChar.getConstant().getErrorPage404();
}
return null;
}
@Override
public void response(FastAction action) {
try {
HttpServletResponse response = action.getResponse();
if (FastStringUtils.isEmpty(getErrorPage())) {
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setStatus(getStatus());
response.setContentType(toContentType());
response.setCharacterEncoding(getCharset());
PrintWriter writer = response.getWriter();
writer.write(toHtml());
writer.flush();
}else{
response.sendRedirect(FastChar.wrapperUrl(getErrorPage()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}