Example #1
0
  /**
   * Metodo estico que retorna el array de bytes de un content
   *
   * @param content
   * @return
   * @throws IOException
   */
  public static byte[] getByteContent(Content content) throws Exception {

    ContentFormat format = content.getFormat();
    String mimeType = format.getMimetype();
    String ext = mimeType.split("/")[1];
    File fileTemp;
    byte[] contentBytes = null;

    try {
      fileTemp = File.createTempFile(AlfrescoKeys.FILE_TEMP, "." + ext);

      // Se convierte el contenido en un fichero temporal
      ContentUtils.copyContentToFile(content, fileTemp);

      // Se recupera el inputStream del fichero temporal y se retornan los byte[]
      InputStream viewStream = new FileInputStream(fileTemp);
      contentBytes = ContentUtils.convertToByteArray(viewStream);

      // Se borra el fichero temproal
      boolean a = fileTemp.delete();
    } catch (IOException e) {
      log.error("Error en el fichero", e);
      throw e;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      log.error(e);
      throw e;
    }

    return contentBytes;
  }
Example #2
0
  /**
   * Gets the content from Alfresco DMS.
   *
   * @param reference not null {@link Reference}
   * @param store the store.
   * @return not null {@link Document}
   * @throws RemoteException If the {@link Reference} is not equivalent to an existing {@link Node}.
   * @throws DMSException if an error occurs in DMS back end
   */
  private Document getContent(Reference reference, Store store)
      throws RemoteException, DMSException {
    Validate.notNull(reference);
    Validate.notNull(store);

    /** Get the content service. */
    final ContentServiceSoapBindingStub contentService = this.getContentService();
    Document result = null;

    /** Read the content from the respository. */
    Content[] readResult;
    readResult =
        contentService.read(
            new Predicate(new Reference[] {reference}, store, null), Constants.PROP_CONTENT);
    Content content = readResult[0];

    if (readResult != null && content != null && content.getUrl() != null) {
      final String fileName = this.getName(content.getUrl());
      File tempFile = new File(ResourcesLocator.getResourcesTempDir() + fileName);

      /** Get the content from the download servlet using the URL and display it. */
      /** Copy content in a temporary direcoty. */
      ContentUtils.copyContentToFile(content, tempFile);

      try {
        FileInputStream fin;
        fin = new FileInputStream(tempFile);
        int bytesCount = (int) tempFile.length();
        byte[] contentBytes = new byte[bytesCount];
        fin.read(contentBytes);
        result = new Document(fileName, content.getFormat().getMimetype(), contentBytes);
        fin.close();
        this.deleteTempFile(tempFile);
      } catch (IOException e) {
        String message = "IO Error: ";
        LOG.error(message, e);
        DMSFault dmsFault = new DMSFault();
        dmsFault.setMessage(message);
        throw new DMSException(message, dmsFault, e);
      }
    }

    return result;
  }