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 | 208 | ||||
-rw-r--r-- | container-search-gui/src/main/java/com/yahoo/search/query/restapi/ErrorResponse.java | 80 |
2 files changed, 0 insertions, 288 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 deleted file mode 100644 index e370b1f19d3..00000000000 --- a/container-search-gui/src/main/java/com/yahoo/search/query/gui/GUIHandler.java +++ /dev/null @@ -1,208 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.search.query.gui; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.yahoo.component.annotation.Inject; -import com.yahoo.container.QrSearchersConfig; -import com.yahoo.container.jdisc.HttpRequest; -import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; -import com.yahoo.prelude.IndexModel; -import com.yahoo.prelude.querytransform.RecallSearcher; -import com.yahoo.restapi.Path; -import com.yahoo.search.Query; -import com.yahoo.search.config.IndexInfoConfig; -import com.yahoo.search.query.Model; -import com.yahoo.search.query.Presentation; -import com.yahoo.search.query.Ranking; -import com.yahoo.search.query.Trace; -import com.yahoo.search.query.ranking.Diversity; -import com.yahoo.search.query.ranking.MatchPhase; -import com.yahoo.search.query.restapi.ErrorResponse; -import com.yahoo.search.yql.MinimalQueryInserter; -import com.yahoo.vespa.config.search.RankProfilesConfig; -import com.yahoo.yolean.Exceptions; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UncheckedIOException; -import java.util.logging.Level; - - -/** - * Takes requests on /querybuilder - * - * @author Henrik Høiness - */ -public class GUIHandler extends ThreadedHttpRequestHandler { - - private static final ObjectMapper jsonMapper = new ObjectMapper(); - - private final IndexModel indexModel; - private final RankProfilesConfig rankProfilesConfig; - - @Inject - public GUIHandler(Context parentContext, IndexInfoConfig indexInfo, QrSearchersConfig clusters, RankProfilesConfig rankProfilesConfig) { - super(parentContext); - this.indexModel = new IndexModel(indexInfo, clusters); - this.rankProfilesConfig = rankProfilesConfig; - - } - - @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) { - Path path = new Path(request.getUri()); - if (path.matches("/querybuilder/")) { - return new FileResponse("_includes/index.html", null, null); - } - if ( ! path.matches("/querybuilder/{*}") ) { - return ErrorResponse.notFoundError("Nothing at path:" + path); - } - String filepath = String.join("/", path.getRest().segments()); - if ( ! filepath.equals("config.json") && ! isValidPath(filepath)){ - return ErrorResponse.notFoundError("Nothing at path:" + filepath); - } - return new FileResponse(filepath, indexModel, rankProfilesConfig); - } - - private static boolean isValidPath(String path) { - InputStream in = GUIHandler.class.getClassLoader().getResourceAsStream("gui/" + path); - if (in != null){ - try { in.close(); } catch (IOException e) { throw new UncheckedIOException(e); } - return true; - } - return false; - } - - private static class FileResponse extends HttpResponse { - - private final String path; - private final IndexModel indexModel; - private final RankProfilesConfig rankProfilesConfig; - - public FileResponse(String relativePath, IndexModel indexModel, RankProfilesConfig rankProfilesConfig) { - super(200); - this.path = relativePath; - this.indexModel = indexModel; - this.rankProfilesConfig = rankProfilesConfig; - - } - - @Override - public void render(OutputStream out) throws IOException { - InputStream is; - if (this.path.equals("config.json")){ - String json = "{}"; - try { json = getGUIConfig(); } catch (IOException e) { /*Something happened while parsing JSON */ } - is = new ByteArrayInputStream(json.getBytes()); - } else { - 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); - } - } - - @Override - public String getContentType() { - if (path.endsWith(".css")) { - return "text/css"; - } else if (path.endsWith(".js")) { - return "application/javascript"; - } else if (path.endsWith(".html")) { - return "text/html"; - } else if (path.endsWith(".php")) { - return "text/php"; - } else if (path.endsWith(".svg")) { - return "image/svg+xml"; - } else if (path.endsWith(".eot")) { - return "application/vnd.ms-fontobject"; - } else if (path.endsWith(".ttf")) { - return "font/ttf"; - } else if (path.endsWith(".woff")) { - return "font/woff"; - } else if (path.endsWith(".woff2")) { - return "font/woff2"; - } else if (path.endsWith(".otf")) { - return "font/otf"; - } else if (path.endsWith(".png")) { - return "image/png"; - } else if (path.endsWith(".xml")) { - return "application/xml"; - } else if (path.endsWith(".ico")) { - return "image/x-icon"; - } else if (path.endsWith(".json")) { - return "application/json"; - } - return "text/html"; - } - - private String getGUIConfig() throws IOException { - ObjectNode json = jsonMapper.createObjectNode(); - json.set("ranking_properties", jsonMapper.createArrayNode().add("propertyname")); - json.set("ranking_features", jsonMapper.createArrayNode().add("featurename")); - - ArrayNode sources = jsonMapper.createArrayNode(); - - try { - indexModel.getMasterClusters().keySet().forEach(sources::add); - } catch (NullPointerException ex){ /* clusters are not set */ } - json.set("model_sources", sources); - - ArrayNode rankProfiles = jsonMapper.createArrayNode(); - try { - rankProfilesConfig.rankprofile().forEach(rankProfile -> rankProfiles.add(rankProfile.name())); - } catch (NullPointerException ex){ /* rankprofiles are not set*/ } - json.set("ranking_profile", rankProfiles); - - - // Creating map from parent to children for GUI: parameter --> child-parameters - ObjectNode childMap = jsonMapper.createObjectNode(); - childMap.set(Model.MODEL, jsonMapper.createArrayNode().add(Model.DEFAULT_INDEX).add(Model.ENCODING).add(Model.LANGUAGE).add(Model.QUERY_STRING).add(Model.RESTRICT).add(Model.SEARCH_PATH).add(Model.SOURCES).add(Model.TYPE)); - childMap.set(Ranking.RANKING, jsonMapper.createArrayNode().add(Ranking.LOCATION).add(Ranking.FEATURES).add(Ranking.LIST_FEATURES).add(Ranking.PROFILE).add(Ranking.PROPERTIES).add(Ranking.SORTING).add(Ranking.FRESHNESS).add(Ranking.QUERYCACHE).add(Ranking.MATCH_PHASE)); - childMap.set(Ranking.RANKING +"."+ Ranking.MATCH_PHASE, jsonMapper.createArrayNode().add(MatchPhase.MAX_HITS).add(MatchPhase.ATTRIBUTE).add(MatchPhase.ASCENDING).add(Ranking.DIVERSITY)); - childMap.set(Ranking.RANKING +"."+ Ranking.MATCH_PHASE +"."+Ranking.DIVERSITY, jsonMapper.createArrayNode().add(Diversity.ATTRIBUTE).add(Diversity.MINGROUPS)); - childMap.set(Presentation.PRESENTATION, jsonMapper.createArrayNode().add(Presentation.BOLDING).add(Presentation.FORMAT).add(Presentation.SUMMARY).add("template").add(Presentation.TIMING )); - childMap.set("trace", jsonMapper.createArrayNode().add("timestamps")); - childMap.set("tracelevel", jsonMapper.createArrayNode().add("rules")); - childMap.set("metrics", jsonMapper.createArrayNode().add("ignore")); - childMap.set("collapse", jsonMapper.createArrayNode().add("summary")); - childMap.set("pos", jsonMapper.createArrayNode().add("ll").add("radius").add("bb").add("attribute")); - childMap.set("streaming", jsonMapper.createArrayNode().add("userid").add("groupname").add("selection").add("priority").add("maxbucketspervisitor")); - childMap.set("rules", jsonMapper.createArrayNode().add("off").add("rulebase")); - json.set("childMap", childMap); - - ArrayNode levelZeroParameters = jsonMapper.createArrayNode().add(MinimalQueryInserter.YQL.toString()).add(Query.HITS.toString()).add(Query.OFFSET.toString()) - .add("queryProfile").add(Query.NO_CACHE.toString()).add(Query.GROUPING_SESSION_CACHE.toString()) - .add(Query.SEARCH_CHAIN.toString()).add(Query.TIMEOUT.toString()).add("trace") - .add("tracelevel").add("traceLevel") // TODO: Remove on Vespa 9 - .add("explainLevel").add("explainlevel") // TODO: Remove on Vespa 9 - .add(Model.MODEL).add(Ranking.RANKING).add("collapse").add("collapsesize").add("collapsefield") - .add(Presentation.PRESENTATION).add("pos").add("streaming").add("rules").add(RecallSearcher.recallName.toString()).add("user") - .add("metrics").add(""); - json.set("levelZeroParameters", levelZeroParameters); - - return jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json); - } - } -}
\ No newline at end of file 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 deleted file mode 100644 index a5bdfc10743..00000000000 --- a/container-search-gui/src/main/java/com/yahoo/search/query/restapi/ErrorResponse.java +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright Yahoo. 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"; } - -} |