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 { FileInputStream fs = new FileInputStream(DynRes.resFile); resp.setData(fs); return resp; } catch (FileNotFoundException e) { return new HttpResponse(HttpStatusCode.INTERNAL_SERVER_ERROR); } } return new HttpResponse(HttpStatusCode.NOT_FOUND); } public RequestHandler(String relativePath) { r = relativePath; } }