Search in sources :

Example 1 with FileBackedOutputStream

use of com.google.common.io.FileBackedOutputStream in project GeoGig by boundlessgeo.

the class ConsoleResourceResource method processRequest.

private JsonObject processRequest(JsonObject json, final GeoGIG geogig) {
    JsonObject response;
    final String command = json.get("method").getAsString();
    final String queryId = json.get("id").getAsString();
    // not used, we're getting the whole command and args in the "method" object
    // JsonArray paramsArray = json.get("params").getAsJsonArray();
    InputStream in = new ByteArrayInputStream(new byte[0]);
    // dumps output to a temp file if > threshold
    FileBackedOutputStream out = new FileBackedOutputStream(4096);
    try {
        // pass it a BufferedOutputStream 'cause it doesn't buffer the internal FileOutputStream
        ConsoleReader console = new ConsoleReader(in, new BufferedOutputStream(out), new UnsupportedTerminal());
        Platform platform = geogig.getPlatform();
        GeogigCLI geogigCLI = new GeogigCLI(geogig, console);
        geogigCLI.setPlatform(platform);
        geogigCLI.disableProgressListener();
        String[] args = ArgumentTokenizer.tokenize(command);
        final int exitCode = geogigCLI.execute(args);
        response = new JsonObject();
        response.addProperty("id", queryId);
        final int charCountLimit = getOutputLimit(geogig.getContext());
        final StringBuilder output = getLimitedOutput(out, charCountLimit);
        if (exitCode == 0) {
            response.addProperty("result", output.toString());
            response.addProperty("error", (String) null);
        } else {
            Exception exception = geogigCLI.exception;
            JsonObject error = buildError(exitCode, output, exception);
            response.add("error", error);
        }
        return response;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    } finally {
        // delete temp file
        try {
            out.reset();
        } catch (IOException ignore) {
            ignore.printStackTrace();
        }
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) Platform(org.locationtech.geogig.api.Platform) UnsupportedTerminal(jline.UnsupportedTerminal) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) GeogigCLI(org.locationtech.geogig.cli.GeogigCLI) ByteArrayInputStream(java.io.ByteArrayInputStream) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with FileBackedOutputStream

use of com.google.common.io.FileBackedOutputStream in project ddf by codice.

the class ReliableResourceInputStreamTest method setup.

@Before
public void setup() {
    fbos = new FileBackedOutputStream(THRESHOLD);
    countingFbos = new CountingOutputStream(fbos);
    downloadState = mock(DownloadManagerState.class);
    when(downloadState.getDownloadState()).thenReturn(DownloadManagerState.DownloadState.COMPLETED);
    reliableResourceCallable = mock(ReliableResourceCallable.class);
    downloadFuture = mock(Future.class);
    downloadIdentifier = UUID.randomUUID().toString();
    resourceResponse = mock(ResourceResponse.class);
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) ResourceResponse(ddf.catalog.operation.ResourceResponse) Future(java.util.concurrent.Future) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) Before(org.junit.Before)

Example 3 with FileBackedOutputStream

use of com.google.common.io.FileBackedOutputStream in project ddf by codice.

the class ReliableResourceDownloader method setupDownload.

public ResourceResponse setupDownload(Metacard metacard, DownloadStatusInfo downloadStatusInfo) {
    Resource resource = resourceResponse.getResource();
    MimeType mimeType = resource.getMimeType();
    String resourceName = resource.getName();
    fbos = new FileBackedOutputStream(DEFAULT_FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
    countingFbos = new CountingOutputStream(fbos);
    streamReadByClient = new ReliableResourceInputStream(fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
    this.metacard = metacard;
    // Create new ResourceResponse to return that will encapsulate the
    // ReliableResourceInputStream that will be read by the client simultaneously as the product
    // is cached to disk (if caching is enabled)
    ResourceImpl newResource = new ResourceImpl(streamReadByClient, mimeType, resourceName);
    resourceResponse = new ResourceResponseImpl(resourceResponse.getRequest(), resourceResponse.getProperties(), newResource);
    // Get handle to retrieved product's InputStream
    resourceInputStream = resource.getInputStream();
    eventListener.setDownloadMap(downloadIdentifier, resourceResponse);
    downloadStatusInfo.addDownloadInfo(downloadIdentifier, this, resourceResponse);
    if (downloaderConfig.isCacheEnabled()) {
        CacheKey keyMaker = null;
        String key = null;
        try {
            keyMaker = new CacheKey(metacard, resourceResponse.getRequest());
            key = keyMaker.generateKey();
        } catch (IllegalArgumentException e) {
            LOGGER.info("Cannot create cache key for resource with metacard ID = {}", metacard.getId());
            return resourceResponse;
        }
        if (!resourceCache.isPending(key)) {
            // Fully qualified path to cache file that will be written to.
            // Example:
            // <INSTALL-DIR>/data/product-cache/<source-id>-<metacard-id>
            // <INSTALL-DIR>/data/product-cache/ddf.distribution-abc123
            filePath = FilenameUtils.concat(resourceCache.getProductCacheDirectory(), key);
            if (filePath == null) {
                LOGGER.info("Unable to create cache for cache directory {} and key {} - no caching will be done.", resourceCache.getProductCacheDirectory(), key);
                return resourceResponse;
            }
            reliableResource = new ReliableResource(key, filePath, mimeType, resourceName, metacard);
            resourceCache.addPendingCacheEntry(reliableResource);
            try {
                fos = FileUtils.openOutputStream(new File(filePath));
                doCaching = true;
                this.downloadState.setCacheEnabled(true);
            } catch (IOException e) {
                LOGGER.info("Unable to open cache file {} - no caching will be done.", filePath);
            }
        } else {
            LOGGER.debug("Cache key {} is already pending caching", key);
        }
    }
    return resourceResponse;
}
Also used : ReliableResource(ddf.catalog.resource.data.ReliableResource) Resource(ddf.catalog.resource.Resource) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) MimeType(javax.activation.MimeType) ReliableResource(ddf.catalog.resource.data.ReliableResource) CountingOutputStream(com.google.common.io.CountingOutputStream) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) File(java.io.File) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 4 with FileBackedOutputStream

use of com.google.common.io.FileBackedOutputStream in project ddf by codice.

the class TemporaryFileBackedOutputStream method reset.

/**
     * Reset fileBackedOutputStream and retry if it fails.
     */
@SuppressWarnings("unchecked")
private void reset() {
    RetryPolicy retryPolicy = new RetryPolicy().retryOn(IOException.class).withBackoff(INITIAL_RETRY_SLEEP, MAX_DELAY, INITIAL_RETRY_SLEEP_UNIT).withMaxRetries(MAX_RETRY_ATTEMPTS);
    Failsafe.with(retryPolicy).onFailedAttempt(throwable -> LOGGER.debug("failed to delete temporary file, will retry", throwable)).onFailure(throwable -> LOGGER.debug("failed to delete temporary file", throwable)).run(fileBackedOutputStream::reset);
}
Also used : OutputStream(java.io.OutputStream) TimeUnit(java.util.concurrent.TimeUnit) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) RetryPolicy(net.jodah.failsafe.RetryPolicy) ByteSource(com.google.common.io.ByteSource) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) Failsafe(net.jodah.failsafe.Failsafe) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 5 with FileBackedOutputStream

use of com.google.common.io.FileBackedOutputStream in project ddf by codice.

the class RESTEndpoint method parseAttachment.

CreateInfo parseAttachment(Attachment contentPart) {
    CreateInfo createInfo = new CreateInfo();
    InputStream stream = null;
    FileBackedOutputStream fbos = null;
    String filename = null;
    String contentType = null;
    // at the beginning
    try {
        stream = contentPart.getDataHandler().getInputStream();
        if (stream != null && stream.available() == 0) {
            stream.reset();
        }
        createInfo.setStream(stream);
    } catch (IOException e) {
        LOGGER.info("IOException reading stream from file attachment in multipart body", e);
    }
    // Content-Type: application/json;id=geojson
    if (contentPart.getContentType() != null) {
        contentType = contentPart.getContentType().toString();
    }
    if (contentPart.getContentDisposition() != null) {
        filename = contentPart.getContentDisposition().getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME);
    }
    // specified content type.
    if (StringUtils.isEmpty(filename)) {
        LOGGER.debug("No filename parameter provided - generating default filename");
        String fileExtension = DEFAULT_FILE_EXTENSION;
        try {
            // DDF-2307
            fileExtension = mimeTypeMapper.getFileExtensionForMimeType(contentType);
            if (StringUtils.isEmpty(fileExtension)) {
                fileExtension = DEFAULT_FILE_EXTENSION;
            }
        } catch (MimeTypeResolutionException e) {
            LOGGER.debug("Exception getting file extension for contentType = {}", contentType);
        }
        // DDF-2263
        filename = DEFAULT_FILE_NAME + "." + fileExtension;
        LOGGER.debug("No filename parameter provided - default to {}", filename);
    } else {
        filename = FilenameUtils.getName(filename);
        // 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);
            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;
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) IOException(java.io.IOException)

Aggregations

FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)5 IOException (java.io.IOException)4 CountingOutputStream (com.google.common.io.CountingOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ByteSource (com.google.common.io.ByteSource)1 JsonObject (com.google.gson.JsonObject)1 CacheKey (ddf.catalog.cache.impl.CacheKey)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)1 Resource (ddf.catalog.resource.Resource)1 ReliableResource (ddf.catalog.resource.data.ReliableResource)1 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)1 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 MimeType (javax.activation.MimeType)1