2020-09-07 13:29:44 +02:00
|
|
|
package io.gitlab.jfronny.dynres.web;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.dynres.DynRes;
|
|
|
|
import io.gitlab.jfronny.dynres.web.bluemapcore.HttpRequest;
|
|
|
|
import io.gitlab.jfronny.dynres.web.bluemapcore.HttpRequestHandler;
|
|
|
|
import io.gitlab.jfronny.dynres.web.bluemapcore.HttpResponse;
|
|
|
|
import io.gitlab.jfronny.dynres.web.bluemapcore.HttpStatusCode;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
public class RequestHandler implements HttpRequestHandler {
|
|
|
|
String r;
|
|
|
|
@Override
|
|
|
|
public HttpResponse handle(HttpRequest request) {
|
|
|
|
String method = request.getMethod().toUpperCase();
|
|
|
|
if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
|
|
|
|
HttpResponse resp = new HttpResponse(HttpStatusCode.BAD_REQUEST);
|
|
|
|
resp.setData(method + " method not supported");
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
String path = DynRes.simplifyElement(request.getPath());
|
|
|
|
|
|
|
|
if (Objects.equals(path, r)) {
|
|
|
|
HttpResponse resp = new HttpResponse(HttpStatusCode.OK);
|
|
|
|
resp.addHeader("Server", "DynRes using BlueMap");
|
|
|
|
resp.addHeader("Cache-Control", "no-cache");
|
|
|
|
resp.addHeader("Content-Type", "application/zip");
|
|
|
|
|
|
|
|
try {
|
2020-09-07 15:03:15 +02:00
|
|
|
FileInputStream fs = new FileInputStream(DynRes.resFile);
|
2020-09-07 13:29:44 +02:00
|
|
|
resp.setData(fs);
|
|
|
|
return resp;
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
return new HttpResponse(HttpStatusCode.INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new HttpResponse(HttpStatusCode.NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:03:15 +02:00
|
|
|
public RequestHandler(String relativePath) {
|
2020-09-07 13:29:44 +02:00
|
|
|
r = relativePath;
|
|
|
|
}
|
|
|
|
}
|