Search in sources :

Example 1 with MimeMessageData

use of com.zimbra.cs.service.mail.ParseMimeMessage.MimeMessageData in project zm-mailbox by Zimbra.

the class SendMsg method handle.

public Element handle(Element request, Map<String, Object> context, QName respQname) throws ServiceException {
    try {
        ZimbraSoapContext zsc = getZimbraSoapContext(context);
        Mailbox mbox = getRequestedMailbox(zsc);
        AccountUtil.checkQuotaWhenSendMail(mbox);
        OperationContext octxt = getOperationContext(zsc, context);
        ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
        // <m>
        Element msgElem = request.getElement(MailConstants.E_MSG);
        // check to see whether the entire message has been uploaded under separate cover
        String attachId = msgElem.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
        boolean needCalendarSentByFixup = request.getAttributeBool(MailConstants.A_NEED_CALENDAR_SENTBY_FIXUP, false);
        boolean isCalendarForward = request.getAttributeBool(MailConstants.A_IS_CALENDAR_FORWARD, false);
        boolean noSaveToSent = request.getAttributeBool(MailConstants.A_NO_SAVE_TO_SENT, false);
        boolean fetchSavedMsg = request.getAttributeBool(MailConstants.A_FETCH_SAVED_MSG, false);
        String origId = msgElem.getAttribute(MailConstants.A_ORIG_ID, null);
        ItemId iidOrigId = origId == null ? null : new ItemId(origId, zsc);
        String replyType = msgElem.getAttribute(MailConstants.A_REPLY_TYPE, MailSender.MSGTYPE_REPLY);
        String identityId = msgElem.getAttribute(MailConstants.A_IDENTITY_ID, null);
        String dataSourceId = msgElem.getAttribute(MailConstants.A_DATASOURCE_ID, null);
        String draftId = msgElem.getAttribute(MailConstants.A_DRAFT_ID, null);
        ItemId iidDraft = draftId == null ? null : new ItemId(draftId, zsc);
        boolean sendFromDraft = msgElem.getAttributeBool(MailConstants.A_SEND_FROM_DRAFT, false);
        SendState state = SendState.NEW;
        ItemId savedMsgId = null;
        Pair<String, ItemId> sendRecord = null;
        // get the "send uid" and check that this isn't a retry of a pending send
        String sendUid = request.getAttribute(MailConstants.A_SEND_UID, null);
        if (sendUid != null) {
            long delay = MAX_IN_FLIGHT_DELAY_MSECS;
            do {
                if (state == SendState.PENDING) {
                    try {
                        delay -= RETRY_CHECK_PERIOD_MSECS;
                        Thread.sleep(RETRY_CHECK_PERIOD_MSECS);
                    } catch (InterruptedException ie) {
                    }
                }
                Pair<SendState, Pair<String, ItemId>> result = findPendingSend(mbox.getId(), sendUid);
                state = result.getFirst();
                sendRecord = result.getSecond();
            } while (state == SendState.PENDING && delay >= 0);
        }
        if (state == SendState.SENT) {
            // message successfully sent by another thread
            savedMsgId = sendRecord.getSecond();
        } else if (state == SendState.PENDING) {
            // tired of waiting for another thread to complete the send
            throw MailServiceException.TRY_AGAIN("message send already in progress: " + sendUid);
        } else if (state == SendState.NEW) {
            MimeMessageData mimeData = new MimeMessageData();
            try {
                // holds return data about the MimeMessage
                MimeMessage mm;
                if (attachId != null) {
                    mm = parseUploadedMessage(zsc, attachId, mimeData, needCalendarSentByFixup);
                } else if (iidDraft != null && sendFromDraft) {
                    Message msg = mbox.getMessageById(octxt, iidDraft.getId());
                    mm = msg.getMimeMessage(false);
                } else {
                    mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, null, mimeData);
                }
                savedMsgId = doSendMessage(octxt, mbox, mm, mimeData.uploads, iidOrigId, replyType, identityId, dataSourceId, noSaveToSent, needCalendarSentByFixup, isCalendarForward);
                //   a null ItemId makes the send appear to still be PENDING)
                if (savedMsgId == null) {
                    savedMsgId = NO_MESSAGE_SAVED_TO_SENT;
                }
                // and record it in the table in case the client retries the send
                if (sendRecord != null) {
                    sendRecord.setSecond(savedMsgId);
                }
            } catch (ServiceException e) {
                clearPendingSend(mbox.getId(), sendRecord);
                throw e;
            } catch (RuntimeException re) {
                clearPendingSend(mbox.getId(), sendRecord);
                throw re;
            } finally {
                // purge the messages fetched from other servers.
                if (mimeData.fetches != null) {
                    FileUploadServlet.deleteUploads(mimeData.fetches);
                }
            }
        }
        if (iidDraft != null) {
            deleteDraft(iidDraft, octxt, mbox, zsc);
        }
        Element response = zsc.createElement(respQname);
        if (savedMsgId != null && savedMsgId != NO_MESSAGE_SAVED_TO_SENT && savedMsgId.getId() > 0) {
            if (fetchSavedMsg) {
                Message msg = GetMsg.getMsg(octxt, mbox, savedMsgId, false);
                ToXML.encodeMessageAsMP(response, ifmt, octxt, msg, null, 0, true, true, null, false, false, LC.mime_encode_missing_blob.booleanValue(), MsgContent.both);
            } else {
                Element respElement = response.addElement(MailConstants.E_MSG);
                respElement.addAttribute(MailConstants.A_ID, ifmt.formatItemId(savedMsgId));
            }
        } else {
            response.addElement(MailConstants.E_MSG);
        }
        return response;
    } finally {
        processor = null;
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId) MimeMessageData(com.zimbra.cs.service.mail.ParseMimeMessage.MimeMessageData) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Pair(com.zimbra.common.util.Pair)

Example 2 with MimeMessageData

use of com.zimbra.cs.service.mail.ParseMimeMessage.MimeMessageData in project zm-mailbox by Zimbra.

the class TestContentTransferEncoding method sendForwardedMessage.

private MimeMessage sendForwardedMessage(SendMsgRequest req, Message origMsg) throws Exception {
    Element reqElt = JaxbUtil.jaxbToElement(req);
    Element msgElt = reqElt.getElement(MailConstants.E_MSG);
    AuthToken at = AuthProvider.getAuthToken(mbox.getAccount());
    ZimbraSoapContext zsc = new ZimbraSoapContext(at, mbox.getAccountId(), SoapProtocol.Soap12, SoapProtocol.Soap12);
    return ParseMimeMessage.parseMimeMsgSoap(zsc, null, mbox, msgElt, null, new MimeMessageData());
}
Also used : MimeMessageData(com.zimbra.cs.service.mail.ParseMimeMessage.MimeMessageData) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) AuthToken(com.zimbra.cs.account.AuthToken)

Aggregations

Element (com.zimbra.common.soap.Element)2 MimeMessageData (com.zimbra.cs.service.mail.ParseMimeMessage.MimeMessageData)2 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)2 ZMailbox (com.zimbra.client.ZMailbox)1 ServiceException (com.zimbra.common.service.ServiceException)1 Pair (com.zimbra.common.util.Pair)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 AuthToken (com.zimbra.cs.account.AuthToken)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Message (com.zimbra.cs.mailbox.Message)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ItemId (com.zimbra.cs.service.util.ItemId)1 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)1 MimeMessage (javax.mail.internet.MimeMessage)1