Search in sources :

Example 1 with FluentAsset

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

the class DatastoreBlobstoreRestoreTestHelper method assetMatch.

private static void assetMatch(final Optional<FluentAsset> asset, final BlobStore blobStore) {
    assertTrue(asset.isPresent());
    assertTrue(asset.get().blob().isPresent());
    AssetBlob assetBlob = asset.orElseThrow(AssertionError::new).blob().orElseThrow(AssertionError::new);
    Blob blob = blobStore.get(assetBlob.blobRef().getBlobId());
    assertThat(blob, notNullValue());
    assertThat(asset.map(FluentAsset::path).orElse("MISSING_FLUENT_ASSET"), equalTo(blob.getHeaders().get(BlobStore.BLOB_NAME_HEADER)));
    assertThat(assetBlob.createdBy().orElse("MISSING_ASSET_BLOB"), equalTo(blob.getHeaders().get(BlobStore.CREATED_BY_HEADER)));
    assertThat(assetBlob.createdByIp().orElse("MISSING_CREATED_BY"), equalTo(blob.getHeaders().get(BlobStore.CREATED_BY_IP_HEADER)));
    assertThat(assetBlob.contentType(), equalTo(blob.getHeaders().get(BlobStore.CONTENT_TYPE_HEADER)));
    assertThat(assetBlob.checksums().get(SHA1.name()), equalTo(blob.getMetrics().getSha1Hash()));
    assertThat(assetBlob.blobSize(), equalTo(blob.getMetrics().getContentSize()));
}
Also used : AssetBlob(org.sonatype.nexus.repository.content.AssetBlob) Blob(org.sonatype.nexus.blobstore.api.Blob) AssetBlob(org.sonatype.nexus.repository.content.AssetBlob) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset)

Example 2 with FluentAsset

use of org.sonatype.nexus.repository.content.fluent.FluentAsset 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 3 with FluentAsset

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

the class DatastoreDeadBlobFinderTest method createAsset.

private FluentAsset createAsset(final AssetBlob assetBlob) {
    FluentAsset asset = mock(FluentAsset.class);
    when(asset.path()).thenReturn("foo");
    when(assetBlob.checksums()).thenReturn(Collections.singletonMap(HashAlgorithm.SHA1.name(), "1234"));
    when(assetBlob.blobRef()).thenReturn(blobRef);
    when(asset.blob()).thenReturn(Optional.of(assetBlob));
    return asset;
}
Also used : FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset)

Example 4 with FluentAsset

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

the class DatastoreDeadBlobFinderTest method anAssetBlobCanBeDeletedWhileTheSystemIsInspected.

@Test
public void anAssetBlobCanBeDeletedWhileTheSystemIsInspected() {
    AssetBlob missingAssetBlob = mockAssetBlob(mock(AssetBlob.class));
    // first pass we have a missing blobRef
    when(asset.blob()).thenReturn(Optional.of(missingAssetBlob));
    FluentAsset reloadedAsset = createAsset(assetBlob);
    // second pass the blobRef is there but file does not exist
    Blob reloadedBlob = mock(Blob.class);
    when(reloadedBlob.getMetrics()).thenReturn(blobMetrics);
    BlobId missingBlobId = reloadedAsset.blob().get().blobRef().getBlobId();
    when(blobStore.get(missingBlobId)).thenReturn(reloadedBlob);
    mockAssetBrowse();
    mockAssetReload(reloadedAsset);
    when(reloadedBlob.getMetrics()).thenReturn(blobMetrics);
    when(reloadedBlob.getInputStream()).thenThrow(new BlobStoreException("Blob has been deleted", new BlobId("foo")));
    List<DeadBlobResult<Asset>> result = deadBlobFinder.find(repository, true);
    assertThat(result, hasSize(1));
    assertThat(result.get(0).getResultState(), is(DELETED));
}
Also used : AssetBlob(org.sonatype.nexus.repository.content.AssetBlob) Blob(org.sonatype.nexus.blobstore.api.Blob) AssetBlob(org.sonatype.nexus.repository.content.AssetBlob) DeadBlobResult(org.sonatype.nexus.repository.tools.DeadBlobResult) BlobId(org.sonatype.nexus.blobstore.api.BlobId) BlobStoreException(org.sonatype.nexus.blobstore.api.BlobStoreException) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset) Test(org.junit.Test)

Example 5 with FluentAsset

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

the class DatastoreOrphanedBlobFinderTest method setupOrphanedBlob.

private void setupOrphanedBlob(final BlobStore blobStore, final boolean deleted) {
    when(blobStore.getBlobIdStream()).thenAnswer(i -> Stream.of(new BlobId(ORPHANED_BLOB_ID), new BlobId(USED_BLOB_ID)));
    when(blobStore.getBlobAttributes(new BlobId(ORPHANED_BLOB_ID))).thenReturn(new TestBlobAttributes(deleted));
    when(blobStore.getBlobAttributes(new BlobId(USED_BLOB_ID))).thenReturn(new TestBlobAttributes(deleted));
    when(blobStore.getBlobStoreConfiguration()).thenReturn(blobStoreConfiguration);
    FluentAsset asset = buildAssetWithBlobId(USED_BLOB_ID);
    setupTransactionToFindAsset(asset);
}
Also used : BlobId(org.sonatype.nexus.blobstore.api.BlobId) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset)

Aggregations

FluentAsset (org.sonatype.nexus.repository.content.fluent.FluentAsset)55 AssetBlob (org.sonatype.nexus.repository.content.AssetBlob)16 Test (org.junit.Test)15 ContentFacet (org.sonatype.nexus.repository.content.facet.ContentFacet)12 IOException (java.io.IOException)11 Map (java.util.Map)11 Asset (org.sonatype.nexus.repository.content.Asset)10 FluentComponent (org.sonatype.nexus.repository.content.fluent.FluentComponent)10 ArrayList (java.util.ArrayList)7 Collections (java.util.Collections)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Named (javax.inject.Named)7 BlobId (org.sonatype.nexus.blobstore.api.BlobId)7 Collection (java.util.Collection)6 HashMap (java.util.HashMap)6 Optional (java.util.Optional)6 Date (java.util.Date)5 Set (java.util.Set)5 MavenContentFacet (org.sonatype.nexus.content.maven.MavenContentFacet)5