Search in sources :

Example 11 with CommandFailedException

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

the class Pop3Sync method fetchAndAddMessage.

private void fetchAndAddMessage(int msgno, int size, String uid, boolean allowFilterToMountpoint) throws ServiceException, IOException {
    ContentInputStream cis = null;
    MessageContent mc = null;
    checkIsEnabled();
    try {
        cis = connection.getMessage(msgno);
        mc = MessageContent.read(cis, size);
        ParsedMessage pm = mc.getParsedMessage(null, indexAttachments);
        if (pm == null) {
            LOG.warn("Empty message body for UID %d. Must be ignored.", uid);
            return;
        }
        pm.setDataSourceId(dataSource.getId());
        Message msg = null;
        // bug 47796: Set received date to sent date if available otherwise use current time
        try {
            Date sentDate = pm.getMimeMessage().getSentDate();
            if (sentDate == null) {
                LOG.warn("null sent date; probably due to parse error. Date header value: [%s]", pm.getMimeMessage().getHeader("Date", null));
            }
            pm.setReceivedDate(sentDate != null ? sentDate.getTime() : System.currentTimeMillis());
        } catch (MessagingException e) {
            LOG.warn("unable to get sent date from parsed message due to exception, must use current time", e);
            pm.setReceivedDate(System.currentTimeMillis());
        }
        DeliveryContext dc = mc.getDeliveryContext();
        if (isOffline()) {
            msg = addMessage(null, pm, size, dataSource.getFolderId(), Flag.BITMASK_UNREAD, dc);
        } else {
            Integer localId = getFirstLocalId(RuleManager.applyRulesToIncomingMessage(null, mbox, pm, size, dataSource.getEmailAddress(), dc, dataSource.getFolderId(), true, allowFilterToMountpoint));
            if (localId != null) {
                msg = mbox.getMessageById(null, localId);
            }
        }
        if (msg != null && uid != null) {
            PopMessage msgTracker = new PopMessage(dataSource, msg.getId(), uid);
            msgTracker.add();
        }
    } catch (CommandFailedException e) {
        LOG.warn("Error fetching message number %d: %s", msgno, e.getMessage());
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (ParseException pe) {
                LOG.error("ParseException while closing ContentInputStream. Assuming cis is effectively closed", pe);
            }
        }
        if (mc != null) {
            mc.cleanup();
        }
    }
}
Also used : Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MessagingException(javax.mail.MessagingException) ContentInputStream(com.zimbra.cs.mailclient.pop3.ContentInputStream) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParseException(com.zimbra.cs.mailclient.ParseException) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Date(java.util.Date) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException)

Example 12 with CommandFailedException

use of com.zimbra.cs.mailclient.CommandFailedException 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 13 with CommandFailedException

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

the class ConnectionManager method isImportingSelf.

private static boolean isImportingSelf(DataSource ds, ImapConnection ic) throws IOException, ServiceException {
    if (!ds.isOffline() && ic.hasCapability(ImapCapabilities.ID)) {
        try {
            IDInfo clientId = new IDInfo();
            clientId.put(IDInfo.NAME, IDInfo.DATASOURCE_IMAP_CLIENT_NAME);
            clientId.put(IDInfo.VERSION, BuildInfo.VERSION);
            IDInfo id = ic.id(clientId);
            if ("Zimbra".equalsIgnoreCase(id.get(IDInfo.NAME)) && ds.getAccount() != null) {
                String user = id.get("user");
                String server = id.get("server");
                return user != null && user.equals(ds.getAccount().getName()) && server != null && server.equals(Provisioning.getInstance().getLocalServer().getId());
            }
        } catch (CommandFailedException e) {
            // Skip check if ID command fails
            LOG.warn("ID command failed, assuming not importing self", e);
        }
    }
    return false;
}
Also used : IDInfo(com.zimbra.cs.mailclient.imap.IDInfo) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException)

Aggregations

CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)13 Flags (com.zimbra.cs.mailclient.imap.Flags)7 Literal (com.zimbra.cs.mailclient.imap.Literal)7 Date (java.sql.Date)7 Test (org.junit.Test)6 IOException (java.io.IOException)3 ServiceException (com.zimbra.common.service.ServiceException)2 MailException (com.zimbra.cs.mailclient.MailException)2 ParseException (com.zimbra.cs.mailclient.ParseException)2 CharMatcher (com.google.common.base.CharMatcher)1 Splitter (com.google.common.base.Splitter)1 RemoteServiceException (com.zimbra.common.service.RemoteServiceException)1 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 Message (com.zimbra.cs.mailbox.Message)1 AppendResult (com.zimbra.cs.mailclient.imap.AppendResult)1 FetchResponseHandler (com.zimbra.cs.mailclient.imap.FetchResponseHandler)1 IDInfo (com.zimbra.cs.mailclient.imap.IDInfo)1 ImapConfig (com.zimbra.cs.mailclient.imap.ImapConfig)1 ImapConnection (com.zimbra.cs.mailclient.imap.ImapConnection)1