All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javawebstack.httpserver.handler.StaticFileHandler Maven / Gradle / Ivy

Go to download

This library provides a routing and request mapping stack on top of the well known and industry proven eclipse jetty http server. It also supports websockets.

There is a newer version: 1.0.2
Show newest version
package org.javawebstack.httpserver.handler;

import org.javawebstack.httpserver.Exchange;
import org.javawebstack.httpserver.helper.MimeType;
import org.javawebstack.httpserver.util.FileProvider;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class StaticFileHandler implements RequestHandler {

    private final List providers = new ArrayList<>();

    public StaticFileHandler add(FileProvider provider) {
        providers.add(provider);
        return this;
    }

    public Object handle(Exchange exchange) {
        String path = exchange.path("path");
        InputStream stream = null;
        for (FileProvider provider : providers) {
            stream = provider.getFile(path);
            if (stream != null)
                break;
        }
        if (stream == null)
            return null;
        exchange.contentType(MimeType.byFileName(path));
        try {
            exchange.write(stream);
        } catch (IOException exception) {
            return null;
        }
        return "";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy