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 newName */
 public void renameFolder(String newName) throws Exception {
   Folder fOld = getFolder();
   Folder fNew = handler.getStore().getFolder(profile.getFolderNameSpace() + newName);
   closeFolder(fOld);
   fOld.renameTo(fNew);
   fNew.setSubscribed(true);
 }
Ejemplo n.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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;
  }