Search in sources :

Example 11 with ImapConnection

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();
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig) Random(java.util.Random) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ImapConnection(com.zimbra.cs.mailclient.imap.ImapConnection) ImapAppender(com.zimbra.cs.datasource.imap.ImapAppender) Ignore(org.junit.Ignore)

Example 12 with ImapConnection

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;
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig) ImapConnection(com.zimbra.cs.mailclient.imap.ImapConnection)

Example 13 with ImapConnection

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

Example 14 with ImapConnection

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;
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig) IOException(java.io.IOException) ImapConnection(com.zimbra.cs.mailclient.imap.ImapConnection)

Example 15 with ImapConnection

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();
}
Also used : ImapConnection(com.zimbra.cs.mailclient.imap.ImapConnection)

Aggregations

ImapConnection (com.zimbra.cs.mailclient.imap.ImapConnection)27 ImapConfig (com.zimbra.cs.mailclient.imap.ImapConfig)11 IOException (java.io.IOException)7 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)5 ServiceException (com.zimbra.common.service.ServiceException)4 Test (org.junit.Test)4 Account (com.zimbra.cs.account.Account)2 AuthenticatorFactory (com.zimbra.cs.mailclient.auth.AuthenticatorFactory)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 Ignore (org.junit.Ignore)2 LocalConfig (com.zimbra.common.localconfig.LocalConfig)1 RemoteServiceException (com.zimbra.common.service.RemoteServiceException)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 CacheEntry (com.zimbra.cs.account.Provisioning.CacheEntry)1 ImapAppender (com.zimbra.cs.datasource.imap.ImapAppender)1 PurgedConversation (com.zimbra.cs.db.DbDataSource.PurgedConversation)1 PurgedMessage (com.zimbra.cs.db.DbDataSource.PurgedMessage)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1