Mercurial Hosting > luan
comparison src/goodjava/webserver/handlers/FileHandler.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/FileHandler.java@643cf1c37723 |
children | 8a7519dee55c |
comparison
equal
deleted
inserted
replaced
1401:ef1620aa99cb | 1402:27efb1fcbcb5 |
---|---|
1 package goodjava.webserver.handlers; | |
2 | |
3 import java.io.File; | |
4 import java.io.FileInputStream; | |
5 import java.io.Writer; | |
6 import java.io.OutputStreamWriter; | |
7 import java.io.IOException; | |
8 import java.text.DateFormat; | |
9 import java.text.SimpleDateFormat; | |
10 import java.util.Date; | |
11 import goodjava.webserver.Handler; | |
12 import goodjava.webserver.Request; | |
13 import goodjava.webserver.Response; | |
14 import goodjava.webserver.ResponseOutputStream; | |
15 | |
16 | |
17 public class FileHandler implements Handler { | |
18 final File dir; | |
19 | |
20 public FileHandler() { | |
21 this("."); | |
22 } | |
23 | |
24 public FileHandler(String pathname) { | |
25 this(new File(pathname)); | |
26 } | |
27 | |
28 public FileHandler(File dir) { | |
29 if( !dir.isDirectory() ) | |
30 throw new RuntimeException("must be a directory"); | |
31 this.dir = dir; | |
32 } | |
33 | |
34 File file(Request request) { | |
35 return new File(dir,request.path); | |
36 } | |
37 | |
38 public Response handle(Request request) { | |
39 try { | |
40 File file = file(request); | |
41 if( file.isFile() ) { | |
42 Response response = new Response(); | |
43 response.body = new Response.Body( file.length(), new FileInputStream(file) ); | |
44 return response; | |
45 } | |
46 return null; | |
47 } catch(IOException e) { | |
48 throw new RuntimeException(e); | |
49 } | |
50 } | |
51 } |