Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/SafeHandler.java @ 1786:bda4ec615c5b
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 10 Dec 2023 21:17:21 -0700 |
parents | fa066aaa068c |
children |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver.handlers; |
1137 | 2 |
3 import java.io.Writer; | |
4 import java.io.OutputStreamWriter; | |
5 import java.io.PrintWriter; | |
6 import java.io.IOException; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
7 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
8 import goodjava.logging.LoggerFactory; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
9 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
10 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
11 import goodjava.webserver.Response; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
12 import goodjava.webserver.ResponseOutputStream; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
13 import goodjava.webserver.Status; |
1137 | 14 |
15 | |
16 public final class SafeHandler implements Handler { | |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
17 private static final Logger logger = LoggerFactory.getLogger(SafeHandler.class); |
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
18 |
1137 | 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) { | |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
31 logger.error("",e); |
1137 | 32 Response response = new Response(); |
33 response.status = Status.INTERNAL_SERVER_ERROR; | |
1607 | 34 response.headers.put( "Content-Type", "text/plain; charset=utf-8" ); |
1137 | 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 } | |
1161 | 41 return Response.errorResponse( Status.NOT_FOUND, request.path+" not found\n" ); |
1137 | 42 } |
43 | |
44 } |