LastDownloadedHandler extends ComponentSupport implements org.sonatype.nexus.repository.view.handlers.LastDownloadedHandler { @Override public Response handle(final Context context) throws Exception { Response response = context.proceed(); try { if (isSuccessfulRequestWithContent(context, response)) { Content content = (Content) response.getPayload(); maybeUpdateLastDownloaded(content.getAttributes()); } } catch (Exception e) { log.error("Failed to update last downloaded time for request {}", context.getRequest().getPath(), e); } return response; } @Inject  LastDownloadedHandler(final GlobalRepositorySettings globalSettings); @Override Response handle(final Context context);  }
@Test public void shouldMarkAssetAsDownloadedWhenSuccessfulGetRequest() throws Exception { Response handledResponse = underTest.handle(context); verify(asset).markAsDownloaded(); assertThat(handledResponse, is(equalTo(response))); }
@Test public void shouldMarkAssetAsDownloadedWhenSuccessfulHeadRequest() throws Exception { when(request.getAction()).thenReturn(HEAD); Response handledResponse = underTest.handle(context); verify(asset).markAsDownloaded(); assertThat(handledResponse, is(equalTo(response))); }
@Test public void shouldMarkAssetAsDownloadedWhenNotModified() throws Exception { when(response.getStatus()).thenReturn(new Status(false, 304)); Response handledResponse = underTest.handle(context); verify(asset).markAsDownloaded(); assertThat(handledResponse, is(equalTo(response))); }
@Test public void shouldUpdateIfNotRecentlyChanged() throws Exception { when(asset.lastDownloaded()).thenReturn(of(UTC.now().minusDays(2))); when(globalSettings.getLastDownloadedInterval()).thenReturn(ofDays(1)); Response handledResponse = underTest.handle(context); verify(asset).markAsDownloaded(); assertThat(handledResponse, is(equalTo(response))); }