@Inject public LogHandler(Executor executor) { this(executor, new LogReader()); }
protected JSONObject readLogs(String logDirectory, long earliestLogThreshold, long latestLogThreshold) throws IOException, JSONException { this.earliestLogThreshold = earliestLogThreshold; this.latestLogThreshold = latestLogThreshold; JSONObject json = new JSONObject(); File root = new File(logDirectory); traverse_folder(root, json, ""); return json; }
@Override public HttpResponse handle(HttpRequest request) { JSONObject responseJSON = new JSONObject(); HashMap<String, String> apiParams = getParameters(request); long earliestLogThreshold = getEarliestThreshold(apiParams); long latestLogThreshold = getLatestThreshold(apiParams); try { JSONObject logJson = logReader.readLogs(LOG_DIRECTORY, earliestLogThreshold, latestLogThreshold); responseJSON.put("logs", logJson); } catch (IOException | JSONException e) { return new HttpResponse(404) { @Override public void render(OutputStream outputStream) {} }; } return new HttpResponse(200) { @Override public void render(OutputStream outputStream) throws IOException { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); outputStreamWriter.write(responseJSON.toString()); outputStreamWriter.close(); } }; }
private void traverse_folder(File root, JSONObject json, String filename) throws IOException, JSONException { File[] files = root.listFiles(); for(File child : files) { long logTime = Files.readAttributes(child.toPath(), BasicFileAttributes.class).creationTime().toMillis(); if(child.isFile() && earliestLogThreshold < logTime && logTime < latestLogThreshold) { json.put(filename + child.getName(), Base64.getEncoder().encodeToString(Files.readAllBytes(child.toPath()))); } else if (!child.isFile()){ traverse_folder(child, json, filename + child.getName() + "-"); } } }