Java Code Examples for org.sonatype.nexus.repository.storage.Asset#markAsDownloaded()

The following examples show how to use org.sonatype.nexus.repository.storage.Asset#markAsDownloaded() . 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: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
@TransactionalTouchBlob
public Content get(final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();

  final Asset asset = findAsset(tx, path);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }

  final Blob blob = tx.requireBlob(asset.requireBlobRef());
  return toContent(asset, blob);
}
 
Example 2
Source File: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreMetadata
public Asset getOrCreateAsset(final String path,
                              final String group,
                              final String name,
                              final String version)
{
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());

  Component component = findComponent(tx, group, name, version);
  if (component == null) {
    component = tx.createComponent(bucket, format).group(group).name(name).version(version);
    tx.saveComponent(component);
  }

  Asset asset = findAsset(tx, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);
  }

  asset.markAsDownloaded();

  return asset;
}
 
Example 3
Source File: HelmHostedFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
@TransactionalTouchBlob
public Content get(final String path) {
  checkNotNull(path);
  StorageTx tx = UnitOfWork.currentTx();

  Optional<Asset> assetOpt = helmFacet.findAsset(tx, path);
  if (!assetOpt.isPresent()) {
    return null;
  }
  Asset asset = assetOpt.get();
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }

  return helmFacet.toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 4
Source File: HelmFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Save an asset and create blob.
 *
 * @return blob content
 */
@Override
public Content saveAsset(final StorageTx tx,
                         final Asset asset,
                         final Supplier<InputStream> contentSupplier,
                         @Nullable final String contentType,
                         @Nullable final AttributesMap contentAttributes) throws IOException
{
  Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes));
  AssetBlob assetBlob = tx.setBlob(
      asset, asset.name(), contentSupplier, HASH_ALGORITHMS, null, contentType, false
  );
  asset.markAsDownloaded();
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example 5
Source File: RFacetUtils.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Save an asset && create blob.
 *
 * @return blob content
 */
public static Content saveAsset(final StorageTx tx,
                                final Asset asset,
                                final Supplier<InputStream> contentSupplier,
                                final String contentType,
                                @Nullable final AttributesMap contentAttributes) throws IOException
{
  Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes));
  AssetBlob assetBlob = tx.setBlob(
      asset, asset.name(), contentSupplier, HASH_ALGORITHMS, null, contentType, false
  );

  asset.markAsDownloaded();
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example 6
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Save an asset and create a blob
 *
 * @return blob content
 */
private Content saveAsset(final StorageTx tx,
                          final Asset asset,
                          final Supplier<InputStream> contentSupplier,
                          final String contentType,
                          final AttributesMap contentAttributes,
                          final HashCode hash) throws IOException
{
  Content.applyToAsset(asset, maintainLastModified(asset, contentAttributes));
  AssetBlob assetBlob = tx.setBlob(
      asset, asset.name(), contentSupplier, HASH_ALGORITHMS, null, contentType, false
  );

  if (!hashVerifier.verify(hash, assetBlob.getHashes().get(MD5))) {
    return null;
  }
  asset.markAsDownloaded();
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example 7
Source File: ConanHostedFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@TransactionalTouchBlob
protected Content doGet(final String path) {
  checkNotNull(path);

  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findAsset(tx, tx.findBucket(getRepository()), path);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }
  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 8
Source File: DefaultComponentMetadataProducerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void defaultLastDownloadedWhenSingleAssetMarkedAsDownloaded() throws Exception {
  Bucket bucket = createBucket(REPO_NAME);
  Component component = createDetachedComponent(bucket, GROUP, NAME, VERSION);
  Asset expected = createDetachedAsset(bucket, NAME, component);
  expected.markAsDownloaded(standardHours(12));
  Iterable<Asset> assets = newArrayList(
      createDetachedAsset(bucket, "asset1", component),
      expected,
      createDetachedAsset(bucket, "asset2", component)
  );

  DateTime actual = underTest.lastDownloaded(assets).get();

  assertThat(actual, is(expected.lastDownloaded()));
}
 
Example 9
Source File: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreMetadata
public Asset getOrCreateAsset(final String path) {
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, format);
    asset.name(path);
  }

  asset.markAsDownloaded();

  return asset;
}
 
Example 10
Source File: P2FacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Save an asset && create blob.
 *
 * @return blob content
 */
public Content saveAsset(final StorageTx tx,
                         final Asset asset,
                         final Supplier<InputStream> contentSupplier,
                         final String contentType,
                         @Nullable final AttributesMap contentAttributes) throws IOException
{
  Content.applyToAsset(asset, Content.maintainLastModified(asset, contentAttributes));
  AssetBlob assetBlob = tx.setBlob(
      asset, asset.name(), contentSupplier, HASH_ALGORITHMS, null, contentType, false
  );
  asset.markAsDownloaded();
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example 11
Source File: P2ProxyFacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalTouchBlob
protected Content getAsset(final String name) {
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = facet(P2Facet.class).findAsset(tx, tx.findBucket(getRepository()), name);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }
  return facet(P2Facet.class).toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 12
Source File: HelmProxyFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalTouchBlob
protected Content getAsset(final String name) {
  StorageTx tx = UnitOfWork.currentTx();

  Optional<Asset> assetOpt = helmFacet.findAsset(tx, name);
  if (!assetOpt.isPresent()) {
    return null;
  }

  Asset asset = assetOpt.get();
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }
  return helmFacet.toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 13
Source File: RProxyFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalTouchBlob
protected Content getAsset(final String name) {
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findAsset(tx, tx.findBucket(getRepository()), name);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }
  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 14
Source File: RHostedFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalTouchBlob
public Content getStoredContent(final String contentPath) {
  checkNotNull(contentPath);
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findAsset(tx, tx.findBucket(getRepository()), contentPath);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }
  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 15
Source File: ConanHostedFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
private Content saveAsset(final StorageTx tx,
                          final Asset asset,
                          final Supplier<InputStream> contentSupplier,
                          final String contentType,
                          final AttributesMap contentAttributes) throws IOException
{
  Content.applyToAsset(asset, maintainLastModified(asset, contentAttributes));
  AssetBlob assetBlob = tx.setBlob(
      asset, asset.name(), contentSupplier, HASH_ALGORITHMS, null, contentType, false
  );

  asset.markAsDownloaded();
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example 16
Source File: RundeckMavenResource.java    From nexus3-rundeck-plugin with MIT License 4 votes vote down vote up
@GET
@Path("content")
public Response content(
        @QueryParam("r") String repositoryName,
        @QueryParam("g") String groupId,
        @QueryParam("a") String artifactId,
        @QueryParam("v") String version,
        @QueryParam("c") String classifier,
        @QueryParam("p") @DefaultValue("jar") String extension
) {


    // default version
    if ("LATEST".equals(version)) {
        version = null;
    }
    version = Optional.ofNullable(version).orElse(latestVersion(
            repositoryName, groupId, artifactId, classifier, extension
    ));

    // valid params
    if (isBlank(repositoryName) || isBlank(groupId) || isBlank(artifactId) || isBlank(version)) {
        return NOT_FOUND;
    }

    Repository repository = repositoryManager.get(repositoryName);
    if (null == repository || !repository.getFormat().getValue().equals("maven2")) {
        return NOT_FOUND;
    }

    StorageFacet facet = repository.facet(StorageFacet.class);
    Supplier<StorageTx> storageTxSupplier = facet.txSupplier();

    log.debug("rundeck download repository: {}", repository);
    final StorageTx tx = storageTxSupplier.get();
    tx.begin();
    Bucket bucket = tx.findBucket(repository);
    log.debug("rundeck download bucket: {}", bucket);

    if (null == bucket) {
        return commitAndReturn(NOT_FOUND, tx);
    }

    String fileName = artifactId + "-" + version + (isBlank(classifier) ? "" : ("-" + classifier)) + "." + extension;
    String path = groupId.replace(".", "/") +
            "/" + artifactId +
            "/" + version +
            "/" + fileName;
    Asset asset = tx.findAssetWithProperty("name", path, bucket);
    log.debug("rundeck download asset: {}", asset);
    if (null == asset) {
        return commitAndReturn(NOT_FOUND, tx);
    }
    asset.markAsDownloaded();
    tx.saveAsset(asset);
    Blob blob = tx.requireBlob(asset.requireBlobRef());
    Response.ResponseBuilder ok = Response.ok(blob.getInputStream());
    ok.header("Content-Type", blob.getHeaders().get("BlobStore.content-type"));
    ok.header("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    return commitAndReturn(ok.build(), tx);
}