public String saveEmbededResponse(
      String downloadDirectory,
      String localFileName,
      com.actuate.schemas.DownloadFileResponse downloadFileResponse,
      boolean decomposed)
      throws IOException, RemoteException {

    String downloadName = null;
    BufferedOutputStream outStream = null;
    String localFilePath = downloadDirectory + "/" + localFileName;

    try {
      if (!decomposed) {
        outStream = new BufferedOutputStream(new FileOutputStream(localFilePath));
      }

      com.actuate.schemas.Attachment attachment = downloadFileResponse.getContent();
      if (attachment != null) {
        byte[] b = attachment.getContentData();
        System.out.println("Attachment retrived as " + attachment.getContentId());
        if (b != null && outStream != null) {
          outStream.write(b);
        }
      }

      com.actuate.schemas.ArrayOfAttachment arrayOfAttachment =
          downloadFileResponse.getContainedFiles();

      if (arrayOfAttachment != null) {
        com.actuate.schemas.Attachment[] attachments = arrayOfAttachment.getAttachment();
        for (int i = 0; i < attachments.length; i++) {
          if (attachments[i] != null) {
            byte[] b = attachments[i].getContentData();
            System.out.println("Attachment retrived as " + attachments[i].getContentId());
            if (b != null) {
              if (outStream != null) {
                outStream.write(b);
              } else {
                String decomposedDocAttachment =
                    downloadDirectory + "/" + attachments[i].getContentId();
                BufferedOutputStream tempOutStream =
                    new BufferedOutputStream(new FileOutputStream(decomposedDocAttachment));
                tempOutStream.write(b);
                tempOutStream.close();
              }
            }
          }
        }
      }

    } catch (RemoteException e) {
      throw e;
    } finally {
      if (outStream != null) {
        downloadName = localFilePath;
        outStream.close();
      }
    }
    return downloadName;
  }
  /**
   * Utility function to save an embedded attachment to a directory
   *
   * @param attachment
   * @param downloadDirectory
   * @return String the file name saved
   */
  public static String saveAttachment(
      com.actuate.schemas.Attachment attachment, String downloadDirectory) {
    if (null == attachment) return null;
    if (null == downloadDirectory) downloadDirectory = ".";

    // an embedded attachment is stored in the ContentData
    byte[] contentData = attachment.getContentData();

    if (contentData != null) {

      String FileName = attachment.getContentId();
      String saveName = downloadDirectory + "/" + FileName;

      try {
        FileOutputStream os = new FileOutputStream(saveName);
        os.write(contentData);
      } catch (Exception e) {
        System.out.println("Failed to save file");
      }

      return FileName;
    }
    return null;
  }