comparison 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
comparison
equal deleted inserted replaced
1401:ef1620aa99cb 1402:27efb1fcbcb5
1 package goodjava.webserver.handlers;
2
3 import java.io.Writer;
4 import java.io.OutputStreamWriter;
5 import java.io.PrintWriter;
6 import java.io.IOException;
7 import goodjava.logging.Logger;
8 import goodjava.logging.LoggerFactory;
9 import goodjava.webserver.Handler;
10 import goodjava.webserver.Request;
11 import goodjava.webserver.Response;
12 import goodjava.webserver.ResponseOutputStream;
13 import goodjava.webserver.Status;
14
15
16 public final class SafeHandler implements Handler {
17 private static final Logger logger = LoggerFactory.getLogger(SafeHandler.class);
18
19 private final Handler handler;
20
21 public SafeHandler(Handler handler) {
22 this.handler = handler;
23 }
24
25 public Response handle(Request request) {
26 try {
27 Response response = handler.handle(request);
28 if( response != null )
29 return response;
30 } catch(RuntimeException e) {
31 logger.error("",e);
32 Response response = new Response();
33 response.status = Status.INTERNAL_SERVER_ERROR;
34 response.headers.put( "content-type", "text/plain; charset=utf-8" );
35 PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
36 writer.write( "Internel Server Error\n\n" );
37 e.printStackTrace(writer);
38 writer.close();
39 return response;
40 }
41 return Response.errorResponse( Status.NOT_FOUND, request.path+" not found\n" );
42 }
43
44 }