Search in sources :

Example 6 with StoreException

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

the class S3ContentStoreAdapter method doFindFile.

/**
 * {@inheritDoc}
 */
@Override
protected File doFindFile(Context context, String path) throws InvalidContextException, StoreException {
    if (context.ignoreHiddenFiles() && isHidden(path)) {
        return null;
    }
    S3Context s3Context = (S3Context) context;
    String key = StringUtils.appendIfMissing(s3Context.getKey(), path);
    logger.debug("Getting file for key {}", key);
    if (StringUtils.isEmpty(FilenameUtils.getExtension(key))) {
        // If it is a folder, check if there are objects with the prefix
        try {
            ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(s3Context.getBucket()).withPrefix(key).withDelimiter(DELIMITER);
            ListObjectsV2Result result = client.listObjectsV2(request);
            if (!isResultEmpty(result)) {
                return new S3Prefix(key);
            }
        } catch (AmazonS3Exception e) {
            if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                logger.debug("No object found for key {}", key);
            } else {
                throw new StoreException("Error listing objects for key " + key, e);
            }
        }
    } else {
        // If it is a file, check if the key exist
        try {
            if (client.doesObjectExist(s3Context.getBucket(), key)) {
                return new S3File(key);
            } else {
                logger.debug("No object found for key {}", key);
            }
        } catch (AmazonS3Exception e) {
            throw new StoreException("Error checking if object for key " + key + " exists", e);
        }
    }
    return null;
}
Also used : 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