blob: 0f4eebd7fd70fa530eaab17334605787e2035a5d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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";
}
}
}
|