org.javawebstack.httpserver.handler.StaticFileHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http-server Show documentation
Show all versions of http-server Show documentation
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.
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 "";
}
}