diff options
author | Arnstein Ressem <aressem@gmail.com> | 2018-07-12 08:56:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-12 08:56:39 +0200 |
commit | f7b9c0261930c81132c66e3f174b8c468894b92f (patch) | |
tree | 2ab512c304adece0a9126add4b3e7b8f5f9bd588 /container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java | |
parent | e243642ab09dc3aed3a10c7fc1634e80b4f23a57 (diff) | |
parent | ef5ac16e81ddfbb2e5553769aac53c268944e8b9 (diff) |
Merge pull request #6385 from vespa-engine/revert-6366-henrhoi/adding-gui-handler-when-building-containermodel
Revert "Adding handler and binding when services.xml has <search>-field"
Diffstat (limited to 'container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java')
-rw-r--r-- | container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java | 70 |
1 files changed, 33 insertions, 37 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 index 95adbbea3fc..0f4eebd7fd7 100644 --- 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 @@ -5,13 +5,17 @@ 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.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.logging.Level; /** @@ -43,83 +47,75 @@ public class GUIHandler extends LoggingRequestHandler { } private HttpResponse handleGET(HttpRequest request) { - com.yahoo.restapi.Path path = new com.yahoo.restapi.Path(request.getUri().getPath()); + 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:" + path); + return ErrorResponse.notFoundError("Nothing at " + path); } String filepath = path.getRest(); - if (!isValidPath(filepath)){ - return ErrorResponse.notFoundError("Nothing at path:" + filepath); + 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) { - InputStream in = GUIHandler.class.getClassLoader().getResourceAsStream("gui/"+path); - boolean isValid = (in != null); - if(isValid){ - try { in.close(); } catch (IOException e) {/* Problem with closing inputstream */} - } - - return isValid; + File file = new File(path); + return file.exists(); } private static class FileResponse extends HttpResponse { - private final String path; + private final Path path; public FileResponse(String relativePath) { super(200); - this.path = relativePath; + this.path = Paths.get(GUIHandler.class.getClassLoader().getResource("gui").getFile(), relativePath); } @Override public void render(OutputStream out) throws IOException { - InputStream is = GUIHandler.class.getClassLoader().getResourceAsStream("gui/"+this.path); - byte[] buf = new byte[1024]; - int numRead; - while ( (numRead = is.read(buf) ) >= 0) { - out.write(buf, 0, numRead); - } + byte[] data = Files.readAllBytes(path); + out.write(data); } @Override public String getContentType() { - if (path.endsWith(".css")) { + if (path.toString().endsWith(".css")) { return "text/css"; - } else if (path.endsWith(".js")) { + } else if (path.toString().endsWith(".js")) { return "application/javascript"; - } else if (path.endsWith(".html")) { + } else if (path.toString().endsWith(".html")) { return "text/html"; - } else if (path.endsWith(".php")) { + }else if (path.toString().endsWith(".php")) { return "text/php"; - } else if (path.endsWith(".svg")) { + }else if (path.toString().endsWith(".svg")) { return "image/svg+xml"; - } else if (path.endsWith(".eot")) { + }else if (path.toString().endsWith(".eot")) { return "application/vnd.ms-fontobject"; - } else if (path.endsWith(".ttf")) { + }else if (path.toString().endsWith(".ttf")) { return "font/ttf"; - } else if (path.endsWith(".woff")) { + }else if (path.toString().endsWith(".woff")) { return "font/woff"; - } else if (path.endsWith(".woff2")) { + }else if (path.toString().endsWith(".woff2")) { return "font/woff2"; - } else if (path.endsWith(".otf")) { + }else if (path.toString().endsWith(".otf")) { return "font/otf"; - } else if (path.endsWith(".png")) { + }else if (path.toString().endsWith(".png")) { return "image/png"; - } else if (path.endsWith(".xml")) { + }else if (path.toString().endsWith(".xml")) { return "application/xml"; - } else if (path.endsWith(".ico")) { + }else if (path.toString().endsWith(".ico")) { return "image/x-icon"; - } else if (path.endsWith(".json")) { + }else if (path.toString().endsWith(".json")) { return "application/json"; - } else if (path.endsWith(".ttf")) { + }else if (path.toString().endsWith(".ttf")) { return "font/ttf"; } return "text/html"; } } -}
\ No newline at end of file + +} |