com.blade.web.http.ResponsePrint Maven / Gradle / Ivy
package com.blade.web.http;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import blade.kit.FileKit;
import blade.kit.StreamKit;
import com.blade.Blade;
import com.blade.Const;
public final class ResponsePrint {
private static boolean isDev = Blade.me().isDev();
private ResponsePrint() {
}
/**
* Print Error Message
* @param err
* @param code
* @param response
*/
public static void printError(Throwable err, int code, HttpServletResponse response){
err.printStackTrace();
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintWriter writer = new PrintWriter(baos);
// If the developer mode, the error output to the page
if(isDev){
writer.println(String.format(HTML, err.getClass() + " : " + err.getMessage()));
writer.println();
err.printStackTrace(writer);
writer.println(END);
} else {
if(code == 404){
writer.write(err.getMessage());
} else {
writer.write(Const.INTERNAL_ERROR);
}
}
writer.close();
response.setStatus(code);
InputStream body = new ByteArrayInputStream(baos.toByteArray());
print(body, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Print
* @param body
* @param out
* @throws IOException
*/
public static void print(InputStream body, OutputStream out) throws IOException {
StreamKit.io(body, out, true, true);
/*
try {
int size = in.available();
byte[] content = new byte[size];
in.read(content);
out.write(content);
} finally {
in.close();
out.close();
}*/
}
/**
* Print static file
* @param uri
* @param realpath
* @param httpResponse
*/
public static void printStatic(String uri, String realpath, HttpServletResponse httpResponse) {
try {
File file = new File(realpath);
if(FileKit.exist(file)){
FileInputStream in = new FileInputStream(file);
print(in, httpResponse.getOutputStream());
} else {
HttpException httpException = new HttpException(404, uri + " not found");
ResponsePrint.printError(httpException, 404, httpResponse);
}
} catch (FileNotFoundException e) {
ResponsePrint.printError(e, 404, httpResponse);
} catch (IOException e) {
ResponsePrint.printError(e, 500, httpResponse);
}
}
private static final String HTML = "Blade Framework Error Page "
+ ""
+ "%s
";
private static final String END = "
Blade-" + Const.BLADE_VERSION + "(Blade Framework) ";
}