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