Java Code Examples for org.sonatype.nexus.repository.Repository#optionalFacet()

The following examples show how to use org.sonatype.nexus.repository.Repository#optionalFacet() . 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: RepositoryBrowseResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset getAssetById(final Repository repository, final EntityId assetId) {
  Optional<GroupFacet> optionalGroupFacet = repository.optionalFacet(GroupFacet.class);
  List<Repository> members = optionalGroupFacet.isPresent() ? optionalGroupFacet.get().allMembers()
      : Collections.singletonList(repository);

  return Transactional.operation.withDb(repository.facet(StorageFacet.class).txSupplier()).call(() -> {
    StorageTx tx = UnitOfWork.currentTx();
    Asset candidate = tx.findAsset(assetId);
    if (candidate != null) {
      final String asssetBucketRepositoryName = bucketStore.getById(candidate.bucketId()).getRepositoryName();
      if (members.stream().anyMatch(repo -> repo.getName().equals(asssetBucketRepositoryName))) {
        return candidate;
      }
    }

    return null;
  });
}
 
Example 2
Source File: RepositorySearchContribution.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void contribute(final BoolQueryBuilder query, final String type, final String value) {
  if (value == null) {
    return;
  }

  Repository repository = repositoryManager.get(value);

  if (repository != null) {
    Optional<GroupFacet> groupFacet = repository.optionalFacet(GroupFacet.class);
    if (groupFacet.isPresent()) {
      List<Repository> members = groupFacet.get().leafMembers();

      query.must(QueryBuilders.termsQuery(type, members.stream().map(Repository::getName).toArray(String[]::new)));
      return;
    }
  }

  super.contribute(query, type, value);
}
 
Example 3
Source File: P2RestoreBlobStrategy.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canAttemptRestore(@Nonnull final P2RestoreBlobData p2RestoreBlobData) {
  Repository repository = getRepository(p2RestoreBlobData);
  Optional<P2RestoreFacet> p2RestoreFacetFacet = repository.optionalFacet(P2RestoreFacet.class);

  if (!p2RestoreFacetFacet.isPresent()) {
    log.warn("Skipping as P2 Restore Facet not found on repository: {}", repository.getName());
    return false;
  }
  return true;
}
 
Example 4
Source File: HelmRestoreBlobStrategy.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canAttemptRestore(@Nonnull final HelmRestoreBlobData helmRestoreBlobData) {
  Repository repository = getRepository(helmRestoreBlobData);
  Optional<HelmRestoreFacet> helmRestoreFacetFacet = repository.optionalFacet(HelmRestoreFacet.class);

  if (!helmRestoreFacetFacet.isPresent()) {
    log.warn("Skipping as Helm Restore Facet not found on repository: {}", repository.getName());
    return false;
  }
  return true;
}
 
Example 5
Source File: RRestoreBlobStrategy.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canAttemptRestore(@Nonnull final RRestoreBlobData rRestoreBlobData) {
  Repository repository = getRepository(rRestoreBlobData);
  Optional<RRestoreFacet> rRestoreFacetFacet = repository.optionalFacet(RRestoreFacet.class);

  if (!rRestoreFacetFacet.isPresent()) {
    log.warn("Skipping as R Restore Facet not found on repository: {}", repository.getName());
    return false;
  }
  return true;
}
 
Example 6
Source File: AptRestoreBlobStrategy.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canAttemptRestore(@Nonnull final AptRestoreBlobData data) {
  Repository repository = data.getBlobData().getRepository();
  Optional<AptRestoreFacet> aptRestoreFacetFacet = repository.optionalFacet(AptRestoreFacet.class);

  if (!aptRestoreFacetFacet.isPresent()) {
    log.warn("Skipping as APT Restore Facet not found on repository: {}", repository.getName());
    return false;
  }
  return true;
}
 
Example 7
Source File: NpmRestoreBlobStrategy.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canAttemptRestore(@Nonnull final NpmRestoreBlobData data) {
  Repository repository = data.getBlobData().getRepository();
  Optional<NpmFacet> npmFacet = repository.optionalFacet(NpmFacet.class);

  if (!npmFacet.isPresent()) {
    log.warn("Skipping as NPM Facet not found on repository: {}", repository.getName());
    return false;
  }

  return true;
}
 
Example 8
Source File: MavenRestoreBlobStrategy.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canAttemptRestore(@Nonnull final MavenRestoreBlobData data) {
  MavenPath mavenPath = data.getMavenPath();
  RestoreBlobData blobData = data.getBlobData();
  Repository repository = blobData.getRepository();

  if (mavenPath.getCoordinates() == null && !mavenPathParser.isRepositoryMetadata(mavenPath)) {
    if (log.isWarnEnabled()) {
      log.warn(
          "Skipping blob in repository named {}, because no maven coordinates found for blob named {} in blob store named {} and the blob not maven metadata",
          repository.getName(),
          blobData.getBlobName(),
          blobData.getBlobStoreName());
    }
    return false;
  }

  Optional<MavenFacet> mavenFacet = repository.optionalFacet(MavenFacet.class);

  if (!mavenFacet.isPresent()) {
    if (log.isWarnEnabled()) {
      log.warn("Skipping as Maven Facet not found on repository: {}", repository.getName());
    }
    return false;
  }

  return true;
}
 
Example 9
Source File: DatastoreBrowseNodeStoreImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Equivalence<BrowseNode<Integer>> getIdentity(final Repository repository) {
  Optional<BrowseNodeFacet> browseNodeFacet = repository.optionalFacet(BrowseNodeFacet.class);
  if (browseNodeFacet.isPresent()) {
    return Equivalence.equals().onResultOf(input -> browseNodeFacet.get().browseNodeIdentity().apply(input));
  }
  else {
    return Equivalence.equals().onResultOf(BrowseNode::getName);
  }
}
 
Example 10
Source File: RepositoryManagerImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void removeRepositoryFromAllGroups(final Repository repositoryToRemove) throws Exception {
  for (Repository group : repositories.values()) {
    Optional<GroupFacet> groupFacet = group.optionalFacet(GroupFacet.class);
    if (groupFacet.isPresent() && groupFacet.get().member(repositoryToRemove)) {
      removeRepositoryFromGroup(repositoryToRemove, group);
    }
  }
}
 
Example 11
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);
}
 
Example 12
Source File: OrientBrowseNodeStoreImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
@Guarded(by = STARTED)
public Iterable<BrowseNode<EntityId>> getByPath(
    final String repositoryName,
    final List<String> path,
    final int maxNodes)
{
  Repository repository = repositoryManager.get(repositoryName);
  String format = repository.getFormat().getValue();

  List<SelectorConfiguration> selectors = emptyList();
  if (!hasBrowsePermission(repositoryName, format)) {
    // user doesn't have repository-wide access so need to apply content selection
    selectors = selectorManager.browseActive(asList(repositoryName), asList(format));
    if (selectors.isEmpty()) {
      return emptyList(); // no browse permission and no selectors -> no results
    }
  }

  Map<String, Object> filterParameters = new HashMap<>();
  String assetFilter = buildAssetFilter(repository, selectors, filterParameters);

  BrowseNodeFilter filter = browseNodeFilters.getOrDefault(repository.getFormat().getValue(), (node, name) -> true);

  List<BrowseNode<EntityId>> results;
  if (repository.getType() instanceof GroupType) {
    Equivalence<OrientBrowseNode> browseNodeIdentity;
    Optional<BrowseNodeFacet> browseNodeFacet = repository.optionalFacet(BrowseNodeFacet.class);
    if (browseNodeFacet.isPresent()) {
      browseNodeIdentity = Equivalence.equals()
          .onResultOf(input -> browseNodeFacet.get().browseNodeIdentity().apply(input));
    }
    else {
      browseNodeIdentity = Equivalence.equals().onResultOf(BrowseNode::getName);
    }

    // overlay member results, first-one-wins if there are any nodes with the same name
    results = members(repository)
        .map(m -> getByPath(m.getName(), path, maxNodes, assetFilter, filterParameters))
        .flatMap(List::stream)
        .map(browseNodeIdentity::wrap)
        .distinct()
        .map(Wrapper::get)
        .filter(node -> filter.test(node, repositoryName.equals(node.getRepositoryName())))
        .limit(maxNodes)
        .collect(toList());
  }
  else {
    results = getByPath(repository.getName(), path, maxNodes, assetFilter, filterParameters).stream()
        .filter(node -> filter.test(node, repositoryName.equals(node.getRepositoryName())))
        .collect(toList());
  }

  results.sort(getBrowseNodeComparator(format));

  return results;
}