Search in sources :

Example 61 with ByteSource

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);
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) Test(org.junit.Test)

Example 62 with ByteSource

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);
}
Also used : CreateStorageResponse(ddf.catalog.content.operation.CreateStorageResponse) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl) Test(org.junit.Test)

Example 63 with ByteSource

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;
}
Also used : EnvironmentConfig(com.sleepycat.je.EnvironmentConfig) ByteSource(com.google.common.io.ByteSource) Environment(com.sleepycat.je.Environment) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ResolveGeogigDir(org.locationtech.geogig.api.plumbing.ResolveGeogigDir) File(java.io.File) URL(java.net.URL)

Example 64 with ByteSource

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;
}
Also used : Blob(org.jclouds.blobstore.domain.Blob) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) NullInputStream(org.apache.commons.io.input.NullInputStream)

Example 65 with 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);
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) InputStream(java.io.InputStream) FileResource(org.hisp.dhis.fileresource.FileResource) IOException(java.io.IOException) URI(java.net.URI) DataElement(org.hisp.dhis.dataelement.DataElement) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) Event(org.hisp.dhis.dxf2.events.event.Event) ByteSource(com.google.common.io.ByteSource) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ByteSource (com.google.common.io.ByteSource)68 Test (org.junit.Test)33 IOException (java.io.IOException)30 InputStream (java.io.InputStream)22 ByteArrayInputStream (java.io.ByteArrayInputStream)18 Metacard (ddf.catalog.data.Metacard)15 ContentItem (ddf.catalog.content.data.ContentItem)14 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)14 File (java.io.File)14 ArrayList (java.util.ArrayList)10 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)9 URI (java.net.URI)8 HashMap (java.util.HashMap)8 CreateStorageResponse (ddf.catalog.content.operation.CreateStorageResponse)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Path (java.nio.file.Path)7 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)7 FileInputStream (java.io.FileInputStream)6 Serializable (java.io.Serializable)6 StringWriter (java.io.StringWriter)6