Search in sources :

Example 1 with Asset

use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.

the class MavenContentFacetImpl method hardLink.

@Override
public void hardLink(FluentAsset asset, Path contentPath) throws IOException {
    String mimeType = mimeSupport.detectMimeType(Files.newInputStream(contentPath), contentPath.toString());
    Map<String, String> headers = ImmutableMap.of(BLOB_NAME_HEADER, contentPath.toString(), CONTENT_TYPE_HEADER, mimeType);
    byte[] bytes = Files.readAllBytes(contentPath);
    Map<HashAlgorithm, HashCode> hashes = HashType.ALGORITHMS.stream().collect(Collectors.toMap(Function.identity(), a -> a.function().hashBytes(bytes)));
    Blob blob = blobs().ingest(contentPath, headers, hashes.get(SHA1), Files.size(contentPath));
    asset.attach(blob, hashes);
}
Also used : FluentComponent(org.sonatype.nexus.repository.content.fluent.FluentComponent) Coordinates(org.sonatype.nexus.repository.maven.MavenPath.Coordinates) Arrays(java.util.Arrays) VersionPolicy(org.sonatype.nexus.repository.maven.VersionPolicy) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset) ConfigurationFacet(org.sonatype.nexus.repository.config.ConfigurationFacet) GAV(org.sonatype.nexus.content.maven.store.GAV) StringUtils(org.apache.commons.lang3.StringUtils) MavenAttributesHelper.assetKind(org.sonatype.nexus.content.maven.internal.recipe.MavenAttributesHelper.assetKind) MimeSupport(org.sonatype.nexus.mime.MimeSupport) SHA1(org.sonatype.nexus.common.hash.HashAlgorithm.SHA1) TempBlob(org.sonatype.nexus.repository.view.payloads.TempBlob) Map(java.util.Map) P_VERSION(org.sonatype.nexus.repository.maven.internal.Attributes.P_VERSION) MetadataRebuilder(org.sonatype.nexus.repository.maven.internal.hosted.metadata.MetadataRebuilder) METADATA_FORCE_REBUILD(org.sonatype.nexus.repository.maven.MavenMetadataRebuildFacet.METADATA_FORCE_REBUILD) P_ARTIFACT_ID(org.sonatype.nexus.repository.maven.internal.Attributes.P_ARTIFACT_ID) Path(java.nio.file.Path) Collectors.toSet(java.util.stream.Collectors.toSet) MavenPath(org.sonatype.nexus.repository.maven.MavenPath) OVERLAY(org.sonatype.nexus.repository.content.AttributeOperation.OVERLAY) Maven2ComponentData(org.sonatype.nexus.content.maven.store.Maven2ComponentData) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ALLOW(org.sonatype.nexus.repository.config.WritePolicy.ALLOW) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) MavenContentFacet(org.sonatype.nexus.content.maven.MavenContentFacet) ComponentStore(org.sonatype.nexus.repository.content.store.ComponentStore) Content(org.sonatype.nexus.repository.view.Content) UncheckedIOException(java.io.UncheckedIOException) REPOSITORY_INDEX(org.sonatype.nexus.repository.maven.internal.Attributes.AssetKind.REPOSITORY_INDEX) List(java.util.List) FluentAssetBuilder(org.sonatype.nexus.repository.content.fluent.FluentAssetBuilder) HashType(org.sonatype.nexus.repository.maven.MavenPath.HashType) Optional(java.util.Optional) P_GROUP_ID(org.sonatype.nexus.repository.maven.internal.Attributes.P_GROUP_ID) RebuildMavenArchetypeCatalogEvent(org.sonatype.nexus.content.maven.internal.event.RebuildMavenArchetypeCatalogEvent) Model(org.apache.maven.model.Model) P_EXTENSION(org.sonatype.nexus.repository.maven.internal.Attributes.P_EXTENSION) ProxyType(org.sonatype.nexus.repository.types.ProxyType) StringUtils.prependIfMissing(org.apache.commons.lang3.StringUtils.prependIfMissing) CONTENT_TYPE_HEADER(org.sonatype.nexus.blobstore.api.BlobStore.CONTENT_TYPE_HEADER) HashMap(java.util.HashMap) Function(java.util.function.Function) EventManager(org.sonatype.nexus.common.event.EventManager) Blob(org.sonatype.nexus.blobstore.api.Blob) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Repository(org.sonatype.nexus.repository.Repository) HostedType(org.sonatype.nexus.repository.types.HostedType) Payload(org.sonatype.nexus.repository.view.Payload) P_CLASSIFIER(org.sonatype.nexus.repository.maven.internal.Attributes.P_CLASSIFIER) Configuration(org.sonatype.nexus.repository.config.Configuration) Named(javax.inject.Named) WritePolicy(org.sonatype.nexus.repository.config.WritePolicy) LayoutPolicy(org.sonatype.nexus.repository.maven.LayoutPolicy) METADATA_REBUILD(org.sonatype.nexus.repository.maven.MavenMetadataRebuildFacet.METADATA_REBUILD) RepositoryContent(org.sonatype.nexus.repository.content.RepositoryContent) ContentFacet(org.sonatype.nexus.repository.content.facet.ContentFacet) FluentComponentBuilder(org.sonatype.nexus.repository.content.fluent.FluentComponentBuilder) FormatStoreManager(org.sonatype.nexus.repository.content.store.FormatStoreManager) REPOSITORY_METADATA(org.sonatype.nexus.repository.maven.internal.Attributes.AssetKind.REPOSITORY_METADATA) Files(java.nio.file.Files) NAME(org.sonatype.nexus.repository.maven.internal.Maven2Format.NAME) HashCode(com.google.common.hash.HashCode) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IOException(java.io.IOException) Asset(org.sonatype.nexus.repository.content.Asset) P_BASE_VERSION(org.sonatype.nexus.repository.maven.internal.Attributes.P_BASE_VERSION) ARTIFACT_SUBORDINATE(org.sonatype.nexus.repository.maven.internal.Attributes.AssetKind.ARTIFACT_SUBORDINATE) MavenPathParser(org.sonatype.nexus.repository.maven.MavenPathParser) MetadataUtils.metadataPath(org.sonatype.nexus.repository.maven.internal.hosted.metadata.MetadataUtils.metadataPath) MavenModels.readModel(org.sonatype.nexus.repository.maven.internal.MavenModels.readModel) Component(org.sonatype.nexus.repository.content.Component) Maven2Format(org.sonatype.nexus.repository.maven.internal.Maven2Format) MavenAttributesHelper.setMavenAttributes(org.sonatype.nexus.content.maven.internal.recipe.MavenAttributesHelper.setMavenAttributes) BLOB_NAME_HEADER(org.sonatype.nexus.blobstore.api.BlobStore.BLOB_NAME_HEADER) MAVEN_ARCHETYPE_KIND(org.sonatype.nexus.content.maven.internal.recipe.MavenArchetypeCatalogFacetImpl.MAVEN_ARCHETYPE_KIND) HashAlgorithm(org.sonatype.nexus.common.hash.HashAlgorithm) MavenMetadataContentValidator(org.sonatype.nexus.repository.maven.internal.validation.MavenMetadataContentValidator) Collections(java.util.Collections) Maven2ComponentStore(org.sonatype.nexus.content.maven.store.Maven2ComponentStore) ALLOW_ONCE(org.sonatype.nexus.repository.config.WritePolicy.ALLOW_ONCE) METADATA_FILENAME(org.sonatype.nexus.repository.maven.internal.Constants.METADATA_FILENAME) InputStream(java.io.InputStream) ContentFacetSupport(org.sonatype.nexus.repository.content.facet.ContentFacetSupport) TempBlob(org.sonatype.nexus.repository.view.payloads.TempBlob) Blob(org.sonatype.nexus.blobstore.api.Blob) HashCode(com.google.common.hash.HashCode) HashAlgorithm(org.sonatype.nexus.common.hash.HashAlgorithm)

Example 2 with Asset

use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.

the class AptProxyFacet method doIndicateVerified.

protected void doIndicateVerified(final Content content, final CacheInfo cacheInfo, final String assetPath) {
    AptContentFacet contentFacet = facet(AptContentFacet.class);
    Asset asset = content.getAttributes().get(Asset.class);
    if (asset != null) {
        contentFacet.assets().with(asset).markAsCached(cacheInfo);
        return;
    }
    facet(AptContentFacet.class).getAsset(assetPath).ifPresent(a -> contentFacet.assets().with(a).markAsCached(cacheInfo));
}
Also used : AptContentFacet(org.sonatype.nexus.repository.apt.datastore.AptContentFacet) Asset(org.sonatype.nexus.repository.content.Asset)

Example 3 with Asset

use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.

the class DataStoreMavenTestHelper method getLastDownloadedTime.

@Override
public DateTime getLastDownloadedTime(final Repository repository, final String assetPath) throws IOException {
    MavenContentFacet mavenContentFacet = repository.facet(MavenContentFacet.class);
    MavenPath mavenPath = mavenContentFacet.getMavenPathParser().parsePath(assetPath);
    return mavenContentFacet.get(mavenPath).map(Content::getAttributes).map(attributes -> attributes.get(Asset.class)).map(Asset::lastDownloaded).filter(Optional::isPresent).map(Optional::get).map(DateHelper::toDateTime).orElse(null);
}
Also used : FluentComponent(org.sonatype.nexus.repository.content.fluent.FluentComponent) MD5(org.sonatype.nexus.common.hash.HashAlgorithm.MD5) Date(java.util.Date) StringUtils.prependIfMissing(org.apache.commons.lang3.StringUtils.prependIfMissing) EntityId(org.sonatype.nexus.common.entity.EntityId) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) DateHelper(org.sonatype.nexus.common.time.DateHelper) ImmutableList(com.google.common.collect.ImmutableList) SHA1(org.sonatype.nexus.common.hash.HashAlgorithm.SHA1) Repository(org.sonatype.nexus.repository.Repository) TempBlob(org.sonatype.nexus.repository.view.payloads.TempBlob) Map(java.util.Map) Payload(org.sonatype.nexus.repository.view.Payload) InternalIds(org.sonatype.nexus.repository.content.store.InternalIds) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ZoneOffset(java.time.ZoneOffset) Named(javax.inject.Named) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Nonnull(javax.annotation.Nonnull) MavenPath(org.sonatype.nexus.repository.maven.MavenPath) Collections.emptyMap(java.util.Collections.emptyMap) Continuations.streamOf(org.sonatype.nexus.common.entity.Continuations.streamOf) ContentFacet(org.sonatype.nexus.repository.content.facet.ContentFacet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) Continuations.iterableOf(org.sonatype.nexus.common.entity.Continuations.iterableOf) DateTime(org.joda.time.DateTime) ContentMaintenanceFacet(org.sonatype.nexus.repository.content.maintenance.ContentMaintenanceFacet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Asset(org.sonatype.nexus.repository.content.Asset) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) MavenContentFacet(org.sonatype.nexus.content.maven.MavenContentFacet) Content(org.sonatype.nexus.repository.view.Content) MavenPathParser(org.sonatype.nexus.repository.maven.MavenPathParser) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Stream(java.util.stream.Stream) HashType(org.sonatype.nexus.repository.maven.MavenPath.HashType) AssetBlob(org.sonatype.nexus.repository.content.AssetBlob) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) MavenMetadataRebuildFacet(org.sonatype.nexus.repository.maven.MavenMetadataRebuildFacet) Collections(java.util.Collections) InputStream(java.io.InputStream) MavenPath(org.sonatype.nexus.repository.maven.MavenPath) Optional(java.util.Optional) Content(org.sonatype.nexus.repository.view.Content) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset) Asset(org.sonatype.nexus.repository.content.Asset) MavenContentFacet(org.sonatype.nexus.content.maven.MavenContentFacet)

Example 4 with Asset

use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.

the class InternalIdsTest method shouldFetchComponentIdFromComponentWhenComponentIdIsNullOnAsset.

@Test
public void shouldFetchComponentIdFromComponentWhenComponentIdIsNullOnAsset() {
    ComponentData component = new ComponentData();
    component.componentId = 2;
    AssetData asset = new AssetData();
    asset.assetId = 3;
    Asset fluentAsset = new FluentAssetImpl(contentFacet, asset);
    assertThat(internalComponentId(asset), is(empty()));
    assertThat(internalComponentId(fluentAsset), is(empty()));
    asset.setComponent(component);
    asset.componentId = null;
    assertThat(internalComponentId(asset).getAsInt(), is(component.componentId));
    assertThat(internalComponentId(fluentAsset).getAsInt(), is(component.componentId));
}
Also used : FluentAssetImpl(org.sonatype.nexus.repository.content.fluent.internal.FluentAssetImpl) Asset(org.sonatype.nexus.repository.content.Asset) Test(org.junit.Test)

Example 5 with Asset

use of org.sonatype.nexus.repository.content.Asset in project nexus-public by sonatype.

the class AssetDAOTest method testAttachingBlobs.

@Test
public void testAttachingBlobs() throws InterruptedException {
    AssetBlobData assetBlob1 = randomAssetBlob();
    AssetBlobData assetBlob2 = randomAssetBlob();
    AssetData asset = randomAsset(repositoryId);
    String path = asset.path();
    Asset tempResult;
    try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
        AssetBlobDAO dao = session.access(TestAssetBlobDAO.class);
        dao.createAssetBlob(assetBlob1);
        dao.createAssetBlob(assetBlob2);
        session.access(TestAssetDAO.class).createAsset(asset);
        session.getTransaction().commit();
        assertThat(dao.browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1), sameBlob(assetBlob2)));
    }
    // ATTACH BLOB
    // NOSONAR
    Thread.sleep(2);
    try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
        AssetDAO dao = session.access(TestAssetDAO.class);
        tempResult = dao.readPath(repositoryId, path).get();
        OffsetDateTime oldCreated = tempResult.created();
        OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
        assertFalse(tempResult.blob().isPresent());
        asset.setAssetBlob(assetBlob1);
        dao.updateAssetBlobLink(asset);
        tempResult = dao.readPath(repositoryId, path).get();
        assertTrue(tempResult.blob().isPresent());
        assertThat(tempResult.blob().get(), sameBlob(assetBlob1));
        assertThat(tempResult, sameBlob(asset));
        assertThat(tempResult.created(), is(oldCreated));
        assertTrue(tempResult.lastUpdated().isAfter(oldLastUpdated));
        session.getTransaction().commit();
        assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob2)));
    }
    // REPLACE BLOB
    // NOSONAR
    Thread.sleep(2);
    try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
        AssetDAO dao = session.access(TestAssetDAO.class);
        tempResult = dao.readPath(repositoryId, path).get();
        OffsetDateTime oldCreated = tempResult.created();
        OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
        assertThat(tempResult.blob().get(), sameBlob(assetBlob1));
        asset.setAssetBlob(assetBlob2);
        dao.updateAssetBlobLink(asset);
        tempResult = dao.readPath(repositoryId, path).get();
        assertTrue(tempResult.blob().isPresent());
        assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
        assertThat(tempResult, sameBlob(asset));
        assertThat(tempResult.created(), is(oldCreated));
        assertTrue(tempResult.lastUpdated().isAfter(oldLastUpdated));
        session.getTransaction().commit();
        assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1)));
    }
    // REPLACING WITH SAME BLOB DOESN'T UPDATE
    // NOSONAR
    Thread.sleep(2);
    try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
        AssetDAO dao = session.access(TestAssetDAO.class);
        tempResult = dao.readPath(repositoryId, path).get();
        OffsetDateTime oldCreated = tempResult.created();
        OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
        assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
        asset.setAssetBlob(assetBlob2);
        dao.updateAssetBlobLink(asset);
        tempResult = dao.readPath(repositoryId, path).get();
        assertTrue(tempResult.blob().isPresent());
        assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
        assertThat(tempResult, sameBlob(asset));
        assertThat(tempResult.created(), is(oldCreated));
        assertThat(tempResult.lastUpdated(), is(oldLastUpdated));
        session.getTransaction().commit();
        assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1)));
    }
    // DETACH BLOB
    // NOSONAR
    Thread.sleep(2);
    try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
        AssetDAO dao = session.access(TestAssetDAO.class);
        tempResult = dao.readPath(repositoryId, path).get();
        OffsetDateTime oldCreated = tempResult.created();
        OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
        assertThat(tempResult.blob().get(), sameBlob(assetBlob2));
        asset.setAssetBlob(null);
        dao.updateAssetBlobLink(asset);
        tempResult = dao.readPath(repositoryId, path).get();
        assertFalse(tempResult.blob().isPresent());
        assertThat(tempResult.created(), is(oldCreated));
        assertTrue(tempResult.lastUpdated().isAfter(oldLastUpdated));
        session.getTransaction().commit();
        assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1), sameBlob(assetBlob2)));
    }
    // DETACHING BLOB AGAIN DOESN'T UPDATE
    // NOSONAR
    Thread.sleep(2);
    try (DataSession<?> session = sessionRule.openSession(DEFAULT_DATASTORE_NAME)) {
        AssetDAO dao = session.access(TestAssetDAO.class);
        tempResult = dao.readPath(repositoryId, path).get();
        OffsetDateTime oldCreated = tempResult.created();
        OffsetDateTime oldLastUpdated = tempResult.lastUpdated();
        assertFalse(tempResult.blob().isPresent());
        asset.setAssetBlob(null);
        dao.updateAssetBlobLink(asset);
        tempResult = dao.readPath(repositoryId, path).get();
        assertFalse(tempResult.blob().isPresent());
        assertThat(tempResult.created(), is(oldCreated));
        assertThat(tempResult.lastUpdated(), is(oldLastUpdated));
        session.getTransaction().commit();
        assertThat(session.access(TestAssetBlobDAO.class).browseUnusedAssetBlobs(10, null), contains(sameBlob(assetBlob1), sameBlob(assetBlob2)));
    }
}
Also used : TestAssetDAO(org.sonatype.nexus.repository.content.store.example.TestAssetDAO) OffsetDateTime(java.time.OffsetDateTime) TestAssetBlobDAO(org.sonatype.nexus.repository.content.store.example.TestAssetBlobDAO) Asset(org.sonatype.nexus.repository.content.Asset) TestAssetDAO(org.sonatype.nexus.repository.content.store.example.TestAssetDAO) TestAssetData(org.sonatype.nexus.repository.content.store.example.TestAssetData) Test(org.junit.Test)

Aggregations

Asset (org.sonatype.nexus.repository.content.Asset)60 Test (org.junit.Test)34 Component (org.sonatype.nexus.repository.content.Component)20 AssetBlob (org.sonatype.nexus.repository.content.AssetBlob)17 Repository (org.sonatype.nexus.repository.Repository)16 OffsetDateTime (java.time.OffsetDateTime)14 BrowsePath (org.sonatype.nexus.repository.browse.node.BrowsePath)14 FluentAsset (org.sonatype.nexus.repository.content.fluent.FluentAsset)13 Optional (java.util.Optional)12 Named (javax.inject.Named)12 List (java.util.List)11 Inject (javax.inject.Inject)11 ContentFacet (org.sonatype.nexus.repository.content.facet.ContentFacet)11 TestAssetDAO (org.sonatype.nexus.repository.content.store.example.TestAssetDAO)10 Collections (java.util.Collections)9 Collectors (java.util.stream.Collectors)9 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)8 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 Map (java.util.Map)7