Search in sources :

Example 6 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class S3ProductContentFileManager method removeImages.

@Override
public void removeImages(String merchantStoreCode) throws ServiceException {
    try {
        // get buckets
        String bucketName = bucketName();
        final AmazonS3 s3 = s3Client();
        s3.deleteObject(bucketName, nodePath(merchantStoreCode));
        LOGGER.info("Remove folder");
    } catch (final Exception e) {
        LOGGER.error("Error while removing folder", e);
        throw new ServiceException(e);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 7 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class S3ProductContentFileManager method getImages.

@Override
public List<OutputContentFile> getImages(String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    try {
        // get buckets
        String bucketName = bucketName();
        ListObjectsV2Request listObjectsRequest = new ListObjectsV2Request().withBucketName(bucketName).withPrefix(nodePath(merchantStoreCode));
        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);
            }
        }
        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 8 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class S3ProductContentFileManager method addProductImage.

@Override
public void addProductImage(ProductImage productImage, ImageContentFile contentImage) throws ServiceException {
    try {
        // get buckets
        String bucketName = bucketName();
        final AmazonS3 s3 = s3Client();
        String nodePath = this.nodePath(productImage.getProduct().getMerchantStore().getCode(), productImage.getProduct().getSku(), contentImage);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(contentImage.getMimeType());
        PutObjectRequest request = new PutObjectRequest(bucketName, nodePath + productImage.getProductImage(), contentImage.getFile(), metadata);
        request.setCannedAcl(CannedAccessControlList.PublicRead);
        s3.putObject(request);
        LOGGER.info("Product add file");
    } catch (final Exception e) {
        LOGGER.error("Error while removing file", e);
        throw new ServiceException(e);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ServiceException(com.salesmanager.core.business.exception.ServiceException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) ServiceException(com.salesmanager.core.business.exception.ServiceException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 9 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class GCPProductContentFileManager method addProductImage.

@Override
public void addProductImage(ProductImage productImage, ImageContentFile contentImage) throws ServiceException {
    Storage storage = StorageOptions.getDefaultInstance().getService();
    String bucketName = bucketName();
    if (!this.bucketExists(storage, bucketName)) {
        createBucket(storage, bucketName);
    }
    // build filename
    StringBuilder fileName = new StringBuilder().append(filePath(productImage.getProduct().getMerchantStore().getCode(), productImage.getProduct().getSku(), contentImage.getFileContentType())).append(productImage.getProductImage());
    try {
        byte[] targetArray = IOUtils.toByteArray(contentImage.getFile());
        BlobId blobId = BlobId.of(bucketName, fileName.toString());
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("image/jpeg").build();
        storage.create(blobInfo, targetArray);
        Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
    } catch (IOException ioe) {
        throw new ServiceException(ioe);
    }
}
Also used : Storage(com.google.cloud.storage.Storage) ServiceException(com.salesmanager.core.business.exception.ServiceException) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) IOException(java.io.IOException) BlobId(com.google.cloud.storage.BlobId)

Example 10 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class GCPProductContentFileManager method getImages.

/**
 * List files
 */
@Override
public List<OutputContentFile> getImages(String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    InputStream inputStream = null;
    try {
        Storage storage = StorageOptions.getDefaultInstance().getService();
        String bucketName = bucketName();
        if (!this.bucketExists(storage, bucketName)) {
            return null;
        }
        Page<Blob> blobs = storage.list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(merchantStoreCode));
        List<OutputContentFile> files = new ArrayList<OutputContentFile>();
        for (Blob blob : blobs.iterateAll()) {
            blob.getName();
            ReadChannel reader = blob.reader();
            inputStream = Channels.newInputStream(reader);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, outputStream);
            OutputContentFile ct = new OutputContentFile();
            ct.setFile(outputStream);
            files.add(ct);
        }
        return files;
    } catch (final Exception e) {
        LOGGER.error("Error while getting files", e);
        throw new ServiceException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception ignore) {
            }
        }
    }
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) ServiceException(com.salesmanager.core.business.exception.ServiceException) InputStream(java.io.InputStream) OutputContentFile(com.salesmanager.core.model.content.OutputContentFile) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceException(com.salesmanager.core.business.exception.ServiceException) IOException(java.io.IOException) ReadChannel(com.google.cloud.ReadChannel)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)230 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)88 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)54 ArrayList (java.util.ArrayList)45 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)44 Language (com.salesmanager.core.model.reference.language.Language)38 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)27 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)22 Autowired (org.springframework.beans.factory.annotation.Autowired)21 OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 Product (com.salesmanager.core.model.catalog.product.Product)19 HashMap (java.util.HashMap)19 Map (java.util.Map)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)17 InputContentFile (com.salesmanager.core.model.content.InputContentFile)17 Optional (java.util.Optional)17 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)16