Java Code Examples for org.sonatype.nexus.common.collect.NestedAttributesMap#get()

The following examples show how to use org.sonatype.nexus.common.collect.NestedAttributesMap#get() . 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: 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 2
Source File: NpmPublishParser.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void updateMaintainerList(final NestedAttributesMap versionToUpdate, final String currentUserId) {
  Object maintainersObject = versionToUpdate.get(MAINTAINERS_KEY);

  if (nonNull(maintainersObject)) {
    if(maintainersObject instanceof List) {
      List maintainers = (List) maintainersObject;

      Object maintainer = maintainers.get(0);
      if (maintainer instanceof Map) {
        updateMaintainerAsMap(maintainers, currentUserId);
      }
      else if (maintainer instanceof String) {
        updateMaintainerAsString(maintainers, currentUserId);
      }
    }
    else if (maintainersObject instanceof String) {
      versionToUpdate.set(MAINTAINERS_KEY, currentUserId);
    }
  }
}
 
Example 3
Source File: NpmPackageRootMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private static void copy(final NestedAttributesMap map, final NestedAttributesMap src, final String field) {
  Object object = src.get(field);
  if (object instanceof Map) {
    NestedAttributesMap destChild = map.child(field);
    NestedAttributesMap srcChild = src.child(field);
    for (String key : srcChild.keys()) {
      if (srcChild.get(field) instanceof Map) {
        copy(destChild, srcChild, key);
      }
      else {
        destChild.set(key, srcChild.get(key));
      }
    }
  }
  else if (object != null) {
    map.set(field, object);
  }
}
 
Example 4
Source File: NpmMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Rewrites dist/tarball entry URLs to point back to this Nexus instance and given repository.
 */
public static void rewriteTarballUrl(final String repositoryName, final NestedAttributesMap packageRoot) {
  if (BaseUrlHolder.isSet()) {
    NestedAttributesMap versions = packageRoot.child(VERSIONS);
    for (String v : versions.keys()) {
      if (versions.get(v) instanceof Map) { // only if not incomplete
        NestedAttributesMap version = versions.child(v);
        NestedAttributesMap dist = version.child(DIST);
        String tarballName = extractTarballName(dist.get(TARBALL, String.class));
        dist.set(
            TARBALL,
            String.format(
                "%s/repository/%s/%s/-/%s", BaseUrlHolder.get(), repositoryName, version.get(NAME), tarballName
            )
        );
      }
    }
  }
}
 
Example 5
Source File: CreateIndexServiceImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
private void parseAssetIntoChartEntry(final ChartIndex index, final Asset asset) {
  NestedAttributesMap formatAttributes = asset.formatAttributes();
  NestedAttributesMap assetAttributes = asset.attributes();
  ChartEntry chartEntry = new ChartEntry();
  chartEntry.setName(formatAttributes.get(NAME.getPropertyName(), String.class));
  chartEntry.setVersion(formatAttributes.get(VERSION.getPropertyName(), String.class));
  chartEntry.setDescription(formatAttributes.get(DESCRIPTION.getPropertyName(), String.class));
  chartEntry.setIcon(formatAttributes.get(ICON.getPropertyName(), String.class));
  chartEntry.setCreated(asset.blobCreated());

  @SuppressWarnings("unchecked")
  List<Map<String, String>> maintainers = formatAttributes.get(MAINTAINERS.getPropertyName(), List.class);
  chartEntry.setMaintainers(maintainers);
  chartEntry.setAppVersion(formatAttributes.get(APP_VERSION.getPropertyName(), String.class));
  chartEntry.setDigest(assetAttributes.get("checksum", Map.class)
      .get("sha256").toString());
  createListOfRelativeUrls(formatAttributes, chartEntry);
  chartEntry.setSources(formatAttributes.get(SOURCES.getPropertyName(), List.class));
  index.addEntry(chartEntry);
}
 
Example 6
Source File: NpmPackageRootMetadataUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static void assertDependencies(final NestedAttributesMap metadata,
                                       final String fieldName,
                                       final String dep,
                                       final String depVersion)
{
  assertTrue(metadata.contains(fieldName));
  Map<String, Object> dependencies = metadata.get(fieldName, Map.class);
  assertThat(dependencies.keySet(), hasSize(1));
  assertThat(dependencies.get(dep), is(depVersion));
}
 
Example 7
Source File: AptApiRepositoryAdapter.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private AptSigningRepositoriesAttributes createAptSigningRepositoriesAttributes(final Repository repository) {
  NestedAttributesMap aptAttributes = repository.getConfiguration().attributes(AptFormat.NAME);
  String keypair = aptAttributes.get("keypair", String.class);
  String passphrase = aptAttributes.get("passphrase", String.class);
  if (!Strings2.isBlank(passphrase)) {
    return new AptSigningRepositoriesAttributes(keypair, null);
  }
  return null;
}
 
Example 8
Source File: SimpleApiRepositoryAdapter.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected HostedStorageAttributes getHostedStorageAttributes(final Repository repository) {
  NestedAttributesMap configuration = repository.getConfiguration().attributes(ConfigurationConstants.STORAGE);

  String blobStoreName = configuration.get(ConfigurationConstants.BLOB_STORE_NAME, String.class);
  Boolean strictContentTypeValidation =
      configuration.get(ConfigurationConstants.STRICT_CONTENT_TYPE_VALIDATION, Boolean.class, Boolean.TRUE);
  String writePolicy =
      toString(configuration.get(ConfigurationConstants.WRITE_POLICY), ConfigurationConstants.WRITE_POLICY_DEFAULT);

  return new HostedStorageAttributes(blobStoreName, strictContentTypeValidation, writePolicy);
}
 
Example 9
Source File: PurgeUnusedSnapshotsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void deleteComponent(final Component component) {
  log.debug("Deleting unused snapshot component {}", component);
  final StorageTx tx = UnitOfWork.currentTx();
  tx.deleteComponent(component);

  NestedAttributesMap attributes = component.formatAttributes();
  String groupId = attributes.get(P_GROUP_ID, String.class);
  String artifactId = attributes.get(P_ARTIFACT_ID, String.class);
  String baseVersion = attributes.get(P_BASE_VERSION, String.class);

  try {
    Bucket bucket = tx.findBucket(getRepository());

    getRepository().facet(MavenFacet.class).maybeDeleteOrFlagToRebuildMetadata(bucket, metadataPath(groupId, artifactId, baseVersion));

    //for GA metadata, if there are no other GAVs matching the GA, delete it, otherwise mark as invalid so it will
    //be rebuilt on request
    getRepository().facet(MavenFacet.class).maybeDeleteOrFlagToRebuildMetadata(bucket, metadataPath(groupId, artifactId, null));

    //for G metadata, if there are no other GAs matching the G, delete it, otherwise mark as invalid so it will
    //be rebuilt on request
    getRepository().facet(MavenFacet.class).maybeDeleteOrFlagToRebuildMetadata(bucket, metadataPath(groupId, null, null));
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 10
Source File: CacheInfo.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
public static CacheInfo extractFromAsset(final NestedAttributesMap attributes) {
  checkNotNull(attributes);
  final NestedAttributesMap cache = attributes.child(CACHE);
  final DateTime lastVerified = toDateTime(cache.get(LAST_VERIFIED, Date.class));
  if (lastVerified == null) {
    return null;
  }
  final String cacheToken = cache.get(CACHE_TOKEN, String.class);
  return new CacheInfo(lastVerified, cacheToken);
}
 
Example 11
Source File: SimpleApiRepositoryAdapter.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected StorageAttributes getStorageAttributes(final Repository repository) {
  NestedAttributesMap configuration = repository.getConfiguration().attributes(ConfigurationConstants.STORAGE);

  String blobStoreName = configuration.get(ConfigurationConstants.BLOB_STORE_NAME, String.class);
  Boolean strictContentTypeValidation =
      configuration.get(ConfigurationConstants.STRICT_CONTENT_TYPE_VALIDATION, Boolean.class, Boolean.TRUE);

  return new StorageAttributes(blobStoreName, strictContentTypeValidation);
}
 
Example 12
Source File: CacheInfo.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Extracts non-format specific cache info from passed in {@link Asset}.
 */
@Nullable
public static CacheInfo extractFromAsset(final Asset asset) {
  checkNotNull(asset);
  final NestedAttributesMap cache = asset.attributes().child(CACHE);
  final DateTime lastVerified = toDateTime(cache.get(LAST_VERIFIED, Date.class));
  if (lastVerified == null) {
    return null;
  }
  final String cacheToken = cache.get(CACHE_TOKEN, String.class);
  return new CacheInfo(lastVerified, cacheToken);
}
 
Example 13
Source File: NpmPackageRootMetadataUtilsTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static void assertHuman(final NestedAttributesMap parent,
                                final String fieldName,
                                final String email,
                                final String name)
{

  assertTrue(parent.contains(fieldName));
  List<Map<String, Object>> humans = parent.get(fieldName, List.class);
  assertThat(humans, hasSize(1));
  assertThat(humans.get(0).get("email"), is(email));
  assertThat(humans.get(0).get("name"), is(name));
}
 
Example 14
Source File: OrientNpmHostedFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void updateRevision(final NestedAttributesMap packageRoot,
                            final Asset packageRootAsset,
                            final boolean createdPackageRoot)
{
  String newRevision = "1";

  if (!createdPackageRoot) {
    if (packageRoot.contains(META_REV)) {
      String rev = packageRoot.get(META_REV, String.class);
      newRevision = Integer.toString(Integer.parseInt(rev) + 1);
    }
    else {
      /*
        This is covering the edge case when a new package is uploaded to a repository where the packageRoot already 
        exists.
        
        If that packageRoot was created using an earlier version of NXRM where we didn't store the rev then we need
        to add it in. We also add the rev in on download but it is possible that someone is uploading a package where
        the packageRoot has never been downloaded before.
       */
      newRevision = EntityHelper.version(packageRootAsset).getValue();
    }
  }

  packageRoot.set(META_ID, packageRootAsset.name());
  packageRoot.set(META_REV, newRevision);
}
 
Example 15
Source File: AssetBlobCleanupTaskManager.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Subscribe
@AllowConcurrentEvents
public void on(final RepositoryStartedEvent event) {
  String format = event.getRepository().getFormat().getValue();

  Configuration repositoryConfiguration = event.getRepository().getConfiguration();
  NestedAttributesMap storageAttributes = repositoryConfiguration.attributes(STORAGE);
  String contentStore = (String) storageAttributes.get(DATA_STORE_NAME, CONTENT_DATASTORE_NAME);

  if (activeFormatStores.put(format, contentStore) && isStarted()) {
    scheduleAssetBlobCleanupTask(format, contentStore);
  }
}
 
Example 16
Source File: NpmPackageRootMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static void setBugsUrl(NestedAttributesMap packageJson, NestedAttributesMap packageRoot) {
  Object bugs = packageJson.get(NpmAttributes.P_BUGS);
  String bugsUrl = null;

  if (bugs instanceof String) {
    bugsUrl = (String) bugs;
  } else if (bugs != null) {
    bugsUrl = packageJson.child(NpmAttributes.P_BUGS).get(NpmAttributes.P_URL, String.class);
  }

  if (bugsUrl != null) {
    packageRoot.set(NpmAttributes.P_BUGS, bugsUrl);
  }
}
 
Example 17
Source File: NpmPublishParser.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private String getNpmUser(final NestedAttributesMap packageEntry) {
  if (packageEntry.contains(NPM_USER)) {
    NestedAttributesMap npmUser = packageEntry.child(NPM_USER);
    return npmUser.get(NAME, String.class);
  }
  return null;
}
 
Example 18
Source File: MergeObjectMapperTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void verifyMergeMultipleContentsResult(final NestedAttributesMap result) {
  assertThat(result.backing(), hasEntry("_id", "id"));
  assertThat(result.backing(), hasEntry("_rev", "rev"));
  assertThat(result.backing(), hasEntry("name", "dominant"));
  assertThat(result.child("dist-tags").get("latest"), equalTo("1.0.0"));
  assertThat(result.child("versions").child("1.0").backing(), hasEntry("foo", "baz"));
  assertThat(result.child("versions").child("1.0").backing(), hasEntry("foo2", "bar2"));

  assertThat(result.backing(), hasEntry("notindominant", "always present"));
  assertThat(result.backing(), hasEntry("notinrecessive", "always present"));
  assertThat(result.child("not").child("in").backing(), hasEntry("dominant", "always present"));

  assertThat((List<String>) result.child("languages").child("en").get("alphabet"), contains("a", "b", "c"));

  assertThat((List<String>) result.get("alphabet"), contains("a", "b", "c"));
  assertThat((List<String>) result.get("recessive-numbers"), contains(1, 2, 3));
  assertThat((List<String>) result.get("dominant-numbers"), contains(9, 8, 7));

  Map<String, String> map = ((List<Map<String, String>>) result.get("maintainers")).get(0);
  assertThat(map, hasEntry("name", "j1"));
  assertThat(map, hasEntry("email", "[email protected]"));

  map = ((List<Map<String, String>>) result.get("maintainers")).get(1);
  assertThat(map, hasEntry("name", "j2"));
  assertThat(map, hasEntry("email", "[email protected]"));

  map = ((List<Map<String, String>>) result.get("recessive-maintainers")).get(0);
  assertThat(map, hasEntry("name", "jeremy"));
  assertThat(map, hasEntry("email", "[email protected]"));

  map = ((List<Map<String, String>>) result.get("dominant-maintainers")).get(0);
  assertThat(map, hasEntry("name", "nate"));
  assertThat(map, hasEntry("email", "[email protected]"));

  List<String> list = ((List<List<String>>) result.get("array-of-arrays")).get(0);
  assertThat(list, contains("ab", "cd", "ef"));

  list = ((List<List<String>>) result.get("array-of-arrays")).get(1);
  assertThat(list, contains("uv", "wx", "yz"));

  list = ((List<List<String>>) result.get("recessive-array-of-arrays")).get(0);
  assertThat(list, contains("gh", "ij", "kl"));

  list = ((List<List<String>>) result.get("recessive-array-of-arrays")).get(1);
  assertThat(list, contains("op", "qr", "st"));

  list = ((List<List<String>>) result.get("dominant-array-of-arrays")).get(0);
  assertThat(list, contains("ab", "cd", "ef"));

  list = ((List<List<String>>) result.get("dominant-array-of-arrays")).get(1);
  assertThat(list, contains("uv", "wx", "yz"));

  assertThat((List<Object>) result.get("null-array"), contains(nullValue(), nullValue(), nullValue()));

  assertThat((List<String>) result.get("alphabet"), contains("a", "b", "c"));

  List<Object> objects = (List<Object>) result.get("mixed-array");
  assertThat(objects.get(0), equalTo("circle"));
  assertThat(objects.get(1), equalTo(3.14));
  assertThat(((Map<String, String>) objects.get(2)), hasEntry("color", "blue"));
}
 
Example 19
Source File: NpmMergeObjectMapperTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void verifyMergeMultipleContentsResult(final NestedAttributesMap result) {
  assertThat(result.backing(), not(hasEntry("_id", "id")));
  assertThat(result.backing(), not(hasEntry("_rev", "rev")));
  assertThat(result.backing(), hasEntry("name", "dominant"));
  assertThat(result.child("dist-tags").get("latest"), equalTo("2.0.1"));
  assertThat(result.child("versions").child("1.0").backing(), hasEntry("foo", "baz"));
  assertThat(result.child("versions").child("1.0").backing(), hasEntry("foo2", "bar2"));

  assertThat(result.backing(), hasEntry("notindominant", "always present"));
  assertThat(result.backing(), hasEntry("notinrecessive", "always present"));
  assertThat(result.child("not").child("in").backing(), hasEntry("dominant", "always present"));

  assertThat((List<String>) result.child("languages").child("en").get("alphabet"), contains("a", "b", "c"));

  assertThat((List<String>) result.get("alphabet"), contains("a", "b", "c"));
  assertThat((List<String>) result.get("recessive-numbers"), contains(1, 2, 3));
  assertThat((List<String>) result.get("dominant-numbers"), contains(9, 8, 7));

  Map<String, String> map = ((List<Map<String, String>>) result.get("maintainers")).get(0);
  assertThat(map, hasEntry("name", "j1"));
  assertThat(map, hasEntry("email", "[email protected]"));

  map = ((List<Map<String, String>>) result.get("maintainers")).get(1);
  assertThat(map, hasEntry("name", "j2"));
  assertThat(map, hasEntry("email", "[email protected]"));

  map = ((List<Map<String, String>>) result.get("recessive-maintainers")).get(0);
  assertThat(map, hasEntry("name", "jeremy"));
  assertThat(map, hasEntry("email", "[email protected]"));

  map = ((List<Map<String, String>>) result.get("dominant-maintainers")).get(0);
  assertThat(map, hasEntry("name", "nate"));
  assertThat(map, hasEntry("email", "[email protected]"));

  List<String> list = ((List<List<String>>) result.get("array-of-arrays")).get(0);
  assertThat(list, contains("ab", "cd", "ef"));

  list = ((List<List<String>>) result.get("array-of-arrays")).get(1);
  assertThat(list, contains("uv", "wx", "yz"));

  list = ((List<List<String>>) result.get("recessive-array-of-arrays")).get(0);
  assertThat(list, contains("gh", "ij", "kl"));

  list = ((List<List<String>>) result.get("recessive-array-of-arrays")).get(1);
  assertThat(list, contains("op", "qr", "st"));

  list = ((List<List<String>>) result.get("dominant-array-of-arrays")).get(0);
  assertThat(list, contains("ab", "cd", "ef"));

  list = ((List<List<String>>) result.get("dominant-array-of-arrays")).get(1);
  assertThat(list, contains("uv", "wx", "yz"));

  assertThat((List<Object>) result.get("null-array"), contains(nullValue(), nullValue(), nullValue()));

  assertThat((List<String>) result.get("alphabet"), contains("a", "b", "c"));

  List<Object> objects = (List<Object>) result.get("mixed-array");
  assertThat(objects.get(0), equalTo("circle"));
  assertThat(objects.get(1), equalTo(3.14));
  assertThat(((Map<String, String>) objects.get(2)), hasEntry("color", "blue"));

  verifyWhatGoesInMustComeOut(result);
}
 
Example 20
Source File: MergeObjectMapperTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private void verifyReadFirstJson(final NestedAttributesMap result) {
  assertThat(result.size(), equalTo(9));
  assertThat(result.get("name"), equalTo("first"));

  List maintainers = (List) result.get("maintainers");
  assertThat(((Map) maintainers.get(0)).get("email"), equalTo("[email protected]"));
  assertThat(((Map) maintainers.get(0)).get("name"), equalTo("first"));
  assertThat(((Map) maintainers.get(1)).get("email"), equalTo("[email protected]"));
  assertThat(((Map) maintainers.get(1)).get("name"), equalTo("first2"));

  List keywords = (List) result.get("keywords");
  assertThat(keywords.get(0), equalTo("array"));
  assertThat(keywords.get(1), equalTo("first"));

  assertThat(result.get("readme"), is(nullValue()));
  assertThat(result.get("multi-null-field"), is(nullValue()));

  NestedAttributesMap mulitDepth = result.child("multi-depth");
  NestedAttributesMap first = mulitDepth.child("first");

  assertThat(first.get("title"), equalTo("This is the first depth"));

  NestedAttributesMap reverseMulitDepth = result.child("reverse-multi-depth");
  first = reverseMulitDepth.child("first");
  NestedAttributesMap second = first.child("second");
  NestedAttributesMap third = second.child("third");

  assertThat(first.get("title"), equalTo("This is the first depth"));
  assertThat(second.get("title"), equalTo("This is the second depth"));
  assertThat(third.get("title"), equalTo("This is the third depth"));

  NestedAttributesMap mulitDepthMerge = result.child("multi-depth-merge");
  first = mulitDepthMerge.child("first");
  assertThat(first.get("title"), equalTo("This is the first depth"));

  NestedAttributesMap dependencies = result.child("dependencies");
  assertThat(dependencies.get("equire(\"orchestrator\""), equalTo("*"));
  assertThat(dependencies.get("@types/node"), equalTo("*"));
  assertThat(dependencies.get("@types/orchestrator"), equalTo("*"));

  NestedAttributesMap funckyFieldName = dependencies.child("funcky(\"fieldname\"");
  NestedAttributesMap godeep = funckyFieldName.child("godeep");
  first = godeep.child("first");
  assertThat(first.get("title"), equalTo("This is the first depth"));
}