Search in sources :

Example 1 with BlobStore

use of org.craftercms.commons.file.blob.BlobStore in project commons by craftercms.

the class BlobStoreResolverImpl method findStore.

protected BlobStore findStore(HierarchicalConfiguration config, Predicate<HierarchicalConfiguration> predicate) throws ConfigurationException {
    Optional<HierarchicalConfiguration> storeConfig = config.configurationsAt(CONFIG_KEY_STORE).stream().filter(predicate).findFirst();
    if (storeConfig.isPresent()) {
        HierarchicalConfiguration store = storeConfig.get();
        String type = store.getString(CONFIG_KEY_TYPE);
        BlobStore instance = applicationContext.getBean(type, BlobStore.class);
        instance.init(store);
        return instance;
    }
    return null;
}
Also used : HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) BlobStore(org.craftercms.commons.file.blob.BlobStore)

Example 2 with BlobStore

use of org.craftercms.commons.file.blob.BlobStore in project core by craftercms.

the class ContentStoreServiceImpl method findContent.

@Override
public Content findContent(Context context, CachingOptions cachingOptions, String url) throws InvalidContextException, StoreException {
    if (context.getStoreAdapter().exists(context, cachingOptions, url)) {
        return context.getStoreAdapter().findContent(context, cachingOptions, url);
    } else {
        String blobUrl = blobUrlResolver.getBlobUrl(url);
        if (context.getStoreAdapter().exists(context, cachingOptions, blobUrl)) {
            Content content = context.getStoreAdapter().findContent(context, cachingOptions, blobUrl);
            try (InputStream is = content.getInputStream()) {
                Blob blob = mapper.readValue(is, Blob.class);
                BlobStore store = blobStoreResolver.getById(new ConfigurationProviderImpl(cachingOptions, context), blob.getStoreId());
                return new ResourceBasedContent(store.getResource(url, blob));
            } catch (IOException | ConfigurationException e) {
                throw new StoreException("Error reading blob file at " + blobUrl, e);
            }
        }
    }
    return null;
}
Also used : Blob(org.craftercms.commons.file.blob.Blob) ConfigurationException(org.craftercms.commons.config.ConfigurationException) Content(org.craftercms.core.service.Content) InputStream(java.io.InputStream) IOException(java.io.IOException) BlobStore(org.craftercms.commons.file.blob.BlobStore) StoreException(org.craftercms.core.exception.StoreException)

Example 3 with BlobStore

use of org.craftercms.commons.file.blob.BlobStore in project studio by craftercms.

the class StudioBlobStoreResolverImpl method getByPaths.

@Override
public BlobStore getByPaths(String site, String... paths) throws ServiceLayerException, ConfigurationException {
    logger.debug("Looking blob store for paths {} for site {}", Arrays.toString(paths), site);
    HierarchicalConfiguration config;
    logger.debug("Checking cache for config");
    String cacheKey = site + CACHE_KEY_CONFIG;
    Element element = cache.get(cacheKey);
    if (element == null || element.isExpired()) {
        logger.debug("Config not found in cache");
        try {
            config = getConfiguration(new ConfigurationProviderImpl(site));
            cache.put(new Element(cacheKey, config));
        } catch (ConfigurationException e) {
            throw new RuntimeException("Error getting blob store configuration for site " + site, e);
        }
    } else {
        config = (HierarchicalConfiguration) element.getObjectValue();
    }
    if (config != null) {
        String storeId = findStoreId(config, store -> paths[0].matches(store.getString(CONFIG_KEY_PATTERN)));
        BlobStore blobStore;
        logger.debug("Checking cache for blob store {}", storeId);
        cacheKey = site + CACHE_KEY_STORE + storeId;
        element = cache.get(cacheKey);
        if (element == null || element.isExpired()) {
            logger.debug("Blob store {} not found in cache", storeId);
            try {
                blobStore = getById(config, storeId);
                cache.put(new Element(cacheKey, blobStore));
            } catch (ConfigurationException e) {
                throw new RuntimeException("Error looking for blob store " + storeId, e);
            }
        } else {
            blobStore = (BlobStore) element.getObjectValue();
        }
        // We have to compare each one to know if the exception should be thrown
        if (blobStore != null && !Stream.of(paths).allMatch(blobStore::isCompatible)) {
            throw new ServiceLayerException("Unsupported operation for paths " + Arrays.toString(paths));
        }
        return blobStore;
    }
    return null;
}
Also used : ConfigurationException(org.craftercms.commons.config.ConfigurationException) Element(net.sf.ehcache.Element) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) BlobStore(org.craftercms.commons.file.blob.BlobStore)

Example 4 with BlobStore

use of org.craftercms.commons.file.blob.BlobStore in project studio by craftercms.

the class StudioBlobStoreResolverImplTest method getByLocalPathTest.

@Test
public void getByLocalPathTest() throws ServiceLayerException, ConfigurationException, IOException {
    BlobStore store = resolver.getByPaths(SITE_ID, LOCAL_PATH, LOCAL_PATH);
    assertNull(store, "store should be null");
}
Also used : StudioBlobStore(org.craftercms.studio.api.v2.repository.blob.StudioBlobStore) BlobStore(org.craftercms.commons.file.blob.BlobStore) Test(org.testng.annotations.Test)

Example 5 with BlobStore

use of org.craftercms.commons.file.blob.BlobStore in project studio by craftercms.

the class StudioBlobStoreResolverImplTest method getByRemotePathTest.

@Test
public void getByRemotePathTest() throws ServiceLayerException, ConfigurationException, IOException {
    BlobStore store = resolver.getByPaths(SITE_ID, REMOTE_PATH, REMOTE_PATH);
    assertNotNull(store, "store should not be null");
    assertNotEquals(store.getId(), STORE_ID);
}
Also used : StudioBlobStore(org.craftercms.studio.api.v2.repository.blob.StudioBlobStore) BlobStore(org.craftercms.commons.file.blob.BlobStore) Test(org.testng.annotations.Test)

Aggregations

BlobStore (org.craftercms.commons.file.blob.BlobStore)5 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)2 ConfigurationException (org.craftercms.commons.config.ConfigurationException)2 StudioBlobStore (org.craftercms.studio.api.v2.repository.blob.StudioBlobStore)2 Test (org.testng.annotations.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Element (net.sf.ehcache.Element)1 Blob (org.craftercms.commons.file.blob.Blob)1 StoreException (org.craftercms.core.exception.StoreException)1 Content (org.craftercms.core.service.Content)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1