Search in sources :

Example 1 with HashAlgorithm

use of org.sonatype.nexus.common.hash.HashAlgorithm 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 HashAlgorithm

use of org.sonatype.nexus.common.hash.HashAlgorithm in project nexus-public by sonatype.

the class Content method extractFromAsset.

/**
 * Extracts non-format specific content attributes into the passed in {@link AttributesMap} (usually originating from
 * {@link Content#getAttributes()}) from passed in {@link Asset} and format required hashes.
 */
public static void extractFromAsset(final Asset asset, final Iterable<HashAlgorithm> hashAlgorithms, final AttributesMap contentAttributes) {
    checkNotNull(asset);
    checkNotNull(hashAlgorithms);
    final NestedAttributesMap assetAttributes = asset.attributes().child(CONTENT);
    final DateTime lastModified = toDateTime(assetAttributes.get(P_LAST_MODIFIED, Date.class));
    final String etag = assetAttributes.get(P_ETAG, String.class);
    final Map<HashAlgorithm, HashCode> checksums = asset.getChecksums(hashAlgorithms);
    contentAttributes.set(Asset.class, asset);
    contentAttributes.set(Content.CONTENT_LAST_MODIFIED, lastModified);
    contentAttributes.set(Content.CONTENT_ETAG, etag);
    contentAttributes.set(Content.CONTENT_HASH_CODES_MAP, checksums);
    contentAttributes.set(CacheInfo.class, CacheInfo.extractFromAsset(asset));
}
Also used : HashCode(com.google.common.hash.HashCode) NestedAttributesMap(org.sonatype.nexus.common.collect.NestedAttributesMap) DateTime(org.joda.time.DateTime) DateHelper.toDateTime(org.sonatype.nexus.common.time.DateHelper.toDateTime) Date(java.util.Date) DateHelper.toDate(org.sonatype.nexus.common.time.DateHelper.toDate) HashAlgorithm(org.sonatype.nexus.common.hash.HashAlgorithm)

Example 3 with HashAlgorithm

use of org.sonatype.nexus.common.hash.HashAlgorithm in project nexus-public by sonatype.

the class StorageTxImpl method attachBlob.

@Override
@Guarded(by = ACTIVE)
public void attachBlob(final Asset asset, final AssetBlob assetBlob) {
    checkNotNull(asset);
    checkNotNull(assetBlob);
    checkArgument(!assetBlob.isAttached(), "Blob is already attached to an asset");
    final WritePolicy effectiveWritePolicy = writePolicySelector.select(asset, writePolicy);
    if (!effectiveWritePolicy.checkCreateAllowed()) {
        throw new IllegalOperationException("Repository is read only: " + repositoryName);
    }
    NestedAttributesMap checksums = asset.attributes().child(CHECKSUM);
    if (!isDuplicateBlob(asset, assetBlob, effectiveWritePolicy, checksums)) {
        maybeDeleteBlob(asset, assetBlob, effectiveWritePolicy);
        asset.blobRef(assetBlob.getBlobRef());
        asset.size(assetBlob.getSize());
        asset.contentType(assetBlob.getContentType());
        // Set attributes map to contain computed checksum metadata
        for (Entry<HashAlgorithm, HashCode> entry : assetBlob.getHashes().entrySet()) {
            HashAlgorithm algorithm = entry.getKey();
            HashCode checksum = entry.getValue();
            checksums.set(algorithm.name(), checksum.toString());
        }
        // Mark assets whose checksums were not verified locally, for possible later verification
        NestedAttributesMap provenance = asset.attributes().child(PROVENANCE);
        provenance.set(HASHES_NOT_VERIFIED, !assetBlob.getHashesVerified());
        Map<String, String> blobHeaders = assetBlob.getBlob().getHeaders();
        if (blobHeaders.containsKey(BlobStore.CREATED_BY_HEADER)) {
            asset.createdBy(blobHeaders.get(BlobStore.CREATED_BY_HEADER));
        }
        if (blobHeaders.containsKey(BlobStore.CREATED_BY_IP_HEADER)) {
            asset.createdByIp(blobHeaders.get(BlobStore.CREATED_BY_IP_HEADER));
        }
        assetBlob.setAttached(true);
    }
}
Also used : HashCode(com.google.common.hash.HashCode) IllegalOperationException(org.sonatype.nexus.repository.IllegalOperationException) NestedAttributesMap(org.sonatype.nexus.common.collect.NestedAttributesMap) WritePolicy(org.sonatype.nexus.repository.config.WritePolicy) HashAlgorithm(org.sonatype.nexus.common.hash.HashAlgorithm) Guarded(org.sonatype.nexus.common.stateguard.Guarded)

Example 4 with HashAlgorithm

use of org.sonatype.nexus.common.hash.HashAlgorithm in project nexus-public by sonatype.

the class StorageTxImpl method compareChecksums.

/**
 * Returns {@code true} when incoming hashes all match existing checksums.
 */
private boolean compareChecksums(final NestedAttributesMap checksums, final AssetBlob assetBlob) {
    for (Entry<HashAlgorithm, HashCode> entry : assetBlob.getHashes().entrySet()) {
        HashAlgorithm algorithm = entry.getKey();
        HashCode checksum = entry.getValue();
        if (!checksum.toString().equals(checksums.get(algorithm.name()))) {
            return false;
        }
    }
    return true;
}
Also used : HashCode(com.google.common.hash.HashCode) HashAlgorithm(org.sonatype.nexus.common.hash.HashAlgorithm)

Example 5 with HashAlgorithm

use of org.sonatype.nexus.common.hash.HashAlgorithm in project nexus-public by sonatype.

the class MavenContentGroupFacetImpl method cache.

/**
 * Attempts to cache the merged content, falling back to temporary uncached result if necessary.
 */
private Content cache(final MavenPath mavenPath, final TempBlob tempBlob, final String contentType) {
    Content content = null;
    try {
        content = new Content(getRepository().facet(MavenContentFacet.class).put(mavenPath, new BlobPayload(tempBlob.getBlob(), contentType)));
        maintainCacheInfo(content.getAttributes());
        mayAddETag(content.getAttributes(), tempBlob.getHashes());
        for (Entry<HashAlgorithm, HashCode> entry : tempBlob.getHashes().entrySet()) {
            getRepository().facet(MavenContentFacet.class).put(mavenPath.hash(entry.getKey()), new StringPayload(entry.getValue().toString(), TEXT_PLAIN));
        }
    } catch (Exception e) {
        log.warn("Problem caching merged content {} : {}", getRepository().getName(), mavenPath.getPath(), e);
    }
    getRepository().facet(ContentFacet.class).assets().path(prependIfMissing(mavenPath.getPath(), "/")).find().ifPresent(FluentAsset::markAsStale);
    return content;
}
Also used : HashCode(com.google.common.hash.HashCode) BlobPayload(org.sonatype.nexus.repository.view.payloads.BlobPayload) Content(org.sonatype.nexus.repository.view.Content) StringPayload(org.sonatype.nexus.repository.view.payloads.StringPayload) MavenContentFacet(org.sonatype.nexus.content.maven.MavenContentFacet) IOException(java.io.IOException) HashAlgorithm(org.sonatype.nexus.common.hash.HashAlgorithm) FluentAsset(org.sonatype.nexus.repository.content.fluent.FluentAsset)

Aggregations

HashAlgorithm (org.sonatype.nexus.common.hash.HashAlgorithm)22 HashCode (com.google.common.hash.HashCode)18 DateTime (org.joda.time.DateTime)8 Content (org.sonatype.nexus.repository.view.Content)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 UncheckedIOException (java.io.UncheckedIOException)4 AttributesMap (org.sonatype.nexus.common.collect.AttributesMap)4 HashType (org.sonatype.nexus.repository.maven.MavenPath.HashType)4 BufferedInputStream (java.io.BufferedInputStream)3 Path (java.nio.file.Path)3 NestedAttributesMap (org.sonatype.nexus.common.collect.NestedAttributesMap)3 OrientMavenFacet (org.sonatype.nexus.orient.maven.OrientMavenFacet)3 MavenPath (org.sonatype.nexus.repository.maven.MavenPath)3 File (java.io.File)2 Files (java.nio.file.Files)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2