Search in sources :

Example 1 with BinaryDecodingException

use of com.zimbra.cs.imap.ImapPartSpecifier.BinaryDecodingException in project zm-mailbox by Zimbra.

the class ImapURL method getContentAsStream.

public Pair<Long, InputStream> getContentAsStream(ImapHandler handler, ImapCredentials creds, String tag) throws ImapException {
    ImapHandler.State state = handler.getState();
    if (state == ImapHandler.State.NOT_AUTHENTICATED) {
        throw new ImapUrlException(tag, mURL, "must be in AUTHENTICATED state");
    }
    try {
        Account acct = Provisioning.getInstance().get(AccountBy.name, mUsername);
        if (acct == null) {
            throw new ImapUrlException(tag, mURL, "cannot find user: " + mUsername);
        }
        ImapSession i4session = handler.getCurrentSession();
        OperationContext octxt = creds.getContext().setSession(i4session);
        Pair<Long, InputStream> content = null;
        // special-case the situation where the relevant folder is already SELECTed
        ImapFolder i4folder = handler.getSelectedFolder();
        if (state == ImapHandler.State.SELECTED && i4session != null && i4folder != null) {
            if (acct.getId().equals(i4session.getTargetAccountId()) && mPath.isEquivalent(i4folder.getPath())) {
                ImapMessage i4msg = i4folder.getByImapId(mUid);
                if (i4msg == null || i4msg.isExpunged()) {
                    throw new ImapUrlException(tag, mURL, "no such message");
                }
                MailItem item = i4folder.getMailbox().getItemById(octxt, i4msg.msgId, i4msg.getType());
                content = ImapMessage.getContent(item);
            }
        }
        // if not, have to fetch by IMAP UID if we're local
        if (content == null && mPath.onLocalServer()) {
            Mailbox mbox = (Mailbox) mPath.getOwnerMailbox();
            MailItem item = mbox.getItemByImapId(octxt, mUid, mPath.asItemId().getId());
            if (!ImapMessage.SUPPORTED_TYPES.contains(item.getType())) {
                throw new ImapUrlException(tag, mURL, "no such message");
            }
            content = ImapMessage.getContent(item);
        }
        // last option: handle off-server URLs
        if (content == null) {
            Account authacct = Provisioning.getInstance().get(AccountBy.id, creds.getAccountId());
            AuthToken auth = AuthProvider.getAuthToken(authacct, System.currentTimeMillis() + 60 * 1000);
            HashMap<String, String> params = new HashMap<String, String>();
            params.put(UserServlet.QP_IMAP_ID, Integer.toString(mUid));
            UserServlet.HttpInputStream is = UserServlet.getRemoteContentAsStream(auth, acct, mPath.asResolvedPath(), params);
            content = new Pair<Long, InputStream>((long) is.getContentLength(), is);
        }
        // fetch the content of the message
        if (mPart == null) {
            return content;
        }
        // and return the appropriate subpart of the selected message
        MimeMessage mm;
        try {
            mm = new Mime.FixedMimeMessage(JMSession.getSession(), content.getSecond());
        } finally {
            content.getSecond().close();
        }
        Pair<Long, InputStream> part = mPart.getContent(mm);
        if (part == null) {
            throw new ImapUrlException(tag, mURL, "no such part");
        }
        return part;
    } catch (NoSuchItemException e) {
        ZimbraLog.imap.info("no such message", e);
    } catch (ServiceException e) {
        ZimbraLog.imap.info("can't fetch content from IMAP URL", e);
    } catch (MessagingException e) {
        ZimbraLog.imap.info("can't fetch content from IMAP URL", e);
    } catch (IOException e) {
        ZimbraLog.imap.info("error reading content from IMAP URL", e);
    } catch (BinaryDecodingException e) {
        ZimbraLog.imap.info("can't fetch content from IMAP URL", e);
    }
    throw new ImapUrlException(tag, mURL, "error fetching IMAP URL content");
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) BinaryDecodingException(com.zimbra.cs.imap.ImapPartSpecifier.BinaryDecodingException) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) OperationContext(com.zimbra.cs.mailbox.OperationContext) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) IOException(java.io.IOException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Mime(com.zimbra.cs.mime.Mime) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) UserServlet(com.zimbra.cs.service.UserServlet) AuthToken(com.zimbra.cs.account.AuthToken)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)1 Account (com.zimbra.cs.account.Account)1 AuthToken (com.zimbra.cs.account.AuthToken)1 BinaryDecodingException (com.zimbra.cs.imap.ImapPartSpecifier.BinaryDecodingException)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 Mime (com.zimbra.cs.mime.Mime)1 UserServlet (com.zimbra.cs.service.UserServlet)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1