示例#1
0
  /** Import EML file as MailNode. */
  private String importEml(String path, InputStream is)
      throws MessagingException, PathNotFoundException, ItemExistsException, VirusDetectedException,
          AccessDeniedException, RepositoryException, DatabaseException, UserQuotaExceededException,
          UnsupportedMimeTypeException, FileSizeExceededException, ExtensionException,
          AutomationException, IOException {
    log.debug("importEml({}, {})", path, is);
    Properties props = System.getProperties();
    props.put("mail.host", "smtp.dummydomain.com");
    props.put("mail.transport.protocol", "smtp");
    String errorMsg = null;

    try {
      // Convert file
      Session mailSession = Session.getDefaultInstance(props, null);
      MimeMessage msg = new MimeMessage(mailSession, is);
      Mail mail = MailUtils.messageToMail(msg);

      // Create phantom path. In this case we don't have the IMAP message ID, son create a random
      // one.
      mail.setPath(
          path + "/" + UUID.randomUUID().toString() + "-" + PathUtils.escape(mail.getSubject()));

      // Import files
      OKMMail.getInstance().create(null, mail);
      MailUtils.addAttachments(null, mail, msg, PrincipalUtils.getUser());
    } catch (IOException e) {
      log.error("Error importing eml", e);
      throw e;
    } finally {
      IOUtils.closeQuietly(is);
    }

    log.debug("importEml: {}", errorMsg);
    return errorMsg;
  }
  @Override
  public GWTTestImap testImapConnection(
      String host, String user, String password, String imapFolder) {
    log.debug(
        "testImapConnection({}, {}, {}, {})", new Object[] {host, user, password, imapFolder});
    GWTTestImap test = new GWTTestImap();
    updateSessionManager();

    try {
      test.setError(false);
      MailAccount ma = new MailAccount();
      ma.setMailProtocol(MailAccount.PROTOCOL_IMAP);
      ma.setMailHost(host);
      ma.setMailUser(user);
      ma.setMailPassword(password);
      ma.setMailFolder(imapFolder);
      ma.setMailMarkSeen(true);
      MailUtils.testConnection(ma);
    } catch (IOException e) {
      test.setError(true);
      test.setErrorMsg(e.getMessage());
      e.printStackTrace();
    }

    log.debug("testImapConnection: {}", test);
    return test;
  }
示例#3
0
  /** Import MSG file as MailNode. */
  private String importMsg(String path, InputStream is)
      throws MessagingException, PathNotFoundException, ItemExistsException, VirusDetectedException,
          AccessDeniedException, RepositoryException, DatabaseException, UserQuotaExceededException,
          UnsupportedMimeTypeException, FileSizeExceededException, ExtensionException,
          AutomationException, IOException {
    log.debug("importMsg({}, {})", path, is);
    String errorMsg = null;

    try {
      // Convert file
      MsgParser msgp = new MsgParser();
      Message msg = msgp.parseMsg(is);
      Mail mail = MailUtils.messageToMail(msg);

      // Create phantom path. In this case we don't have the IMAP message ID, son create a random
      // one.
      mail.setPath(
          path + "/" + UUID.randomUUID().toString() + "-" + PathUtils.escape(mail.getSubject()));

      // Import files
      OKMMail.getInstance().create(null, mail);

      for (Attachment att : msg.getAttachments()) {
        if (att instanceof FileAttachment) {
          FileAttachment fileAtt = (FileAttachment) att;
          log.debug("Importing attachment: {}", fileAtt.getFilename());

          String fileName = fileAtt.getFilename();
          String fileExtension = fileAtt.getExtension();
          String testName = fileName + "." + fileExtension;

          // Test if already exists a document with the same name in the mail
          for (int j = 1;
              OKMRepository.getInstance().hasNode(null, mail.getPath() + "/" + testName);
              j++) {
            // log.debug("Trying with: {}", testName);
            testName = fileName + " (" + j + ")." + fileExtension;
          }

          Document attachment = new Document();
          String mimeType = MimeTypeConfig.mimeTypes.getContentType(testName.toLowerCase());
          attachment.setMimeType(mimeType);
          attachment.setPath(mail.getPath() + "/" + testName);
          ByteArrayInputStream bais = new ByteArrayInputStream(fileAtt.getData());

          if (Config.REPOSITORY_NATIVE) {
            new DbDocumentModule()
                .create(null, attachment, bais, fileAtt.getSize(), PrincipalUtils.getUser());
          } else {
            new JcrDocumentModule().create(null, attachment, bais, PrincipalUtils.getUser());
          }

          IOUtils.closeQuietly(bais);
        }
      }
    } catch (IOException e) {
      log.error("Error importing msg", e);
      throw e;
    } finally {
      IOUtils.closeQuietly(is);
    }

    log.debug("importMsg: {}", errorMsg);
    return errorMsg;
  }