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;
}
Aggregations