|
|
|
@ -33,12 +33,32 @@ class CommunityHandler implements HttpHandler {
|
|
|
|
|
subJSON.put("permalink", sub.getPermalink());
|
|
|
|
|
return subJSON;
|
|
|
|
|
}
|
|
|
|
|
private void sendResponse(HttpExchange t, String response) throws IOException {
|
|
|
|
|
System.out.println("response > "+response);
|
|
|
|
|
Headers headers = t.getResponseHeaders();
|
|
|
|
|
headers.set("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
|
System.out.println(response.length());
|
|
|
|
|
OutputStream os = t.getResponseBody();
|
|
|
|
|
headers.add("Connection", "close");
|
|
|
|
|
t.sendResponseHeaders(200, response.length());
|
|
|
|
|
os.write(response.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
os.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void handle(HttpExchange t) throws IOException {
|
|
|
|
|
System.out.println("request at > "+t.getRequestURI().toString());
|
|
|
|
|
String[] pathArray = t.getRequestURI().toString().split("/", -1);
|
|
|
|
|
|
|
|
|
|
JSONObject responseJSON = new JSONObject();
|
|
|
|
|
JSONArray subsJSON = new JSONArray();
|
|
|
|
|
|
|
|
|
|
if (pathArray.length < 5 || pathArray[4].equals("")){
|
|
|
|
|
responseJSON.put("error", "no subreddit specified");
|
|
|
|
|
sendResponse(t, responseJSON.toString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Submission> submissions = null;
|
|
|
|
|
try {
|
|
|
|
|
submissions = this.api.getSub(pathArray[4]);
|
|
|
|
@ -47,8 +67,6 @@ class CommunityHandler implements HttpHandler {
|
|
|
|
|
}
|
|
|
|
|
assert submissions != null;
|
|
|
|
|
|
|
|
|
|
JSONObject responseJSON = new JSONObject();
|
|
|
|
|
JSONArray subsJSON = new JSONArray();
|
|
|
|
|
if ( pathArray.length < 6 || pathArray[5].equals("") || pathArray[5].equals("random")) {
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
subsJSON.put(subToJSON(
|
|
|
|
@ -76,16 +94,6 @@ class CommunityHandler implements HttpHandler {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
responseJSON.put("subs", subsJSON);
|
|
|
|
|
|
|
|
|
|
String response = responseJSON.toString();
|
|
|
|
|
System.out.println("response > "+response);
|
|
|
|
|
Headers headers = t.getResponseHeaders();
|
|
|
|
|
headers.set("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
|
System.out.println(response.length());
|
|
|
|
|
OutputStream os = t.getResponseBody();
|
|
|
|
|
headers.add("Connection", "close");
|
|
|
|
|
t.sendResponseHeaders(200, response.length());
|
|
|
|
|
os.write(response.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
os.close();
|
|
|
|
|
sendResponse(t, responseJSON.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|