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);
}
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));
}
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);
}
}
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;
}
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;
}
Aggregations