Java Code Examples for org.sonatype.nexus.blobstore.api.Blob#getInputStream()

The following examples show how to use org.sonatype.nexus.blobstore.api.Blob#getInputStream() . 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: ComposerJsonExtractor.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Extracts the contents for the first matching {@code composer.json} file (of which there should only be one) as a
 * map representing the parsed JSON content. If no such file is found then an empty map is returned.
 */
public Map<String, Object> extractFromZip(final Blob blob) throws IOException {
  try (InputStream is = blob.getInputStream()) {
    try (ArchiveInputStream ais = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) {
      ArchiveEntry entry = ais.getNextEntry();
      while (entry != null) {
        Map<String, Object> contents = processEntry(ais, entry);
        if (!contents.isEmpty()) {
          return contents;
        }
        entry = ais.getNextEntry();
      }
    }
    return Collections.emptyMap();
  }
  catch (ArchiveException e) {
    throw new IOException("Error reading from archive", e);
  }
}
 
Example 2
Source File: NpmFacetUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private static InputStream packageRootAssetToInputStream(final Repository repository, final Asset packageRootAsset) {
  BlobStore blobStore = repository.facet(StorageFacet.class).blobStore();
  if (isNull(blobStore)) {
    throw new MissingAssetBlobException(packageRootAsset);
  }

  BlobRef blobRef = packageRootAsset.requireBlobRef();
  Blob blob = blobStore.get(blobRef.getBlobId());
  if (isNull(blob)) {
    throw new MissingAssetBlobException(packageRootAsset);
  }

  try {
    return blob.getInputStream();
  }
  catch (BlobStoreException ignore) { // NOSONAR
    // we want any issue with the blob store stream to be caught during the getting of the input stream as throw the
    // the same type of exception as a missing asset blob, so that we can pass the associated asset around.
    throw new MissingAssetBlobException(packageRootAsset);
  }
}
 
Example 3
Source File: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void hardDeletePreventsGetDespiteOpenStreams() throws Exception {
  final byte[] content = new byte[TEST_DATA_LENGTH];
  new Random().nextBytes(content);

  final Blob blob = underTest.create(new ByteArrayInputStream(content), TEST_HEADERS);

  final InputStream inputStream = blob.getInputStream();

  // Read half the data
  inputStream.read(new byte[content.length / 2]);

  // force delete
  underTest.deleteHard(blob.getId());

  final Blob newBlob = underTest.get(blob.getId());
  assertThat(newBlob, is(nullValue()));
}
 
Example 4
Source File: DatastoreDeadBlobFinder.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Verify that the Blob exists and is in agreement with the stored Asset metadata.;
 */
private void verifyBlob(final Blob blob, final Asset asset) throws MismatchedSHA1Exception, BlobUnavilableException, IOException {
  BlobMetrics metrics = blob.getMetrics();

  String assetChecksum =
      asset.blob().map(AssetBlob::checksums).map(checksums -> checksums.get(HashAlgorithm.SHA1.name())).orElse(null);
  if (!metrics.getSha1Hash().equals(assetChecksum)) {
    throw new MismatchedSHA1Exception();
  }

  try (InputStream blobstream = blob.getInputStream()) {
    if (metrics.getContentSize() > 0 && blobstream.available() == 0) {
      throw new BlobUnavilableException();
    }
  }
}
 
Example 5
Source File: AptRestoreFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Query getComponentQuery(final Blob blob) throws IOException {
  final InputStream inputStream = blob.getInputStream();
  final PackageInfo packageInfo = new PackageInfo(AptPackageParser.parsePackage(() -> inputStream));
  return Query.builder()
      .where(P_NAME).eq(packageInfo.getPackageName())
      .and(P_VERSION).eq(packageInfo.getVersion())
      .and(P_GROUP).eq(packageInfo.getArchitecture())
      .build();
}
 
Example 6
Source File: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private byte[] extractContent(final Blob blob) throws IOException {
  try (InputStream inputStream = blob.getInputStream()) {
    return ByteStreams.toByteArray(inputStream);
  }
}