-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVC.java
97 lines (85 loc) · 3.13 KB
/
SVC.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
import java.io.*;
import java.net.*;
import java.util.*;
import com.sun.net.httpserver.*;
import javax.json.*;
public class SVC {
public static void main(String[] args) throws Exception {
System.out.println("Hi");
HttpServer server = HttpServer.create(new InetSocketAddress(4008), 0);
server.createContext("/form", new FormHandler());
server.createContext("/json", new JsonHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class FormHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response =
"<html>" +
"<form action=\"/json\">" +
" <textarea rows=10 columns=120 name=\"text\">" +
" </textarea>" +
" <input type=\"submit\" name=\"btn\"/>" +
"</form>" +
"</html>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class JsonHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
try {
handle2(t);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void handle2(HttpExchange t) throws IOException {
System.err.println("handle");
Map<String, List<String>> sp = splitQuery(t.getRequestURI().getQuery());
String json_str = sp.get("text").get(0);
System.err.println(json_str);
JsonReader jsonReader = Json.createReader(new StringReader(json_str));
JsonObject jobj = jsonReader.readObject();
JsonArray ja = jobj.getJsonArray("a");
System.err.println(ja);
JsonArray jb = jobj.getJsonArray("b");
System.err.println(jb);
JsonBuilderFactory factory = Json.createBuilderFactory(null);
JsonArrayBuilder jresult = factory.createArrayBuilder();
for (int i=0; i<ja.size(); ++i) {
long result = 0;
JsonArray row = ja.getJsonArray(i);
for (int j=0; j<jb.size(); ++j) {
result += row.getInt(j)*jb.getInt(j);
}
jresult.add(result);
}
JsonObject value = factory.createObjectBuilder().add("result", jresult.build()).build();
String response = value.toString();
System.err.println(response);
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
public static Map<String, List<String>> splitQuery(String qp) throws UnsupportedEncodingException {
final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
final String[] pairs = qp.split("&");
for (String pair : pairs) {
final int idx = pair.indexOf("=");
final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
if (!query_pairs.containsKey(key)) {
query_pairs.put(key, new LinkedList<String>());
}
final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
query_pairs.get(key).add(value);
}
return query_pairs;
}
}