-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathFrontend.java
155 lines (139 loc) · 5.53 KB
/
Frontend.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.net.InetSocketAddress;
import java.net.URI;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class Frontend {
private static Gson gson = new Gson();
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new IndexHandler());
server.createContext("/cats", new CatsHandler());
server.createContext("/static", new StaticHandler());
server.setExecutor(null);
server.start();
}
static class CatsHandler implements HttpHandler {
private class Req {
public String[] names;
}
@Override
public void handle(HttpExchange t) throws IOException {
try {
String body = getBody(t);
Req req = gson.fromJson(body, Req.class);
if (req.names.length > 5) {
t.sendResponseHeaders(400, 0);
return;
}
String traceId = getTraceId(t);
String[] cats = getCats(req.names, traceId);
String response = gson.toJson(cats);
t.getResponseHeaders().set("Content-Type", "application/json");
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
} catch (Exception e) {
System.out.println(e);
t.sendResponseHeaders(500, 0);
} finally {
t.close();
}
}
private String getBody(HttpExchange t) throws IOException {
StringBuilder sb = new StringBuilder();
InputStream ios = t.getRequestBody();
int i;
while ((i = ios.read()) != -1) {
sb.append((char) i);
}
return sb.toString();
}
private String getTraceId(HttpExchange t) {
String query = t.getRequestURI().getQuery();
return query.replaceFirst("^traceId=", "");
}
private String[] getCats(String[] names, String trace) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response) throws IOException {
return EntityUtils.toString(response.getEntity());
}
};
try {
String[] cats = new String[names.length];
for (int i = 0; i < names.length; i++) {
String name = names[i].trim();
URI url = new URI("http://localhost:8085/cat/" + URLEncoder.encode(name, "UTF-8"));
HttpGet httpget = new HttpGet(url);
httpget.setHeader("X-Trace", trace);
String response = httpclient.execute(httpget, responseHandler);
cats[i] = response;
}
return cats;
} finally {
httpclient.close();
}
}
}
static class IndexHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
try {
File file = new File("index.html");
t.getResponseHeaders().set("Content-Type", "text/html");
t.sendResponseHeaders(200, file.length());
OutputStream os = t.getResponseBody();
Files.copy(file.toPath(), os);
os.close();
} catch (Exception e) {
System.out.println(e);
t.sendResponseHeaders(500, 0);
} finally {
t.close();
}
}
}
static class StaticHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
try {
Integer catNumber = getCatNumber(t);
String filePath = "static/" + catNumber.toString() + ".jpg";
File file = new File(filePath);
t.getResponseHeaders().set("Content-Type", "image/jpeg");
t.sendResponseHeaders(200, file.length());
OutputStream os = t.getResponseBody();
Files.copy(file.toPath(), os);
os.close();
} catch (Exception e) {
System.out.println(e);
t.sendResponseHeaders(500, 0);
} finally {
t.close();
}
}
private Integer getCatNumber(HttpExchange t) throws Exception {
String path = t.getRequestURI().getPath();
Integer n = Integer.valueOf(path.replaceFirst("^/static/", ""));
if (n < 0 || n > 8) {
throw new Exception("out of catnip");
}
return n;
}
}
}