Example usage for com.google.common.io FileBackedOutputStream asByteSource

List of usage examples for com.google.common.io FileBackedOutputStream asByteSource

Introduction

In this page you can find the example usage for com.google.common.io FileBackedOutputStream asByteSource.

Prototype

public ByteSource asByteSource() 

Source Link

Document

Returns a readable ByteSource view of the data that has been written to this stream.

Usage

From source file:org.locationtech.geogig.spring.service.LegacyConsoleService.java

private StringBuilder getLimitedOutput(FileBackedOutputStream out, final int limit) throws IOException {

    CharSource charSource = out.asByteSource().asCharSource(Charsets.UTF_8);
    BufferedReader reader = charSource.openBufferedStream();
    final StringBuilder output = new StringBuilder();
    int count = 0;
    String line;/*from w  w  w .j av  a 2  s . c o  m*/
    while ((line = reader.readLine()) != null) {
        output.append(line).append('\n');
        count += line.length();
        if (count >= limit) {
            output.append("\nNote: output limited to ").append(count)
                    .append(" characters. Run config web.console.limit <newlimit> to change the current ")
                    .append(limit).append(" soft limit.");
            break;
        }
    }
    return output;
}

From source file:org.codice.alliance.transformer.video.MpegTsInputTransformer.java

/**
 * Call the inner transformer with the content data and return a metacard based on
 * {@link #metacardTypes} that is populated by the inner transformer and with the
 * content type set to {@link #CONTENT_TYPE}.
 *
 * @param id//from   w ww  . j  a v  a 2  s. c  o m
 * @param fileBackedOutputStream
 * @return metacard
 * @throws IOException
 * @throws CatalogTransformerException
 */
private MetacardImpl extractInnerTransformerMetadata(String id, FileBackedOutputStream fileBackedOutputStream)
        throws IOException, CatalogTransformerException {

    try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {

        MetacardType metacardType = metacardTypes.stream().findFirst().orElseThrow(
                () -> new CatalogTransformerException("no matching metacard type found! id = " + id));

        Metacard innerMetacard = innerTransformer.transform(inputStream, id);

        MetacardImpl metacard = new MetacardImpl(innerMetacard, metacardType);

        metacard.setContentTypeName(CONTENT_TYPE);

        return metacard;
    }
}

From source file:org.codice.alliance.transformer.video.MpegTsInputTransformer.java

private void extractStanag4609Metadata(MetacardImpl metacard, FileBackedOutputStream fbos)
        throws CatalogTransformerException {

    Stanag4609Parser stanag4609Parser = stanagParserFactory.createParser(fbos.asByteSource());

    Map<Integer, List<DecodedKLVMetadataPacket>> decodedMetadata;
    try {/* w  w  w.  ja v a2 s.c o m*/
        decodedMetadata = stanag4609Parser.parse();
    } catch (Stanag4609ParseException e) {
        throw new CatalogTransformerException("failed to extract STANAG 4609 metadata", e);
    }

    Map<String, KlvHandler> handlers = klvHandlerFactory.createStanag4609Handlers();

    stanag4609Processor.handle(handlers, defaultKlvHandler, decodedMetadata);

    KlvProcessor.Configuration klvProcessConfiguration = new KlvProcessor.Configuration();
    klvProcessConfiguration.set(KlvProcessor.Configuration.SUBSAMPLE_COUNT, subsampleCount);

    klvProcessor.process(handlers, metacard, klvProcessConfiguration);

}

From source file:ddf.catalog.resource.download.ReliableResourceInputStream.java

/**
 * @param fbos          the @FileBackedOutputStream this object will read from
 * @param countingFbos  wrapped @FileBackedOutputStream that counts the number of bytes written so far
 * @param downloadState the current state of the resource's download
 *//*from w w  w  .j a  v a2  s.  c o m*/
public ReliableResourceInputStream(FileBackedOutputStream fbos, CountingOutputStream countingFbos,
        DownloadManagerState downloadState, String downloadIdentifier, ResourceResponse resourceResponse) {
    this.fbos = fbos;
    fbosByteSource = fbos.asByteSource();
    this.countingFbos = countingFbos;
    this.downloadState = downloadState;
    this.downloadIdentifier = downloadIdentifier;
    this.resourceResponse = resourceResponse;
}

From source file:ddf.catalog.core.ftp.ftplets.FtpRequestHandler.java

protected String getMimeType(String fileExtension, FileBackedOutputStream outputStream) {
    String mimeType = "";

    if (mimeTypeMapper != null) {
        try (InputStream inputStream = outputStream.asByteSource().openBufferedStream()) {
            if (fileExtension.equals("xml")) {
                mimeType = mimeTypeMapper.guessMimeType(inputStream, fileExtension);
            } else {
                mimeType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
            }/*w  w  w. j  a  v  a2s . com*/

        } catch (MimeTypeResolutionException | IOException e) {
            LOGGER.warn("Did not find the MimeTypeMapper service. Proceeding with empty mimetype");
        }
    } else {
        LOGGER.warn("Did not find the MimeTypeMapper service. Proceeding with empty mimetype");
    }

    return mimeType;
}

From source file:org.codice.ddf.security.crl.generator.CrlGenerator.java

/** Pulls down the CRL file provided via URL and writes it locally. */
@Override/*from  ww w.  ja  v a2  s  .com*/
public synchronized void run() {
    if (!crlByUrlEnabled || crlLocationUrl == null) {
        return;
    }

    if (!crlLocationUrl.startsWith(HTTPS)) {
        postErrorEvent("The provided URL was not an HTTPS URL.");
        return;
    }

    Object entity = getRemoteCrl();
    if (!(entity instanceof InputStream)) {
        postErrorEvent("Unable to download the remote CRL.");
        return;
    }

    FileBackedOutputStream fileBackedOutputStream = null;
    try {
        // Read the response content and get the byte source
        ByteSource byteSource;
        try (InputStream entityStream = (InputStream) entity) {
            fileBackedOutputStream = new FileBackedOutputStream(FILE_BACKED_STREAM_THRESHOLD);
            IOUtils.copy(entityStream, fileBackedOutputStream);
            fileBackedOutputStream.close();
            byteSource = fileBackedOutputStream.asByteSource();
        }

        File crlFile = getCrlFile(byteSource);
        // Verify that the CRL is valid
        if (!crlIsValid(byteSource)) {
            postErrorEvent("An error occurred while validating the CRL.");
            return;
        }
        writeCrlToFile(byteSource, crlFile);
    } catch (IOException e) {
        LOGGER.warn("Unable to copy CRL to local CRL. {}", e.getMessage());
        postErrorEvent("An error occurred while downloading the CRL.");
    } finally {
        // Cleanup temp file
        if (fileBackedOutputStream != null) {
            try {
                fileBackedOutputStream.reset();
            } catch (IOException e) {
                LOGGER.warn("Error occurred while deleting the temporary file. {}", e.getMessage());
            }
        }
    }
}

From source file:ddf.camel.component.content.ContentProducer.java

@Override
public void process(Exchange exchange) throws ContentComponentException, ContentFrameworkException {
    LOGGER.debug("ENTERING: process");

    if (!exchange.getPattern().equals(ExchangePattern.InOnly)) {
        return;/*from   w ww.j a v  a 2 s. c o m*/
    }

    Message in = exchange.getIn();
    Object body = in.getBody();
    File ingestedFile = null;
    if (body instanceof GenericFile) {
        GenericFile<File> genericFile = (GenericFile<File>) body;
        ingestedFile = (File) genericFile.getFile();
    } else {
        LOGGER.warn("Unable to cast message body to Camel GenericFile, so unable to process ingested file");
        throw new ContentComponentException(
                "Unable to cast message body to Camel GenericFile, so unable to process ingested file");
    }

    if (ingestedFile == null) {
        LOGGER.debug("EXITING: process - ingestedFile is NULL");
        return;
    }

    String operation = in.getHeader(Request.OPERATION, String.class);
    if (StringUtils.isEmpty(operation)) {
        throw new ContentComponentException("Unable to process file " + ingestedFile.getName()
                + "  -  Must specify an operation of create, read, update, or delete");
    }

    String directive = in.getHeader(Request.DIRECTIVE, String.class);
    if (StringUtils.isEmpty(directive)) {
        throw new ContentComponentException("Unable to process file " + ingestedFile.getName()
                + "  -  Must specify a directive of STORE, PROCESS, or STORE_PROCESS");
    }

    String contentUri = (String) in.getHeader(Request.CONTENT_URI, "");

    LOGGER.debug("operation = {}", operation);
    LOGGER.debug("directive = {}", directive);
    LOGGER.debug("contentUri = {}", contentUri);

    FileInputStream fis = null;
    FileBackedOutputStream fbos = null;
    try {
        fis = FileUtils.openInputStream(ingestedFile);
    } catch (IOException e) {
        throw new ContentComponentException("Unable to open file {}" + ingestedFile.getName());
    }

    Request.Directive requestDirective = Request.Directive.valueOf(directive);

    String fileExtension = FilenameUtils.getExtension(ingestedFile.getAbsolutePath());

    String mimeType = null;
    if (fileExtension != null) {
        MimeTypeMapper mimeTypeMapper = endpoint.getComponent().getMimeTypeMapper();
        if (mimeTypeMapper != null) {
            try {
                // XML files are mapped to multiple mime types, e.g., CSW, XML Metacard, etc.
                // If processing a .xml file, then use a stream that allows multiple reads on it
                // i.e., FileBackedOutputStream (FBOS), since the MimeTypeMapper will need to introspect
                // the xml file for its root element namespace and later the stream will need to
                // be read to persist it to the content store and/or create a metacard for ingest.
                // The FBOS is not used for every ingested file because do not want the performance
                // hit of potentially reading in a 1 GB NITF file that does not need to be introspected
                // to determine the mime type.
                if (fileExtension.equals("xml")) {
                    // FBOS reads file into byte array in memory up to this threshold, then it transitions
                    // to writing to a file.
                    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
                    IOUtils.copy(fis, fbos);
                    // Using fbos.asByteSource().openStream() allows us to pass in a copy of the InputStream
                    mimeType = mimeTypeMapper.guessMimeType(fbos.asByteSource().openStream(), fileExtension);
                } else {
                    mimeType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
                }

            } catch (MimeTypeResolutionException | IOException e) {
                IOUtils.closeQuietly(fis);
                IOUtils.closeQuietly(fbos);
                throw new ContentComponentException(e);
            }
        } else {
            IOUtils.closeQuietly(fis);
            LOGGER.error("Did not find a MimeTypeMapper service");
            throw new ContentComponentException(
                    "Unable to find a mime type for the ingested file " + ingestedFile.getName());
        }
    }

    try {
        LOGGER.debug("Preparing content item for mimeType = {}", mimeType);

        if (StringUtils.isNotEmpty(mimeType)) {
            ContentItem newItem;
            // If the FileBackedOutputStream (FBOS) was used, then must be processing an XML file and need to
            // get a fresh InputStream from the FBOS to use in the content item
            if (fbos != null) {
                newItem = new IncomingContentItem(fbos.asByteSource().openStream(), mimeType,
                        ingestedFile.getName());
            } else {
                // Otherwise, the InputStream created from the ingested file has never been read,
                // so can use it in the content item
                newItem = new IncomingContentItem(fis, mimeType, ingestedFile.getName());
            }
            newItem.setUri(contentUri);

            LOGGER.debug("Creating content item.");

            CreateRequest createRequest = new CreateRequestImpl(newItem, null);
            CreateResponse createResponse = endpoint.getComponent().getContentFramework().create(createRequest,
                    requestDirective);
            if (createResponse != null) {
                ContentItem contentItem = createResponse.getCreatedContentItem();

                LOGGER.debug("content item created with id = {}", contentItem.getId());
                LOGGER.debug(contentItem.toString());
            }
        } else {
            LOGGER.debug("mimeType is NULL");
            throw new ContentComponentException(
                    "Unable to determine mime type for the file " + ingestedFile.getName());
        }
    } catch (IOException e) {
        throw new ContentComponentException(
                "Unable to determine mime type for the file " + ingestedFile.getName());
    } finally {
        IOUtils.closeQuietly(fis);
        if (fbos != null) {
            try {
                fbos.reset();
            } catch (IOException e) {
                LOGGER.info("Unable to reset FileBackedOutputStream");
            }
        }
    }

    LOGGER.debug("EXITING: process");
}

From source file:ddf.content.endpoint.rest.ContentEndpoint.java

CreateInfo parseAttachment(Attachment contentPart) {
    CreateInfo createInfo = new CreateInfo();

    InputStream stream = null;//from   w ww .  java  2s  .  co m
    FileBackedOutputStream fbos = null;
    String filename = null;
    String contentType = null;

    // Get the file contents as an InputStream and ensure the stream is positioned
    // at the beginning
    try {
        stream = contentPart.getDataHandler().getInputStream();
        if (stream != null && stream.available() == 0) {
            stream.reset();
        }
        createInfo.setStream(stream);
    } catch (IOException e) {
        LOGGER.warn("IOException reading stream from file attachment in multipart body", e);
    }

    // Example Content-Type header:
    // Content-Type: application/json;id=geojson
    if (contentPart.getContentType() != null) {
        contentType = contentPart.getContentType().toString();
    }

    filename = contentPart.getContentDisposition().getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME);

    // Only interested in attachments for file uploads. Any others should be covered by
    // the FormParam arguments.
    // If the filename was not specified, then generate a default filename based on the
    // specified content type.
    if (StringUtils.isEmpty(filename)) {
        LOGGER.debug("No filename parameter provided - generating default filename");
        String fileExtension = DEFAULT_FILE_EXTENSION;
        try {
            fileExtension = mimeTypeMapper.getFileExtensionForMimeType(contentType); // DDF-2307
            if (StringUtils.isEmpty(fileExtension)) {
                fileExtension = DEFAULT_FILE_EXTENSION;
            }
        } catch (MimeTypeResolutionException e) {
            LOGGER.debug("Exception getting file extension for contentType = {}", contentType);
        }
        filename = DEFAULT_FILE_NAME + "." + fileExtension; // DDF-2263
        LOGGER.debug("No filename parameter provided - default to {}", filename);
    } else {
        filename = FilenameUtils.getName(filename);

        // DDF-908: filename with extension was specified by the client. If the
        // contentType is null or the browser default, try to refine the contentType
        // by determining the mime type based on the filename's extension.
        if (StringUtils.isEmpty(contentType) || REFINEABLE_MIME_TYPES.contains(contentType)) {
            String fileExtension = FilenameUtils.getExtension(filename);
            LOGGER.debug("fileExtension = {}, contentType before refinement = {}", fileExtension, contentType);
            if (fileExtension.equals("xml")) {
                // FBOS reads file into byte array in memory up to this threshold, then it transitions
                // to writing to a file.
                fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
                try {
                    IOUtils.copy(stream, fbos);
                    // Using fbos.asByteSource().openStream() allows us to pass in a copy of the InputStream
                    contentType = mimeTypeMapper.guessMimeType(fbos.asByteSource().openStream(), fileExtension);
                    createInfo.setStream(fbos.asByteSource().openStream());
                } catch (IOException | MimeTypeResolutionException e) {
                    LOGGER.debug("Unable to refine contentType {} based on filename extension {}", contentType,
                            fileExtension);
                }
            } else {
                try {
                    contentType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
                } catch (MimeTypeResolutionException e) {
                    LOGGER.debug("Unable to refine contentType {} based on filename extension {}", contentType,
                            fileExtension);
                }
            }
            LOGGER.debug("Refined contentType = {}", contentType);
        }
    }

    createInfo.setContentType(contentType);
    createInfo.setFilename(filename);

    return createInfo;
}