use of org.simpleframework.http.resource.Index in project gradle by gradle.
the class SimpleFileServerContainer method handle.
public void handle(Request req, Response resp) {
Index requestIndex = context.getIndex(req.getTarget());
File targetFile = requestIndex.getFile();
if (!targetFile.exists()) {
resp.setCode(404);
resp.setText("Not Found");
try {
resp.getPrintStream().println(String.format("File '%s' does not exist", targetFile.getAbsolutePath()));
resp.commit();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
String contentType = requestIndex.getContentType();
resp.set("Content-Type", contentType);
OutputStream output = null;
try {
output = resp.getOutputStream();
if (contentType.startsWith("text/")) {
resp.set("Content-Encoding", Charset.defaultCharset().name());
Reader input = new FileReader(requestIndex.getFile());
IOUtils.copy(input, output);
IOUtils.closeQuietly(input);
} else {
InputStream input = new FileInputStream(requestIndex.getFile());
IOUtils.copy(input, output);
IOUtils.closeQuietly(input);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IOUtils.closeQuietly(output);
}
}
Aggregations