Search in sources :

Example 1 with ImapConfig

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

the class ConnectionManager method newImapConfig.

public static ImapConfig newImapConfig(DataSource ds) {
    ImapConfig config = new ImapConfig();
    Map<String, String> props = new HashMap<String, String>();
    config.setHost(ds.getHost());
    config.setPort(ds.getPort());
    config.setAuthenticationId(ds.getUsername());
    config.setSecurity(getSecurity(ds));
    config.setMechanism(ds.getAuthMechanism());
    config.setAuthorizationId(ds.getAuthId());
    if (SaslAuthenticator.XOAUTH2.equalsIgnoreCase(ds.getAuthMechanism())) {
        try {
            JMSession.addOAuth2Properties(ds.getDecryptedOAuthToken(), props, config.getProtocol());
            config.setSaslProperties(props);
        } catch (ServiceException e) {
            ZimbraLog.datasource.warn("Exception in decrypting the oauth token", e);
        }
    }
    // bug 37982: Disable use of LITERAL+ due to problems with Yahoo IMAP.
    // Avoiding LITERAL+ also gives servers a chance to reject uploaded
    // messages that are too big, since the server must send a continuation
    // response before the literal data can be sent.
    config.setUseLiteralPlus(false);
    // Enable support for trace output
    if (ds.isDebugTraceEnabled()) {
        config.setLogger(SyncUtil.getTraceLogger(ZimbraLog.imap_client, ds.getId()));
    }
    config.setSocketFactory(SocketFactories.defaultSocketFactory());
    config.setSSLSocketFactory(SocketFactories.defaultSSLSocketFactory());
    config.setConnectTimeout(ds.getConnectTimeout(LC.javamail_imap_timeout.intValue()));
    config.setReadTimeout(ds.getReadTimeout(LC.javamail_imap_timeout.intValue()));
    LOG.debug("Connect timeout = %d, read timeout = %d", config.getConnectTimeout(), config.getReadTimeout());
    return config;
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap)

Example 2 with ImapConfig

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

the class TestImapClient method getConfig.

private static ImapConfig getConfig(MailConfig.Security security) {
    ImapConfig config = new ImapConfig(HOST);
    config.setPort(PORT);
    if (security != null) {
        config.setSecurity(security);
        if (security == MailConfig.Security.SSL) {
            config.setPort(SSL_PORT);
            config.setSSLSocketFactory(SSLUtil.getDummySSLContext().getSocketFactory());
        }
    }
    config.getLogger().setLevel(Log.Level.trace);
    config.setMechanism("PLAIN");
    config.setAuthenticationId(USER);
    //config.setRawMode(true);
    return config;
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig)

Example 3 with ImapConfig

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

the class TestImapClient method testGMailAppend.

@Test
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) Test(org.junit.Test)

Example 4 with ImapConfig

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

the class TestImapUtil method testUtf8Text.

public void testUtf8Text() throws Exception {
    String msg = "UTF8: Àå";
    byte[] b = (msg + "\r\n").getBytes("UTF8");
    ImapInputStream is = new ImapInputStream(new ByteArrayInputStream(b), new ImapConfig());
    assertEquals(msg, is.readText());
    is.skipCRLF();
}
Also used : ImapConfig(com.zimbra.cs.mailclient.imap.ImapConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) ImapInputStream(com.zimbra.cs.mailclient.imap.ImapInputStream)

Example 5 with ImapConfig

use of com.zimbra.cs.mailclient.imap.ImapConfig 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
     */
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)

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