Search in sources :

Example 1 with StoreException

use of org.craftercms.core.exception.StoreException in project core by craftercms.

the class ContentStoreServiceImpl method createContext.

/**
 * {@inheritDoc}
 */
@Override
public Context createContext(String storeType, String storeServerUrl, String username, String password, String rootFolderPath, boolean mergingOn, boolean cacheOn, int maxAllowedItemsInCache, boolean ignoreHiddenFiles) throws InvalidStoreTypeException, RootFolderNotFoundException, StoreException, AuthenticationException {
    String id = createContextId(storeType, storeServerUrl, username, password, rootFolderPath, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
    if (!contexts.containsKey(id)) {
        ContentStoreAdapter storeAdapter = storeAdapterRegistry.get(storeType);
        if (storeAdapter == null) {
            throw new InvalidStoreTypeException("No registered content store adapter for store type " + storeType);
        }
        Context context = storeAdapter.createContext(id, storeServerUrl, username, password, rootFolderPath, mergingOn, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
        cacheTemplate.getCacheService().addScope(context);
        contexts.put(id, context);
        return context;
    } else {
        throw new StoreException("A context for id '" + id + "' already exists");
    }
}
Also used : Context(org.craftercms.core.service.Context) InvalidStoreTypeException(org.craftercms.core.exception.InvalidStoreTypeException) ContentStoreAdapter(org.craftercms.core.store.ContentStoreAdapter) StoreException(org.craftercms.core.exception.StoreException)

Example 2 with StoreException

use of org.craftercms.core.exception.StoreException in project engine by craftercms.

the class S3ContentStoreAdapter method getContent.

@Override
protected Content getContent(Context context, CachingOptions cachingOptions, File file) throws InvalidContextException, StoreException {
    S3Context s3Context = (S3Context) context;
    String key = ((S3File) file).getKey();
    logger.debug("Getting content for key {}", key);
    try {
        GetObjectRequest request = new GetObjectRequest(s3Context.getBucket(), key);
        S3Object object = client.getObject(request);
        return new S3Content(object);
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            throw new StoreException("No item found for key " + key);
        } else {
            throw new StoreException("Error getting item for key " + key, e);
        }
    }
}
Also used : StoreException(org.craftercms.core.exception.StoreException)

Example 3 with StoreException

use of org.craftercms.core.exception.StoreException in project core by craftercms.

the class AbstractFileBasedContentStoreAdapter method doFindItem.

@Override
protected Item doFindItem(Context context, CachingOptions cachingOptions, String path, boolean withDescriptor) throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);
    File file = findFile(context, path);
    if (file == null) {
        return null;
    }
    Item item = new Item();
    item.setName(file.getName());
    item.setUrl(path);
    item.setFolder(file.isDirectory());
    if (withDescriptor) {
        File descriptorFile;
        // a DOM.
        if (file.isFile() && item.getName().endsWith(descriptorFileExtension)) {
            item.setDescriptorUrl(path);
            descriptorFile = file;
        // If it's not a file (a dir) or is not a descriptor (a static asset, like an image), locate the file's
        // descriptor by appending a metadata file extension to the file name. If the file exists, load it as
        // a DOM.
        } else {
            String descriptorPath = FilenameUtils.removeExtension(path) + metadataFileExtension;
            item.setDescriptorUrl(descriptorPath);
            descriptorFile = findFile(context, descriptorPath);
            if (descriptorFile != null && !descriptorFile.isFile()) {
                throw new StoreException("Descriptor file at " + descriptorFile + " is not really a file");
            }
        }
        if (descriptorFile != null) {
            try {
                InputStream fileInputStream = new BufferedInputStream(descriptorFile.getInputStream());
                Reader fileReader = new InputStreamReader(fileInputStream, charset);
                try {
                    item.setDescriptorDom(createXmlReader().read(fileReader));
                } finally {
                    IOUtils.closeQuietly(fileReader);
                }
            } catch (IOException e) {
                throw new StoreException("Unable to open input stream for descriptor file at " + descriptorFile, e);
            } catch (DocumentException e) {
                throw new XmlFileParseException("Error while parsing xml document at " + descriptorFile, e);
            }
        }
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) DocumentException(org.dom4j.DocumentException) SAXReader(org.dom4j.io.SAXReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) StoreException(org.craftercms.core.exception.StoreException) XmlFileParseException(org.craftercms.core.exception.XmlFileParseException)

Example 4 with StoreException

use of org.craftercms.core.exception.StoreException in project core by craftercms.

the class AbstractFileBasedContentStoreAdapter method doFindItems.

@Override
protected List<Item> doFindItems(Context context, CachingOptions cachingOptions, String path, boolean withDescriptor) throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);
    File dir = findFile(context, path);
    if (dir == null) {
        return null;
    }
    if (!dir.isDirectory()) {
        throw new StoreException("The path " + dir + " doesn't correspond to a dir");
    }
    List<File> children = getChildren(context, dir);
    CachingAwareList<Item> items = new CachingAwareList<>(children.size());
    if (CollectionUtils.isNotEmpty(children)) {
        for (File child : children) {
            // items.
            if (!child.isFile() || !child.getName().endsWith(metadataFileExtension)) {
                String fileRelPath = path + (!path.equals("/") ? "/" : "") + child.getName();
                Item item = findItem(context, cachingOptions, fileRelPath, withDescriptor);
                if (item != null) {
                    items.add(item);
                    items.addDependencyKey(item.getKey());
                }
            }
        }
    }
    return items;
}
Also used : Item(org.craftercms.core.service.Item) StoreException(org.craftercms.core.exception.StoreException) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList)

Example 5 with StoreException

use of org.craftercms.core.exception.StoreException in project core by craftercms.

the class FileSystemContentStoreAdapter method createContext.

@Override
public Context createContext(String id, String storeServerUrl, String username, String password, String rootFolderPath, boolean mergingOn, boolean cacheOn, int maxAllowedItemsInCache, boolean ignoreHiddenFiles) throws RootFolderNotFoundException, StoreException, AuthenticationException {
    Resource rootFolderResource = resourceLoader.getResource(rootFolderPath);
    if (!rootFolderResource.exists()) {
        throw new RootFolderNotFoundException("Root folder " + rootFolderPath + " not found (make sure that it has a valid URL " + "prefix (e.g. file:))");
    }
    FileSystemFile rootFolder;
    try {
        rootFolder = new FileSystemFile(rootFolderResource.getFile());
    } catch (IOException e) {
        throw new StoreException("Unable to retrieve file handle for root folder " + rootFolderPath, e);
    }
    return new FileSystemContext(id, this, null, rootFolderPath, rootFolder, mergingOn, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
}
Also used : Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) RootFolderNotFoundException(org.craftercms.core.exception.RootFolderNotFoundException) StoreException(org.craftercms.core.exception.StoreException)

Aggregations

StoreException (org.craftercms.core.exception.StoreException)6 IOException (java.io.IOException)2 Item (org.craftercms.core.service.Item)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 InvalidStoreTypeException (org.craftercms.core.exception.InvalidStoreTypeException)1 RootFolderNotFoundException (org.craftercms.core.exception.RootFolderNotFoundException)1 XmlFileParseException (org.craftercms.core.exception.XmlFileParseException)1 Context (org.craftercms.core.service.Context)1 ContentStoreAdapter (org.craftercms.core.store.ContentStoreAdapter)1 CachingAwareList (org.craftercms.core.util.cache.impl.CachingAwareList)1 DocumentException (org.dom4j.DocumentException)1 SAXReader (org.dom4j.io.SAXReader)1 Resource (org.springframework.core.io.Resource)1