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

The following examples show how to use org.sonatype.nexus.repository.storage.StorageTx#findBucket() . 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: CondaProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content findOrCreateAsset(final TempBlob tempBlob,
                                    final Content content,
                                    final AssetKind assetKind,
                                    final String assetPath,
                                    final Component component) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = getCondaFacet().findAsset(tx, bucket, assetPath);

  if (asset == null) {
    if (ARCH_TAR_PACKAGE.equals(assetKind) || ARCH_CONDA_PACKAGE.equals(assetKind)) {
      asset = tx.createAsset(bucket, component);
    }
    else {
      asset = tx.createAsset(bucket, getRepository().getFormat());
    }
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }

  return getCondaFacet().saveAsset(tx, asset, tempBlob, content);
}
 
Example 2
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content doSaveMetadata(final TempBlob metadataContent,
                                 final Payload payload,
                                 final AssetKind assetKind,
                                 final ConanCoords coords) throws IOException
{
  HashCode hash = null;
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = getOrCreateComponent(tx, bucket, coords);

  String assetPath = getProxyAssetPath(coords, assetKind);
  Asset asset = findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
    hash = hashVerifier.lookupHashFromAsset(tx, bucket, assetPath);
  }
  else if (!asset.componentId().equals(EntityHelper.id(component))) {
    asset.componentId(EntityHelper.id(component));
  }
  return saveAsset(tx, asset, metadataContent, payload, hash);
}
 
Example 3
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 4
Source File: AptSnapshotFacetSupport.java    From nexus-repository-apt with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = { ONeedRetryException.class })
@Override
public void createSnapshot(String id, SnapshotComponentSelector selector) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  StorageFacet storageFacet = facet(StorageFacet.class);
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.createComponent(bucket, getRepository().getFormat()).name(id);
  tx.saveComponent(component);
  for (SnapshotItem item : collectSnapshotItems(selector)) {
    String assetName = createAssetPath(id, item.specifier.path);
    Asset asset = tx.createAsset(bucket, component).name(assetName);
    try (final TempBlob streamSupplier = storageFacet.createTempBlob(item.content.openInputStream(), FacetHelper.hashAlgorithms)) {
      AssetBlob blob = tx.createBlob(item.specifier.path, streamSupplier, FacetHelper.hashAlgorithms, null,
          FacetHelper.determineContentType(item), true);
      tx.attachBlob(asset, blob);
    }
    finally {
      item.content.close();
    }
    tx.saveAsset(asset);
  }
}
 
Example 5
Source File: OrientNpmHostedFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalDeleteBlob
public Set<String> deleteTarball(final NpmPackageId packageId, final String tarballName, final boolean deleteBlob) {
  checkNotNull(packageId);
  checkNotNull(tarballName);
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset tarballAsset = NpmFacetUtils.findTarballAsset(tx, bucket, packageId, tarballName);
  if (tarballAsset == null) {
    return Collections.emptySet();
  }
  Component tarballComponent = tx.findComponentInBucket(tarballAsset.componentId(), bucket);
  if (tarballComponent == null) {
    return Collections.emptySet();
  }
  return tx.deleteComponent(tarballComponent, deleteBlob);
}
 
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: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalStoreBlob
@Nullable
public Content getRootIndex() {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, bucket, INDEX_PATH_PREFIX);
  if (asset == null) {
    try {
      return createAndSaveRootIndex(bucket);
    }
    catch (IOException e) {
      log.error("Unable to create root index for repository: {}", getRepository().getName(), e);
      return null;
    }
  }

  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example 8
Source File: HelmFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find an asset by its name.
 *
 * @return found Optional<Asset> or Optional.empty if not found
 */
@Override
public Optional<Asset> findAsset(final StorageTx tx, final String assetName) {
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAssetWithProperty(P_NAME, assetName, bucket);
  return Optional.ofNullable(asset);
}
 
Example 9
Source File: GolangDataAccess.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreBlob
public Content maybeCreateAndSaveComponent(final Repository repository,
                                           final GolangAttributes golangAttributes,
                                           final String assetPath,
                                           final TempBlob tempBlob,
                                           final Payload payload,
                                           final AssetKind assetKind) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(repository);

  Component component = findComponent(tx,
      repository,
      golangAttributes.getModule(),
      golangAttributes.getVersion());

  if (component == null) {
    component = tx.createComponent(bucket, repository.getFormat())
        .name(golangAttributes.getModule())
        .version(golangAttributes.getVersion());
    tx.saveComponent(component);
  }

  Asset asset = findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }
  return saveAsset(tx, asset, tempBlob, payload);
}
 
Example 10
Source File: AptFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Asset findOrCreateMetadataAsset(final StorageTx tx, final String path) {
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAssetWithProperty(P_NAME, path, bucket);
  return asset != null
      ? asset
      : tx.createAsset(bucket, getRepository().getFormat()).name(path);
}
 
Example 11
Source File: NpmPackageRootMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Fetches the package root as {@link NestedAttributesMap}
 * @param tx
 * @param repository
 * @param packageId
 * @return package root if found otherwise null
 * @throws IOException
 */
@Nullable
public static NestedAttributesMap getPackageRoot(final StorageTx tx,
                                                 final Repository repository,
                                                 final NpmPackageId packageId) throws IOException
{
  Bucket bucket = tx.findBucket(repository);

  Asset packageRootAsset = findPackageRootAsset(tx, bucket, packageId);
  if (packageRootAsset != null) {
    return loadPackageRoot(tx, packageRootAsset);
  }
  return null;
}
 
Example 12
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
@TransactionalStoreBlob
protected Content doGet(MavenPath path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();

  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    return null;
  }

  Blob blob = tx.requireBlob(asset.requireBlobRef());

  if (needsRebuild(path, asset)) {
    try {
      removeRebuildFlag(asset);
      tx.saveAsset(asset);
      rebuildMetadata(blob);

      // locate the rebuilt asset + blob
      asset = findAsset(tx, bucket, path);
      if (asset == null) {
        return null;
      }

      blob = tx.requireBlob(asset.requireBlobRef());
    }
    catch (OModificationOperationProhibitedException e) {
      log.debug("Cannot rebuild metadata when NXRM is read-only {} : {}",
          getRepository().getName(), path.getPath(), e);
    }
  }

  return toContent(asset, blob);
}
 
Example 13
Source File: AptHostedFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreBlob
protected Asset ingestAsset(final ControlFile control, final TempBlob body, final long size, final String contentType) throws IOException {
  AptFacet aptFacet = getRepository().facet(AptFacet.class);
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  PackageInfo info = new PackageInfo(control);
  String name = info.getPackageName();
  String version = info.getVersion();
  String architecture = info.getArchitecture();

  String assetPath = FacetHelper.buildAssetPath(name, version, architecture);

  Content content = aptFacet.put(
      assetPath,
      new StreamPayload(() -> body.get(), size, contentType),
      info);

  Asset asset = Content.findAsset(tx, bucket, content);
  String indexSection =
      buildIndexSection(control, asset.size(), asset.getChecksums(FacetHelper.hashAlgorithms), assetPath);
  asset.formatAttributes().set(P_ARCHITECTURE, architecture);
  asset.formatAttributes().set(P_PACKAGE_NAME, name);
  asset.formatAttributes().set(P_PACKAGE_VERSION, version);
  asset.formatAttributes().set(P_INDEX_SECTION, indexSection);
  asset.formatAttributes().set(P_ASSET_KIND, "DEB");
  tx.saveAsset(asset);

  rebuildIndexes(singletonList(new AssetChange(AssetAction.ADDED, asset)));
  return asset;
}
 
Example 14
Source File: AptSnapshotFacetSupport.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = { ONeedRetryException.class })
@Override
public void deleteSnapshot(String id) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.findComponentWithProperty(P_NAME, id, bucket);
  if (component == null) {
    return;
  }
  tx.deleteComponent(component);
}
 
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: OrientPyPiComponentMaintenance.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes the asset. If the associated component has no additional assets, then the component is also deleted.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());
  final Asset asset = tx.findAsset(assetId, bucket);
  if (asset == null) {
    return Collections.emptySet();
  }
  Set<String> deletedAssets = new HashSet<>();
  deletedAssets.addAll(super.deleteAssetTx(assetId, deleteBlob));

  final EntityId componentId = asset.componentId();
  if (componentId != null) {
    deleteRootIndex();

    deleteCachedIndexForPackage(asset);

    final Component component = tx.findComponentInBucket(componentId, bucket);
    if (component != null && !tx.browseAssets(component).iterator().hasNext()) {
      deletedAssets.addAll(deleteComponentTx(componentId, deleteBlob).getAssets());
    }
  }

  return deletedAssets;
}
 
Example 17
Source File: OrientPyPiIndexFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalDeleteBlob
public void deleteRootIndex() {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset rootIndex = findAsset(tx, bucket, INDEX_PATH_PREFIX);
  if (rootIndex != null) {
    tx.deleteAsset(rootIndex);
  }
}
 
Example 18
Source File: CocoapodsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@TransactionalStoreBlob
public Content getOrCreateAsset(final String path, final Content content, boolean toAttachComponent)
    throws IOException
{
  StorageFacet storageFacet = facet(StorageFacet.class);
  try (final TempBlob tempBlob = storageFacet.createTempBlob(content, HASH_ALGORITHMS)) {
    StorageTx tx = UnitOfWork.currentTx();
    Bucket bucket = tx.findBucket(getRepository());

    Asset asset = tx.findAssetWithProperty(P_NAME, path, bucket);
    if (asset == null) {
      if (toAttachComponent) {
        Component component = findOrCreateComponent(tx, bucket, path);
        asset = tx.createAsset(bucket, component).name(path);
      }
      else {
        asset = tx.createAsset(bucket, getRepository().getFormat()).name(path);
      }
    }
    Content.applyToAsset(asset, content.getAttributes());
    AssetBlob blob = tx.setBlob(asset, path, tempBlob, HASH_ALGORITHMS, null, null, false);
    tx.saveAsset(asset);

    final Content updatedContent = new Content(new BlobPayload(blob.getBlob(), asset.requireContentType()));
    Content.extractFromAsset(asset, HASH_ALGORITHMS, updatedContent.getAttributes());
    return updatedContent;
  }
}
 
Example 19
Source File: AptFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Asset findOrCreateDebAsset(final StorageTx tx, final String path, final PackageInfo packageInfo)
{
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAssetWithProperty(P_NAME, path, bucket);
  if (asset == null) {
    Component component = findOrCreateComponent(
        tx,
        bucket,
        packageInfo);
    asset = tx.createAsset(bucket, component).name(path);
  }

  return asset;
}
 
Example 20
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);
}