-
Notifications
You must be signed in to change notification settings - Fork 4
/
MyHandler.java
86 lines (70 loc) · 2.59 KB
/
MyHandler.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
import java.io.*;
import java.net.*;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import net.arnx.jsonic.JSON;
import net.arnx.jsonic.JSONException;
class MyHandler implements HttpHandler {
private EveryDetector evr;
public void setDetector(EveryDetector everyword) {
this.evr = everyword;
}
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("GET")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
String s = "{\"error\":\"true\",\"message\":\"Only post is accepted\"}";
responseBody.write(s.getBytes());
responseBody.close();
} else if (requestMethod.equalsIgnoreCase("POST")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
String s = "";
/*******************/
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = exchange.getRequestBody();
if (inputStream != null) {
s = evr.detectLanguage( new InputStreamReader(inputStream,"UTF-8") );
} else {
s = "";
}
} catch (IOException ex) {
s = "";
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
s = "";
}
}
}
/*******************/
if (!s.isEmpty()){
Map<String,String> mp=new HashMap<String, String>();
mp.put("error", "false");
mp.put("result", s);
responseBody.write(JSON.encode(mp).getBytes());
} else {
s = "{\"error\":\"true\",\"message\":\"Could not process\"}";
responseBody.write(s.getBytes());
}
responseBody.close();
}
}
}