org.sonatype.nexus.common.hash.HashAlgorithm Java Examples

The following examples show how to use org.sonatype.nexus.common.hash.HashAlgorithm. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlobTx.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private PrefetchedAssetBlob createPrefetchedAssetBlob(final Blob blob,
                                                      final Map<HashAlgorithm, HashCode> hashes,
                                                      final boolean hashesVerified,
                                                      final String contentType)
{
  PrefetchedAssetBlob assetBlob = new PrefetchedAssetBlob(
      nodeAccess,
      blobStore,
      blob,
      contentType,
      hashes,
      hashesVerified);

  newlyCreatedBlobs.add(assetBlob);
  return assetBlob;
}
 
Example #2
Source File: OrientNpmHostedFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Asset putPackage(final Map<String, Object> packageJson, final TempBlob tempBlob) throws IOException {
  checkNotNull(packageJson);
  checkNotNull(tempBlob);

  log.debug("Storing package: {}", packageJson);

  checkNotNull(packageJson.get(NpmAttributes.P_NAME), "Uploaded package is invalid, or is missing package.json");

  NestedAttributesMap metadata = createFullPackageMetadata(
      new NestedAttributesMap("metadata", packageJson),
      getRepository().getName(),
      tempBlob.getHashes().get(HashAlgorithm.SHA1).toString(),
      null,
      extractAlwaysPackageVersion);

  NpmPackageId packageId = NpmPackageId.parse((String) metadata.get(NpmAttributes.P_NAME));

  return putPackage(packageId, metadata, tempBlob);
}
 
Example #3
Source File: AssetXOBuilderTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void setup() {
  Map<String,Object> checksum = Maps.newHashMap(ImmutableMap.of(HashAlgorithm.SHA1.name(), "87acec17cd9dcd20a716cc2cf67417b71c8a7016"));

  when(assetOne.name()).thenReturn("nameOne");
  when(assetOne.getEntityMetadata()).thenReturn(assetOneEntityMetadata);
  when(assetOne.attributes()).thenReturn(new NestedAttributesMap(Asset.CHECKSUM, checksum));

  when(assetOneORID.toString()).thenReturn("assetOneORID");

  when(assetOneEntityMetadata.getId()).thenReturn(assetOneEntityId);
  when(assetOneEntityId.getValue()).thenReturn("assetOne");

  when(repository.getName()).thenReturn("maven-releases");
  when(repository.getUrl()).thenReturn("http://localhost:8081/repository/maven-releases");
  when(repository.getFormat()).thenReturn(new Format("maven2") {});
}
 
Example #4
Source File: FluentBlobsImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public TempBlob ingest(final InputStream in,
                       @Nullable final String contentType,
                       final Iterable<HashAlgorithm> hashing)
{
  Optional<ClientInfo> clientInfo = facet.clientInfo();

  Builder<String, String> tempHeaders = ImmutableMap.builder();
  tempHeaders.put(TEMPORARY_BLOB_HEADER, "");
  tempHeaders.put(REPO_NAME_HEADER, facet.repository().getName());
  tempHeaders.put(BLOB_NAME_HEADER, "temp");
  tempHeaders.put(CREATED_BY_HEADER, clientInfo.map(ClientInfo::getUserid).orElse("system"));
  tempHeaders.put(CREATED_BY_IP_HEADER, clientInfo.map(ClientInfo::getRemoteIP).orElse("system"));
  tempHeaders.put(CONTENT_TYPE_HEADER, ofNullable(contentType).orElse(APPLICATION_OCTET_STREAM));

  MultiHashingInputStream hashingStream = new MultiHashingInputStream(hashing, in);
  Blob blob = facet.stores().blobStore.create(hashingStream, tempHeaders.build());

  return new TempBlob(blob, hashingStream.hashes(), true, facet.stores().blobStore);
}
 
Example #5
Source File: BlobTx.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private AssetBlob createAssetBlob(final Function<BlobStore, Blob> blobFunction,
                                  final Map<HashAlgorithm, HashCode> hashes,
                                  final boolean hashesVerified,
                                  final String contentType)
{
  AssetBlob assetBlob = new AssetBlob(
      nodeAccess,
      blobStore,
      blobFunction,
      contentType,
      hashes,
      hashesVerified);

  newlyCreatedBlobs.add(assetBlob);
  return assetBlob;
}
 
Example #6
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content doPut(final MavenPath path,
                        final Path sourceFile,
                        final String contentType,
                        final AttributesMap contentAttributes,
                        final Map<HashAlgorithm, HashCode> hashes,
                        final long size)
    throws IOException
{
  final StorageTx tx = UnitOfWork.currentTx();

  final AssetBlob assetBlob = tx.createBlob(
      path.getPath(),
      sourceFile,
      hashes,
      null,
      contentType,
      size
  );

  return doPutAssetBlob(path, contentAttributes, tx, assetBlob);
}
 
Example #7
Source File: OrientMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Writes passed in metadata as XML.
 */
public static void write(final Repository repository, final MavenPath mavenPath, final Metadata metadata)
    throws IOException
{
  MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  MavenModels.writeMetadata(buffer, metadata);
  mavenFacet.put(mavenPath, new BytesPayload(buffer.toByteArray(),
      MavenMimeRulesSource.METADATA_TYPE));
  final Map<HashAlgorithm, HashCode> hashCodes = mavenFacet.get(mavenPath).getAttributes()
      .require(Content.CONTENT_HASH_CODES_MAP, Content.T_CONTENT_HASH_CODES_MAP);
  checkState(hashCodes != null, "hashCodes");
  for (HashType hashType : HashType.values()) {
    MavenPath checksumPath = mavenPath.hash(hashType);
    HashCode hashCode = hashCodes.get(hashType.getHashAlgorithm());
    checkState(hashCode != null, "hashCode: type=%s", hashType);
    mavenFacet.put(checksumPath, new StringPayload(hashCode.toString(), Constants.CHECKSUM_CONTENT_TYPE));
  }
}
 
Example #8
Source File: DatastoreDeadBlobFinderTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void commonBlobMock(
    final int passes,
    final InputStream stream,
    final BlobMetrics blobMetrics,
    final String sha1)
{
  when(blobRef.getStore()).thenReturn("my-blobstore");

  BlobId blobId = new BlobId("blobId");
  when(blobRef.getBlobId()).thenReturn(blobId);
  when(blobStore.get(blobId)).thenReturn(blob);
  when(assetBlob.checksums()).thenReturn(Collections.singletonMap(HashAlgorithm.SHA1.name(), sha1));
  when(blob.getMetrics()).thenReturn(blobMetrics);
  when(blob.getInputStream()).thenReturn(stream);

}
 
Example #9
Source File: PurgeUnusedSnapshotsFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset createMetadataAsset(String name, String cacheToken) {
  Asset asset = new Asset();
  asset.contentType(TEXT_XML);
  asset.name(name);
  asset.format(Maven2Format.NAME);
  asset.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>()));
  asset.formatAttributes().set(P_ASSET_KIND, REPOSITORY_METADATA.name());
  asset.attributes().child(CHECKSUM).set(HashType.SHA1.getExt(),
      HashAlgorithm.SHA1.function().hashString("foobar", StandardCharsets.UTF_8).toString());
  asset.attributes().child(CHECKSUM).set(HashType.MD5.getExt(),
      HashAlgorithm.MD5.function().hashString("foobar", StandardCharsets.UTF_8).toString());
  asset.attributes().child(CONTENT).set(P_LAST_MODIFIED, new Date());
  asset.attributes().child(CONTENT).set(P_ETAG, "ETAG");
  asset.attributes().child(CACHE).set(LAST_VERIFIED, new Date());
  asset.attributes().child(CACHE).set(CACHE_TOKEN, cacheToken);
  asset.blobRef(new BlobRef("node", "store", "blobid"));
  return asset;
}
 
Example #10
Source File: MavenFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset createMetadataAsset(String name, String cacheToken) {
  Asset asset = new Asset();
  asset.contentType(TEXT_XML);
  asset.name(name);
  asset.format(Maven2Format.NAME);
  asset.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>()));
  asset.formatAttributes().set(P_ASSET_KIND, REPOSITORY_METADATA.name());
  asset.attributes().child(CHECKSUM).set(HashType.SHA1.getExt(),
      HashAlgorithm.SHA1.function().hashString("foobar", StandardCharsets.UTF_8).toString());
  asset.attributes().child(CHECKSUM).set(HashType.MD5.getExt(),
      HashAlgorithm.MD5.function().hashString("foobar", StandardCharsets.UTF_8).toString());
  asset.attributes().child(CONTENT).set(P_LAST_MODIFIED, new Date());
  asset.attributes().child(CONTENT).set(P_ETAG, "ETAG");
  asset.attributes().child(CACHE).set(LAST_VERIFIED, new Date());
  asset.attributes().child(CACHE).set(CACHE_TOKEN, cacheToken);
  asset.blobRef(new BlobRef("node", "store", "blobid"));
  return asset;
}
 
Example #11
Source File: DatastoreDeadBlobFinder.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Verify that the Blob exists and is in agreement with the stored Asset metadata.;
 */
private void verifyBlob(final Blob blob, final Asset asset) throws MismatchedSHA1Exception, BlobUnavilableException, IOException {
  BlobMetrics metrics = blob.getMetrics();

  String assetChecksum =
      asset.blob().map(AssetBlob::checksums).map(checksums -> checksums.get(HashAlgorithm.SHA1.name())).orElse(null);
  if (!metrics.getSha1Hash().equals(assetChecksum)) {
    throw new MismatchedSHA1Exception();
  }

  try (InputStream blobstream = blob.getInputStream()) {
    if (metrics.getContentSize() > 0 && blobstream.available() == 0) {
      throw new BlobUnavilableException();
    }
  }
}
 
Example #12
Source File: Content.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #13
Source File: FluentAssetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private AssetBlobData createAssetBlob(final BlobRef blobRef,
                                      final Blob blob,
                                      final Map<HashAlgorithm, HashCode> checksums)
{
  BlobMetrics metrics = blob.getMetrics();
  Map<String, String> headers = blob.getHeaders();

  AssetBlobData assetBlob = new AssetBlobData();
  assetBlob.setBlobRef(blobRef);
  assetBlob.setBlobSize(metrics.getContentSize());
  assetBlob.setContentType(headers.get(CONTENT_TYPE_HEADER));

  assetBlob.setChecksums(checksums.entrySet().stream().collect(
      toImmutableMap(
          e -> e.getKey().name(),
          e -> e.getValue().toString())));

  assetBlob.setBlobCreated(toOffsetDateTime(metrics.getCreationTime()));
  assetBlob.setCreatedBy(headers.get(CREATED_BY_HEADER));
  assetBlob.setCreatedByIp(headers.get(CREATED_BY_IP_HEADER));

  facet.stores().assetBlobStore.createAssetBlob(assetBlob);

  return assetBlob;
}
 
Example #14
Source File: StorageTxImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@Guarded(by = ACTIVE)
public AssetBlob createBlob(final String blobName,
                            final Supplier<InputStream> streamSupplier,
                            final Iterable<HashAlgorithm> hashAlgorithms,
                            @Nullable final Map<String, String> headers,
                            @Nullable final String declaredContentType,
                            final boolean skipContentVerification) throws IOException
{
  checkNotNull(blobName);
  checkNotNull(streamSupplier);
  checkNotNull(hashAlgorithms);

  if (!writePolicy.checkCreateAllowed()) {
    throw new IllegalOperationException("Repository is read only: " + repositoryName);
  }

  Map<String, String> storageHeadersMap = buildStorageHeaders(blobName, streamSupplier, headers, declaredContentType,
      skipContentVerification);
  return blobTx.create(
      streamSupplier.get(),
      storageHeadersMap,
      hashAlgorithms,
      storageHeadersMap.get(BlobStore.CONTENT_TYPE_HEADER)
  );
}
 
Example #15
Source File: StorageTxImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #16
Source File: TembBlobFactoryImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TempBlob create(final Repository repository,
                       final InputStream inputStream,
                       final Iterable<HashAlgorithm> hashAlgorithms)
{
  return repository.facet(ContentFacet.class).blobs().ingest(inputStream, null, hashAlgorithms);
}
 
Example #17
Source File: StorageTxImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = ACTIVE)
public AssetBlob createBlob(final String blobName,
                            final Path sourceFile,
                            final Map<HashAlgorithm, HashCode> hashes,
                            @Nullable final Map<String, String> headers,
                            final String declaredContentType,
                            final long size) throws IOException
{
  checkNotNull(blobName);
  checkNotNull(sourceFile);
  checkNotNull(hashes);
  checkArgument(!Strings2.isBlank(declaredContentType), "no declaredContentType provided");

  if (!writePolicy.checkCreateAllowed()) {
    throw new IllegalOperationException("Repository is read only: " + repositoryName);
  }

  Map<String, String> storageHeaders = buildStorageHeaders(blobName, null, headers, declaredContentType, true);
  return blobTx.createByHardLinking(
      sourceFile,
      storageHeaders,
      hashes,
      declaredContentType,
      size
  );
}
 
Example #18
Source File: PrefetchedAssetBlob.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public PrefetchedAssetBlob(final NodeAccess nodeAccess,
                           final BlobStore blobStore,
                           final Blob blob,
                           final String contentType,
                           final Map<HashAlgorithm, HashCode> hashes,
                           final boolean hashesVerified)
{
  super(nodeAccess, blobStore, store -> blob, contentType, hashes, hashesVerified);
  /*
   * Pre-fetched blobs are stored in the blob-store but when a duplicate is encountered it is not used meaning it needs
   * to be cleaned up. Because it hasn't been ingested the cleanup fails on BlobTx.commit, this method ingests the blob
   * to ensure that it is correctly deleted. We use this class so that we don't eagerly ingest ALL blobs.
   */
  getBlob();
}
 
Example #19
Source File: StorageFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TempBlob createTempBlob(final InputStream inputStream, final Iterable<HashAlgorithm> hashAlgorithms) {
  BlobStore blobStore = checkNotNull(blobStoreManager.get(config.blobStoreName));
  MultiHashingInputStream hashingStream = new MultiHashingInputStream(hashAlgorithms, inputStream);
  Blob blob = blobStore.create(hashingStream,
      ImmutableMap.of(
          BlobStore.BLOB_NAME_HEADER, "temp",
          BlobStore.CREATED_BY_HEADER, createdBy(),
          BlobStore.CREATED_BY_IP_HEADER, createdByIp(),
          BlobStore.TEMPORARY_BLOB_HEADER, ""));
  return new TempBlob(blob, hashingStream.hashes(), true, blobStore);
}
 
Example #20
Source File: TembBlobFactoryImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TempBlob create(final Repository repository,
                       final Payload payload,
                       final Iterable<HashAlgorithm> hashAlgorithms)
{
  return repository.facet(ContentFacet.class).blobs().ingest(payload, hashAlgorithms);
}
 
Example #21
Source File: TempBlobFactoryImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TempBlob create(final Repository repository,
                       final Payload payload,
                       final Iterable<HashAlgorithm> hashAlgorithms)
{
  return repository.facet(StorageFacet.class).createTempBlob(payload, hashAlgorithms);
}
 
Example #22
Source File: BaseRestoreBlobStrategy.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreMetadata
protected void doCreateAssetFromBlob(final RestoreBlobData blobData,
                                     final T restoreData,
                                     final Blob blob) throws IOException
{
  List<HashAlgorithm> hashTypes = getHashAlgorithms();

  AssetBlob assetBlob = new AssetBlob(nodeAccess,
      blobStoreManager.get(blobData.getBlobStoreName()),
      blobStore -> blob,
      blobData.getProperty(HEADER_PREFIX + CONTENT_TYPE_HEADER),
      hash(hashTypes, blob.getInputStream()), true);

  createAssetFromBlob(assetBlob, restoreData);
}
 
Example #23
Source File: StorageTxImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = ACTIVE)
public AssetBlob setBlob(final Asset asset,
                         final String blobName,
                         final Path sourceFile,
                         final Map<HashAlgorithm, HashCode> hashes,
                         @Nullable final Map<String, String> headers,
                         final String declaredContentType,
                         final long size) throws IOException
{
  checkNotNull(asset);
  checkArgument(!Strings2.isBlank(declaredContentType), "no declaredContentType provided");

  // Enforce write policy ahead, as we have asset here
  BlobRef oldBlobRef = asset.blobRef();
  if (oldBlobRef != null) {
    if (!writePolicySelector.select(asset, writePolicy).checkUpdateAllowed()) {
      throw new IllegalOperationException( "Repository does not allow updating assets: " + repositoryName);
    }
  }
  final AssetBlob assetBlob = createBlob(
      blobName,
      sourceFile,
      hashes,
      headers,
      declaredContentType,
      size
  );
  attachBlob(asset, assetBlob);
  return assetBlob;
}
 
Example #24
Source File: AptHostedFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void addSignatureItem(final StringBuilder builder, final HashAlgorithm algo, final Content content, final String filename) {
  Map<HashAlgorithm, HashCode> hashMap = content.getAttributes().get(Content.CONTENT_HASH_CODES_MAP,
      Content.T_CONTENT_HASH_CODES_MAP);
  builder.append("\n ");
  builder.append(hashMap.get(algo).toString());
  builder.append(" ");
  builder.append(content.getSize());
  builder.append(" ");
  builder.append(filename);
}
 
Example #25
Source File: AptHostedFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private String buildIndexSection(final ControlFile cf, final long size, final Map<HashAlgorithm, HashCode> hashes, final String assetPath) {
  Paragraph modified = cf.getParagraphs().get(0)
      .withFields(Arrays.asList(
          new ControlFile.ControlField("Filename", assetPath),
          new ControlFile.ControlField("Size", Long.toString(size)),
          new ControlFile.ControlField("MD5Sum", hashes.get(MD5).toString()),
          new ControlFile.ControlField("SHA1", hashes.get(SHA1).toString()),
          new ControlFile.ControlField("SHA256", hashes.get(SHA256).toString())));
  return modified.toString();
}
 
Example #26
Source File: AptHostedFacet.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private void addSignatureItem(StringBuilder builder, HashAlgorithm algo, Content content, String filename) {
  Map<HashAlgorithm, HashCode> hashMap = content.getAttributes().get(Content.CONTENT_HASH_CODES_MAP,
      Content.T_CONTENT_HASH_CODES_MAP);
  builder.append("\n ");
  builder.append(hashMap.get(algo).toString());
  builder.append(" ");
  builder.append(content.getSize());
  builder.append(" ");
  builder.append(filename);
}
 
Example #27
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Before
public void setup() throws IOException {
  when(versionPolicyValidator.validArtifactPath(any(), any())).thenReturn(true);
  when(contentPermissionChecker.isPermitted(eq(REPO_NAME), eq(Maven2Format.NAME), eq(BreadActions.EDIT), any()))
      .thenReturn(true);

  when(mavenPomGenerator.generatePom(any(), any(), any(), any())).thenReturn("<project/>");

  Maven2MavenPathParser pathParser = new Maven2MavenPathParser();
  underTest = new MavenUploadHandler(pathParser, new OrientMavenVariableResolverAdapter(pathParser),
      contentPermissionChecker, versionPolicyValidator, mavenPomGenerator, emptySet());

  when(repository.getName()).thenReturn(REPO_NAME);
  when(repository.getFormat()).thenReturn(new Maven2Format());
  when(repository.facet(MavenFacet.class)).thenReturn(mavenFacet);
  when(repository.facet(MavenHostedFacet.class)).thenReturn(mavenHostedFacet);

  StorageFacet storageFacet = mock(StorageFacet.class);
  when(storageFacet.txSupplier()).thenReturn(() -> storageTx);
  when(repository.facet(StorageFacet.class)).thenReturn(storageFacet);
  when(storageFacet.createTempBlob(any(Payload.class), any())).thenReturn(tempBlob);

  when(mavenFacet.getVersionPolicy()).thenReturn(VersionPolicy.RELEASE);

  Content content = mock(Content.class);
  AttributesMap attributesMap = mock(AttributesMap.class);
  Asset assetPayload = mock(Asset.class);
  when(assetPayload.componentId()).thenReturn(new DetachedEntityId("nuId"));
  when(attributesMap.get(Asset.class)).thenReturn(assetPayload);
  when(attributesMap.require(eq(Content.CONTENT_LAST_MODIFIED), eq(DateTime.class))).thenReturn(DateTime.now());
  Map<HashAlgorithm, HashCode> checksums = Collections.singletonMap(
      HashAlgorithm.SHA1,
      HashCode.fromString("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
  when(attributesMap.require(eq(Content.CONTENT_HASH_CODES_MAP), eq(Content.T_CONTENT_HASH_CODES_MAP)))
      .thenReturn(checksums);
  when(content.getAttributes()).thenReturn(attributesMap);
  when(mavenFacet.put(any(), any())).thenReturn(content);
}
 
Example #28
Source File: ContentTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private NestedAttributesMap nestedAttributesMap() {
  NestedAttributesMap attributes = new NestedAttributesMap(P_ATTRIBUTES, Maps.<String, Object>newHashMap());
  attributes.child(CHECKSUM).set(
      HashAlgorithm.SHA1.name(),
      HashAlgorithm.SHA1.function().hashString("foobar", StandardCharsets.UTF_8).toString()
  );
  return attributes;
}
 
Example #29
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalTouchBlob
public Content getPackage(final String packagePath) {
  checkNotNull(packagePath);
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findAsset(tx, tx.findBucket(getRepository()), packagePath);
  if (asset == null) {
    return null;
  }
  Content content = toContent(asset, tx.requireBlob(asset.requireBlobRef()));
  mayAddEtag(content.getAttributes(), asset.getChecksum(HashAlgorithm.SHA1));
  return content;
}
 
Example #30
Source File: Asset.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Extract checksums of asset blob if checksums are present in asset attributes.
 */
public Map<HashAlgorithm, HashCode> getChecksums(final Iterable<HashAlgorithm> hashAlgorithms)
{
  final NestedAttributesMap checksumAttributes = attributes().child(CHECKSUM);
  final Map<HashAlgorithm, HashCode> hashCodes = Maps.newHashMap();
  for (HashAlgorithm algorithm : hashAlgorithms) {
    final HashCode hashCode = HashCode.fromString(checksumAttributes.require(algorithm.name(), String.class));
    hashCodes.put(algorithm, hashCode);
  }
  return hashCodes;
}