org.sonatype.nexus.repository.cache.CacheController Java Examples

The following examples show how to use org.sonatype.nexus.repository.cache.CacheController. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyFacetSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void doConfigure(final Configuration configuration) throws Exception {
  config = facet(ConfigurationFacet.class).readSection(configuration, CONFIG_KEY, Config.class);

  cacheControllerHolder = new CacheControllerHolder(
      new CacheController(Time.minutes(config.contentMaxAge).toSecondsI(), null),
      new CacheController(Time.minutes(config.metadataMaxAge).toSecondsI(), null)
  );

  // normalize URL path to contain trailing slash
  if (!config.remoteUrl.getPath().endsWith("/")) {
    config.remoteUrl = config.remoteUrl.resolve(config.remoteUrl.getPath() + "/");
  }

  log.debug("Config: {}", config);
}
 
Example #2
Source File: AptProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected CacheController getCacheController(final Context context) {
  if (isDebPackageContentType(assetPath(context))) {
    return cacheControllerHolder.getContentCacheController();
  }
  return cacheControllerHolder.getMetadataCacheController();
}
 
Example #3
Source File: GroupFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void whenCachePresentTheNotStale() {
  when(content.getAttributes()).thenReturn(attributesMap);
  when(attributesMap.get(CacheInfo.class)).thenReturn(cacheInfo);
  CacheController cacheController = mock(CacheController.class);
  underTest.cacheController = cacheController;
  when(cacheController.isStale(cacheInfo)).thenReturn(false);

  assertThat(underTest.isStale(content), is(false));
}
 
Example #4
Source File: GroupFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void doConfigure(final Configuration configuration) throws Exception {
  config = facet(ConfigurationFacet.class).readSection(configuration, CONFIG_KEY, Config.class);

  cacheController = new CacheController(-1, null);

  log.debug("Config: {}", config);
}
 
Example #5
Source File: AptProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<SnapshotItem> fetchLatest(final ContentSpecifier spec) throws IOException {
  AptFacet aptFacet = getRepository().facet(AptFacet.class);
  ProxyFacet proxyFacet = facet(ProxyFacet.class);
  HttpClientFacet httpClientFacet = facet(HttpClientFacet.class);
  HttpClient httpClient = httpClientFacet.getHttpClient();
  CacheController cacheController = cacheControllerHolder.getMetadataCacheController();
  CacheInfo cacheInfo = cacheController.current();
  Content oldVersion = aptFacet.get(spec.path);

  URI fetchUri = proxyFacet.getRemoteUrl().resolve(spec.path);
  HttpGet getRequest = buildFetchRequest(oldVersion, fetchUri);

  HttpResponse response = httpClient.execute(getRequest);
  StatusLine status = response.getStatusLine();

  if (status.getStatusCode() == HttpStatus.SC_OK) {
    HttpEntity entity = response.getEntity();
    Content fetchedContent = new Content(new HttpEntityPayload(response, entity));
    AttributesMap contentAttrs = fetchedContent.getAttributes();
    contentAttrs.set(Content.CONTENT_LAST_MODIFIED, getDateHeader(response, HttpHeaders.LAST_MODIFIED));
    contentAttrs.set(Content.CONTENT_ETAG, getQuotedStringHeader(response, HttpHeaders.ETAG));
    contentAttrs.set(CacheInfo.class, cacheInfo);
    Content storedContent = getAptFacet().put(spec.path, fetchedContent);
    return Optional.of(new SnapshotItem(spec, storedContent));
  }

  try {
    if (status.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
      checkState(oldVersion != null, "Received 304 without conditional GET (bad server?) from %s", fetchUri);
      doIndicateVerified(oldVersion, cacheInfo, spec.path);
      return Optional.of(new SnapshotItem(spec, oldVersion));
    }
    throwProxyExceptionForStatus(response);
  }
  finally {
    HttpClientUtils.closeQuietly(response);
  }

  return Optional.empty();
}
 
Example #6
Source File: MavenProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected CacheController getCacheController(@Nonnull final Context context) {
  if (mavenFacet.getMavenPathParser().isRepositoryMetadata(mavenPath(context))) {
    return cacheControllerHolder.getMetadataCacheController();
  }
  else {
    return cacheControllerHolder.getContentCacheController();
  }
}
 
Example #7
Source File: AptProxyFacet.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<SnapshotItem> fetchLatest(ContentSpecifier spec) throws IOException {
  AptFacet aptFacet = getRepository().facet(AptFacet.class);
  ProxyFacet proxyFacet = facet(ProxyFacet.class);
  HttpClientFacet httpClientFacet = facet(HttpClientFacet.class);
  HttpClient httpClient = httpClientFacet.getHttpClient();
  CacheController cacheController = cacheControllerHolder.getMetadataCacheController();
  CacheInfo cacheInfo = cacheController.current();
  Content oldVersion = aptFacet.get(spec.path);

  URI fetchUri = proxyFacet.getRemoteUrl().resolve(spec.path);
  HttpGet getRequest = buildFetchRequest(oldVersion, fetchUri);

  HttpResponse response = httpClient.execute(getRequest);
  StatusLine status = response.getStatusLine();

  if (status.getStatusCode() == HttpStatus.SC_OK) {
    HttpEntity entity = response.getEntity();
    Content fetchedContent = new Content(new HttpEntityPayload(response, entity));
    AttributesMap contentAttrs = fetchedContent.getAttributes();
    contentAttrs.set(Content.CONTENT_LAST_MODIFIED, getDateHeader(response, HttpHeaders.LAST_MODIFIED));
    contentAttrs.set(Content.CONTENT_ETAG, getQuotedStringHeader(response, HttpHeaders.ETAG));
    contentAttrs.set(CacheInfo.class, cacheInfo);
    Content storedContent = getAptFacet().put(spec.path, fetchedContent);
    return Optional.of(new SnapshotItem(spec, storedContent));
  }

  try {
    if (status.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
      checkState(oldVersion != null, "Received 304 without conditional GET (bad server?) from %s", fetchUri);
      doIndicateVerified(oldVersion, cacheInfo, spec.path);
      return Optional.of(new SnapshotItem(spec, oldVersion));
    }
    throwProxyExceptionForStatus(response);
  }
  finally {
    HttpClientUtils.closeQuietly(response);
  }

  return Optional.empty();
}
 
Example #8
Source File: AptProxyFacet.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected CacheController getCacheController(Context context) {
  if (assetPath(context).endsWith(".deb") || assetPath(context).endsWith(".udeb")) {
    return cacheControllerHolder.getContentCacheController();
  }
  return cacheControllerHolder.getMetadataCacheController();
}
 
Example #9
Source File: OrientPyPiProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Nonnull
@Override
protected CacheController getCacheController(@Nonnull final Context context) {
  AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  return cacheControllerHolder.require(assetKind.getCacheType());
}
 
Example #10
Source File: OrientNpmProxyFacet.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
@Nonnull
protected CacheController getCacheController(@Nonnull final Context context) {
  final ProxyTarget proxyTarget = context.getAttributes().require(ProxyTarget.class);
  return cacheControllerHolder.require(proxyTarget.getCacheType());
}
 
Example #11
Source File: ComposerProxyFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 4 votes vote down vote up
@Nonnull
@Override
protected CacheController getCacheController(@Nonnull final Context context) {
  final AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  return cacheControllerHolder.require(assetKind.getCacheType());
}
 
Example #12
Source File: CocoapodsProxyFacet.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
@Nonnull
protected CacheController getCacheController(final Context context) {
  final AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  return checkNotNull(cacheControllerHolder.get(assetKind.getCacheType()));
}
 
Example #13
Source File: FluentAssetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean isStale(final CacheController cacheController) {
  CacheInfo cacheInfo = CacheInfo.fromMap(attributes(CACHE));
  return cacheInfo != null && cacheController.isStale(cacheInfo);
}
 
Example #14
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 4 votes vote down vote up
@Nonnull
@Override
protected CacheController getCacheController(@Nonnull final Context context) {
  final AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  return checkNotNull(cacheControllerHolder.get(assetKind.getCacheType()));
}
 
Example #15
Source File: ProxyFacetSupport.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Get the appropriate cache controller for the type of content being requested. Must never return {@code null}.
 */
@Nonnull
protected CacheController getCacheController(@Nonnull final Context context) {
  return cacheControllerHolder.getContentCacheController();
}
 
Example #16
Source File: RProxyFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected CacheController getCacheController(final Context context) {
  final AssetKind assetKind = context.getAttributes().require(AssetKind.class);
  return checkNotNull(cacheControllerHolder.get(assetKind.getCacheType()));
}
 
Example #17
Source File: FluentAsset.java    From nexus-public with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Is this generated/proxied asset stale?
 *
 * @since 3.24
 */
boolean isStale(CacheController cacheController);