@Test void testWithToken() throws Exception { final MockTokenlessClientLogger logger = new MockTokenlessClientLogger(); final ServiceRequestContext ctx = mock(ServiceRequestContext.class); final HttpRequest req = newRequestWithToken(); logger.serve(ctx, req); assertThat(logger.hostname).isNull(); assertThat(logger.ip).isNull(); verify(delegate, times(1)).serve(ctx, req); verify(clock, never()).instant(); }
@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);  }