org.sonatype.nexus.repository.Repository Java Examples

The following examples show how to use org.sonatype.nexus.repository.Repository. 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: OrientMavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void verifyHashesExistAndCorrect(final Repository repository, final String path) throws Exception {
  final MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final MavenPath mavenPath = mavenFacet.getMavenPathParser().parsePath(path);
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    final Content content = mavenFacet.get(mavenPath);
    assertThat(content, notNullValue());
    final Map<HashAlgorithm, HashCode> hashCodes =
        content.getAttributes().require(Content.CONTENT_HASH_CODES_MAP, Content.T_CONTENT_HASH_CODES_MAP);
    for (HashType hashType : HashType.values()) {
      final Content contentHash = mavenFacet.get(mavenPath.hash(hashType));
      final String storageHash = hashCodes.get(hashType.getHashAlgorithm()).toString();
      assertThat(storageHash, notNullValue());
      try (InputStream is = contentHash.openInputStream()) {
        final String mavenHash = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
        assertThat(storageHash, equalTo(mavenHash));
      }
    }
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #2
Source File: RepairMetadataComponentTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void whenTypeDoesNotMatchShouldNotRepair() {
  underTest = new RepairMetadataComponentForTest(repositoryManager, assetEntityAdapter, type, format)
  {
    @Override
    protected void doRepairRepository(final Repository repository) {
      throw new UnsupportedOperationException();
    }
  };

  when(repository.getType()).thenReturn(new ProxyType());

  try {
    underTest.repairRepository(repository);
  }
  catch (Exception e) {
    fail("repair should not have been attempted");
  }
}
 
Example #3
Source File: AptComponentDirector.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void afterMove(final List<Map<String, String>> components, final Repository destination) {
  destination.facet(StorageFacet.class).txSupplier();
  UnitOfWork.begin(destination.facet(StorageFacet.class).txSupplier());
  try {
    AptHostedFacet hostedFacet = destination.facet(AptHostedFacet.class);
    hostedFacet.rebuildIndexes();
  }
  catch (IOException e) {
    log.error("Failed to update metadata, repository: {}", destination.getName(), e);
    throw new UncheckedIOException(e);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #4
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  State state = context.getAttributes().require(TokenMatcher.State.class);
  Repository repository = context.getRepository();
  log.debug("[getPackage] repository: {} tokens: {}", repository.getName(), state.getTokens());

  NpmPackageId packageId = packageId(state);
  Content content = repository.facet(NpmHostedFacet.class)
      .getDistTags(packageId);
  if (content != null) {
    return NpmResponses.ok(content);
  }
  else {
    return NpmResponses.packageNotFound(packageId);
  }
}
 
Example #5
Source File: CondaFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@Nullable
public Component findComponent(final StorageTx tx,
                               final Repository repository,
                               final String arch,
                               final String name,
                               final String version)
{
  Iterable<Component> components = tx.findComponents(
      Query.builder()
          .where(P_NAME).eq(name)
          .and(P_GROUP).eq(arch)
          .and(P_VERSION).eq(version)
          .build(),
      singletonList(repository)
  );
  if (components.iterator().hasNext()) {
    return components.iterator().next();
  }
  return null;
}
 
Example #6
Source File: RepositoryManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Construct a new repository from configuration.
 */
private Repository newRepository(final Configuration configuration) throws Exception {
  String recipeName = configuration.getRecipeName();
  Recipe recipe = recipe(recipeName);
  log.debug("Using recipe: [{}] {}", recipeName, recipe);

  Repository repository = factory.create(recipe.getType(), recipe.getFormat());

  // attach mandatory facets
  repository.attach(configFacet.get());

  // apply recipe to repository
  recipe.apply(repository);

  // verify required facets
  repository.facet(ViewFacet.class);

  // ensure configuration sanity, once all facets are attached
  repository.validate(configuration);

  // initialize repository
  repository.init(configuration);

  return repository;
}
 
Example #7
Source File: OrientMavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void writeWithoutValidation(
    final Repository repository,
    final String path,
    final Payload payload) throws IOException
{
  final MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final StorageFacet storageFacet = repository.facet(StorageFacet.class);

  final MavenPath mavenPath = mavenFacet.getMavenPathParser().parsePath(path);
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    try (TempBlob tempBlob = storageFacet.createTempBlob(payload, HashType.ALGORITHMS)) {

      mavenFacet.put(mavenPath, tempBlob, MavenMimeRulesSource.METADATA_TYPE, new AttributesMap());
    }
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #8
Source File: NpmRequestParser.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses an incoming "npm publish" or "npm unpublish" request, returning the results. Note that you should probably
 * call this from within a try-with-resources block to manage the lifecycle of any temp blobs created during the
 * operation.
 */
public NpmPublishRequest parsePublish(final Repository repository, final Payload payload) throws IOException {
  checkNotNull(repository);
  checkNotNull(payload);
  StorageFacet storageFacet = repository.facet(StorageFacet.class);
  try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) {
    try {
      return parseNpmPublish(storageFacet, tempBlob, UTF_8);
    }
    catch (JsonParseException e) {
      // fallback
      if (e.getMessage().contains("Invalid UTF-8")) {
        // try again, but assume ISO8859-1 encoding now, that is illegal for JSON
        return parseNpmPublish(storageFacet, tempBlob, ISO_8859_1);
      }
      throw new InvalidContentException("Invalid JSON input", e);
    }
  }
}
 
Example #9
Source File: OrientComponentAssetTestHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean assetWithComponentExists(
    final Repository repository,
    final String path,
    final String group,
    final String name)
{
  return findAssetByName(repository, path)
      .map(Asset::componentId)
      .flatMap(componentId -> {
        try (StorageTx tx = repository.facet(StorageFacet.class).txSupplier().get()) {
          tx.begin();
          return Optional.ofNullable(tx.findComponent(componentId));
        }
      })
      .filter(component -> Objects.equals(component.group(), group))
      .filter(component -> Objects.equals(component.name(), name))
      .isPresent();
}
 
Example #10
Source File: UploadComponentMultipartHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parse a multipart-form submission creating file uploads in the blob store of the repository. Reminder, callers must
 * call {@code close} on {@code TempBlobs} returned from this method.
 */
public BlobStoreMultipartForm parse(final Repository repository, final HttpServletRequest request)
    throws FileUploadException
{
  BlobStoreMultipartForm multipartForm = new BlobStoreMultipartForm();
  TempBlobServletFileUpload upload = new TempBlobServletFileUpload(repository, multipartForm);

  upload.parseRequest(request);

  // ExtJs results in fields with the upload name for some reason
  multipartForm.getFiles().keySet().forEach(assetName -> multipartForm.getFormFields().remove(assetName));

  String token = multipartForm.getFormFields().remove(AntiCsrfHelper.ANTI_CSRF_TOKEN_NAME);
  antiCsrfHelper.requireValidToken(request, token);

  return multipartForm;
}
 
Example #11
Source File: MavenIndexPublisher.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Publishes MI index into {@code target}, sourced from repository's own CMA structures.
 */
public static void publishHostedIndex(final Repository repository,
                                      final DuplicateDetectionStrategy<Record> duplicateDetectionStrategy)
    throws IOException
{
  checkNotNull(repository);
  Transactional.operation.throwing(IOException.class).call(
      () -> {
        final StorageTx tx = UnitOfWork.currentTx();
        try (Maven2WritableResourceHandler resourceHandler = new Maven2WritableResourceHandler(repository)) {
          try (IndexWriter indexWriter = new IndexWriter(resourceHandler, repository.getName(), false)) {
            indexWriter.writeChunk(
                transform(
                    decorate(
                        filter(getHostedRecords(tx, repository), duplicateDetectionStrategy),
                        repository.getName()
                    ),
                    RECORD_COMPACTOR::apply
                ).iterator()
            );
          }
        }
        return null;
      }
  );
}
 
Example #12
Source File: RepositoryPermissionChecker.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private List<Repository> subjectHasAnyContentSelectorAccessTo(final Subject subject,
                                                              final List<Repository> repositories)
{
  List<String> repositoryNames = repositories.stream().map(r -> r.getName()).collect(Collectors.toList());
  List<String> formats = repositories.stream().map(r -> r.getFormat().getValue()).distinct()
      .collect(Collectors.toList());
  List<SelectorConfiguration> selectors = selectorManager.browseActive(repositoryNames, formats);

  if (selectors.isEmpty()) {
    return Collections.emptyList();
  }

  List<Repository> permittedRepositories = new ArrayList<>();
  for (Repository repository : repositories) {
    Permission[] permissions = selectors.stream()
        .map(s -> new RepositoryContentSelectorPermission(s, repository, singletonList(BROWSE)))
        .toArray(Permission[]::new);
    if (securityHelper.anyPermitted(subject, permissions)) {
      permittedRepositories.add(repository);
    }
  }

  return permittedRepositories;
}
 
Example #13
Source File: HelmResourceIT.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void updateProxy() throws Exception {
  repos.createHelmProxy(PROXY_NAME, "http://example.com");

  AbstractRepositoryApiRequest request = createProxyRequest(false);

  Response response = put(getUpdateRepositoryPathUrl(ProxyType.NAME, PROXY_NAME), request);
  assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());

  Repository repository = repositoryManager.get(request.getName());
  assertNotNull(repository);

  assertThat(repository.getConfiguration().attributes("storage")
          .get("strictContentTypeValidation"),
      is(false));
  repositoryManager.delete(PROXY_NAME);
}
 
Example #14
Source File: RemoveSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find all GAVs that qualify for deletion.
 */
@VisibleForTesting
Set<GAV> findSnapshotCandidates(final StorageTx tx, final Repository repository)
{
  log.info(PROGRESS, "Searching for GAVS with snapshots that qualify for deletion on repository '{}'",
      repository.getName());

  final Bucket bucket = tx.findBucket(repository);
  final OResultSet<ODocument> result = tx.getDb().command(new OSQLSynchQuery<>(GAVS_WITH_SNAPSHOTS))
      .execute(AttachedEntityHelper.id(bucket));
  return result.stream().map((doc) -> {
    String group = doc.field(P_GROUP, String.class);
    String name = doc.field(P_NAME, String.class);
    String baseVersion = doc.field("baseVersion", String.class);
    Integer count = doc.field("cnt", Integer.class);
    return new GAV(group, name, baseVersion, count);
  }).collect(Collectors.toSet());
}
 
Example #15
Source File: OrientNpmGroupFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get {@link Content} wrapping the NPM Package root for the {@link Context} of the current request
 * to a Group Repository.
 *
 * @param responses {@link Map} of {@link Repository}s in a given group and the {@link Response}
 *                  for the associated {@link Repository}
 * @param context {@link Context} of the current request to a Group Repository
 * @return Content
 * @throws IOException if unable to load package root
 */
@Nullable
public NpmContent getFromCache(final Map<Repository, Response> responses, final Context context) throws IOException
{
  Asset packageRootAsset = getPackageRootAssetFromCache(context);
  if (isNull(packageRootAsset)) {
    return null;
  }

  NpmContent npmContent = toContent(getRepository(), packageRootAsset);
  npmContent.fieldMatchers(rewriteTarballUrlMatcher(getRepository(), packageRootAsset.name()));
  npmContent.missingBlobInputStreamSupplier(
      (missingBlobException) -> buildMergedPackageRootOnMissingBlob(responses, context, missingBlobException));

  return !isStale(npmContent) ? npmContent : null;
}
 
Example #16
Source File: GroupMemberMappingCacheTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testGetGroups_repositoryContainedInMultipleGroupsWhichAlsoContainEachOther() {
  Repository repo = mockRepository("repo");
  Repository group1 = mockGroupRepository("group1", repo);
  Repository group2 = mockGroupRepository("group2", repo);
  Repository group3 = mockGroupRepository("group3", group1);
  Repository group4 = mockGroupRepository("group4", group3);
  Repository group5 = mockGroupRepository("group5", group4);

  //putting in reverse order just to prove out the expected sorting
  when(repositoryManager.browse()).thenReturn(Arrays.asList(group5, group4, group3, group2, group1, repo));

  List<String> groups = underTest.getGroups("repo");
  assertThat(groups, contains("group1", "group2", "group3", "group4", "group5"));
}
 
Example #17
Source File: MavenApiRepositoryAdapterTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testAdapt_proxyRepository() throws Exception {
  Repository repository = createRepository(new ProxyType(), LayoutPolicy.STRICT, VersionPolicy.MIXED);

  MavenProxyApiRepository proxyRepository = (MavenProxyApiRepository) underTest.adapt(repository);
  assertRepository(proxyRepository, "proxy", true);
  assertThat(proxyRepository.getMaven().getLayoutPolicy(), is("STRICT"));
  assertThat(proxyRepository.getMaven().getVersionPolicy(), is("MIXED"));
  // Check fields are populated, actual values validated with SimpleApiRepositoryAdapterTest
  assertThat(proxyRepository.getCleanup(), nullValue());
  assertThat(proxyRepository.getHttpClient(), notNullValue());
  assertThat(proxyRepository.getNegativeCache(), notNullValue());
  assertThat(proxyRepository.getProxy(), notNullValue());
  assertThat(proxyRepository.getStorage(), notNullValue());
}
 
Example #18
Source File: RepositoryAuditor.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Map<String, Object> createFullAttributes(final Repository repository) {
  boolean baseUrlAbsent = !BaseUrlHolder.isSet();
  try {
    if (baseUrlAbsent) {
      BaseUrlHolder.set(""); // use empty base URL placeholder during conversion to avoid log-spam
    }

    AbstractApiRepository apiObject = convert(repository);

    ObjectWriter writer = mapper.writerFor(apiObject.getClass());

    String json = writer.writeValueAsString(apiObject);

    return mapper.readerFor(new TypeReference<Map<String, Object>>()
    {
    }).readValue(json);
  }
  catch (Exception e) {
    log.error("Failed to convert repo object falling back to simple", e);
    return createSimple(repository);
  }
  finally {
    if (baseUrlAbsent) {
      BaseUrlHolder.unset();
    }
  }
}
 
Example #19
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 #20
Source File: RoutingRuleHelperImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean isAllowed(final Repository repository, final String path) {
  RoutingRule routingRule = routingRuleCache.getRoutingRule(repository);

  if (routingRule == null) {
    return true;
  }

  return isAllowed(routingRule, path);
}
 
Example #21
Source File: NpmHandlers.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nonnull
@Override
public Response handle(@Nonnull final Context context) throws Exception {
  Repository repository = context.getRepository();
  Parameters parameters = context.getRequest().getParameters();
  log.debug("[searchV1] repository: {} parameters: {}", repository.getName(), parameters);

  return NpmResponses.ok(repository.facet(NpmSearchFacet.class).searchV1(parameters));
}
 
Example #22
Source File: StorageTxImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = ACTIVE)
public Iterable<Asset> findAssets(@Nullable String whereClause,
                                  @Nullable Map<String, Object> parameters,
                                  @Nullable Iterable<Repository> repositories,
                                  @Nullable String querySuffix)
{
  return assetEntityAdapter.browseByQuery(db, whereClause, parameters, bucketsOf(repositories), querySuffix);
}
 
Example #23
Source File: SecurityFacetSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void ensurePermitted(final Request request) {
  checkNotNull(request);

  // determine permission action from request
  String action = action(request);

  Repository repo = getRepository();

  VariableSource variableSource = variableResolverAdapter.fromRequest(request, getRepository());
  if (!contentPermissionChecker.isPermitted(repo.getName(), repo.getFormat().getValue(), action, variableSource)) {
    throw new AuthorizationException();
  }
}
 
Example #24
Source File: AssetsResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Asset getAsset(final String id, final Repository repository, final DetachedEntityId entityId)
{
  try {
    return ofNullable(browseService
        .getAssetById(assetEntityAdapter.recordIdentity(entityId), repository))
        .orElseThrow(() -> new NotFoundException("Unable to locate asset with id " + id));
  }
  catch (IllegalArgumentException e) {
    log.debug("IllegalArgumentException caught retrieving asset with id {}", entityId, e);
    throw new WebApplicationException(format("Unable to process asset with id %s", entityId), UNPROCESSABLE_ENTITY);
  }
}
 
Example #25
Source File: OrientReindexNpmRepositoryTask.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Updates a batch of assets, opening each asset that represents a tarball and repopulating its format-specific
 * metadata from the tarball itself. The ID for the last asset examined is returned for use in finding the next page.
 */
@Nullable
private String updateAssets(final Repository repository, final Iterable<Asset> assets) {
  String lastId = null;
  for (Asset asset : assets) {
    lastId = assetEntityAdapter.recordIdentity(asset).toString();
    maybeUpdateAsset(repository, asset);
  }
  return lastId;
}
 
Example #26
Source File: OrientOrphanedBlobFinder.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Delete orphaned blobs associated with a given repository
 *
 * @param repository - where to look for orphaned blobs
 */
@Override
public void delete(final Repository repository) {
  log.info("Starting delete of orphaned blobs for {}", repository.getName());

  delete(getBlobStoreForRepository(repository));

  log.info("Finished deleting orphaned blobs for {}", repository.getName());
}
 
Example #27
Source File: RComponentDirector.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
private Optional<Repository> repositoryFor(final Component component) {
  return Optional.of(component)
      .map(Component::bucketId)
      .map(bucketStore::getById)
      .map(Bucket::getRepositoryName)
      .map(repositoryManager::get);
}
 
Example #28
Source File: UploadManagerImplTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void expectExceptionOnUpload(final Repository repository, final String message) throws IOException {
  try {
    underTest.handle(repository, request);
    fail("Expected exception to be thrown");
  }
  catch (ValidationErrorsException exception) {
    List<String> messages = exception.getValidationErrors().stream().map(ValidationErrorXO::getMessage)
        .collect(Collectors.toList());
    assertThat(messages, contains(message));
  }
}
 
Example #29
Source File: BrowseServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private <T extends MetadataNode<?>> T getById(final ORID orid,
                                              final Repository repository,
                                              final MetadataNodeEntityAdapter<T> adapter)
{
  String sql = format("SELECT FROM %s WHERE contentAuth(@this.name, @this.format, :browsedRepository) == true", orid);

  Map<String, Object> params = ImmutableMap.of("browsedRepository", repository.getName());

  try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) {
    storageTx.begin();
    return stream(storageTx.browse(sql, params).spliterator(), false)
        .map(adapter::readEntity).findFirst().orElse(null);
  }
}
 
Example #30
Source File: CleanupITSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected long countAssets(final String repositoryName) {
  Repository repository = repositoryManager.get(repositoryName);
  try (StorageTx tx = repository.facet(StorageFacet.class).txSupplier().get()) {
    tx.begin();

    return tx.countAssets(Query.builder().where("1").eq("1").build(), ImmutableList.of(repository));
  }
}