-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrontendSearchHandler.java
More file actions
105 lines (93 loc) · 3.37 KB
/
FrontendSearchHandler.java
File metadata and controls
105 lines (93 loc) · 3.37 KB
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
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
public class FrontendSearchHandler implements HttpRequestHandler {
private static Gson g = new Gson();
private static Logger log = Logger.getLogger(FrontendSearchHandler.class);
public void handle(final HttpRequest request, final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String method = request.getRequestLine().getMethod()
.toUpperCase(Locale.ENGLISH);
log.info("Frontend: Handling Search; Line = "
+ request.getRequestLine());
if (method.equals("GET")) {
final String target = request.getRequestLine().getUri();
Pattern p = Pattern.compile("/search\\?q=(.*)$");
Matcher m = p.matcher(target);
if (m.find()) {
String hash = m.group(1);
FrontendDataCache dc = FrontendDataCache.getInstance();
VectorClock currentVersion = dc.getVersion(hash);
HttpClient httpclient = new DefaultHttpClient();
log.info("Connecting to backend to search for #" + hash);
HttpGet httpget = new HttpGet("http://"
+ ServerStateManager.getMyBackendIp()
+ ":"
+ ServerStateManager.getMyBackendPort()
+ "/data/query?q="
+ hash
+ "&v="
+ URLEncoder.encode(
g.toJson(currentVersion, VectorClock.class),
"UTF-8"));
HttpResponse r = null;
try {
r = httpclient.execute(httpget);
} catch (Exception e) {
ServerStateManager.removeDeadBackend();
ServerStateManager.updateSnapshot(ServerStateManager.getMyBackendIp(), ServerStateManager.getMyBackendPort());
handle(request, response, context);
return;
}
HttpEntity entity = r.getEntity();
String entityContent = null;
try {
entityContent = EntityUtils.toString(entity);
} catch (ParseException e) {
} catch (IOException e) {
}
log.info("Backend returns: " + entityContent);
final String Content = dc.getTweets(hash, entityContent);
EntityTemplate body = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream)
throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(
outstream, "UTF-8");
writer.write(Content);
writer.write("\n");
writer.flush();
}
});
body.setContentType("application/json; charset=UTF-8");
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(body);
} else {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
}
} else {
throw new MethodNotSupportedException(method
+ " method not supported\n");
}
}
}