Search in sources :

Example 31 with Asset

use of org.sonatype.nexus.repository.storage.Asset in project nexus-repository-r by sonatype-nexus-community.

the class RHostedIT method testPackageUpload.

@Test
public void testPackageUpload() throws Exception {
    // Verify DB contains data about uploaded component and asset
    Component component = findComponent(repository, AGRICOLAE_121_TARGZ.packageName);
    assertThat(component.name(), is(equalTo(AGRICOLAE_121_TARGZ.packageName)));
    assertThat(component.version(), is(equalTo(AGRICOLAE_121_TARGZ.packageVersion)));
    assertThat(component.group(), is(equalTo(AGRICOLAE_121_TARGZ.basePath)));
    // Verify Asset is created.
    Asset asset = findAsset(repository, AGRICOLAE_121_TARGZ.fullPath);
    assertThat(asset.name(), is(equalTo(AGRICOLAE_121_TARGZ.fullPath)));
    assertThat(asset.format(), is(equalTo(R_FORMAT_NAME)));
}
Also used : Asset(org.sonatype.nexus.repository.storage.Asset) Component(org.sonatype.nexus.repository.storage.Component) Test(org.junit.Test)

Example 32 with Asset

use of org.sonatype.nexus.repository.storage.Asset in project nexus-repository-r by sonatype-nexus-community.

the class RProxyIT method testDeletingComponentDeletesAllAssociatedAssets.

@Test
public void testDeletingComponentDeletesAllAssociatedAssets() throws IOException {
    client.fetch(AGRICOLAE_131_TGZ.fullPath);
    final Asset asset = findAsset(repository, AGRICOLAE_131_TGZ.fullPath);
    assertNotNull(asset);
    assertNotNull(asset.componentId());
    final Component component = findComponentById(repository, asset.componentId());
    assertNotNull(component);
    ComponentMaintenance maintenanceFacet = repository.facet(ComponentMaintenance.class);
    maintenanceFacet.deleteComponent(component.getEntityMetadata().getId(), true);
    assertNull(findAsset(repository, AGRICOLAE_131_TGZ.fullPath));
    assertNull(findComponentById(repository, asset.componentId()));
}
Also used : ComponentMaintenance(org.sonatype.nexus.repository.storage.ComponentMaintenance) Asset(org.sonatype.nexus.repository.storage.Asset) Component(org.sonatype.nexus.repository.storage.Component) Test(org.junit.Test)

Example 33 with Asset

use of org.sonatype.nexus.repository.storage.Asset in project nexus-repository-r by sonatype-nexus-community.

the class RComponentMaintenance method deleteAssetTx.

/**
 * Deletes the asset and its component if it's the only asset in it.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
    StorageTx tx = UnitOfWork.currentTx();
    final Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
    if (asset == null) {
        return Collections.emptySet();
    }
    final EntityId componentId = asset.componentId();
    if (componentId == null) {
        // Assets without components should be deleted on their own
        return super.deleteAssetTx(assetId, deleteBlob);
    }
    final Component component = tx.findComponent(componentId);
    if (Iterables.size(tx.browseAssets(component)) == 1) {
        // Component with only one asset should be deleted as well with its asset
        return deleteComponentTx(componentId, deleteBlob).getAssets();
    } else {
        return super.deleteAssetTx(assetId, deleteBlob);
    }
}
Also used : EntityId(org.sonatype.nexus.common.entity.EntityId) StorageTx(org.sonatype.nexus.repository.storage.StorageTx) Asset(org.sonatype.nexus.repository.storage.Asset) Component(org.sonatype.nexus.repository.storage.Component) TransactionalDeleteBlob(org.sonatype.nexus.repository.transaction.TransactionalDeleteBlob)

Example 34 with Asset

use of org.sonatype.nexus.repository.storage.Asset in project nexus-repository-r by sonatype-nexus-community.

the class RFacetImpl method findOrCreateAsset.

@Override
public Asset findOrCreateAsset(final StorageTx tx, final Component component, final String path, final Map<String, String> attributes) {
    Bucket bucket = tx.findBucket(getRepository());
    Asset asset = findAsset(tx, bucket, path);
    if (asset == null) {
        asset = tx.createAsset(bucket, component);
        asset.name(path);
        // TODO: Make this a bit more robust (could be problematic if keys are removed in later versions, or if keys clash)
        for (Entry<String, String> attribute : attributes.entrySet()) {
            asset.formatAttributes().set(attribute.getKey(), attribute.getValue());
        }
        asset.formatAttributes().set(P_ASSET_KIND, getAssetKind(path).name());
        tx.saveAsset(asset);
    }
    return asset;
}
Also used : Bucket(org.sonatype.nexus.repository.storage.Bucket) RFacetUtils.findAsset(org.sonatype.nexus.repository.r.internal.util.RFacetUtils.findAsset) Asset(org.sonatype.nexus.repository.storage.Asset)

Example 35 with Asset

use of org.sonatype.nexus.repository.storage.Asset in project nexus-repository-r by sonatype-nexus-community.

the class RRestoreFacetImpl method restore.

@Override
@TransactionalTouchBlob
public void restore(final AssetBlob assetBlob, final String path) throws IOException {
    StorageTx tx = UnitOfWork.currentTx();
    RFacet facet = facet(RFacet.class);
    Asset asset;
    if (componentRequired(path)) {
        Map<String, String> attributes;
        try (InputStream is = assetBlob.getBlob().getInputStream()) {
            attributes = extractDescriptionFromArchive(path, is);
        }
        Component component = facet.findOrCreateComponent(tx, path, attributes);
        asset = facet.findOrCreateAsset(tx, component, path, attributes);
    } else {
        asset = facet.findOrCreateAsset(tx, path);
    }
    tx.attachBlob(asset, assetBlob);
    Content.applyToAsset(asset, Content.maintainLastModified(asset, new AttributesMap()));
    tx.saveAsset(asset);
}
Also used : RFacet(org.sonatype.nexus.repository.r.RFacet) StorageTx(org.sonatype.nexus.repository.storage.StorageTx) InputStream(java.io.InputStream) AttributesMap(org.sonatype.nexus.common.collect.AttributesMap) Asset(org.sonatype.nexus.repository.storage.Asset) Component(org.sonatype.nexus.repository.storage.Component) TransactionalTouchBlob(org.sonatype.nexus.repository.transaction.TransactionalTouchBlob)

Aggregations

Asset (org.sonatype.nexus.repository.storage.Asset)35 StorageTx (org.sonatype.nexus.repository.storage.StorageTx)17 Component (org.sonatype.nexus.repository.storage.Component)13 Test (org.junit.Test)10 RFacetUtils.findAsset (org.sonatype.nexus.repository.r.internal.util.RFacetUtils.findAsset)10 RFacetUtils.saveAsset (org.sonatype.nexus.repository.r.internal.util.RFacetUtils.saveAsset)8 TransactionalStoreBlob (org.sonatype.nexus.repository.transaction.TransactionalStoreBlob)8 RFacetUtils.findAsset (org.sonatype.nexus.repository.r.internal.RFacetUtils.findAsset)7 RFacetUtils.saveAsset (org.sonatype.nexus.repository.r.internal.RFacetUtils.saveAsset)7 InputStream (java.io.InputStream)6 RFacet (org.sonatype.nexus.repository.r.RFacet)6 TransactionalTouchBlob (org.sonatype.nexus.repository.transaction.TransactionalTouchBlob)6 Bucket (org.sonatype.nexus.repository.storage.Bucket)5 TransactionalTouchMetadata (org.sonatype.nexus.repository.transaction.TransactionalTouchMetadata)4 BrowsePaths (org.sonatype.nexus.repository.browse.BrowsePaths)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Map (java.util.Map)2 NestedAttributesMap (org.sonatype.nexus.common.collect.NestedAttributesMap)2 RHostedFacet (org.sonatype.nexus.repository.r.RHostedFacet)2