diff options
Diffstat (limited to 'container-search-gui/src/main/java')
-rw-r--r-- | container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java | 121 | ||||
-rw-r--r-- | container-search-gui/src/main/java/com/yahoo/search/query/restapi/ErrorResponse.java | 80 |
2 files changed, 201 insertions, 0 deletions
diff --git a/container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java b/container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java new file mode 100644 index 00000000000..a7990f34577 --- /dev/null +++ b/container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java @@ -0,0 +1,121 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.gui; + +import com.google.inject.Inject; +import com.yahoo.container.jdisc.HttpRequest; +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.container.jdisc.LoggingRequestHandler; + + +import com.yahoo.search.query.restapi.ErrorResponse; +import com.yahoo.yolean.Exceptions; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.logging.Level; + +/** + * Takes requests on Querybuilder + * + * @author Henrik Høiness + */ + +public class GUIHandler extends LoggingRequestHandler { + + @Inject + public GUIHandler(Context parentCtx) { + super(parentCtx); + } + + @Override + public HttpResponse handle(HttpRequest request) { + try { + switch (request.getMethod()) { + case GET: return handleGET(request); + default: return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported"); + } + } catch (IllegalArgumentException e) { + return ErrorResponse.badRequest(Exceptions.toMessageString(e)); + } catch (RuntimeException e) { + log.log(Level.WARNING, "Unexpected error handling '" + request.getUri() + "'", e); + return ErrorResponse.internalServerError(Exceptions.toMessageString(e)); + } + } + + private HttpResponse handleGET(HttpRequest request) { + com.yahoo.restapi.Path path = new com.yahoo.restapi.Path(request.getUri().getPath()); + if (path.matches("/querybuilder/")) { + return new FileResponse("_includes/index.html"); + } + if (!path.matches("/querybuilder/{*}") ) { + return ErrorResponse.notFoundError("Nothing at " + path); + } + String filepath = path.getRest(); + if (!isValidPath(GUIHandler.class.getClassLoader().getResource("gui").getFile()+"/"+filepath)){ + return ErrorResponse.notFoundError("Nothing at " + path); + } + return new FileResponse(filepath); + } + + private static boolean isValidPath(String path) { + File file = new File(path); + return file.exists(); + } + + private static class FileResponse extends HttpResponse { + + private final Path path; + + public FileResponse(String relativePath) { + super(200); + this.path = Paths.get(GUIHandler.class.getClassLoader().getResource("gui").getFile(), relativePath); + } + + @Override + public void render(OutputStream out) throws IOException { + byte[] data = Files.readAllBytes(path); + out.write(data); + } + + @Override + public String getContentType() { + if (path.toString().endsWith(".css")) { + return "text/css"; + } else if (path.toString().endsWith(".js")) { + return "application/javascript"; + } else if (path.toString().endsWith(".html")) { + return "text/html"; + }else if (path.toString().endsWith(".php")) { + return "text/php"; + }else if (path.toString().endsWith(".svg")) { + return "image/svg+xml"; + }else if (path.toString().endsWith(".eot")) { + return "application/vnd.ms-fontobject"; + }else if (path.toString().endsWith(".ttf")) { + return "font/ttf"; + }else if (path.toString().endsWith(".woff")) { + return "font/woff"; + }else if (path.toString().endsWith(".woff2")) { + return "font/woff2"; + }else if (path.toString().endsWith(".otf")) { + return "font/otf"; + }else if (path.toString().endsWith(".png")) { + return "image/png"; + }else if (path.toString().endsWith(".xml")) { + return "application/xml"; + }else if (path.toString().endsWith(".ico")) { + return "image/x-icon"; + }else if (path.toString().endsWith(".json")) { + return "application/json"; + }else if (path.toString().endsWith(".ttf")) { + return "font/ttf"; + } + return "text/html"; + } + } + +} diff --git a/container-search-gui/src/main/java/com/yahoo/search/query/restapi/ErrorResponse.java b/container-search-gui/src/main/java/com/yahoo/search/query/restapi/ErrorResponse.java new file mode 100644 index 00000000000..2cce4785708 --- /dev/null +++ b/container-search-gui/src/main/java/com/yahoo/search/query/restapi/ErrorResponse.java @@ -0,0 +1,80 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query.restapi; + +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.slime.Cursor; +import com.yahoo.slime.JsonFormat; +import com.yahoo.slime.Slime; + +import java.io.IOException; +import java.io.OutputStream; + +import static com.yahoo.jdisc.Response.Status.BAD_REQUEST; +import static com.yahoo.jdisc.Response.Status.FORBIDDEN; +import static com.yahoo.jdisc.Response.Status.INTERNAL_SERVER_ERROR; +import static com.yahoo.jdisc.Response.Status.METHOD_NOT_ALLOWED; +import static com.yahoo.jdisc.Response.Status.NOT_FOUND; +import static com.yahoo.jdisc.Response.Status.UNAUTHORIZED; + +/** + * Error responses with JSON bodies + * + * @author bratseth + */ +public class ErrorResponse extends HttpResponse { + + private final Slime slime = new Slime(); + private final String message; + + public enum ErrorCode { + FORBIDDEN, + UNAUTHORIZED, + NOT_FOUND, + BAD_REQUEST, + METHOD_NOT_ALLOWED, + INTERNAL_SERVER_ERROR + } + + private ErrorResponse(int code, ErrorCode errorCode, String message) { + super(code); + this.message = message; + Cursor root = slime.setObject(); + root.setString("error-code", errorCode.name()); + root.setString("message", message); + } + + public String message() { return message; } + + public static ErrorResponse notFoundError(String message) { + return new ErrorResponse(NOT_FOUND, ErrorCode.NOT_FOUND, message); + } + + public static ErrorResponse internalServerError(String message) { + return new ErrorResponse(INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR, message); + } + + public static ErrorResponse badRequest(String message) { + return new ErrorResponse(BAD_REQUEST, ErrorCode.BAD_REQUEST, message); + } + + public static ErrorResponse methodNotAllowed(String message) { + return new ErrorResponse(METHOD_NOT_ALLOWED, ErrorCode.METHOD_NOT_ALLOWED, message); + } + + public static ErrorResponse unauthorized(String message) { + return new ErrorResponse(UNAUTHORIZED, ErrorCode.UNAUTHORIZED, message); + } + + public static ErrorResponse forbidden(String message) { + return new ErrorResponse(FORBIDDEN, ErrorCode.FORBIDDEN, message); + } + + @Override + public void render(OutputStream stream) throws IOException { + new JsonFormat(true).encode(stream, slime); + } + + @Override + public String getContentType() { return "application/json"; } + +} |