package sh.adb.RandomRedditMemesAPI; import com.github.jreddit.oauth.exception.RedditOAuthException; import com.github.jreddit.parser.entity.Submission; import com.github.jreddit.parser.exception.RedditParseException; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import org.json.JSONArray; import org.json.JSONObject; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Random; class CommunityHandler implements HttpHandler { RedditAPI api; public CommunityHandler(RedditAPI redditClient) { this.api = redditClient; } private JSONObject subToJSON(Submission sub){ JSONObject subJSON = new JSONObject(); subJSON.put("title", sub.getTitle()); subJSON.put("text", sub.getSelftext()); subJSON.put("author", sub.getAuthor()); subJSON.put("subreddit", sub.getSubreddit()); subJSON.put("time", sub.getCreatedUTC()); subJSON.put("score", sub.getScore()); subJSON.put("downvotes", sub.getDownVotes()); subJSON.put("nsfw", sub.isNSFW()); subJSON.put("url", sub.getURL()); 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 submissions = null; try { submissions = this.api.getSub(pathArray[4]); } catch (RedditParseException | RedditOAuthException e) { responseJSON.put("error", "reddit api error"); sendResponse(t, responseJSON.toString()); e.printStackTrace(); return; } assert submissions != null; if ( pathArray.length < 6 || pathArray[5].equals("") || pathArray[5].equals("random")) { Random rand = new Random(); subsJSON.put(subToJSON( submissions.get(rand.nextInt(submissions.size())) )); responseJSON.put("error", false); responseJSON.put("type", "random"); } else if (pathArray[5].equals("all")) { submissions.forEach(sub -> { subsJSON.put(subToJSON(sub)); }); responseJSON.put("error", false); responseJSON.put("type", "all"); } else { try{ int subNum = Integer.parseInt(pathArray[5]); if (subNum >= submissions.size()) responseJSON.put("error", "invalid input"); else{ subsJSON.put(subToJSON(submissions.get(subNum))); responseJSON.put("error", false); responseJSON.put("type", "specific"); } }catch (NumberFormatException ignored){ responseJSON.put("error", "invalid input"); } } responseJSON.put("subs", subsJSON); sendResponse(t, responseJSON.toString()); } }