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();
}
}
}
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);
}
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;
}
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);
}
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;
}
Aggregations