Search in sources :

Example 1 with Index

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);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) FileReader(java.io.FileReader) Index(org.simpleframework.http.resource.Index) UncheckedIOException(org.gradle.api.UncheckedIOException) FileReader(java.io.FileReader) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 Index (org.simpleframework.http.resource.Index)1