org.sonatype.nexus.repository.proxy.ProxyFacet Java Examples

The following examples show how to use org.sonatype.nexus.repository.proxy.ProxyFacet. 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: NpmSearchFacetProxy.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Content searchV1(final Parameters parameters) throws IOException {
  try {
    final Request getRequest = new Request.Builder()
        .action(GET)
        .path("/" + NpmFacetUtils.REPOSITORY_SEARCH_ASSET)
        .parameters(parameters)
        .build();
    Context context = new Context(getRepository(), getRequest);
    context.getAttributes().set(ProxyTarget.class, ProxyTarget.SEARCH_V1_RESULTS);
    Content searchResults = getRepository().facet(ProxyFacet.class).get(context);
    if (searchResults == null) {
      throw new IOException("Could not retrieve registry search");
    }
    return searchResults;
  }
  catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #2
Source File: NpmSearchIndexFacetProxy.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Content searchIndex(@Nullable final DateTime since) throws IOException {
  try {
    final Request getRequest = new Request.Builder()
        .action(GET)
        .path("/" + NpmFacetUtils.REPOSITORY_ROOT_ASSET)
        .build();
    Context context = new Context(getRepository(), getRequest);
    context.getAttributes().set(ProxyTarget.class, ProxyTarget.SEARCH_INDEX);
    Content fullIndex = getRepository().facet(ProxyFacet.class).get(context);
    if (fullIndex == null) {
      throw new IOException("Could not retrieve registry root");
    }
    return NpmSearchIndexFilter.filterModifiedSince(fullIndex, since);
  }
  catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #3
Source File: NpmAuditTarballFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Optional<String> getComponentHashsumForProxyRepo(final Repository repository, final Context context)
    throws TarballLoadingException
{
  try {
    UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
    Content content = repository.facet(ProxyFacet.class).get(context);
    if (content != null) {
      return getHashsum(content.getAttributes());
    }
  }
  catch (IOException e) {
    throw new TarballLoadingException(e.getMessage());
  }
  finally {
    UnitOfWork.end();
  }

  return Optional.empty();
}
 
Example #4
Source File: AuthorizingRepositoryManagerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void invalidateCacheProxyRepository() throws Exception {
  when(repository.getType()).thenReturn(new ProxyType());
  ProxyFacet proxyFacet = mock(ProxyFacet.class);
  when(repository.facet(ProxyFacet.class)).thenReturn(proxyFacet);
  NegativeCacheFacet negativeCacheFacet = mock(NegativeCacheFacet.class);
  when(repository.facet(NegativeCacheFacet.class)).thenReturn(negativeCacheFacet);

  authorizingRepositoryManager.invalidateCache("repository");

  verify(repositoryManager).get(eq("repository"));
  verify(repositoryPermissionChecker).ensureUserCanAdmin(eq(EDIT), eq(repository));
  verify(repository).facet(ProxyFacet.class);
  verify(proxyFacet).invalidateProxyCaches();
  verifyNoMoreInteractions(repositoryManager, repositoryPermissionChecker, proxyFacet);
}
 
Example #5
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 #6
Source File: MavenIndexPublisher.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Primes proxy cache with given path and return {@code true} if succeeds. Accepts only maven proxy type.
 */
private static boolean prefetch(final Repository repository, final String path) throws IOException {
  MavenPath mavenPath = repository.facet(MavenFacet.class).getMavenPathParser().parsePath(path);
  Request getRequest = new Request.Builder()
      .action(GET)
      .path(path)
      .build();
  Context context = new Context(repository, getRequest);
  context.getAttributes().set(MavenPath.class, mavenPath);
  return repository.facet(ProxyFacet.class).get(context) != null;
}
 
Example #7
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 #8
Source File: RepositoryCacheUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Invalidates the proxy and negative caches for given repository.
 */
public static void invalidateProxyAndNegativeCaches(final Repository repository) {
  checkNotNull(repository);
  checkArgument(ProxyType.NAME.equals(repository.getType().getValue()));
  ProxyFacet proxyFacet = repository.facet(ProxyFacet.class);
  proxyFacet.invalidateProxyCaches();
  NegativeCacheFacet negativeCacheFacet = repository.facet(NegativeCacheFacet.class);
  negativeCacheFacet.invalidate();
}
 
Example #9
Source File: RemoveSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void removeSnapshots(RemoveSnapshotsConfig config)
{
  Repository repository = getRepository();
  String repositoryName = repository.getName();
  log.info("Beginning snapshot removal on repository '{}' with configuration: {}", repositoryName, config);
  UnitOfWork.beginBatch(facet(StorageFacet.class).txSupplier());
  Set<GAV> metadataUpdateRequired = new HashSet<>();
  try {
    if (groupType.equals(repository.getType())) {
      processGroup(repository.facet(MavenGroupFacet.class), config);
    }
    else {
      metadataUpdateRequired.addAll(processRepository(repository, config));
    }
  }
  finally {
    UnitOfWork.end();
  }

  //only update metadata for non-proxy repos
  if (!repository.optionalFacet(ProxyFacet.class).isPresent()) {
    log.info("Updating metadata on repository '{}'", repositoryName);
    ProgressLogIntervalHelper intervalLogger = new ProgressLogIntervalHelper(log, 60);

    int processed = 0;
    for (GAV gav : metadataUpdateRequired) {
      Optional<MavenHostedFacet> mavenHostedFacet = repository.optionalFacet(MavenHostedFacet.class);
      if (mavenHostedFacet.isPresent()) {
        try {
          mavenHostedFacet.get().deleteMetadata(gav.group, gav.name, gav.baseVersion);
          intervalLogger
              .info("Elapsed time: {}, updated metadata for {} GAVs", intervalLogger.getElapsed(), ++processed);
        }
        catch (Exception e) {
          log.warn("Unable to delete/rebuild {} {} {}", gav.group, gav.name, gav.baseVersion,
              log.isDebugEnabled() ? e : null);
        }
      }
    }

    intervalLogger.flush();
  }
  else {
    log.info("Skipping metadata updates on proxy repository '{}'", repositoryName);
  }

  log.info("Completed snapshot removal on repository '{}'", repositoryName);
}