Java Code Examples for org.sonatype.nexus.repository.storage.StorageTx#saveAsset()

The following examples show how to use org.sonatype.nexus.repository.storage.StorageTx#saveAsset() . 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: RProxyFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalTouchMetadata
public void setCacheInfo(final Content content, final CacheInfo cacheInfo) {
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = Content.findAsset(tx, tx.findBucket(getRepository()), content);
  if (asset == null) {
    log.debug(
        "Attempting to set cache info for non-existent R asset {}", content.getAttributes().require(Asset.class)
    );
    return;
  }

  log.debug("Updating cacheInfo of {} to {}", asset, cacheInfo);
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example 2
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset putFile(final StorageTx tx,
                      final MavenPath path,
                      final AssetBlob assetBlob,
                      @Nullable final AttributesMap contentAttributes)
    throws IOException
{
  final Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(path.getPath());
    asset.formatAttributes().set(P_ASSET_KIND, fileAssetKindFor(path));
  }

  putAssetPayload(tx, asset, assetBlob, contentAttributes);

  tx.saveAsset(asset);

  return asset;
}
 
Example 3
Source File: AptSnapshotFacetSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = {ONeedRetryException.class})
protected void createSnapshot(final String id, final Iterable<SnapshotItem> snapshots) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  StorageFacet storageFacet = facet(StorageFacet.class);
  Bucket bucket = tx.findBucket(getRepository());
  for (SnapshotItem item : snapshots) {
    String assetName = createAssetPath(id, item.specifier.path);
    Asset asset = tx.createAsset(bucket, getRepository().getFormat()).name(assetName);
    try (final TempBlob streamSupplier = storageFacet
        .createTempBlob(item.content.openInputStream(), FacetHelper.hashAlgorithms)) {
      AssetBlob blob = tx.createBlob(item.specifier.path, streamSupplier, FacetHelper.hashAlgorithms, null,
          item.specifier.role.getMimeType(), true);
      tx.attachBlob(asset, blob);
    }
    finally {
      item.content.close();
    }
    tx.saveAsset(asset);
  }
}
 
Example 4
Source File: P2FacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Asset findOrCreateAsset(final StorageTx tx,
                               final Component component,
                               final String path,
                               final P2Attributes attributes)
{
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);

    asset.formatAttributes().set(PLUGIN_NAME, attributes.getPluginName());
    asset.formatAttributes().set(P_ASSET_KIND, attributes.getAssetKind());
    tx.saveAsset(asset);
  }

  return asset;
}
 
Example 5
Source File: NpmFacetUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Saves the package root JSON content by persisting content into root asset's blob. It also removes some transient
 * fields from JSON document.
 */
static void savePackageRoot(final StorageTx tx,
                            final Asset packageRootAsset,
                            final NestedAttributesMap packageRoot) throws IOException
{
  packageRoot.remove(NpmMetadataUtils.META_ID);
  packageRoot.remove("_attachments");
  packageRootAsset.formatAttributes().set(
      NpmAttributes.P_NPM_LAST_MODIFIED, NpmMetadataUtils.maintainTime(packageRoot).toDate()
  );
  storeContent(
      tx,
      packageRootAsset,
      new StreamCopier<Supplier<InputStream>>(
          outputStream -> serialize(new OutputStreamWriter(outputStream, UTF_8), packageRoot),
          inputStream -> () -> inputStream).read(),
      AssetKind.PACKAGE_ROOT
  );
  tx.saveAsset(packageRootAsset);
}
 
Example 6
Source File: HelmFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
private Asset createAsset(
    final StorageTx tx,
    final String assetPath,
    final HelmAttributes helmAttributes,
    final AssetKind assetKind)
{
  checkNotNull(assetKind);

  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = CacheControllerHolder.METADATA.equals(assetKind.getCacheType())
      ? tx.createAsset(bucket, getRepository().getFormat())
      : tx.createAsset(bucket, findOrCreateComponent(tx, bucket, helmAttributes.getName(), helmAttributes.getVersion()));
  asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  helmAttributes.populate(asset.formatAttributes());
  asset.name(assetPath);
  tx.saveAsset(asset);
  return asset;
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
Source File: HelmRestoreFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalTouchBlob
public void restore(final AssetBlob assetBlob, final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  AssetKind assetKind = AssetKind.getAssetKindByFileName(path);
  HelmAttributes attributes = helmAttributeParser.getAttributes(assetKind, assetBlob.getBlob().getInputStream());
  Asset asset = helmFacet.findOrCreateAsset(tx, path, assetKind, attributes);
  tx.attachBlob(asset, assetBlob);
  Content.applyToAsset(asset, Content.maintainLastModified(asset, new AttributesMap()));
  tx.saveAsset(asset);
}
 
Example 13
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 14
Source File: P2ProxyFacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalTouchMetadata
public void setCacheInfo(final Content content, final CacheInfo cacheInfo) {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = Content.findAsset(tx, tx.findBucket(getRepository()), content);
  if (asset == null) {
    log.debug(
        "Attempting to set cache info for non-existent P2 asset {}", content.getAttributes().require(Asset.class)
    );
    return;
  }
  log.debug("Updating cacheInfo of {} to {}", asset, cacheInfo);
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example 15
Source File: NpmSearchIndexFacetCaching.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
private Content getSearchIndex(final boolean fromCacheOnly) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findRepositoryRootAsset(tx, bucket);
  if (asset == null) {
    if (fromCacheOnly) {
      return null;
    }
    log.debug("Building npm index for {}", getRepository().getName());
    asset = tx.createAsset(bucket, getRepository().getFormat())
        .name(NpmFacetUtils.REPOSITORY_ROOT_ASSET);
    final Path path = Files.createTempFile("npm-searchIndex", "json");
    try {
      Content content = buildIndex(tx, path);
      return saveRepositoryRoot(
          tx,
          asset,
          () -> {
            try {
              return content.openInputStream();
            }
            catch (IOException e) {
              throw new RuntimeException(e);
            }
          },
          content
      );
    }
    finally {
      Files.delete(path);
    }
  }
  else if (assetManager.maybeUpdateLastDownloaded(asset)) {
    tx.saveAsset(asset);
  }
  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 16
Source File: OrientMavenGroupFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tries to invalidate the current main cached asset for the given {@link MavenPath}.
 */
@Transactional(retryOn = ONeedRetryException.class, swallow = ORecordNotFoundException.class)
protected void doInvalidate(final MavenPath mavenPath) {
  StorageTx tx = UnitOfWork.currentTx();
  final Asset asset = findAsset(tx, tx.findBucket(getRepository()), mavenPath.main());
  if (asset != null && CacheInfo.invalidateAsset(asset)) {
    tx.saveAsset(asset);
  }
}
 
Example 17
Source File: AptProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = {ONeedRetryException.class})
protected void doIndicateVerified(final Content content, final CacheInfo cacheInfo, final String assetPath) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = Content.findAsset(tx, bucket, content);
  if (asset == null) {
    asset = tx.findAssetWithProperty(P_NAME, assetPath, bucket);
  }
  if (asset == null) {
    return;
  }
  CacheInfo.applyToAsset(asset, cacheInfo);
  tx.saveAsset(asset);
}
 
Example 18
Source File: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreBlob
protected Content doPutMetadata(final String path,
                                final TempBlob tempBlob,
                                final Payload payload,
                                final AssetKind assetKind)
    throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = getOrCreateAsset(path);

  asset.formatAttributes().set(P_ASSET_KIND, assetKind.toString());

  if (payload instanceof Content) {
    Content.applyToAsset(asset, Content.maintainLastModified(asset, ((Content) payload).getAttributes()));
  }

  AssetBlob assetBlob = tx.setBlob(
      asset,
      path,
      tempBlob,
      null,
      payload.getContentType(),
      false
  );

  tx.saveAsset(asset);

  return toContent(asset, assetBlob.getBlob());
}
 
Example 19
Source File: CondaFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
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);
  tx.saveAsset(asset);
  return toContent(asset, assetBlob.getBlob());
}
 
Example 20
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Asset putArtifact(final StorageTx tx,
                          final MavenPath path,
                          final AssetBlob assetBlob,
                          @Nullable final AttributesMap contentAttributes)
    throws IOException
{
  final Coordinates coordinates = checkNotNull(path.getCoordinates());
  final Bucket bucket = tx.findBucket(getRepository());
  Component component = findComponent(tx, getRepository(), path);
  boolean updatePomModel = false;
  if (component == null) {
    // Create and set top-level properties
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(coordinates.getGroupId())
        .name(coordinates.getArtifactId())
        .version(coordinates.getVersion());

    // Set format specific attributes
    final NestedAttributesMap componentAttributes = component.formatAttributes();
    componentAttributes.set(P_GROUP_ID, coordinates.getGroupId());
    componentAttributes.set(P_ARTIFACT_ID, coordinates.getArtifactId());
    componentAttributes.set(P_VERSION, coordinates.getVersion());
    componentAttributes.set(P_BASE_VERSION, coordinates.getBaseVersion());
    if (path.isPom()) {
      fillInFromModel(path, assetBlob, component.formatAttributes());
    }
    tx.saveComponent(component);
  }
  else if (path.isPom()) {
    // reduce copying by deferring update of pom.xml attributes (which involves reading the blob)
    // until after putAssetPayload, in case the blob is a duplicate and the model has not changed
    updatePomModel = true;
  }

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

    asset.name(path.getPath());

    final NestedAttributesMap assetAttributes = asset.formatAttributes();
    assetAttributes.set(P_GROUP_ID, coordinates.getGroupId());
    assetAttributes.set(P_ARTIFACT_ID, coordinates.getArtifactId());
    assetAttributes.set(P_VERSION, coordinates.getVersion());
    assetAttributes.set(P_BASE_VERSION, coordinates.getBaseVersion());
    assetAttributes.set(P_CLASSIFIER, coordinates.getClassifier());
    assetAttributes.set(P_EXTENSION, coordinates.getExtension());
    assetAttributes.set(
        P_ASSET_KIND,
        path.isSubordinate() ? AssetKind.ARTIFACT_SUBORDINATE.name() : AssetKind.ARTIFACT.name()
    );
  }

  putAssetPayload(tx, asset, assetBlob, contentAttributes);

  tx.saveAsset(asset);

  // avoid re-reading pom.xml if it's a duplicate of the old asset
  if (updatePomModel && !assetBlob.isDuplicate()) {
    fillInFromModel(path, assetBlob, component.formatAttributes());
    tx.saveComponent(component);
  }

  return asset;
}