Search in sources :

Example 11 with ImapConfig

use of com.zimbra.cs.mailclient.imap.ImapConfig in project zm-mailbox by Zimbra.

the class ConnectionManager method newConnection.

public static ImapConnection newConnection(DataSource ds, Authenticator auth) throws ServiceException {
    ImapConfig config = newImapConfig(ds);
    ImapConnection ic = new ImapConnection(config);
    ic.setDataHandler(new FetchDataHandler());
    try {
        ic.connect();
        try {
            if (config.getMechanism() != null) {
                if (SaslAuthenticator.XOAUTH2.equalsIgnoreCase(config.getMechanism())) {
                    auth = AuthenticatorFactory.getDefault().newAuthenticator(config, ds.getDecryptedOAuthToken());
                } else {
                    auth = AuthenticatorFactory.getDefault().newAuthenticator(config, ds.getDecryptedPassword());
                }
            }
            if (auth == null) {
                ic.login(ds.getDecryptedPassword());
            } else {
                ic.authenticate(auth);
            }
        } catch (CommandFailedException e) {
            if (SaslAuthenticator.XOAUTH2.equalsIgnoreCase(config.getMechanism())) {
                try {
                    DataSourceManager.refreshOAuthToken(ds);
                    config.getSaslProperties().put("mail." + config.getProtocol() + ".sasl.mechanisms.oauth2.oauthToken", ds.getDecryptedOAuthToken());
                    auth = AuthenticatorFactory.getDefault().newAuthenticator(config, ds.getDecryptedOAuthToken());
                    ic.authenticate(auth);
                } catch (CommandFailedException e1) {
                    ZimbraLog.datasource.warn("Exception in connecting to data source", e);
                    throw new LoginException(e1.getError());
                }
            } else {
                throw new LoginException(e.getError());
            }
        }
        if (isImportingSelf(ds, ic)) {
            throw ServiceException.INVALID_REQUEST("User attempted to import messages from his/her own mailbox", null);
        }
    } catch (ServiceException e) {
        ic.close();
        throw e;
    } catch (Exception e) {
        ic.close();
        throw ServiceException.FAILURE("Unable to connect to IMAP server: " + ds, e);
    }
    LOG.debug("Created new connection: " + ic);
    return ic;
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig) ServiceException(com.zimbra.common.service.ServiceException) LoginException(javax.security.auth.login.LoginException) ImapConnection(com.zimbra.cs.mailclient.imap.ImapConnection) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) LoginException(javax.security.auth.login.LoginException) ServiceException(com.zimbra.common.service.ServiceException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException)

Aggregations

ImapConfig (com.zimbra.cs.mailclient.imap.ImapConfig)11 ImapConnection (com.zimbra.cs.mailclient.imap.ImapConnection)6 ServiceException (com.zimbra.common.service.ServiceException)2 ImapInputStream (com.zimbra.cs.mailclient.imap.ImapInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 ImapAppender (com.zimbra.cs.datasource.imap.ImapAppender)1 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)1 IDInfo (com.zimbra.cs.mailclient.imap.IDInfo)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 MimeMessage (javax.mail.internet.MimeMessage)1 LoginException (javax.security.auth.login.LoginException)1