@Test void testWithoutToken() throws Exception { final MockTokenlessClientLogger logger = new MockTokenlessClientLogger(); final Instant startTime = Instant.now(); when(clock.instant()).thenReturn(startTime); final ServiceRequestContext ctx = newContext("foo", "192.168.0.1"); final HttpRequest req = newRequestWithoutToken(); logger.serve(ctx, req); assertThat(logger.hostname).isEqualTo("foo"); assertThat(logger.ip).isEqualTo("192.168.0.1"); verify(delegate, times(1)).serve(ctx, req); final ServiceRequestContext ctx2 = newContext("bar", "192.168.0.1"); final HttpRequest req2 = newRequestWithoutToken(); when(clock.instant()).thenReturn(startTime.plus(30, ChronoUnit.MINUTES)); logger.serve(ctx2, req2); assertThat(logger.hostname).isNull(); assertThat(logger.ip).isNull(); verify(delegate, times(1)).serve(ctx2, req2); final ServiceRequestContext ctx3 = newContext("baz", "192.168.0.1"); final HttpRequest req3 = newRequestWithoutToken(); when(clock.instant()).thenReturn(startTime.plus(1, ChronoUnit.DAYS)); logger.serve(ctx3, req3); assertThat(logger.hostname).isEqualTo("baz"); assertThat(logger.ip).isEqualTo("192.168.0.1"); verify(delegate, times(1)).serve(ctx3, req3); final ServiceRequestContext ctx4 = newContext("qux", "192.168.0.2"); final HttpRequest req4 = newRequestWithoutToken(); logger.serve(ctx4, req4); assertThat(logger.hostname).isEqualTo("qux"); assertThat(logger.ip).isEqualTo("192.168.0.2"); verify(delegate, times(1)).serve(ctx4, req4); }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final String authorization = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (authorization == null || !PATTERN.matcher(authorization).matches()) { final InetSocketAddress raddr = ctx.remoteAddress(); final String ip = raddr.getAddress().getHostAddress(); final Instant now = Instant.now(clock); final Instant lastReport = reportedAddresses.putIfAbsent(ip, now); final boolean report; if (lastReport == null) { report = true; } else if (ChronoUnit.DAYS.between(lastReport, now) >= 1) { report = reportedAddresses.replace(ip, lastReport, now); } else { report = false; } if (report) { report(raddr.getHostString(), ip); } } return unwrap().serve(ctx, req); }
TokenlessClientLogger extends SimpleDecoratingHttpService { @Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final String authorization = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (authorization == null || !PATTERN.matcher(authorization).matches()) { final InetSocketAddress raddr = ctx.remoteAddress(); final String ip = raddr.getAddress().getHostAddress(); final Instant now = Instant.now(clock); final Instant lastReport = reportedAddresses.putIfAbsent(ip, now); final boolean report; if (lastReport == null) { report = true; } else if (ChronoUnit.DAYS.between(lastReport, now) >= 1) { report = reportedAddresses.replace(ip, lastReport, now); } else { report = false; } if (report) { report(raddr.getHostString(), ip); } } return unwrap().serve(ctx, req); } }
TokenlessClientLogger extends SimpleDecoratingHttpService { @Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final String authorization = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (authorization == null || !PATTERN.matcher(authorization).matches()) { final InetSocketAddress raddr = ctx.remoteAddress(); final String ip = raddr.getAddress().getHostAddress(); final Instant now = Instant.now(clock); final Instant lastReport = reportedAddresses.putIfAbsent(ip, now); final boolean report; if (lastReport == null) { report = true; } else if (ChronoUnit.DAYS.between(lastReport, now) >= 1) { report = reportedAddresses.replace(ip, lastReport, now); } else { report = false; } if (report) { report(raddr.getHostString(), ip); } } return unwrap().serve(ctx, req); } TokenlessClientLogger(HttpService delegate); @VisibleForTesting  TokenlessClientLogger(HttpService delegate, Clock clock); }
TokenlessClientLogger extends SimpleDecoratingHttpService { @Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final String authorization = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (authorization == null || !PATTERN.matcher(authorization).matches()) { final InetSocketAddress raddr = ctx.remoteAddress(); final String ip = raddr.getAddress().getHostAddress(); final Instant now = Instant.now(clock); final Instant lastReport = reportedAddresses.putIfAbsent(ip, now); final boolean report; if (lastReport == null) { report = true; } else if (ChronoUnit.DAYS.between(lastReport, now) >= 1) { report = reportedAddresses.replace(ip, lastReport, now); } else { report = false; } if (report) { report(raddr.getHostString(), ip); } } return unwrap().serve(ctx, req); } TokenlessClientLogger(HttpService delegate); @VisibleForTesting  TokenlessClientLogger(HttpService delegate, Clock clock); @Override HttpResponse serve(ServiceRequestContext ctx, HttpRequest req); }
TokenlessClientLogger extends SimpleDecoratingHttpService { @Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final String authorization = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (authorization == null || !PATTERN.matcher(authorization).matches()) { final InetSocketAddress raddr = ctx.remoteAddress(); final String ip = raddr.getAddress().getHostAddress(); final Instant now = Instant.now(clock); final Instant lastReport = reportedAddresses.putIfAbsent(ip, now); final boolean report; if (lastReport == null) { report = true; } else if (ChronoUnit.DAYS.between(lastReport, now) >= 1) { report = reportedAddresses.replace(ip, lastReport, now); } else { report = false; } if (report) { report(raddr.getHostString(), ip); } } return unwrap().serve(ctx, req); } TokenlessClientLogger(HttpService delegate); @VisibleForTesting  TokenlessClientLogger(HttpService delegate, Clock clock); @Override HttpResponse serve(ServiceRequestContext ctx, HttpRequest req);  }