Search in sources :

Example 11 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class CmsStaticContentFileManagerImpl method getFiles.

@Override
public List<OutputContentFile> getFiles(final String merchantStoreCode, Optional<String> path, final FileContentType staticContentType) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsStaticContentFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    List<OutputContentFile> images = new ArrayList<OutputContentFile>();
    try {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String nodePath = this.getNodePath(merchantStoreCode, staticContentType);
        final Node<String, Object> merchantNode = this.getNode(nodePath);
        for (String key : merchantNode.getKeys()) {
            byte[] imageBytes = (byte[]) merchantNode.get(key);
            OutputContentFile contentImage = new OutputContentFile();
            InputStream input = new ByteArrayInputStream(imageBytes);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(input, output);
            String contentType = fileNameMap.getContentTypeFor(key);
            contentImage.setFile(output);
            contentImage.setMimeType(contentType);
            contentImage.setFileName(key);
            images.add(contentImage);
        }
    } catch (final Exception e) {
        LOGGER.error("Error while fetching file for {} merchant ", merchantStoreCode);
        throw new ServiceException(e);
    }
    return images;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileNameMap(java.net.FileNameMap) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 12 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class CmsStaticContentFileManagerImpl method getFile.

/**
 * Method to return static data for given Merchant store based on the file
 * name. Content data will be searched in underlying Infinispan cache tree
 * and {@link OutputStaticContentData} will be returned on finding an
 * associated file. In case of no file, null be returned.
 *
 * @param store
 *            Merchant store
 * @param contentFileName
 *            name of file being requested
 * @return {@link OutputStaticContentData}
 * @throws ServiceException
 */
@Override
public OutputContentFile getFile(final String merchantStoreCode, Optional<String> path, final FileContentType fileContentType, final String contentFileName) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
        throw new ServiceException("CmsStaticContentFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    OutputContentFile outputStaticContentData = new OutputContentFile();
    InputStream input = null;
    try {
        String nodePath = this.getNodePath(merchantStoreCode, fileContentType);
        final Node<String, Object> merchantNode = this.getNode(nodePath);
        final byte[] fileBytes = (byte[]) merchantNode.get(contentFileName);
        if (fileBytes == null) {
            LOGGER.warn("file byte is null, no file found");
            return null;
        }
        input = new ByteArrayInputStream(fileBytes);
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(input, output);
        outputStaticContentData.setFile(output);
        outputStaticContentData.setMimeType(URLConnection.getFileNameMap().getContentTypeFor(contentFileName));
        outputStaticContentData.setFileName(contentFileName);
        outputStaticContentData.setFileContentType(fileContentType);
    } catch (final Exception e) {
        LOGGER.error("Error while fetching file for {} merchant ", merchantStoreCode);
        throw new ServiceException(e);
    }
    return outputStaticContentData;
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 13 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class ContentAssetsManager method getOutputContentFile.

default OutputContentFile getOutputContentFile(byte[] byteArray) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
    baos.write(byteArray, 0, byteArray.length);
    OutputContentFile ct = new OutputContentFile();
    ct.setFile(baos);
    return ct;
}
Also used : OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class S3StaticContentAssetsManagerImpl method getFiles.

@Override
public List<OutputContentFile> getFiles(String merchantStoreCode, Optional<String> folderPath, FileContentType fileContentType) throws ServiceException {
    try {
        // get buckets
        String bucketName = bucketName();
        ListObjectsV2Request listObjectsRequest = new ListObjectsV2Request().withBucketName(bucketName).withPrefix(nodePath(merchantStoreCode, fileContentType));
        List<OutputContentFile> files = null;
        final AmazonS3 s3 = s3Client();
        ListObjectsV2Result results = s3.listObjectsV2(listObjectsRequest);
        List<S3ObjectSummary> objects = results.getObjectSummaries();
        for (S3ObjectSummary os : objects) {
            if (files == null) {
                files = new ArrayList<OutputContentFile>();
            }
            String mimetype = URLConnection.guessContentTypeFromName(os.getKey());
            if (!StringUtils.isBlank(mimetype)) {
                S3Object o = s3.getObject(bucketName, os.getKey());
                byte[] byteArray = IOUtils.toByteArray(o.getObjectContent());
                ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
                baos.write(byteArray, 0, byteArray.length);
                OutputContentFile ct = new OutputContentFile();
                ct.setFile(baos);
                files.add(ct);
            }
        }
        LOGGER.info("Content getFiles");
        return files;
    } catch (final Exception e) {
        LOGGER.error("Error while getting files", e);
        throw new ServiceException(e);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ServiceException(com.salesmanager.core.business.exception.ServiceException) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 15 with OutputContentFile

use of com.salesmanager.core.model.content.OutputContentFile in project shopizer by shopizer-ecommerce.

the class GCPStaticContentAssetsManagerImpl method getFiles.

@Override
public List<OutputContentFile> getFiles(String merchantStoreCode, Optional<String> folderPath, FileContentType fileContentType) throws ServiceException {
    try {
        List<String> fileNames = getFileNames(merchantStoreCode, folderPath, fileContentType);
        List<OutputContentFile> files = new ArrayList<OutputContentFile>();
        for (String fileName : fileNames) {
            files.add(getFile(merchantStoreCode, folderPath, fileContentType, fileName));
        }
        LOGGER.info("Content get file names");
        return files;
    } catch (Exception e) {
        LOGGER.error("Error while getting file names", e);
        throw new ServiceException(e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ArrayList(java.util.ArrayList) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException)

Aggregations

OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 ServiceException (com.salesmanager.core.business.exception.ServiceException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 ArrayList (java.util.ArrayList)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 FileContentType (com.salesmanager.core.model.content.FileContentType)4 FileNameMap (java.net.FileNameMap)4 InputContentFile (com.salesmanager.core.model.content.InputContentFile)3 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)3 IOException (java.io.IOException)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)2 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)2 S3Object (com.amazonaws.services.s3.model.S3Object)2 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)2 ReadChannel (com.google.cloud.ReadChannel)2