Java Code Examples for org.sonatype.nexus.repository.view.ContentTypes#APPLICATION_JSON

The following examples show how to use org.sonatype.nexus.repository.view.ContentTypes#APPLICATION_JSON . 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: ConanContentValidator.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@Nonnull
@Override
public String determineContentType(final boolean strictContentTypeValidation,
                                   final Supplier<InputStream> contentSupplier,
                                   @Nullable final MimeRulesSource mimeRulesSource,
                                   @Nullable final String contentName,
                                   @Nullable final String declaredContentType) throws IOException
{
  if (contentName != null) {
    if (contentName.endsWith(".tgz")) {
      return ContentTypes.APPLICATION_GZIP;
    }
    else if (contentName.endsWith("/conanfile.py")) {
      return X_PYTHON;
    }
    else if (ConanFacetUtils.isPackageSnapshot(contentName)) {
      return ContentTypes.APPLICATION_JSON;
    }
    else if (ConanFacetUtils.isDigest(contentName)) {
      return ContentTypes.APPLICATION_JSON;
    }
  }
  return defaultContentValidator.determineContentType(
      strictContentTypeValidation, contentSupplier, mimeRulesSource, contentName, declaredContentType
  );
}
 
Example 2
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
private Content putMetadata(final Context context,
                            final Content content,
                            final AssetKind assetKind,
                            final ConanCoords coords)
    throws IOException
{
  StorageFacet storageFacet = facet(StorageFacet.class);
  try (TempBlob tempBlob = storageFacet.createTempBlob(content.openInputStream(), HASH_ALGORITHMS)) {

    if (assetKind == DOWNLOAD_URL || assetKind == DIGEST) {
      Content saveMetadata = doSaveMetadata(tempBlob, content, assetKind, coords);
      return new Content(
          new StringPayload(
              conanUrlIndexer.updateAbsoluteUrls(context, saveMetadata, getRepository()),
              ContentTypes.APPLICATION_JSON)
      );
    }
    return doSaveMetadata(tempBlob, content, assetKind, coords);
  }
}
 
Example 3
Source File: ComposerJsonProcessor.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Builds a packages.json file as a {@code Content} instance containing the actual JSON for the given providers.
 */
private Content buildPackagesJson(final Repository repository, final Set<String> names) throws IOException {
  Map<String, Object> packagesJson = new LinkedHashMap<>();
  packagesJson.put(PROVIDERS_URL_KEY, repository.getUrl() + PACKAGE_JSON_PATH);
  packagesJson.put(PROVIDERS_KEY, names.stream()
      .collect(Collectors.toMap((each) -> each, (each) -> singletonMap(SHA256_KEY, null))));
  return new Content(new StringPayload(mapper.writeValueAsString(packagesJson), ContentTypes.APPLICATION_JSON));
}
 
Example 4
Source File: NpmResponses.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static Payload statusPayload(final boolean success, @Nullable final String error) {
  NestedAttributesMap errorObject = new NestedAttributesMap("error", Maps.<String, Object>newHashMap());
  errorObject.set("success", Boolean.valueOf(success));
  if (error != null) {
    errorObject.set("error", error);
  }
  return new BytesPayload(NpmJsonUtils.bytes(errorObject), ContentTypes.APPLICATION_JSON);
}
 
Example 5
Source File: NpmSearchFacetHosted.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public Content searchV1(final Parameters parameters) throws IOException {
  String text = npmSearchParameterExtractor.extractText(parameters);
  int size = npmSearchParameterExtractor.extractSize(parameters);
  int from = npmSearchParameterExtractor.extractFrom(parameters);

  // npm search V1 endpoint currently returns an empty result set if no text is provided in the request
  NpmSearchResponse response;
  if (text.isEmpty()) {
    response = npmSearchResponseFactory.buildEmptyResponse();
  }
  else {
    QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(text)
        .allowLeadingWildcard(true)
        .analyzeWildcard(true);
    TermsBuilder terms = AggregationBuilders.terms("name")
        .field("assets.attributes.npm.name")
        .size(v1SearchMaxResults)
        .subAggregation(AggregationBuilders.topHits("versions")
            .addSort(SortBuilders.fieldSort("assets.attributes.npm.search_normalized_version")
                .order(SortOrder.DESC))
            .setTrackScores(true)
            .setSize(1));

    SearchResponse searchResponse = searchQueryService.search(
        repositoryQuery(query).inRepositories(getRepository()), singletonList(terms));
    Aggregations aggregations = searchResponse.getAggregations();
    Terms nameTerms = aggregations.get("name");
    response = npmSearchResponseFactory.buildResponseForResults(nameTerms.getBuckets(), size, from);
  }

  String content = npmSearchResponseMapper.writeString(response);
  return new Content(new StringPayload(content, ContentTypes.APPLICATION_JSON));
}
 
Example 6
Source File: NpmSearchIndexFacetHosted.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Builds the index by querying (read only access) the underlying CMA structures.
 */
@Nonnull
@Override
protected Content buildIndex(final StorageTx tx, final Path path) throws IOException {
  Bucket bucket = tx.findBucket(getRepository());
  try (final Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
    Set<NpmPackageId> packageIds = Sets.newHashSet(NpmFacetUtils.findAllPackageNames(tx, bucket));
    DateTime updated = new DateTime();
    JsonGenerator generator = NpmJsonUtils.mapper.getFactory().createGenerator(writer);
    generator.writeStartObject();
    generator.writeNumberField(NpmMetadataUtils.META_UPDATED, updated.getMillis());
    for (NpmPackageId packageId : packageIds) {
      final Asset packageRootAsset = NpmFacetUtils.findPackageRootAsset(tx, bucket, packageId);
      if (packageRootAsset != null) { // removed during iteration, skip
        NestedAttributesMap packageRoot = NpmFacetUtils.loadPackageRoot(tx, packageRootAsset);
        if (!packageRoot.isEmpty()) {
          NpmMetadataUtils.shrink(packageRoot);
          generator.writeObjectField(packageId.id(), packageRoot.backing());
        }
      }
    }
    generator.writeEndObject();
    generator.flush();
  }

  return new Content(new StreamPayload(
      new InputStreamSupplier()
      {
        @Nonnull
        @Override
        public InputStream get() throws IOException {
          return new BufferedInputStream(Files.newInputStream(path));
        }
      },
      Files.size(path),
      ContentTypes.APPLICATION_JSON)
  );
}
 
Example 7
Source File: ComposerJsonProcessor.java    From nexus-repository-composer with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Merges incoming provider JSON files, producing a merged file containing only the minimal subset of fields that we
 * need to download artifacts.
 */
public Content mergeProviderJson(final Repository repository, final List<Payload> payloads, final DateTime now)
    throws IOException
{
  String currentTime = now.withZone(DateTimeZone.UTC).toString(timeFormatter);

  // TODO: Make this more robust, right now it makes a lot of assumptions and doesn't deal with bad things well,
  // can probably consolidate this with the handling for rewrites for proxy (or at least make it more rational).
  Map<String, Map<String, Object>> packages = new LinkedHashMap<>();
  for (Payload payload : payloads) {
    Map<String, Object> json = parseJson(payload);
    if (json.get(PACKAGES_KEY) instanceof Map) {

      Map<String, Object> packagesMap = (Map<String, Object>) json.get(PACKAGES_KEY);
      for (String packageName : packagesMap.keySet()) {

        Map<String, Object> packageVersions = (Map<String, Object>) packagesMap.get(packageName);
        for (String packageVersion : packageVersions.keySet()) {

          Map<String, Object> versionInfo = (Map<String, Object>) packageVersions.get(packageVersion);
          Map<String, Object> distInfo = (Map<String, Object>) versionInfo.get(DIST_KEY);
          if (distInfo == null) {
            continue;
          }

          if (!packages.containsKey(packageName)) {
            packages.put(packageName, new LinkedHashMap<>());
          }

          String time = (String) versionInfo.get(TIME_KEY);
          if (time == null) {
            time = currentTime;
          }

          Map<String, Object> packagesForName = packages.get(packageName);
          packagesForName.putIfAbsent(packageVersion, buildPackageInfo(repository, packageName, packageVersion,
              (String) distInfo.get(REFERENCE_KEY), (String) distInfo.get(SHASUM_KEY),
              (String) distInfo.get(TYPE_KEY), time, versionInfo));
        }
      }
    }
  }

  return new Content(new StringPayload(mapper.writeValueAsString(singletonMap(PACKAGES_KEY, packages)),
      ContentTypes.APPLICATION_JSON));
}
 
Example 8
Source File: NpmSearchIndexFacetGroup.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Builds the index by merging member indexes with low memory footprint, as JSON indexes might get huge.
 */
@Nonnull
@Override
protected Content buildIndex(final StorageTx tx, final Path path) throws IOException {
  final List<Repository> members = facet(GroupFacet.class).leafMembers();
  final Closer closer = Closer.create();
  try {
    final ArrayList<JsonParser> parsers = new ArrayList<>(members.size());
    pauseTransactionAndProcessMemberRepositories(tx, members, closer, parsers);
    final JsonGenerator generator = closer.register(
        mapper.getFactory().createGenerator(new BufferedOutputStream(Files.newOutputStream(path)))
    );
    generator.writeStartObject();
    generator.writeNumberField(NpmMetadataUtils.META_UPDATED, System.currentTimeMillis());
    final PackageMerger packageMerger = new PackageMerger(parsers);
    while (!packageMerger.isDepleted()) {
      NestedAttributesMap packageRoot = packageMerger.next();
      generator.writeObjectField(packageRoot.getKey(), packageRoot.backing());
    }
    generator.writeEndObject();
    generator.flush();
  }
  catch (Throwable t) {
    throw closer.rethrow(t);
  }
  finally {
    closer.close();
  }

  return new Content(new StreamPayload(
      new InputStreamSupplier()
      {
        @Nonnull
        @Override
        public InputStream get() throws IOException {
          return new BufferedInputStream(Files.newInputStream(path));
        }
      },
      Files.size(path),
      ContentTypes.APPLICATION_JSON)
  );
}
 
Example 9
Source File: NpmSearchIndexFilter.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Filters the npm index document with given predicate/
 */
static Content filter(final Content fullIndex,
                      final Predicate<NestedAttributesMap> predicate) throws IOException
{
  checkNotNull(fullIndex);
  checkNotNull(predicate);
  final Path path = Files.createTempFile("npm-searchIndex-filter", "json");
  final Closer closer = Closer.create();
  try {
    final JsonParser jsonParser = mapper.getFactory().createParser(closer.register(fullIndex.openInputStream()));
    if (jsonParser.nextToken() == JsonToken.START_OBJECT &&
        NpmMetadataUtils.META_UPDATED.equals(jsonParser.nextFieldName())) {
      jsonParser.nextValue(); // skip value
    }
    final JsonGenerator generator = closer.register(
        mapper.getFactory().createGenerator(new BufferedOutputStream(Files.newOutputStream(path)))
    );
    generator.writeStartObject();
    generator.writeNumberField(NpmMetadataUtils.META_UPDATED, System.currentTimeMillis());
    NestedAttributesMap packageRoot;
    while ((packageRoot = getNext(jsonParser)) != null) {
      if (predicate.apply(packageRoot)) {
        generator.writeObjectField(packageRoot.getKey(), packageRoot.backing());
      }
    }
    generator.writeEndObject();
    generator.flush();
  }
  catch (Throwable t) {
    throw closer.rethrow(t);
  }
  finally {
    closer.close();
  }

  return new Content(new StreamPayload(
      new InputStreamSupplier()
      {
        @Nonnull
        @Override
        public InputStream get() throws IOException {
          return new BufferedInputStream(Files.newInputStream(path));
        }
      },
      Files.size(path),
      ContentTypes.APPLICATION_JSON)
  );
}
 
Example 10
Source File: OrientNpmGroupFacetTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Content toContent(final NestedAttributesMap packageRoot) {
  Content content = new Content(new BytesPayload(bytes(packageRoot), ContentTypes.APPLICATION_JSON));
  content.getAttributes().set(NestedAttributesMap.class, packageRoot);
  return content;
}
 
Example 11
Source File: NpmSearchIndexFacetCachingTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected Content buildIndex(StorageTx tx, Path path) throws IOException {
  return new Content(new StringPayload("{}", ContentTypes.APPLICATION_JSON));
}