org.sonatype.nexus.repository.storage.Component Java Examples

The following examples show how to use org.sonatype.nexus.repository.storage.Component. 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: ElasticSearchCleanupComponentBrowse.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public PagedResponse<Component> browseByPage(final CleanupPolicy policy,
                                             final Repository repository,
                                             final QueryOptions options)
{
  checkNotNull(options.getStart());
  checkNotNull(options.getLimit());

  StorageTx tx = UnitOfWork.currentTx();

  QueryBuilder query = convertPolicyToQuery(policy, options);

  log.debug("Searching for components to cleanup using policy {}", policy);

  SearchResponse searchResponse = invokeSearchByPage(policy, repository, options, query);

  List<Component> components = stream(searchResponse.getHits().spliterator(), false)
      .map(searchHit -> tx.findComponent(new DetachedEntityId(searchHit.getId())))
      .filter(Objects::nonNull)
      .collect(toList());

  return new PagedResponse<>(searchResponse.getHits().getTotalHits(), components);
}
 
Example #2
Source File: P2FacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Asset findOrCreateAsset(final StorageTx tx,
                               final Component component,
                               final String path,
                               final P2Attributes attributes)
{
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);

    asset.formatAttributes().set(PLUGIN_NAME, attributes.getPluginName());
    asset.formatAttributes().set(P_ASSET_KIND, attributes.getAssetKind());
    tx.saveAsset(asset);
  }

  return asset;
}
 
Example #3
Source File: GolangDataAccess.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find a component by its name and tag (version)
 *
 * @return found component or null if not found
 */
@Nullable
private Component findComponent(final StorageTx tx,
                                final Repository repository,
                                final String name,
                                final String version)
{
  Iterable<Component> components = tx.findComponents(
      Query.builder()
          .where(P_NAME).eq(name)
          .and(P_VERSION).eq(version)
          .build(),
      singletonList(repository)
  );
  if (components.iterator().hasNext()) {
    return components.iterator().next();
  }
  return null;
}
 
Example #4
Source File: AptSnapshotFacetSupport.java    From nexus-repository-apt with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = { ONeedRetryException.class })
@Override
public Content getSnapshotFile(String id, String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.findComponentWithProperty(P_NAME, id, bucket);
  if (component == null) {
    return null;
  }

  final Asset asset = tx.findAssetWithProperty(P_NAME, createAssetPath(id, path), component);
  if (asset == null) {
    return null;
  }

  final Blob blob = tx.requireBlob(asset.requireBlobRef());
  return FacetHelper.toContent(asset, blob);
}
 
Example #5
Source File: OrientBrowseNodeManagerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Component createComponent(final String name,
                                  final String group,
                                  final String version,
                                  final String componentId)
{
  EntityMetadata metadata = mock(EntityMetadata.class);
  when(metadata.getId()).thenReturn(new DetachedEntityId(componentId));

  Component component = new DefaultComponent();
  component.name(name);
  component.group(group);
  component.version(version);
  component.setEntityMetadata(metadata);

  when(componentStore.read(EntityHelper.id(component))).thenReturn(component);

  return component;
}
 
Example #6
Source File: DefaultComponentMetadataProducer.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
public String getNormalizedVersion(Component component) {
  String version = component.version();
  if (StringUtils.isBlank(version)) {
    return "";
  }

  Matcher matcher = Pattern.compile("\\d+").matcher(version);
  StringBuilder replacementBuilder = new StringBuilder();
  int position = 0;
  while (matcher.find()) {
    replacementBuilder.append(version, position, matcher.start());
    position = matcher.end();
    try {
      replacementBuilder.append(String.format("%09d", Long.parseLong(matcher.group())));
    }
    catch (NumberFormatException e) {
      log.debug("Unable to parse number as long '{}'", matcher.group());
      replacementBuilder.append(matcher.group());
    }
  }
  return replacementBuilder.toString();
}
 
Example #7
Source File: AptSnapshotFacetSupport.java    From nexus-repository-apt with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = { ONeedRetryException.class })
@Override
public void createSnapshot(String id, SnapshotComponentSelector selector) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  StorageFacet storageFacet = facet(StorageFacet.class);
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.createComponent(bucket, getRepository().getFormat()).name(id);
  tx.saveComponent(component);
  for (SnapshotItem item : collectSnapshotItems(selector)) {
    String assetName = createAssetPath(id, item.specifier.path);
    Asset asset = tx.createAsset(bucket, component).name(assetName);
    try (final TempBlob streamSupplier = storageFacet.createTempBlob(item.content.openInputStream(), FacetHelper.hashAlgorithms)) {
      AssetBlob blob = tx.createBlob(item.specifier.path, streamSupplier, FacetHelper.hashAlgorithms, null,
          FacetHelper.determineContentType(item), true);
      tx.attachBlob(asset, blob);
    }
    finally {
      item.content.close();
    }
    tx.saveAsset(asset);
  }
}
 
Example #8
Source File: ConanProxyIT.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void checkComponentRemovedWhenAssetRemoved() throws IOException {
  HttpResponse response = proxyClient.getHttpResponse(PATH_MANIFEST);
  assertThat(status(response), is(HttpStatus.OK));

  String assetPath = PATH_MANIFEST;

  Asset asset = findAsset(proxyRepo, assetPath);
  Component component = findComponent(proxyRepo, LIBRARY_NAME);
  assertThat(component.name(), is(equalTo(LIBRARY_NAME)));
  assertThat(component.version(), is(equalTo(LIBRARY_VERSION)));
  assertThat(component.group(), is(equalTo(LIBRARY_VENDOR)));

  ComponentMaintenance maintenanceFacet = proxyRepo.facet(ComponentMaintenance.class);
  maintenanceFacet.deleteAsset(asset.getEntityMetadata().getId());

  asset = findAsset(proxyRepo, assetPath);
  assertThat(asset, is(equalTo(null)));
  component = findComponent(proxyRepo, LIBRARY_NAME);
  assertThat(component, is(equalTo(null)));
}
 
Example #9
Source File: RawContentFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalTouchMetadata
public void setCacheInfo(final String path, final Content content, final CacheInfo cacheInfo) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  // by EntityId
  Asset asset = Content.findAsset(tx, bucket, content);
  if (asset == null) {
    // by format coordinates
    Component component = tx.findComponentWithProperty(P_NAME, path, bucket);
    if (component != null) {
      asset = tx.firstAsset(component);
    }
  }
  if (asset == null) {
    log.debug("Attempting to set cache info for non-existent raw component {}", path);
    return;
  }

  log.debug("Updating cacheInfo of {} to {}", path, cacheInfo);
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example #10
Source File: ConanFacetUtils.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find a component by its name and tag (version)
 *
 * @return found component or null if not found
 */
@Nullable
public static Component findComponent(final StorageTx tx,
                                      final Repository repository,
                                      final ConanCoords coords)
{
  Iterable<Component> components = tx.findComponents(
      Query.builder()
          .where(P_GROUP).eq(coords.getGroup())
          .and(P_NAME).eq(coords.getProject())
          .and(P_VERSION).eq(getComponentVersion(coords))
          .build(),
      singletonList(repository)
  );
  if (components.iterator().hasNext()) {
    return components.iterator().next();
  }
  return null;
}
 
Example #11
Source File: MavenFacetUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds component in given repository by maven path.
 */
@Nullable
public static Component findComponent(final StorageTx tx,
    final Repository repository,
    final MavenPath mavenPath)
{
  final Coordinates coordinates = mavenPath.getCoordinates();
  final Iterable<Component> components = tx.findComponents(
      Query.builder()
          .where(P_GROUP).eq(coordinates.getGroupId())
          .and(P_NAME).eq(coordinates.getArtifactId())
          .and(P_VERSION).eq(coordinates.getVersion())
          .build(),
      singletonList(repository)
  );
  if (components.iterator().hasNext()) {
    return components.iterator().next();
  }
  return null;
}
 
Example #12
Source File: CondaProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content findOrCreateAsset(final TempBlob tempBlob,
                                    final Content content,
                                    final AssetKind assetKind,
                                    final String assetPath,
                                    final Component component) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = getCondaFacet().findAsset(tx, bucket, assetPath);

  if (asset == null) {
    if (ARCH_TAR_PACKAGE.equals(assetKind) || ARCH_CONDA_PACKAGE.equals(assetKind)) {
      asset = tx.createAsset(bucket, component);
    }
    else {
      asset = tx.createAsset(bucket, getRepository().getFormat());
    }
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }

  return getCondaFacet().saveAsset(tx, asset, tempBlob, content);
}
 
Example #13
Source File: RHostedIT.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testDeletingRemainingAssetAlsoDeletesComponent() {
  final Asset asset = findAsset(repository, AGRICOLAE_121_TARGZ.fullPath);
  assertNotNull(asset);
  assertNotNull(asset.componentId());

  final Component component = findComponentById(repository, asset.componentId());
  assertNotNull(component);
  assertEquals(1, findAssetsByComponent(repository, component).size());

  ComponentMaintenance maintenanceFacet = repository.facet(ComponentMaintenance.class);
  maintenanceFacet.deleteAsset(asset.getEntityMetadata().getId(), true);

  assertNull(findAsset(repository, AGRICOLAE_121_TARGZ.fullPath));
  assertNull(findComponentById(repository, asset.componentId()));
}
 
Example #14
Source File: ComponentComponentTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testReadComponent() {
  Component component = mock(Component.class);
  Asset asset = mock(Asset.class);
  EntityMetadata entityMetadata = mock(EntityMetadata.class);
  VariableSource variableSource = mock(VariableSource.class);
  when(contentPermissionChecker.isPermitted(anyString(),any(), eq(BreadActions.BROWSE), any())).thenReturn(true);
  when(component.getEntityMetadata()).thenReturn(entityMetadata);
  when(entityMetadata.getId()).thenReturn(new DetachedEntityId("someid"));
  when(storageTx.findComponent(eq(new DetachedEntityId("someid")))).thenReturn(component);
  when(storageTx.browseAssets(component)).thenReturn(Arrays.asList(asset));
  when(assetVariableResolver.fromAsset(asset)).thenReturn(variableSource);
  ComponentXO componentXO = underTest.readComponent("someid", "testRepositoryName");

  assertThat(componentXO, is(notNullValue()));
  assertThat(componentXO.getId(), is("someid"));
}
 
Example #15
Source File: ComponentAuditor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Subscribe
@AllowConcurrentEvents
public void on(final ComponentEvent event) {
  if (isRecording() && event.isLocal()) {
    Component component = event.getComponent();

    AuditData data = new AuditData();
    data.setDomain(DOMAIN);
    data.setType(type(event.getClass()));
    data.setContext(component.name());

    Map<String, Object> attributes = data.getAttributes();
    attributes.put("repository.name", event.getRepositoryName());
    attributes.put("format", component.format());
    attributes.put("name", component.name());
    attributes.put("group", component.group());
    attributes.put("version", component.version());

    record(data);
  }
}
 
Example #16
Source File: PurgeUnusedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find all components that were last accessed before specified date. Date when a component was last accessed is the
 * last time an asset of that component was last accessed.
 */
private Iterable<Component> findUnusedComponents(final StorageTx tx, final Date olderThan) {
  final Bucket bucket = tx.findBucket(getRepository());

  String sql = String.format(
      "SELECT FROM (SELECT %s, MAX(%s) AS lastDownloaded FROM asset WHERE %s=:bucket AND %s IS NOT NULL GROUP BY %s) WHERE lastDownloaded < :olderThan",
      P_COMPONENT, P_LAST_DOWNLOADED, P_BUCKET, P_COMPONENT, P_COMPONENT
  );

  Map<String, Object> sqlParams = ImmutableMap.of(
      "bucket", AttachedEntityHelper.id(bucket),
      "olderThan", olderThan
  );

  checkCancellation();
  return Iterables.transform(tx.browse(sql, sqlParams),
      (doc) -> componentEntityAdapter.readEntity(doc.field(P_COMPONENT)));
}
 
Example #17
Source File: ComponentsResourceTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void getComponentById() throws Exception {
  RepositoryItemIDXO repositoryItemXOID = getRepositoryItemIdXO(componentOne);

  ComponentXO componentXO = underTest.getComponentById(repositoryItemXOID.getValue());

  assertThat(componentXO, notNullValue());
  assertThat(componentXO.getGroup(), is("component-one-group"));
  assertThat(componentXO.getName(), is("component-one-name"));
  assertThat(componentXO.getVersion(), is("1.0.0"));
  assertThat(componentXO.getAssets(), hasSize(1));

  verify(componentsResourceExtension).updateComponentXO(any(ComponentXO.class), any(Component.class));
}
 
Example #18
Source File: OrientComponentAssetTestHelper.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean componentExists(final Repository repository, final String name, final String version) {
  if (version.endsWith(SNAPSHOT_VERSION_SUFFIX)) {
    Optional<Component> component = findSnapshotComponent(repository, name, version);
    return component.isPresent();
  }
  else {
    return findComponents(repository).stream()
        .filter(c -> name.equals(c.name()))
        .anyMatch(c -> version.equals(c.version()));
  }
}
 
Example #19
Source File: NpmFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
@Override
public Asset putTarball(final String packageId,
                        final String tarballName,
                        final AssetBlob assetBlob,
                        @Nullable final AttributesMap contentAttributes) throws IOException
{
  final Repository repository = getRepository();
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(repository);
  NpmPackageId npmPackageId = NpmPackageId.parse(packageId);
  Asset asset = NpmFacetUtils.findTarballAsset(tx, bucket, npmPackageId, tarballName);

  if (asset == null) {
    String version = extractVersion(tarballName);
    Component tarballComponent = getOrCreateTarballComponent(tx, repository, npmPackageId, version);
    asset = tx.firstAsset(tarballComponent);

    if (asset == null) {
      asset = tx.createAsset(bucket, tarballComponent).name(tarballAssetName(npmPackageId, tarballName));
    }
  }

  maybeExtractFormatAttributes(tx, packageId, asset, assetBlob);
  saveAsset(tx, asset, assetBlob, TARBALL, contentAttributes);

  return asset;
}
 
Example #20
Source File: OrientBrowseNodeManager.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Inject
public OrientBrowseNodeManager(
    final BrowseNodeCrudStore<Asset, Component> browseNodeStore,
    final ComponentStore componentStore,
    final Map<String, BrowseNodeGenerator> pathGenerators)
{
  this.browseNodeStore = checkNotNull(browseNodeStore);
  this.componentStore = checkNotNull(componentStore);
  this.pathGenerators = checkNotNull(pathGenerators);
  this.defaultGenerator = checkNotNull(pathGenerators.get(DEFAULT_PATH_HANDLER));
}
 
Example #21
Source File: MavenFacetUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testComponentVersionComparator_Release() {
  List<Component> sorted = Stream
      .of(createComponent("1.1", "1.1"), createComponent("1.2", "1.2"), createComponent("1.0", "1.0"))
      .sorted(COMPONENT_VERSION_COMPARATOR)
      .collect(toList());

  assertThat(sorted.get(0).version(), equalTo("1.0"));
  assertThat(sorted.get(1).version(), equalTo("1.1"));
  assertThat(sorted.get(2).version(), equalTo("1.2"));
}
 
Example #22
Source File: RHostedFacetImplTest.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void putArchive() throws Exception {
  List<Component> list = ImmutableList.of(component);
  when(tempBlob.get()).thenReturn(getClass().getResourceAsStream(
      "/org/sonatype/nexus/repository/r/internal/" + REAL_PACKAGE));
  when(asset.name()).thenReturn(REAL_PACKAGE_PATH);
  when(assetBlob.getBlob())
      .thenReturn(blob);
  doReturn(assetBlob)
      .when(storageTx).setBlob(any(),
      any(),
      any(),
      any(),
      any(),
      any(),
      anyBoolean());
  when(storageTx.findComponents(any(), any()))
      .thenReturn(list);
  when(rFacet.findOrCreateComponent(any(storageTx.getClass()), anyString(), anyMapOf(String.class, String.class)))
      .thenReturn(component);
  when(rFacet.findOrCreateAsset(any(storageTx.getClass()), any(component.getClass()), eq(REAL_PACKAGE_PATH),
      anyMapOf(String.class, String.class)))
      .thenReturn(asset);

  underTest.doPutArchive(REAL_PACKAGE_PATH, tempBlob, payload);
  verify(storageTx).saveAsset(asset);
}
 
Example #23
Source File: DefaultComponentMetadataProducerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void defaultIsPreReleaseWhenUsingPrerelease() throws Exception {
  Bucket bucket = createBucket(REPO_NAME);
  Component component = createDetachedComponent(bucket, GROUP, NAME, VERSION + "-SNAPSHOT");

  assertFalse(underTest.isPrerelease(component, emptyList()));
}
 
Example #24
Source File: MavenFacetUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testComponentVersionComparator_Snapshot() {
  List<Component> sorted = Stream
      .of(createComponent("1.1-20170919.212404-2", "1.1"), createComponent("1.1-20170919.212405-3", "1.1"),
          createComponent("1.1-20170919.212403-1", "1.1"))
      .sorted(COMPONENT_VERSION_COMPARATOR)
      .collect(toList());

  assertThat(sorted.get(0).version(), equalTo("1.1-20170919.212403-1"));
  assertThat(sorted.get(1).version(), equalTo("1.1-20170919.212404-2"));
  assertThat(sorted.get(2).version(), equalTo("1.1-20170919.212405-3"));
}
 
Example #25
Source File: DefaultBrowseNodeGeneratorTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void computeComponentPathWithComponent() {
  Component component = createComponent("component", "group", "1.0.0");
  Asset asset = createAsset("path/assetName");

  List<BrowsePaths> paths = generator.computeComponentPaths(asset, component);

  assertPaths(asList(component.group(), component.name(), component.version()), paths, true);
}
 
Example #26
Source File: RBrowseNodeGenerator.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<BrowsePaths> computeComponentPaths(final Asset asset, final Component component)
{
  checkNotNull(asset);
  checkNotNull(component);
  List<BrowsePaths> browsePaths = super.computeComponentPaths(asset, null);
  if (!browsePaths.get(browsePaths.size() - 1).getBrowsePath().equals(component.name())) {
    BrowsePaths.appendPath(browsePaths, component.name(), computeRequestPath(browsePaths, component.name()));
  }
  BrowsePaths.appendPath(browsePaths, component.version(), computeRequestPath(browsePaths, component.version()));
  return browsePaths;
}
 
Example #27
Source File: ComponentComponentTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Component makeTestComponent(final String format) {
  Component component = new DefaultComponent();
  component.setEntityMetadata(new DetachedEntityMetadata(
      new DetachedEntityId("testComponentId"),
      new DetachedEntityVersion("1.0")
  ));
  component.format(format);
  component.group("testGroup");
  component.name("testComponentName");
  return component;
}
 
Example #28
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 #29
Source File: AptBrowseNodeGeneratorTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void computeComponentPath() {
  Component component = createComponent("nano", "amd64", "1.0.0");
  Asset asset = createAsset("path/assetName");

  List<BrowsePaths> paths = generator.computeComponentPaths(asset, component);
  assertPaths(asList("packages", "n", "nano", "1.0.0", "amd64", "nano"), paths, true);
}
 
Example #30
Source File: DefaultBrowseNodeGeneratorTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void computeAssetPathWithComponent() {
  Component component = createComponent("component", "group", "1.0.0");
  Asset asset = createAsset("path/assetName");

  List<BrowsePaths> paths = generator.computeAssetPaths(asset, component);

  assertPaths(asList(component.group(), component.name(), component.version(), "assetName"), paths);
}