Mercurial Hosting > luan
diff src/goodjava/webserver/handlers/SafeHandler.java @ 1402:27efb1fcbcb5
move luan.lib to goodjava
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 17 Sep 2019 01:35:01 -0400 |
parents | src/luan/lib/webserver/handlers/SafeHandler.java@643cf1c37723 |
children | fa066aaa068c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goodjava/webserver/handlers/SafeHandler.java Tue Sep 17 01:35:01 2019 -0400 @@ -0,0 +1,44 @@ +package goodjava.webserver.handlers; + +import java.io.Writer; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.IOException; +import goodjava.logging.Logger; +import goodjava.logging.LoggerFactory; +import goodjava.webserver.Handler; +import goodjava.webserver.Request; +import goodjava.webserver.Response; +import goodjava.webserver.ResponseOutputStream; +import goodjava.webserver.Status; + + +public final class SafeHandler implements Handler { + private static final Logger logger = LoggerFactory.getLogger(SafeHandler.class); + + private final Handler handler; + + public SafeHandler(Handler handler) { + this.handler = handler; + } + + public Response handle(Request request) { + try { + Response response = handler.handle(request); + if( response != null ) + return response; + } catch(RuntimeException e) { + logger.error("",e); + Response response = new Response(); + response.status = Status.INTERNAL_SERVER_ERROR; + response.headers.put( "content-type", "text/plain; charset=utf-8" ); + PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) ); + writer.write( "Internel Server Error\n\n" ); + e.printStackTrace(writer); + writer.close(); + return response; + } + return Response.errorResponse( Status.NOT_FOUND, request.path+" not found\n" ); + } + +}