Ejemplo n.º 1
0
  /**
   * @return
   * @throws Exception
   */
  public synchronized Folder getFolder() throws Exception {
    String folder = Constants.FOLDER_INBOX(profile);

    Folder fold = (Folder) pop3Folders.get(auth.getUsername());
    if (fold != null && fold.isOpen()) {
      return fold;
    } else {
      if (folder != null && handler != null) {
        Store store = handler.getStore();
        if (store == null || !store.isConnected()) {
          System.out.println("Connection is closed. Restoring it...");
          handler = connect(Constants.CONNECTION_READ_WRITE);
          System.out.println("Connection re-established");
        }
        fold = handler.getStore().getFolder(folder);
        if (!fold.isOpen()) {
          System.out.println("Folder :" + folder + " is closed. Opening again.");
          fold.open(Constants.CONNECTION_READ_WRITE);
          System.out.println("Folder is open again.");

          pop3Folders.put(auth.getUsername(), fold);
        }
      }
    }
    return fold;
  }
Ejemplo n.º 2
0
  /**
   * @param auth
   * @param msg
   * @param request
   * @throws Exception
   */
  private void saveDraft(AuthProfile auth, MimeMessage msg, HttpServletRequest request)
      throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    msg.writeTo(bos);
    byte bMsg[] = bos.toByteArray();

    // serialize the message byte array
    ObjectOutputStream os = new ObjectOutputStream(bos);
    os.writeObject(bMsg);

    // create an email db item
    MsgDbObject item = new MsgDbObject();
    item.setEmail(bMsg);
    String md5Header = new String(MD5.getHashString(bMsg)).toUpperCase(new Locale("en", "US"));

    ConnectionMetaHandler handler = getConnectionHandler(request);
    ConnectionProfile profile = getConnectionProfile(request);

    FolderControllerFactory factory = new FolderControllerFactory(auth, profile, handler);
    FolderController foldCont = factory.getFolderController();
    FolderDbObject fItem = foldCont.getDraftsFolder();

    item.setUniqueId(md5Header);
    item.setFolderId(fItem.getId());
    item.setUnread(new Boolean(false));
    item.setUsername(auth.getUsername());
    item.setMsgSize(new Long(bMsg.length));

    // save the email db item.
    MailControllerFactory mailFact =
        new MailControllerFactory(auth, profile, handler, fItem.getFolderName());
    MailController mailCont = mailFact.getMailController();
    mailCont.appendEmail(item);
  }
Ejemplo n.º 3
0
  /** @param f */
  public void closeFolder(Folder f) {
    if (f != null) {
      try {
        if (f.isOpen()) {
          f.close(true);
          log.info("Folder: " + f.getName() + " was open and now closed.");

          HashMap imapUserFolders = (HashMap) imapFolders.get(auth.getUsername());
          imapUserFolders.put(folder, null);
          imapFolders.put(auth.getUsername(), imapUserFolders);
        } else {
          log.info("Folder: " + f.getName() + " was already closed.");
        }
      } catch (MessagingException e) {
        log.info("Error while closing folder: " + f.getName(), e);
      }
    }
  }
Ejemplo n.º 4
0
  /**
   * @return
   * @throws Exception
   */
  public Folder getImapFolder(boolean useCache) throws Exception {
    Folder myFold = null;
    if (folder == null) {
      folder = Constants.FOLDER_INBOX(profile);
    }

    if (folder != null && handler != null) {
      Store store = handler.getStore();
      if (store == null || !store.isConnected()) {
        log.debug("Connection is closed. Restoring it...");
        handler = connect(Constants.CONNECTION_READ_WRITE);
        log.debug("Connection re-established");
      }

      HashMap imapUserFolders = null;
      if (useCache) {
        imapUserFolders = (HashMap) imapFolders.get(auth.getUsername());
        myFold = (Folder) imapUserFolders.get(folder);
      }
      if (myFold == null) {
        myFold = handler.getStore().getFolder(folder);
      }
      if (!myFold.isOpen()) {
        try {
          log.debug("Folder :" + folder + " is closed. Opening.");
          myFold.open(Constants.CONNECTION_READ_WRITE);
          log.debug("Folder is open.");
        } catch (Throwable e) {
          log.debug("nevermind go on");
          // nevermind go on...
        }
      }
      if (useCache) {
        try {
          imapUserFolders.put(folder, myFold);
          imapFolders.put(auth.getUsername(), imapUserFolders);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    return myFold;
  }
Ejemplo n.º 5
0
 /** @param f */
 public void closeFolder(Folder f) {
   if (f != null) {
     try {
       if (f.isOpen()) {
         f.close(true);
       }
     } catch (MessagingException e) {
       System.out.println("Error while closing folder: " + f.getName());
     }
   }
   pop3Folders.put(auth.getUsername(), null);
 }
Ejemplo n.º 6
0
  /**
   * @param profile
   * @param auth
   * @param handler
   */
  ImapProtocolImpl(
      ConnectionProfile profile, AuthProfile auth, ConnectionMetaHandler handler, String folder) {
    this.profile = profile;
    this.auth = auth;
    this.handler = handler;
    this.folder = folder;

    if (imapFolders.get(auth.getUsername()) == null) {
      HashMap imapUserFolders = new HashMap();
      imapFolders.put(auth.getUsername(), imapUserFolders);
    }

    if (this.folder == null
        || this.folder.trim().equals("")
        || this.folder.toLowerCase(loc).equals(Constants.FOLDER_INBOX(profile).toLowerCase(loc))) {
      this.folder = Constants.FOLDER_INBOX(profile);
    } else {
      if (!this.folder.startsWith(profile.getFolderNameSpace())) {
        this.folder = profile.getFolderNameSpace() + this.folder;
      }
    }
  }
Ejemplo n.º 7
0
  /**
   * Disconnects the previously opened data connection if the connection is still alive.
   *
   * @param handler
   */
  public void disconnect() {
    try {
      Folder fold = (Folder) pop3Folders.get(auth.getUsername());
      if (fold != null) {
        fold.close(true);
      }
      try {
        if (handler.getMbox() != null) {
          handler.getMbox().close(true);
        }
      } catch (Exception e) {
      }

      try {
        if (handler.getStore() != null) {
          handler.getStore().close();
        }
      } catch (Exception e) {
      }
    } catch (Exception e) {
    }
    pop3Folders.put(auth.getUsername(), null);
  }
Ejemplo n.º 8
0
  /**
   * Disconnects the previously opened data connection if the connection is still alive.
   *
   * @param handler
   */
  public void disconnect() {
    try {
      HashMap imapUserFolders = (HashMap) imapFolders.get(auth.getUsername());
      Iterator iter = imapUserFolders.keySet().iterator();
      Folder tmp = null;
      while (iter.hasNext()) {
        try {
          tmp = (Folder) imapUserFolders.get((String) iter.next());
          closeFolder(tmp);
          tmp = null;
        } catch (Throwable e) {
          log.debug("Unable to close folder:" + tmp);
        }
      }
      imapFolders.put(auth.getUsername(), new HashMap());
    } catch (Throwable e1) {
    }

    try {
      handler.getStore().close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 9
0
 /* (non-Javadoc)
  * @see org.claros.commons.mail.protocols.FetchProtocol#deleteMessages(int[])
  */
 public ConnectionMetaHandler deleteMessages(int[] messageIds)
     throws MailboxActionException, SystemException, ConnectionException {
   Folder fold = null;
   try {
     fold = getFolder();
     if (messageIds != null && messageIds.length > 0) {
       for (int i = 0; i < messageIds.length; i++) {
         Message msg = fold.getMessage(messageIds[i]);
         msg.setFlag(Flags.Flag.DELETED, true);
       }
     }
     // fold.expunge();
     disconnect();
     connect(Constants.CONNECTION_READ_WRITE);
     return handler;
   } catch (Exception e) {
     pop3Folders.put(auth.getUsername(), null);
     System.out.println("Could not delete message ids: " + messageIds);
     throw new MailboxActionException(e);
   }
 }
Ejemplo n.º 10
0
  /* (non-Javadoc)
   * @see org.claros.commons.mail.protocols.FetchProtocol#connect(int)
   */
  public ConnectionMetaHandler connect(int connectType)
      throws SystemException, ConnectionException, ServerDownException {
    try {
      try {
        disconnect();
        try {
          Thread.sleep(2000);
        } catch (Exception k) {
        }
      } catch (Exception k) {
      }

      if (handler == null || !handler.getStore().isConnected()) {
        Properties props = new Properties();

        if (profile.getFetchSSL() != null && profile.getFetchSSL().toLowerCase().equals("true")) {
          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

          Security.setProperty(
              "ssl.SocketFactory.provider",
              "org.claros.commons.mail.protocols.DummySSLSocketFactory");
          props.setProperty("mail.store.protocol", "pop3");
          props.setProperty("mail.pop3.host", profile.getFetchServer());
          props.setProperty("mail.pop3.port", profile.getFetchPort());

          props.setProperty(
              "mail.pop3.socketFactory.class",
              "org.claros.commons.mail.protocols.DummySSLSocketFactory");
          props.setProperty("mail.pop3.socketFactory.fallback", "false");
          props.setProperty("mail.pop3.port", profile.getFetchPort());
          props.setProperty("mail.pop3.socketFactory.port", profile.getFetchPort());
        }

        Session session = Session.getInstance(props);
        handler = new ConnectionMetaHandler();
        handler.setStore(session.getStore(profile.getProtocol()));
        handler
            .getStore()
            .connect(
                profile.getFetchServer(),
                profile.getIFetchPort(),
                auth.getUsername(),
                auth.getPassword());
        handler.setMbox(handler.getStore().getDefaultFolder());
        handler.setMbox(handler.getMbox().getFolder(Constants.FOLDER_INBOX(profile)));
        handler.getMbox().open(connectType);

        // storing the folder in map
        pop3Folders.put(auth.getUsername(), handler.getMbox());

        handler.setTotalMessagesCount(handler.getMbox().getMessageCount());
      }
    } catch (AuthenticationFailedException e) {
      System.out.println(
          "Pop3 Mailbox was busy with another session and there is a read write lock. A few minutes later when the lock is released everything will be fine.");
    } catch (NoSuchProviderException e) {
      System.out.println(profile.getProtocol() + " provider could not be found.");
      throw new SystemException(e);
    } catch (MessagingException e) {
      System.out.println("Connection could not be established.");
      throw new ConnectionException(e);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return handler;
  }
Ejemplo n.º 11
0
  /* (non-Javadoc)
   * @see org.claros.commons.mail.protocols.Protocol#connect(int)
   */
  public ConnectionMetaHandler connect(int connectType)
      throws SystemException, ConnectionException, ServerDownException {
    Folder fold = null;
    try {
      if (handler == null || handler.getStore() == null || !handler.getStore().isConnected()) {
        Properties props = new Properties();
        //				props.setProperty("mail.imap.separatestoreconnection", "true");
        //				props.setProperty("mail.imap.connectionpoolsize", "0");
        //				props.setProperty("mail.imap.connectionpooltimeout", "100");

        //				props.setProperty("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        log.debug("session instance initiated");
        handler = new ConnectionMetaHandler();
        handler.setStore(session.getStore(profile.getProtocol()));
        log.debug("session store set. protocol is: " + profile.getProtocol());
        handler
            .getStore()
            .connect(
                profile.getFetchServer(),
                profile.getIFetchPort(),
                auth.getUsername(),
                auth.getPassword());
        if (handler.getStore().isConnected()) {
          log.debug("Store has been connected... Successful");
        } else {
          log.warn("Connection unsuccessfull...!!");
        }
      }
      fold = handler.getStore().getFolder(Constants.FOLDER_INBOX(profile));

      HashMap imapUserFolders = (HashMap) imapFolders.get(auth.getUsername());
      imapUserFolders.put("INBOX", fold);
      imapFolders.put(auth.getUsername(), imapUserFolders);

      handler.setMbox(fold);
      log.debug("Got mailbox folder. Folder is: " + fold.getFullName());
      handler.setTotalMessagesCount(fold.getMessageCount());
      log.debug("Message Count:" + handler.getTotalMessagesCount());
    } catch (FolderNotFoundException e) {
      log.fatal(
          profile.getProtocol()
              + " cannot identify the INBOX folder. Please check your folder-namespace variable at config.xml.");
      throw new SystemException(e);
    } catch (NoSuchProviderException e) {
      log.fatal(profile.getProtocol() + " provider could not be found.");
      throw new SystemException(e);
    } catch (MessagingException e) {
      Exception ne = e.getNextException();
      if (ne != null) {
        if (ne instanceof ConnectException || ne instanceof IOException) {
          throw new ServerDownException("Server is unreachable.");
        }
      }
      log.error("Connection could not be established." + e.getMessage());
      //			throw new ConnectionException(e);
    } catch (Exception e) {
      log.error("An unknown exception while connect.", e);
    }
    return handler;
  }