Search in sources :

Example 1 with ProtectedResourceId

use of org.craftercms.commons.security.permissions.annotations.ProtectedResourceId in project studio by craftercms.

the class AwsS3ServiceImpl method listItems.

/**
 * {@inheritDoc}
 */
@Override
@HasPermission(type = DefaultPermission.class, action = "s3 read")
public List<S3Item> listItems(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") String siteId, @ValidateStringParam(name = "profileId") String profileId, @ValidateStringParam(name = "path") String path, @ValidateStringParam(name = "type") String type) throws AwsException {
    S3Profile profile = getProfile(siteId, profileId);
    AmazonS3 client = getS3Client(profile);
    List<S3Item> items = new LinkedList<>();
    Mimetypes mimetypes = Mimetypes.getInstance();
    MimeType filerType = StringUtils.isEmpty(type) || StringUtils.equals(type, ITEM_FILTER) ? MimeTypeUtils.ALL : new MimeType(type);
    String prefix = StringUtils.isEmpty(path) ? path : normalizePrefix(path);
    ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(profile.getBucketName()).withPrefix(prefix).withDelimiter(delimiter);
    ListObjectsV2Result result;
    do {
        result = client.listObjectsV2(request);
        result.getCommonPrefixes().stream().map(p -> new S3Item(StringUtils.removeEnd(StringUtils.removeStart(p, prefix), delimiter), p, true)).forEach(items::add);
        result.getObjectSummaries().stream().filter(o -> !StringUtils.equals(o.getKey(), prefix) && MimeType.valueOf(mimetypes.getMimetype(o.getKey())).isCompatibleWith(filerType)).map(o -> new S3Item(StringUtils.removeStart(o.getKey(), prefix), createUrl(profileId, o.getKey()), false)).forEach(items::add);
        request.setContinuationToken(result.getNextContinuationToken());
    } while (result.isTruncated());
    return items;
}
Also used : S3Item(org.craftercms.studio.model.aws.s3.S3Item) AwsUtils(org.craftercms.studio.impl.v1.service.aws.AwsUtils) S3Item(org.craftercms.studio.model.aws.s3.S3Item) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission) AbstractAwsService(org.craftercms.studio.api.v1.service.aws.AbstractAwsService) AwsS3Service(org.craftercms.studio.api.v2.service.aws.s3.AwsS3Service) S3Profile(org.craftercms.commons.config.profiles.aws.S3Profile) StringUtils.stripStart(org.apache.commons.lang3.StringUtils.stripStart) ProtectedResourceId(org.craftercms.commons.security.permissions.annotations.ProtectedResourceId) MimeTypeUtils(org.springframework.util.MimeTypeUtils) StringUtils.appendIfMissing(org.apache.commons.lang3.StringUtils.appendIfMissing) StringUtils(org.apache.commons.lang3.StringUtils) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) MimeType(org.springframework.util.MimeType) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) AwsException(org.craftercms.studio.api.v1.exception.AwsException) List(java.util.List) Mimetypes(com.amazonaws.services.s3.internal.Mimetypes) S3ClientCachingFactory(org.craftercms.commons.aws.S3ClientCachingFactory) AmazonS3(com.amazonaws.services.s3.AmazonS3) DefaultPermission(org.craftercms.commons.security.permissions.DefaultPermission) Required(org.springframework.beans.factory.annotation.Required) ValidateStringParam(org.craftercms.commons.validation.annotations.param.ValidateStringParam) LinkedList(java.util.LinkedList) InputStream(java.io.InputStream) AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) Mimetypes(com.amazonaws.services.s3.internal.Mimetypes) S3Profile(org.craftercms.commons.config.profiles.aws.S3Profile) LinkedList(java.util.LinkedList) MimeType(org.springframework.util.MimeType) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 2 with ProtectedResourceId

use of org.craftercms.commons.security.permissions.annotations.ProtectedResourceId in project studio by craftercms.

the class WebDavServiceImpl method list.

/**
 * {@inheritDoc}
 */
@Override
@ValidateParams
@HasPermission(type = DefaultPermission.class, action = "webdav_read")
public List<WebDavItem> list(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") final String siteId, @ValidateStringParam(name = "profileId") final String profileId, @ValidateStringParam(name = "path") final String path, @ValidateStringParam(name = "type") final String type) throws WebDavException {
    WebDavProfile profile = getProfile(siteId, profileId);
    String listPath = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
    MimeType filterType;
    try {
        Sardine sardine = createClient(profile);
        if (StringUtils.isEmpty(type) || type.equals(FILTER_ALL_ITEMS)) {
            filterType = MimeType.valueOf(ALL_VALUE);
        } else {
            filterType = new MimeType(type);
        }
        if (StringUtils.isNotEmpty(path)) {
            String[] tokens = StringUtils.split(path, "/");
            for (String token : tokens) {
                if (StringUtils.isNotEmpty(token)) {
                    listPath += StringUtils.appendIfMissing(UriUtils.encode(token, charset.name()), "/");
                }
            }
        }
        if (!sardine.exists(listPath)) {
            logger.debug("Folder {0} doesn't exist", listPath);
            return Collections.emptyList();
        }
        logger.debug("Listing resources at {0}", listPath);
        List<DavResource> resources = sardine.list(listPath, 1, true);
        logger.debug("Found {0} resources at {0}", resources.size(), listPath);
        return resources.stream().skip(// to avoid repeating the folder being listed
        1).filter(r -> r.isDirectory() || filterType.includes(MimeType.valueOf(r.getContentType()))).map(r -> new WebDavItem(getName(r), getUrl(r, profileId, profile), r.isDirectory())).collect(Collectors.toList());
    } catch (Exception e) {
        throw new WebDavException("Error listing resources", e);
    }
}
Also used : Sardine(com.github.sardine.Sardine) Logger(org.craftercms.studio.api.v1.log.Logger) UrlUtils(org.craftercms.commons.lang.UrlUtils) StringUtils(org.apache.commons.lang3.StringUtils) DavResource(com.github.sardine.DavResource) MimeType(org.springframework.util.MimeType) WebDavException(org.craftercms.studio.api.v1.exception.WebDavException) WebDavItem(org.craftercms.studio.api.v1.webdav.WebDavItem) WebDavService(org.craftercms.studio.api.v2.service.webdav.WebDavService) ALL_VALUE(org.springframework.util.MimeTypeUtils.ALL_VALUE) Charset(java.nio.charset.Charset) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams) URI(java.net.URI) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission) WebDavProfile(org.craftercms.commons.config.profiles.webdav.WebDavProfile) ProtectedResourceId(org.craftercms.commons.security.permissions.annotations.ProtectedResourceId) SiteAwareConfigProfileLoader(org.craftercms.studio.impl.v1.util.config.profiles.SiteAwareConfigProfileLoader) Collectors(java.util.stream.Collectors) Sardine(com.github.sardine.Sardine) ConfigurationException(org.craftercms.commons.config.ConfigurationException) List(java.util.List) WebDavUtils.createClient(org.craftercms.commons.file.stores.WebDavUtils.createClient) UriUtils(org.springframework.web.util.UriUtils) DefaultPermission(org.craftercms.commons.security.permissions.DefaultPermission) ValidateStringParam(org.craftercms.commons.validation.annotations.param.ValidateStringParam) Collections(java.util.Collections) InputStream(java.io.InputStream) WebDavItem(org.craftercms.studio.api.v1.webdav.WebDavItem) DavResource(com.github.sardine.DavResource) WebDavException(org.craftercms.studio.api.v1.exception.WebDavException) MimeType(org.springframework.util.MimeType) WebDavException(org.craftercms.studio.api.v1.exception.WebDavException) ConfigurationException(org.craftercms.commons.config.ConfigurationException) WebDavProfile(org.craftercms.commons.config.profiles.webdav.WebDavProfile) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

InputStream (java.io.InputStream)2 List (java.util.List)2 StringUtils (org.apache.commons.lang3.StringUtils)2 DefaultPermission (org.craftercms.commons.security.permissions.DefaultPermission)2 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)2 ProtectedResourceId (org.craftercms.commons.security.permissions.annotations.ProtectedResourceId)2 ValidateStringParam (org.craftercms.commons.validation.annotations.param.ValidateStringParam)2 MimeType (org.springframework.util.MimeType)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 Mimetypes (com.amazonaws.services.s3.internal.Mimetypes)1 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)1 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)1 DavResource (com.github.sardine.DavResource)1 Sardine (com.github.sardine.Sardine)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 Collectors (java.util.stream.Collectors)1 StringUtils.appendIfMissing (org.apache.commons.lang3.StringUtils.appendIfMissing)1