Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/SafeHandler.java @ 1412:4ba0377afa3e
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Oct 2019 12:25:06 -0600 |
parents | 27efb1fcbcb5 |
children | fa066aaa068c |
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; | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1161
diff
changeset
|
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 } |