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

The following examples show how to use org.sonatype.nexus.repository.storage.StorageTx#createAsset() . 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
@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 2
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Asset createIndexAsset(final String name,
                               final StorageTx tx,
                               final String indexPath,
                               final Bucket bucket) throws IOException
{
  String html = buildIndex(name, tx);

  Asset savedIndex = tx.createAsset(bucket, getRepository().getFormat());
  savedIndex.name(indexPath);
  savedIndex.formatAttributes().set(P_ASSET_KIND, AssetKind.INDEX.name());

  StorageFacet storageFacet = getRepository().facet(StorageFacet.class);
  TempBlob tempBlob = storageFacet.createTempBlob(new ByteArrayInputStream(html.getBytes(UTF_8)), HASH_ALGORITHMS);

  saveAsset(tx, savedIndex, tempBlob, TEXT_HTML, new AttributesMap());

  return savedIndex;
}
 
Example 3
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 4
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 5
Source File: RFacetImpl.java    From nexus-repository-r 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 Map<String, String> attributes)
{
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);

    // TODO: Make this a bit more robust (could be problematic if keys are removed in later versions, or if keys clash)
    for (Entry<String, String> attribute : attributes.entrySet()) {
      asset.formatAttributes().set(attribute.getKey(), attribute.getValue());
    }
    asset.formatAttributes().set(P_ASSET_KIND, getAssetKind(path).name());
    tx.saveAsset(asset);
  }

  return asset;
}
 
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: GolangDataAccess.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Save an asset and create blob.
 *
 * @return blob content
 */
@TransactionalStoreBlob
public Content maybeCreateAndSaveAsset(final Repository repository,
                                       final String assetPath,
                                       final AssetKind assetKind,
                                       final TempBlob tempBlob,
                                       final Payload payload) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(repository);

  Asset asset = findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, repository.getFormat());
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
  }
  return saveAsset(tx, asset, tempBlob, payload);
}
 
Example 8
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 9
Source File: P2FacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Asset findOrCreateAsset(final StorageTx tx, final String path) {
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(path);
    asset.formatAttributes().set(P_ASSET_KIND, getAssetKind(path).name());
    tx.saveAsset(asset);
  }

  return asset;
}
 
Example 10
Source File: RFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Asset findOrCreateAsset(final StorageTx tx, final String path) {
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(path);
    asset.formatAttributes().set(P_ASSET_KIND, getAssetKind(path).name());
    tx.saveAsset(asset);
  }

  return asset;
}
 
Example 11
Source File: RawContentFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreMetadata
public Asset getOrCreateAsset(final Repository repository, final String componentName, final String componentGroup,
                              final String assetName) {
  final StorageTx tx = UnitOfWork.currentTx();

  final Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.findComponentWithProperty(P_NAME, componentName, bucket);
  Asset asset;
  if (component == null) {
    // CREATE
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(componentGroup)
        .name(componentName);

    tx.saveComponent(component);

    asset = tx.createAsset(bucket, component);
    asset.name(assetName);
  }
  else {
    // UPDATE
    asset = tx.firstAsset(component);
    asset = asset != null ? asset : tx.createAsset(bucket, component).name(assetName);
  }

  return asset;
}
 
Example 12
Source File: ConanHostedFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreBlob
protected void doPutArchive(final ConanCoords coord,
                            final String path,
                            final TempBlob tempBlob,
                            final AssetKind assetKind) throws IOException
{
  checkNotNull(path);
  checkNotNull(tempBlob);

  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Map<String, String> attributes = new HashMap<>();
  attributes.put(GROUP, coord.getGroup());
  attributes.put(PROJECT, coord.getProject());
  attributes.put(VERSION, coord.getVersion());
  attributes.put(STATE, coord.getChannel());

  Component component = findComponent(tx, getRepository(), coord);
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(coord.getGroup())
        .name(coord.getProject())
        .version(getComponentVersion(coord));
  }
  tx.saveComponent(component);

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

  saveAsset(tx, asset, tempBlob);
}
 
Example 13
Source File: OrientPyPiFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Asset putIndex(final String path, final AssetBlob assetBlob) {
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, bucket, path);

  if (asset == null) {
    asset = tx.createAsset(bucket, getRepository().getFormat());
    asset.name(path);
    saveAsset(tx, asset, assetBlob, AssetKind.INDEX);
  }

  return asset;
}
 
Example 14
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreMetadata
protected Asset createRootIndexAsset(final Bucket bucket) {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = tx.createAsset(bucket, getRepository().getFormat());
  asset.name(INDEX_PATH_PREFIX);
  asset.formatAttributes().set(P_ASSET_KIND, ROOT_INDEX.name());
  return asset;
}
 
Example 15
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 16
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Asset findOrCreateAsset(
    final StorageTx tx,
    final Bucket bucket,
    final String packagePath,
    final Component component, final String assetKind)
{
  Asset asset = findAsset(tx, bucket, packagePath);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(packagePath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind);
  }
  return asset;
}
 
Example 17
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 18
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;
}
 
Example 19
Source File: OrientPyPiFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Asset putPackage(final String path, final AssetBlob assetBlob) throws IOException {
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, bucket, path);

  if (asset == null) {
    final String filename = extractFilenameFromPath(path);

    Map<String, String> attributes;
    try (InputStream is = assetBlob.getBlob().getInputStream()) {
      attributes = extractMetadata(is);
    }

    if (!attributes.containsKey(P_NAME)) {
      log.debug("No name found in metadata for {}, extracting from filename.", filename);
      attributes.put(P_NAME, normalizeName(extractNameFromFilename(filename)));
    }
    if (!attributes.containsKey(P_VERSION)) {
      log.debug("No version found in metadata for {}, extracting from filename.", filename);
      attributes.put(P_VERSION, extractVersionFromFilename(filename));
    }

    final String name = attributes.get(P_NAME);
    final String version = attributes.get(P_VERSION);

    Component  component = findComponent(tx, getRepository(), name, version);
    if (component == null) {
      component = tx.createComponent(bucket, getRepository().getFormat());
      component.name(name);
      component.version(version);
      component.formatAttributes().set(P_SUMMARY, attributes.get(P_SUMMARY));
      tx.saveComponent(component);
    }
    asset = tx.createAsset(bucket, component);
    asset.name(path);
    copyAttributes(asset, attributes);
    saveAsset(tx, asset, assetBlob, AssetKind.PACKAGE);
  }

  return asset;
}
 
Example 20
Source File: OrientPyPiProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@TransactionalStoreBlob
protected Content doPutPackage(final String path,
                               final TempBlob tempBlob,
                               final Payload payload) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  String filename = extractFilenameFromPath(path);
  Map<String, String> attributes;
  try (InputStream is = tempBlob.get()) {
    attributes = extractMetadata(is);
  }

  if (!attributes.containsKey(P_NAME)) {
    log.debug("No name found in metadata for {}, extracting from filename.", filename);
    attributes.put(P_NAME, extractNameFromFilename(filename));
  }
  if (!attributes.containsKey(P_VERSION)) {
    log.debug("No version found in metadata for {}, extracting from filename.", filename);
    attributes.put(P_VERSION, extractVersionFromFilename(filename));
  }

  String name = attributes.get(P_NAME);
  String version = attributes.get(P_VERSION);

  Component component = findComponent(tx, getRepository(), name, version);
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat()).name(name).version(version);
  }
  if (component.isNew() || attributes.containsKey(P_SUMMARY)) {
    component.formatAttributes().set(P_SUMMARY, attributes.get(P_SUMMARY));
    tx.saveComponent(component);
  }

  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);
    asset.formatAttributes().set(P_ASSET_KIND, AssetKind.PACKAGE.name());
  }

  copyAttributes(asset, attributes);
  return saveAsset(tx, asset, tempBlob, payload);
}