-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackendReplicationRequestHandler.java
More file actions
54 lines (45 loc) · 1.68 KB
/
BackendReplicationRequestHandler.java
File metadata and controls
54 lines (45 loc) · 1.68 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
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Locale;
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.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import com.google.gson.Gson;
public class BackendReplicationRequestHandler implements HttpRequestHandler {
private static Gson g = new Gson();
public void handle(final HttpRequest request, final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String method = request.getRequestLine().getMethod()
.toUpperCase(Locale.ENGLISH);
System.out.println("Handling Data Query; Line = "
+ request.getRequestLine());
if (method.equals("GET")) {
final String Content = g.toJson(BackendDataStorage.getInstance().getReplication(),
JSONsnapshot.class);
System.out.println("Backend JSON: " + Content);
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 {
throw new MethodNotSupportedException(method
+ " method not supported\n");
}
}
}