com.fastchar.out.FastOutError Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastchar Show documentation
Show all versions of fastchar Show documentation
FastChar is Web+ORM Framework in Java.
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 POWERED_BY = "FastChar";
public String toHtml(FastAction action) {
if (data == null) {
data = getDescription();
}
String description = data.toString()
.replace("\n", "
")
.replace("\t", " ");
String html = "";
if (getStatus() == 404) {
html = "404 HTTP Status: 404 HTTP Method: "+action.getRequestMethod()+" Message: Http url does not exist! Description: " + description + " Powered By: " + POWERED_BY + " Version: " + FastConstant.FAST_CHAR_VERSION + "
";
} else if (getStatus() == 502) {
html = "502 HTTP Status: 502 HTTP Method: "+action.getRequestMethod()+" Message: The Http response is not received! Description: " + description + " Powered By: " + POWERED_BY + " Version: " + FastConstant.FAST_CHAR_VERSION + "
";
} else {
html = "" + getStatus() + " HTTP Status: 500 HTTP Method: "+action.getRequestMethod()+" Message: An error occurred on the server! Description: " + description + " Powered By: " + POWERED_BY + " Version: " + FastConstant.FAST_CHAR_VERSION + "
";
}
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(action));
response.setCharacterEncoding(getCharset());
try (PrintWriter writer = response.getWriter()) {
writer.write(toHtml(action));
writer.flush();
}
} else {
response.sendRedirect(FastChar.wrapperUrl(getErrorPage()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy