diff options
Diffstat (limited to 'container-search-gui')
4 files changed, 159 insertions, 89 deletions
diff --git a/container-search-gui/CMakeLists.txt b/container-search-gui/CMakeLists.txt deleted file mode 100644 index 95476154443..00000000000 --- a/container-search-gui/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -# Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -install_fat_java_artifact(container-search-gui)
\ No newline at end of file diff --git a/container-search-gui/pom.xml b/container-search-gui/pom.xml index f598758e1de..f2ca70dd1f3 100644 --- a/container-search-gui/pom.xml +++ b/container-search-gui/pom.xml @@ -4,55 +4,46 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>com.yahoo.vespa</groupId> - <artifactId>parent</artifactId> - <version>6-SNAPSHOT</version> - <relativePath>../parent/pom.xml</relativePath> - </parent> - <artifactId>container-search-gui</artifactId> - <packaging>container-plugin</packaging> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.yahoo.vespa</groupId> + <artifactId>parent</artifactId> <version>6-SNAPSHOT</version> - <dependencies> - <dependency> - <groupId>com.google.inject</groupId> - <artifactId>guice</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.yahoo.vespa</groupId> - <artifactId>yolean</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.yahoo.vespa</groupId> - <artifactId>jdisc_http_service</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.yahoo.vespa</groupId> - <artifactId>jdisc_core</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.yahoo.vespa</groupId> - <artifactId>container-core</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - </dependencies> - <build> - <plugins> - <plugin> - <groupId>com.yahoo.vespa</groupId> - <artifactId>bundle-plugin</artifactId> - <extensions>true</extensions> - </plugin> - </plugins> - </build> - + <relativePath>../parent/pom.xml</relativePath> + </parent> + <artifactId>container-search-gui</artifactId> + <packaging>jar</packaging> + <version>6-SNAPSHOT</version> + <dependencies> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>application</artifactId> + <version>6-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <compilerArgs> + <arg>-Xlint:all</arg> + <arg>-Xlint:-rawtypes</arg> + <arg>-Xlint:-unchecked</arg> + <arg>-Xlint:-serial</arg> + <arg>-Xlint:-deprecation</arg> + <arg>-Xlint:-dep-ann</arg> + <arg>-Xlint:-cast</arg> + <arg>-Werror</arg> + </compilerArgs> + </configuration> + </plugin> + </plugins> + </build> </project>
\ No newline at end of file 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 + +} diff --git a/container-search-gui/src/test/java/com/yahoo/search/query/gui/GUIHandlerTest.java b/container-search-gui/src/test/java/com/yahoo/search/query/gui/GUIHandlerTest.java new file mode 100644 index 00000000000..ec515a1bf4a --- /dev/null +++ b/container-search-gui/src/test/java/com/yahoo/search/query/gui/GUIHandlerTest.java @@ -0,0 +1,85 @@ +// 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.yahoo.application.Networking; +import com.yahoo.application.container.JDisc; +import com.yahoo.application.container.handler.Request; +import com.yahoo.application.container.handler.Response; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + + +/** + * @author Henrik Høiness + */ + +public class GUIHandlerTest { + + private JDisc container; + + @Before + public void startContainer() { + container = JDisc.fromServicesXml(servicesXml(), Networking.enable); + } + + @After + public void stopContainer() { + /* + try { + Thread.sleep(100_000); + } catch (InterruptedException e) { + e.printStackTrace(); + }*/ + container.close(); + } + + @Test + public void testRequest() throws Exception { + assertResponse("/querybuilder/", "<!-- Copyright 2018 Yahoo Holdings.","text/html; charset=UTF-8", 200); + } + + @Test + public void testContentTypes() throws Exception{ + assertResponse("/querybuilder/_includes/css/vespa.css", "/**","text/css; charset=UTF-8", 200); + assertResponse("/querybuilder/js/agency.js", "/*!","application/javascript; charset=UTF-8", 200); + assertResponse("/querybuilder/img/reload.svg", "<?xml","image/svg+xml; charset=UTF-8", 200); + assertResponse("/querybuilder/img/Vespa-V2.png", null,"image/png; charset=UTF-8", 200); + } + + @Test + public void testInvalidPath() throws Exception{ + assertResponse("/querybuilder/invalid_filepath", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Nothing at path","application/json; charset=UTF-8", 404); + } + + + private void assertResponse(String path, String expectedStartString, String expectedContentType, int expectedStatusCode) throws IOException { + assertResponse(Request.Method.GET, path, expectedStartString,expectedContentType, expectedStatusCode); + } + + private void assertResponse(Request.Method method, String path, String expectedStartString, String expectedContentType, int expectedStatusCode) throws IOException { + Response response = container.handleRequest(new Request("http://localhost:8080" + path, new byte[0], method)); + Assert.assertEquals("Status code", expectedStatusCode, response.getStatus()); + Assert.assertEquals(expectedContentType, response.getHeaders().getFirst("Content-Type")); + if(expectedStartString != null){ + assertTrue(response.getBodyAsString().startsWith(expectedStartString)); + } + } + + private String servicesXml() { + return "<jdisc version='1.0'>\n" + + " <handler id='com.yahoo.search.query.gui.GUIHandler'>\n" + + " <binding>http://*/querybuilder/*</binding>\n" + + " </handler>\n" + + " <http>\n" + + " <server id='default' port='8080'/>\n" + + " </http>\n" + + "</jdisc>"; + } + +}
\ No newline at end of file |