use of com.google.common.io.ByteSource in project ddf by codice.
the class FileSystemStorageProviderTest method testUpdateWithQualifier.
@Test
public void testUpdateWithQualifier() throws Exception {
CreateStorageResponse createResponse = assertContentItemWithQualifier(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME, "", QUALIFIER);
String id = createResponse.getCreatedContentItems().get(0).getId();
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return IOUtils.toInputStream("Updated NITF");
}
};
ContentItem updateItem = new ContentItemImpl(id, QUALIFIER, byteSource, NITF_MIME_TYPE, mock(Metacard.class));
submitAndVerifySuccessfulUpdateStorageRequest(updateItem);
}
use of com.google.common.io.ByteSource in project ddf by codice.
the class FileSystemStorageProviderTest method testUpdate.
@Test
public void testUpdate() throws Exception {
CreateStorageResponse createResponse = assertContentItem(TEST_INPUT_CONTENTS, NITF_MIME_TYPE, TEST_INPUT_FILENAME);
String id = createResponse.getCreatedContentItems().get(0).getId();
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return IOUtils.toInputStream("Updated NITF");
}
};
ContentItem updateItem = new ContentItemImpl(id, byteSource, NITF_MIME_TYPE, mock(Metacard.class));
submitAndVerifySuccessfulUpdateStorageRequest(updateItem);
}
use of com.google.common.io.ByteSource in project GeoGig by boundlessgeo.
the class EnvironmentBuilder method get.
/**
* @return
* @see com.google.inject.Provider#get()
*/
@Override
public synchronized Environment get() {
final Optional<URL> repoUrl = new ResolveGeogigDir(platform).call();
if (!repoUrl.isPresent() && absolutePath == null) {
throw new IllegalStateException("Can't find geogig repository home");
}
final File storeDirectory;
if (absolutePath != null) {
storeDirectory = absolutePath;
} else {
File currDir;
try {
currDir = new File(repoUrl.get().toURI());
} catch (URISyntaxException e) {
throw Throwables.propagate(e);
}
File dir = currDir;
for (String subdir : path) {
dir = new File(dir, subdir);
}
storeDirectory = dir;
}
if (!storeDirectory.exists() && !storeDirectory.mkdirs()) {
throw new IllegalStateException("Unable to create Environment directory: '" + storeDirectory.getAbsolutePath() + "'");
}
EnvironmentConfig envCfg;
if (this.forceConfig == null) {
File conf = new File(storeDirectory, "je.properties");
if (!conf.exists()) {
String resource = stagingDatabase ? "je.properties.staging" : "je.properties.objectdb";
ByteSource from = Resources.asByteSource((getClass().getResource(resource)));
try {
from.copyTo(Files.asByteSink(conf));
} catch (IOException e) {
Throwables.propagate(e);
}
}
// use the default settings
envCfg = new EnvironmentConfig();
envCfg.setAllowCreate(true);
envCfg.setCacheMode(CacheMode.MAKE_COLD);
envCfg.setLockTimeout(5, TimeUnit.SECONDS);
envCfg.setDurability(Durability.COMMIT_SYNC);
// envCfg.setReadOnly(readOnly);
} else {
envCfg = this.forceConfig;
}
// // envCfg.setSharedCache(true);
// //
// final boolean transactional = false;
// envCfg.setTransactional(transactional);
// envCfg.setCachePercent(75);// Use up to 50% of the heap size for the shared db cache
// envCfg.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, String.valueOf(256 * 1024 * 1024));
// // check <http://www.oracle.com/technetwork/database/berkeleydb/je-faq-096044.html#35>
// envCfg.setConfigParam("je.evictor.lruOnly", "false");
// envCfg.setConfigParam("je.evictor.nodesPerScan", "100");
//
// envCfg.setConfigParam(EnvironmentConfig.CLEANER_MIN_UTILIZATION, "25");
// envCfg.setConfigParam(EnvironmentConfig.CHECKPOINTER_HIGH_PRIORITY, "true");
//
// envCfg.setConfigParam(EnvironmentConfig.CLEANER_THREADS, "4");
// // TODO: check whether we can set is locking to false
// envCfg.setConfigParam(EnvironmentConfig.ENV_IS_LOCKING, String.valueOf(transactional));
//
// envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_CHECKPOINTER,
// String.valueOf(!transactional));
// envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_CLEANER, String.valueOf(!transactional));
//
// // envCfg.setConfigParam(EnvironmentConfig.ENV_RUN_EVICTOR, "false");
Environment env;
try {
env = new Environment(storeDirectory, envCfg);
} catch (RuntimeException lockedEx) {
// lockedEx.printStackTrace();
if (readOnly) {
// this happens when trying to open the env in read only mode when its already open
// in read/write mode inside the same process. So we re-open it read-write but the
// database itself will be open read-only by JEObjectDatabase.
envCfg.setReadOnly(true);
env = new Environment(storeDirectory, envCfg);
} else {
throw lockedEx;
}
}
return env;
}
use of com.google.common.io.ByteSource in project dhis2-core by dhis2.
the class JCloudsFileResourceContentStore method getFileResourceContent.
// -------------------------------------------------------------------------
// FileResourceContentStore implementation
// -------------------------------------------------------------------------
@Override
public ByteSource getFileResourceContent(String key) {
final Blob blob = getBlob(key);
if (blob == null) {
return null;
}
final ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() {
try {
return blob.getPayload().openStream();
} catch (IOException e) {
return new NullInputStream(0);
}
}
};
boolean isEmptyOrFailed;
try {
isEmptyOrFailed = byteSource.isEmpty();
} catch (IOException e) {
isEmptyOrFailed = true;
}
return isEmptyOrFailed ? null : byteSource;
}
use of com.google.common.io.ByteSource in project dhis2-core by dhis2.
the class EventController method getEventDataValueFile.
@RequestMapping(value = "/files", method = RequestMethod.GET)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD') or hasRole('F_TRACKED_ENTITY_DATAVALUE_READ')")
public void getEventDataValueFile(@RequestParam String eventUid, @RequestParam String dataElementUid, HttpServletResponse response, HttpServletRequest request) throws Exception {
Event event = eventService.getEvent(eventUid);
if (event == null) {
throw new WebMessageException(WebMessageUtils.notFound("Event not found for ID " + eventUid));
}
DataElement dataElement = dataElementService.getDataElement(dataElementUid);
if (dataElement == null) {
throw new WebMessageException(WebMessageUtils.notFound("DataElement not found for ID " + dataElementUid));
}
if (!dataElement.isFileType()) {
throw new WebMessageException(WebMessageUtils.conflict("DataElement must be of type file"));
}
// ---------------------------------------------------------------------
// Get file resource
// ---------------------------------------------------------------------
String uid = null;
for (DataValue value : event.getDataValues()) {
if (value.getDataElement() != null && value.getDataElement().equals(dataElement.getUid())) {
uid = value.getValue();
break;
}
}
if (uid == null) {
throw new WebMessageException(WebMessageUtils.conflict("DataElement must be of type file"));
}
FileResource fileResource = fileResourceService.getFileResource(uid);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(WebMessageUtils.notFound("A data value file resource with id " + uid + " does not exist."));
}
if (fileResource.getStorageStatus() != FileResourceStorageStatus.STORED) {
// -----------------------------------------------------------------
// The FileResource exists and is tied to DataValue, however the
// underlying file content still not stored to external file store
// -----------------------------------------------------------------
WebMessage webMessage = WebMessageUtils.conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.");
webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
throw new WebMessageException(webMessage);
}
ByteSource content = fileResourceService.getFileResourceContent(fileResource);
if (content == null) {
throw new WebMessageException(WebMessageUtils.notFound("The referenced file could not be found"));
}
// ---------------------------------------------------------------------
// Attempt to build signed URL request for content and redirect
// ---------------------------------------------------------------------
URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(uid);
if (signedGetUri != null) {
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
return;
}
// ---------------------------------------------------------------------
// Build response and return
// ---------------------------------------------------------------------
response.setContentType(fileResource.getContentType());
response.setContentLength(new Long(fileResource.getContentLength()).intValue());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
// ---------------------------------------------------------------------
// Request signing is not available, stream content back to client
// ---------------------------------------------------------------------
InputStream inputStream = null;
try {
inputStream = content.openStream();
IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
} finally {
IOUtils.closeQuietly(inputStream);
}
}
Aggregations