LogHandler extends ThreadedHttpRequestHandler { @Override public AsyncHttpResponse handle(HttpRequest request) { Instant from = Optional.ofNullable(request.getProperty("from")) .map(Long::valueOf).map(Instant::ofEpochMilli).orElse(Instant.MIN); Instant to = Optional.ofNullable(request.getProperty("to")) .map(Long::valueOf).map(Instant::ofEpochMilli).orElse(Instant.MAX); Optional<String> hostname = Optional.ofNullable(request.getProperty("hostname")); return new AsyncHttpResponse(200) { @Override public void render(OutputStream output, ContentChannel networkChannel, CompletionHandler handler) { try { OutputStream blockingOutput = new BlockingFlushContentChannelOutputStream(networkChannel); logReader.writeLogs(blockingOutput, from, to, hostname); blockingOutput.close(); } catch (Throwable t) { log.log(Level.WARNING, "Failed reading logs from " + from + " to " + to, t); } finally { networkChannel.close(handler); } } }; } @Inject  LogHandler(Executor executor, LogHandlerConfig config);   LogHandler(Executor executor, LogReader logReader); @Override AsyncHttpResponse handle(HttpRequest request);  }
@Test public void handleCorrectlyParsesQueryParameters() throws IOException { MockLogReader mockLogReader = new MockLogReader(); LogHandler logHandler = new LogHandler(mock(Executor.class), mockLogReader); { String uri = "http: AsyncHttpResponse response = logHandler.handle(HttpRequest.createTestRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.GET)); ReadableContentChannel out = new ReadableContentChannel(); new Thread(() -> Exceptions.uncheck(() -> response.render(null, out, null))).start(); String expectedResponse = "newer log"; assertEquals(expectedResponse, new String(out.toStream().readAllBytes(), UTF_8)); } { String uri = "http: AsyncHttpResponse response = logHandler.handle(HttpRequest.createTestRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.GET)); ReadableContentChannel out = new ReadableContentChannel(); new Thread(() -> Exceptions.uncheck(() -> response.render(null, out, null))).start(); String expectedResponse = "older log"; assertEquals(expectedResponse, new String(out.toStream().readAllBytes(), UTF_8)); } }