/
FileRequestHandler.java
228 lines (196 loc) · 6.63 KB
/
FileRequestHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.vonhof.webi.file;
import com.vonhof.webi.HttpException;
import com.vonhof.webi.RequestHandler;
import com.vonhof.webi.Webi;
import com.vonhof.webi.WebiContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.ServletException;
/**
* File request handler. Servers local static file resources.
* @author Henrik Hofmeister <@vonhofdk>
*/
public class FileRequestHandler implements RequestHandler {
@Inject
protected Webi webi;
/**
* Mime type map
*/
private final Map<String,String> mimeTypes = new HashMap<String, String>();
/**
* Document root
*/
private String docRoot = "./";
/**
* Default mime type - used when no mime type could be found from file extension
*/
private String defaultMimeType = "text/plain";
/**
* Index file - defaults to this file if url points to directory
*/
private String indexFileName = "index.html";
@Override
public final void handle(WebiContext ctxt) throws IOException, ServletException {
String filePath = String.format("%s%s",docRoot,ctxt.getPath());
File file = new File(filePath);
if (file.isDirectory()) {
//Redirect to url with ending /
if (!ctxt.getPath().endsWith("/")) {
ctxt.redirect(ctxt.getPath()+"/");
return;
}
String indexFilePath = filePath+"/"+indexFileName;
File indexFile = new File(indexFilePath);
if (!indexFile.exists()) {
serveDir(ctxt, file);
return;
} else {
file = indexFile;
}
}
if (file.exists()) {
serveFile(ctxt, file);;
} else {
unknownFile(ctxt, file, filePath);
}
}
protected void unknownFile(WebiContext ctxt,File file,String path) throws IOException {
ctxt.sendError(new HttpException(HttpException.NOT_FOUND,"Not found"));
if (!file.getName().equalsIgnoreCase("favicon.ico")) {
System.out.println("File not found:"+path);
}
}
/**
* Called when url points to directory and no index file was found.
* Outputs dir contents
* @param req
* @param dir
* @throws IOException
*/
protected void serveDir(WebiContext req,File dir) throws IOException {
req.setResponseType("text/html");
File[] files = dir.listFiles();
StringBuilder sb = new StringBuilder();
sb.append("<html><head><title>Dir:")
.append(dir.getAbsolutePath())
.append("</title></head><body><table><thead><tr><th>File</th><th>Size</th></tr></thead><tbody>");
for(File file:files) {
String path = file.getName();
if (file.isDirectory())
path += "/";
sb.append("<tr>")
.append(String.format("<td><a href=\"%s\">%s</a></td>",
path,path))
.append(String.format("<td>%s</td>","??"));
}
sb.append("</tbody></table></body></html>");
req.getOutputStream().write(sb.toString().getBytes());
req.flushBuffer();
}
/**
* Serve file from local filesystem. Gets mime type from file
* @param req
* @param file
* @throws IOException
*/
protected void serveFile(WebiContext req,File file) throws IOException {
req.setHeader("Content-type",getResponseType(file));
long lastModified = file.lastModified();
long reqLastModified = req.getRequest().getDateHeader("If-Modified-Since");
req.setDateHeader("Last-Modified",lastModified);
if (reqLastModified > 0 && lastModified <= reqLastModified) {
req.setStatus(304);
req.flushBuffer();
return;
}
FileInputStream fileIn = new FileInputStream(file);
while(fileIn.available() > 0) {
req.getOutputStream().write(fileIn.read());
}
req.flushBuffer();
}
/**
* Get mime type from file name (extension)
* @param file
* @return
*/
protected String getResponseType(File file) {
String ext = getFileExt(file);
String mimeType = mimeTypes.get(ext);
if (mimeType == null) {
mimeType = defaultMimeType;
}
return mimeType;
}
/**
* Get file extension
* @param file
* @return
*/
protected String getFileExt(File file) {
String[] parts = file.getName().split("\\.");
return parts[parts.length-1].toLowerCase();
}
/**
* Set default index file for directories
* @param indexFileName
*/
public void setIndexFileName(String indexFileName) {
this.indexFileName = indexFileName;
}
/**
* Set document root
* @param docRoot
*/
public void setDocumentRoot(String docRoot) {
this.docRoot = docRoot;
}
public String getDocumentRoot() {
return docRoot;
}
/**
* Add mime type to file extension mapping
* @param fileExtension
* @param mimeType
*/
public void addMimeType(String fileExtension, String mimeType) {
mimeTypes.put(fileExtension.toLowerCase(), mimeType.toLowerCase());
}
/**
* Set default mime type - used as fallback for file extensions
* not found in mime type map
* @param defaultMimeType
*/
public void setDefaultMimeType(String defaultMimeType) {
this.defaultMimeType = defaultMimeType;
}
/**
* Get file handler initialized with typical mime types
* @return
*/
public static FileRequestHandler getStandardFileHandler() {
FileRequestHandler out = new FileRequestHandler();
out.addDefaultMimeTypes();
return out;
}
public void addDefaultMimeTypes() {
addMimeType("jpg","image/jpeg");
addMimeType("png","image/png");
addMimeType("gif","image/gif");
addMimeType("bmp","image/bmp");
addMimeType("html","text/html");
addMimeType("htm","text/html");
addMimeType("css","text/css");
addMimeType("json","application/json");
addMimeType("xml","text/xml");
addMimeType("js","text/javascript");
addMimeType("woff","application/x-font-woff");
addMimeType("ttf","font/ttf");
addMimeType("eot","font/eot");
addMimeType("otf","font/otf");
}
}