Java Code Examples for org.sonatype.nexus.repository.view.Payload#openInputStream()

The following examples show how to use org.sonatype.nexus.repository.view.Payload#openInputStream() . 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: NpmSearchGroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses a search response, returning the marshaled {@link NpmSearchResponse}.
 */
@Nullable
private NpmSearchResponse parseSearchResponse(final Repository repository, final Response response) {
  Payload payload = response.getPayload();
  if (response.getStatus().getCode() == HttpStatus.OK && payload != null) {
    try (InputStream in = payload.openInputStream()) {
      return npmSearchResponseMapper.readFromInputStream(in);
    }
    catch (IOException e) {
      if (log.isDebugEnabled()) {
        log.warn("Unable to process search response for repository {}, skipping", repository.getName(), e);
      }
      else {
        log.warn("Unable to process search response for repository {}, cause: {}, skipping", repository.getName(),
            e.getMessage());
      }
    }
  }
  return null;
}
 
Example 2
Source File: OrientPyPiHostedHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Handle request for search.
 */
public Handler search() {
  return context -> {
    Payload payload = checkNotNull(context.getRequest().getPayload());
    try (InputStream is = payload.openInputStream()) {
      QueryBuilder query = parseSearchRequest(context.getRepository().getName(), is);
      List<PyPiSearchResult> results = new ArrayList<>();
      for (SearchHit hit : searchQueryService.browse(unrestricted(query))) {
        Map<String, Object> source = hit.getSource();
        Map<String, Object> formatAttributes = (Map<String, Object>) source.getOrDefault(
            MetadataNodeEntityAdapter.P_ATTRIBUTES, Collections.emptyMap());
        Map<String, Object> pypiAttributes = (Map<String, Object>) formatAttributes.getOrDefault(PyPiFormat.NAME,
            Collections.emptyMap());
        String name = Strings.nullToEmpty((String) pypiAttributes.get(PyPiAttributes.P_NAME));
        String version = Strings.nullToEmpty((String) pypiAttributes.get(PyPiAttributes.P_VERSION));
        String summary = Strings.nullToEmpty((String) pypiAttributes.get(PyPiAttributes.P_SUMMARY));
        results.add(new PyPiSearchResult(name, version, summary));
      }
      String response = buildSearchResponse(results);
      return HttpResponses.ok(new StringPayload(response, ContentTypes.APPLICATION_XML));
    }
  };
}
 
Example 3
Source File: SearchGroupHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Builds a context that contains a request that can be "replayed" with its post body content. Since we're repeating
 * the same post request to all the search endpoints, we need to have a new request instance with a payload we can
 * read multiple times.
 */
private Context buildReplayableContext(final Context context) throws IOException {
  checkNotNull(context);
  Request request = checkNotNull(context.getRequest());
  Payload payload = checkNotNull(request.getPayload());
  try (InputStream in = payload.openInputStream()) {
    byte[] content = ByteStreams.toByteArray(in);
    Context replayableContext = new Context(context.getRepository(),
        new Builder()
            .attributes(request.getAttributes())
            .headers(request.getHeaders())
            .action(request.getAction())
            .path(request.getPath())
            .parameters(request.getParameters())
            .payload(new BytesPayload(content, payload.getContentType()))
            .build());

    copyLocalContextAttributes(context, replayableContext);

    return replayableContext;
  }
}
 
Example 4
Source File: PackagesGroupHandler.java    From nexus-repository-r with Eclipse Public License 1.0 5 votes vote down vote up
protected List<Map<String, String>> parseResponse(@Nonnull final Response response) {
  Payload payload = checkNotNull(response.getPayload());
  try (InputStream in = payload.openInputStream()) {
    final CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();
    try (InputStream cin = compressorStreamFactory.createCompressorInputStream(GZIP, in)) {
      return RPackagesUtils.parseMetadata(cin);
    }
  }
  catch (IOException | CompressorException e) {
    throw new RException(null, e);
  }
}
 
Example 5
Source File: OrientNpmHostedFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private String parseVersionToTag(final NpmPackageId packageId,
                                 @Nullable final String tag,
                                 final Payload payload) throws IOException
{
  String version;
  try (InputStream is = payload.openInputStream()) {
    version = IOUtils.toString(is).replaceAll("\"", "");
    log.debug("Adding tag {}:{} to {}", tag, version, packageId);
  }
  return version;
}
 
Example 6
Source File: NpmAuditFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static PackageLock parseRequest(final Payload payload) throws PackageLockParsingException {
  try (GZIPInputStream stream = new GZIPInputStream(payload.openInputStream(), (int) payload.getSize())) {
    return PackageLockParser.parse(IOUtils.toString(stream));
  }
  catch (IOException e) {
    logger.warn(e.getMessage(), e);
    throw new PackageLockParsingException();
  }
}
 
Example 7
Source File: OrientPyPiIndexGroupHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Processes the content of a particular repository's response.
 */
private void processResults(final Response response, final Map<String, PyPiLink> results) throws IOException {
  checkNotNull(response);
  checkNotNull(results);
  Payload payload = checkNotNull(response.getPayload());
  try (InputStream in = payload.openInputStream()) {
    extractLinksFromIndex(in)
        .forEach(link -> results.putIfAbsent(link.getFile().toLowerCase(), link));
  }
}
 
Example 8
Source File: SearchGroupHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Processes a search response, adding any new entries to the map. If an entry exists already, it is left untouched
 * to preserve the ordering of results from member repositories.
 */
private void processResponse(final Response response, final Map<String, PyPiSearchResult> results) throws Exception {
  checkNotNull(response);
  checkNotNull(results);
  Payload payload = checkNotNull(response.getPayload());
  try (InputStream in = payload.openInputStream()) {
    for (PyPiSearchResult result : parseSearchResponse(in)) {
      String key = result.getName() + " " + result.getVersion();
      if (!results.containsKey(key)) {
        results.put(key, result);
      }
    }
  }
}
 
Example 9
Source File: GolangHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private InputStream doGetModAsStream(final Payload content, final String path) {
  try (InputStream contentStream = content.openInputStream()) {
    InputStream inputStream = compressedContentExtractor.extractFile(contentStream, GO_MOD_FILENAME);
    checkNotNull(inputStream, format("Unable to find file %s in %s", GO_MOD_FILENAME, path));
    return inputStream;
  }
  catch (IOException e) {
    throw new RuntimeException(format("Unable to open content %s", path), e);
  }
}
 
Example 10
Source File: CompressedContentExtractor.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if a file exists in the zip
 *
 * @param payload  zip file as a payload
 * @param path     path of stream to be extracted
 * @param fileName file to check exists
 * @return true if it exists
 */
public boolean fileExists(final Payload payload, final String path, final String fileName) {
  try (InputStream projectAsStream = payload.openInputStream()) {
    return extract(projectAsStream, fileName, (ZipInputStream z) -> new NullInputStream(-1)) != null;
  }
  catch (IOException e) {
    log.warn("Unable to open content {}", path, e);
  }
  return false;
}
 
Example 11
Source File: CocoapodsProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private Content transformSpecFile(final Payload payload) throws IOException {
  try (InputStream data = payload.openInputStream()) {
    String specFile = IOUtils.toString(data, Charsets.UTF_8);
    try {
      return new Content(
          new StringPayload(specTransformer.toProxiedSpec(specFile, URI.create(getRepository().getUrl() + "/")),
              "application/json"));
    }
    catch (InvalidSpecFileException e) {
      log.info("Invalid Spec file", e);
      return null;
    }
  }
}
 
Example 12
Source File: StorageFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TempBlob createTempBlob(final Payload payload, final Iterable<HashAlgorithm> hashAlgorithms)
{
  if (payload instanceof TempBlobPartPayload) {
    return ((TempBlobPartPayload) payload).getTempBlob();
  }
  try (InputStream inputStream = payload.openInputStream()) {
    return createTempBlob(inputStream, hashAlgorithms);
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 13
Source File: FluentBlobsImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TempBlob ingest(final Payload payload, final Iterable<HashAlgorithm> hashing) {
  if (payload instanceof TempBlobPartPayload) {
    return ((TempBlobPartPayload) payload).getTempBlob();
  }
  try (InputStream in = payload.openInputStream()) {
    return ingest(in, payload.getContentType(), hashing);
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example 14
Source File: ComposerJsonProcessor.java    From nexus-repository-composer with Eclipse Public License 1.0 4 votes vote down vote up
private Map<String, Object> parseJson(final Payload payload) throws IOException {
  try (InputStream in = payload.openInputStream()) {
    TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() { };
    return mapper.readValue(in, typeReference);
  }
}
 
Example 15
Source File: MavenITSupport.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
protected Metadata parseMetadata(final Payload content) throws Exception {
  assertThat(content, notNullValue());
  try (InputStream is = content.openInputStream()) {
    return parseMetadata(is);
  }
}