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