Example usage for com.google.common.io ByteSource openBufferedStream

List of usage examples for com.google.common.io ByteSource openBufferedStream

Introduction

In this page you can find the example usage for com.google.common.io ByteSource openBufferedStream.

Prototype

public InputStream openBufferedStream() throws IOException 

Source Link

Document

Opens a new buffered InputStream for reading from this source.

Usage

From source file:com.hpe.caf.worker.datastore.cs.StorageServiceDataStore.java

private String store(ByteSource byteSource, String partialReference) throws DataStoreException {

    LOG.debug("Received store request for {}", partialReference);
    numTx.incrementAndGet();//  w  w w  . j ava 2  s .  co  m

    //  Parse incoming partial reference. Extract container identifier and delegation ticket if provided.
    ReferenceComponents refComponents = ReferenceComponents.parseReference(partialReference);
    String containerId = refComponents.getReference();
    String delegationTicket = refComponents.getNamedValue(DELEGATION_TICKET_NAMED_PARAMETER);

    try (InputStream inputStream = byteSource.openBufferedStream()) {

        final GetAssetContainerEncryptionKeyRequest encryptionRequest = new GetAssetContainerEncryptionKeyRequest(
                accessToken, containerId);
        if (delegationTicket != null) {
            encryptionRequest.setDelegationTicket(delegationTicket);
        }

        WrappedKey wrappedKey = callStorageService(c -> c.getAssetContainerEncryptionKey(encryptionRequest));

        UploadAssetRequest uploadRequest = new UploadAssetRequest(accessToken, containerId,
                UUID.randomUUID().toString(), wrappedKey, inputStream);

        //  If delegation ticket has been provided then set it as part of the upload request.
        if (delegationTicket != null) {
            uploadRequest.setDelegationTicket(delegationTicket);
        }

        AssetMetadata assetMetadata = callStorageService(c -> c.uploadAsset(uploadRequest, null));

        String returnValue = null;
        if (delegationTicket != null) {
            //  Return the delegation ticket as part of the CafStoreReference value.
            String encodedDelegationTicket = "?delegationTicket="
                    + URLEncoder.encode(delegationTicket, StandardCharsets.UTF_8.toString());
            returnValue = new CafStoreReference(assetMetadata.getContainerId(), assetMetadata.getAssetId())
                    .toString() + encodedDelegationTicket;
        } else {
            returnValue = new CafStoreReference(assetMetadata.getContainerId(), assetMetadata.getAssetId())
                    .toString();
        }

        return returnValue;
    } catch (IOException e) {
        errors.incrementAndGet();
        throw new DataStoreException("Failed to open buffered stream.", e);
    } catch (StorageClientException | StorageServiceException | StorageServiceConnectException e) {
        throw new DataStoreException("Failed to store data", e);
    }
}

From source file:org.codice.ddf.commands.catalog.IngestCommand.java

private void processIncludeContent(ArrayBlockingQueue<Metacard> metacardQueue) {
    File inputFile = new File(filePath);
    Map<String, Serializable> arguments = new HashMap<>();
    arguments.put(DumpCommand.FILE_PATH, inputFile.getParent() + File.separator);
    arguments.put(FILE_NAME, inputFile.getName());

    ByteSource byteSource = com.google.common.io.Files.asByteSource(inputFile);

    Optional<InputCollectionTransformer> zipDecompression = getZipDecompression();
    if (zipDecompression.isPresent()) {
        try (InputStream inputStream = byteSource.openBufferedStream()) {
            List<Metacard> metacardList = zipDecompression.get().transform(inputStream, arguments).stream()
                    .filter(Objects::nonNull).collect(Collectors.toList());

            if (metacardList.size() != 0) {
                metacardFileMapping = generateFileMap(new File(inputFile.getParent(), CONTENT_PATH));
                fileCount.set(metacardList.size());

                for (Metacard metacard : metacardList) {
                    putMetacardOnQueue(metacardQueue, metacard);
                }/*  w w  w  .ja v  a 2s . co  m*/
            }
        } catch (IOException | CatalogTransformerException e) {
            LOGGER.info("Unable to transform zip file into metacard list.", e);
            INGEST_LOGGER.warn("Unable to transform zip file into metacard list.", e);
        }
    } else {
        LOGGER.info("No Zip Transformer found. Unable to transform zip file into metacard list.");
        INGEST_LOGGER.warn("No Zip Transformer found. Unable to transform zip file into metacard list.");
    }
}