use of com.zimbra.cs.mailclient.imap.ImapConnection in project zm-mailbox by Zimbra.
the class TestImapClient method testGMailAppend.
@Ignore("deprecated")
public void testGMailAppend() throws Exception {
ImapConfig config = new ImapConfig();
config.getLogger().setLevel(Log.Level.trace);
config.setHost("imap.gmail.com");
config.setSecurity(MailConfig.Security.SSL);
config.setSSLSocketFactory(SSLUtil.getDummySSLContext().getSocketFactory());
config.setAuthenticationId("dacztest");
config.setMaxLiteralTraceSize(999999);
connection = new ImapConnection(config);
connection.connect();
connection.login("test1234");
MimeMessage mm = newTestMessage(new Random().nextInt());
// Append and find unique message
ImapAppender appender = new ImapAppender(connection, "INBOX");
long uid1 = appender.appendMessage(getBytes(mm), null);
System.out.println("XXX uid1 = " + uid1);
assertTrue("uid1 not found", uid1 > 0);
// Now append message again and make sure we can find the de-duped copy
long uid2 = appender.appendMessage(getBytes(mm), null);
assertTrue("uid2 not found", uid2 > 0);
assertEquals(uid1, uid2);
connection.close();
}
use of com.zimbra.cs.mailclient.imap.ImapConnection in project zm-mailbox by Zimbra.
the class TestImap method connect.
private ImapConnection connect() throws IOException {
ImapConfig config = new ImapConfig(HOST);
config.setPort(PORT);
config.setAuthenticationId(USER);
config.getLogger().setLevel(Log.Level.trace);
ImapConnection connection = new ImapConnection(config);
connection.connect();
connection.login(PASS);
connection.select("INBOX");
return connection;
}
use of com.zimbra.cs.mailclient.imap.ImapConnection 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;
}
use of com.zimbra.cs.mailclient.imap.ImapConnection in project zm-mailbox by Zimbra.
the class ImapProxy method idle.
/**
* Proxy IDLE command.
*
* @param req IMAP request
* @param begin true to start, false to stop
* @return true to continue, false to close the connection
* @throws ImapProxyException network error with the remote IMAP server
* @throws IOException error on reading the request data
*/
protected boolean idle(final ImapRequest req, final boolean begin) throws ImapProxyException, IOException {
if (begin == ImapHandler.IDLE_STOP) {
// check state -- don't want to send DONE if we're somehow not in IDLE
if (handler == null) {
throw new ImapProxyException("client connection already closed");
}
Thread idle = idleThread;
if (idle == null) {
throw new ImapProxyException("bad proxy state: no IDLE thread active when attempting DONE");
}
// send the DONE, which elicits the tagged response that causes the IDLE thread (below) to exit
writeRequest(req.toByteArray());
// make sure that the idle thread actually exits; otherwise we're in a bad place and we must kill the whole session
idleThread = null;
try {
idle.join(5 * Constants.MILLIS_PER_SECOND);
} catch (InterruptedException ie) {
}
if (idle.isAlive())
handler.dropConnection(false);
} else {
final ImapConnection conn = connection;
if (conn == null) {
throw new ImapProxyException("proxy connection already closed");
}
ImapConfig config = conn.getImapConfig();
final int oldTimeout = config != null ? config.getReadTimeout() : LC.javamail_imap_timeout.intValue();
// necessary because of subsequent race condition with req.cleanup()
final byte[] payload = req.toByteArray();
idleThread = new Thread() {
@Override
public void run() {
boolean success = false;
try {
// the standard aggressive read timeout is inappropriate for IDLE
conn.setReadTimeout(handler.getConfig().getAuthenticatedMaxIdleTime());
// send the IDLE command; this call waits until the subsequent DONE is acknowledged
boolean ok = proxyCommand(payload, true, true);
// restore the old read timeout
conn.setReadTimeout(oldTimeout);
// don't set <code>success</code> until we're past things that can throw IOExceptions
success = ok;
} catch (IOException e) {
ZimbraLog.imap.warn("error encountered during IDLE; dropping connection", e);
}
if (!success) {
handler.dropConnection(true);
}
}
};
idleThread.setName("Imap-Idle-Proxy-" + Thread.currentThread().getName());
idleThread.start();
}
return true;
}
use of com.zimbra.cs.mailclient.imap.ImapConnection in project zm-mailbox by Zimbra.
the class ImapProxy method dropConnection.
protected void dropConnection() {
ImapConnection conn = connection;
connection = null;
if (conn == null)
return;
// FIXME: should close cleanly (i.e. with tagged LOGOUT)
ZimbraLog.imap.info("closing proxy connection %s", conn);
conn.close();
}
Aggregations