@Override
  public List<OutputContentFile> getImages(
      final String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    List<OutputContentFile> images = new ArrayList<OutputContentFile>();
    FileNameMap fileNameMap = URLConnection.getFileNameMap();

    try {

      StringBuilder nodePath = new StringBuilder();
      nodePath.append(merchantStoreCode);

      Node<String, Object> merchantNode = this.getNode(nodePath.toString());

      Set<Node<String, Object>> childs = merchantNode.getChildren();

      Iterator<Node<String, Object>> iterator = childs.iterator();
      // TODO image sizes
      while (iterator.hasNext()) {

        Node<String, Object> node = iterator.next();

        for (String key : node.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 (Exception e) {
      throw new ServiceException(e);
    } finally {

    }

    return images;
  }
  private OutputContentFile getProductImage(
      String merchantStoreCode, String productCode, String imageName, String size)
      throws ServiceException {

    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    InputStream input = null;
    OutputContentFile contentImage = new OutputContentFile();
    try {

      FileNameMap fileNameMap = URLConnection.getFileNameMap();

      // SMALL by default
      StringBuilder nodePath = new StringBuilder();
      nodePath
          .append(merchantStoreCode)
          .append(Constants.SLASH)
          .append(productCode)
          .append(Constants.SLASH)
          .append(size);

      Node<String, Object> productNode = this.getNode(nodePath.toString());

      byte[] imageBytes = (byte[]) productNode.get(imageName);

      input = new ByteArrayInputStream(imageBytes);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      IOUtils.copy(input, output);

      String contentType = fileNameMap.getContentTypeFor(imageName);

      contentImage.setFile(output);
      contentImage.setMimeType(contentType);
      contentImage.setFileName(imageName);

    } catch (Exception e) {
      throw new ServiceException(e);
    } finally {
      if (input != null) {
        try {
          input.close();
        } catch (Exception ignore) {
        }
      }
    }

    return contentImage;
  }
  @Override
  public List<OutputContentFile> getImages(Product product) throws ServiceException {

    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }

    List<OutputContentFile> images = new ArrayList<OutputContentFile>();

    try {

      FileNameMap fileNameMap = URLConnection.getFileNameMap();
      StringBuilder nodePath = new StringBuilder();
      nodePath.append(product.getMerchantStore().getCode());

      Node<String, Object> merchantNode = this.getNode(nodePath.toString());

      if (merchantNode == null) {
        return null;
      }

      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 (Exception e) {
      throw new ServiceException(e);
    } finally {

    }

    return images;
  }
Exemplo n.º 4
0
  @Test
  public void createStoreLogo() throws ServiceException, FileNotFoundException, IOException {

    MerchantStore store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);
    final File file1 = new File("/Users/csamson777/Pictures/peavey.jpg");

    if (!file1.exists() || !file1.canRead()) {
      throw new ServiceException("Can't read" + file1.getAbsolutePath());
    }

    byte[] is = IOUtils.toByteArray(new FileInputStream(file1));
    ByteArrayInputStream inputStream = new ByteArrayInputStream(is);
    InputContentFile cmsContentImage = new InputContentFile();

    cmsContentImage.setFileName(file1.getName());
    cmsContentImage.setFile(inputStream);

    // logo as a content
    contentService.addLogo(store.getCode(), cmsContentImage);

    store.setStoreLogo(file1.getName());
    merchantService.update(store);

    // query the store
    store = merchantService.getByCode(MerchantStore.DEFAULT_STORE);

    // get the logo
    String logo = store.getStoreLogo();

    OutputContentFile image =
        contentService.getContentFile(store.getCode(), FileContentType.LOGO, logo);

    // print image
    OutputStream outputStream =
        new FileOutputStream("/Users/csamson777/Pictures/mexique" + image.getFileName());

    ByteArrayOutputStream baos = image.getFile();
    baos.writeTo(outputStream);

    // remove image
    contentService.removeFile(store.getCode(), FileContentType.LOGO, store.getStoreLogo());
  }