Search in sources :

Example 96 with ItemId

use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.

the class Search method runSimpleAppointmentQuery.

private static void runSimpleAppointmentQuery(Element parent, SearchParams params, OperationContext octxt, ZimbraSoapContext zsc, Account authAcct, Mailbox mbox, List<String> folderIdStrs) throws ServiceException {
    Set<MailItem.Type> types = params.getTypes();
    MailItem.Type type = types.size() == 1 ? Iterables.getOnlyElement(types) : MailItem.Type.APPOINTMENT;
    if (params.getSortBy() != null) {
        parent.addAttribute(MailConstants.A_SORTBY, params.getSortBy().toString());
    }
    parent.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
    parent.addAttribute(MailConstants.A_QUERY_MORE, false);
    List<ItemId> folderIids = new ArrayList<ItemId>(folderIdStrs.size());
    for (String folderIdStr : folderIdStrs) {
        folderIids.add(new ItemId(folderIdStr, zsc));
    }
    Provisioning prov = Provisioning.getInstance();
    MailboxManager mboxMgr = MailboxManager.getInstance();
    Server localServer = prov.getLocalServer();
    Map<Server, Map<String, List<Integer>>> /* folder ids */
    groupedByServer = groupByServer(ItemId.groupFoldersByAccount(octxt, mbox, folderIids));
    // Look up in calendar cache first.
    if (LC.calendar_cache_enabled.booleanValue()) {
        CalSummaryCache calCache = CalendarCacheManager.getInstance().getSummaryCache();
        long rangeStart = params.getCalItemExpandStart();
        long rangeEnd = params.getCalItemExpandEnd();
        for (Iterator<Map.Entry<Server, Map<String, List<Integer>>>> serverIter = groupedByServer.entrySet().iterator(); serverIter.hasNext(); ) {
            Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry = serverIter.next();
            Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
            // for each account
            for (Iterator<Map.Entry<String, List<Integer>>> acctIter = accountFolders.entrySet().iterator(); acctIter.hasNext(); ) {
                Map.Entry<String, List<Integer>> acctEntry = acctIter.next();
                String acctId = acctEntry.getKey();
                List<Integer> folderIds = acctEntry.getValue();
                // Setup ItemIdFormatter appropriate for this folder which might not be in the authed account
                // but also take note of presense of <noqualify/> in SOAP context
                ItemIdFormatter ifmt = new ItemIdFormatter(authAcct.getId(), acctId, zsc.wantsUnqualifiedIds());
                // for each folder
                for (Iterator<Integer> iterFolderId = folderIds.iterator(); iterFolderId.hasNext(); ) {
                    int folderId = iterFolderId.next();
                    try {
                        CalendarDataResult result = calCache.getCalendarSummary(octxt, acctId, folderId, type, rangeStart, rangeEnd, true);
                        if (result != null) {
                            // Found data in cache.
                            iterFolderId.remove();
                            addCalendarDataToResponse(parent, zsc, ifmt, result.data, result.allowPrivateAccess);
                        }
                    } catch (ServiceException e) {
                        String ecode = e.getCode();
                        if (ecode.equals(ServiceException.PERM_DENIED)) {
                            // share permission was revoked
                            ZimbraLog.calendar.warn("Ignoring permission error during calendar search of folder %s", ifmt.formatItemId(folderId), e);
                        } else if (ecode.equals(MailServiceException.NO_SUCH_FOLDER)) {
                            // shared calendar folder was deleted by the owner
                            ZimbraLog.calendar.warn("Ignoring deleted calendar folder %s", ifmt.formatItemId(folderId));
                        } else {
                            throw e;
                        }
                        iterFolderId.remove();
                    }
                }
                if (folderIds.isEmpty())
                    acctIter.remove();
            }
            if (accountFolders.isEmpty())
                serverIter.remove();
        }
    }
    // For any remaining calendars, we have to get the data the hard way.
    for (Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry : groupedByServer.entrySet()) {
        Server server = serverMapEntry.getKey();
        Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
        if (server.equals(localServer)) {
            // local server
            for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
                String acctId = entry.getKey();
                List<Integer> folderIds = entry.getValue();
                if (folderIds.isEmpty())
                    continue;
                Account targetAcct = prov.get(AccountBy.id, acctId);
                if (targetAcct == null) {
                    ZimbraLog.calendar.warn("Skipping unknown account " + acctId + " during calendar search");
                    continue;
                }
                Mailbox targetMbox = mboxMgr.getMailboxByAccount(targetAcct);
                searchLocalAccountCalendars(parent, params, octxt, zsc, authAcct, targetMbox, folderIds, type);
            }
        } else {
            // remote server
            searchRemoteAccountCalendars(parent, params, zsc, authAcct, accountFolders);
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) ArrayList(java.util.ArrayList) CalendarDataResult(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache.CalendarDataResult) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ArrayList(java.util.ArrayList) List(java.util.List) MailItem(com.zimbra.cs.mailbox.MailItem) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) HashMap(java.util.HashMap) Map(java.util.Map) CalSummaryCache(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache)

Example 97 with ItemId

use of com.zimbra.cs.service.util.ItemId 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 98 with ItemId

use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.

the class SendMsg method doSendMessage.

public static ItemId doSendMessage(OperationContext oc, Mailbox mbox, MimeMessage mm, List<Upload> uploads, ItemId origMsgId, String replyType, String identityId, String dataSourceId, boolean noSaveToSent, boolean needCalendarSentByFixup, boolean isCalendarForward) throws ServiceException {
    boolean isCalendarMessage = false;
    ItemId id;
    OutlookICalendarFixupMimeVisitor mv = null;
    if (needCalendarSentByFixup || isCalendarForward) {
        mv = new OutlookICalendarFixupMimeVisitor(mbox.getAccount(), mbox).needFixup(needCalendarSentByFixup).setIsCalendarForward(isCalendarForward);
        try {
            mv.accept(mm);
        } catch (MessagingException e) {
            throw ServiceException.PARSE_ERROR("Error while fixing up SendMsg for SENT-BY", e);
        }
        isCalendarMessage = mv.isCalendarMessage();
    }
    MailSender sender;
    if (dataSourceId == null) {
        sender = isCalendarMessage ? CalendarMailSender.getCalendarMailSender(mbox) : mbox.getMailSender();
        if (noSaveToSent) {
            id = sender.sendMimeMessage(oc, mbox, false, mm, uploads, origMsgId, replyType, null, false, processor);
        } else {
            id = sender.sendMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType, identityId, false, processor);
        }
    } else {
        com.zimbra.cs.account.DataSource dataSource = mbox.getAccount().getDataSourceById(dataSourceId);
        if (dataSource == null) {
            throw ServiceException.INVALID_REQUEST("No data source with id " + dataSourceId, null);
        }
        if (!dataSource.isSmtpEnabled()) {
            throw ServiceException.INVALID_REQUEST("Data source SMTP is not enabled", null);
        }
        sender = mbox.getDataSourceMailSender(dataSource, isCalendarMessage);
        id = sender.sendDataSourceMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType);
    }
    // Send Calendar Forward Invitation notification if applicable
    try {
        if (isCalendarMessage && isCalendarForward && mv.getOriginalInvite() != null) {
            ZOrganizer org = mv.getOriginalInvite().getOrganizer();
            if (org != null) {
                String orgAddress = org.getAddress();
                Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
                if (orgAccount != null && !orgAccount.getId().equals(mbox.getAccount().getId())) {
                    MimeMessage notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(mbox.getAccount(), orgAccount, orgAddress, mm.getAllRecipients(), mv.getOriginalInvite());
                    CalendarMailSender.sendPartial(oc, mbox, notifyMimeMsg, null, null, null, null, false, true);
                }
            }
        }
    } catch (Exception e) {
        ZimbraLog.soap.warn("Ignoring error while sending Calendar Invitation Forward Notification", e);
    }
    return id;
}
Also used : Account(com.zimbra.cs.account.Account) MessagingException(javax.mail.MessagingException) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) CalendarMailSender(com.zimbra.cs.mailbox.calendar.CalendarMailSender) MailSender(com.zimbra.cs.mailbox.MailSender) ItemId(com.zimbra.cs.service.util.ItemId) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 99 with ItemId

use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.

the class ModifySearchFolder method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    Element t = request.getElement(MailConstants.E_SEARCH);
    ItemId iid = new ItemId(t.getAttribute(MailConstants.A_ID), zsc);
    String query = t.getAttribute(MailConstants.A_QUERY);
    String types = t.getAttribute(MailConstants.A_SEARCH_TYPES, null);
    String sort = t.getAttribute(MailConstants.A_SORTBY, null);
    mbox.modifySearchFolder(octxt, iid.getId(), query, types, sort);
    SearchFolder search = mbox.getSearchFolderById(octxt, iid.getId());
    Element response = zsc.createElement(MailConstants.MODIFY_SEARCH_FOLDER_RESPONSE);
    ToXML.encodeSearchFolder(response, ifmt, search);
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Mailbox(com.zimbra.cs.mailbox.Mailbox) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) SearchFolder(com.zimbra.cs.mailbox.SearchFolder) ItemId(com.zimbra.cs.service.util.ItemId)

Example 100 with ItemId

use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.

the class PurgeRevision method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Element revisionElem = request.getElement(MailConstants.E_REVISION);
    ItemId iid = new ItemId(revisionElem.getAttribute(MailConstants.A_ID), zsc);
    int rev = (int) revisionElem.getAttributeLong(MailConstants.A_VERSION);
    boolean includeOlderRevisions = revisionElem.getAttributeBool(MailConstants.A_INCLUDE_OLDER_REVISIONS, false);
    Mailbox mbox = getRequestedMailbox(zsc);
    mbox.purgeRevision(getOperationContext(zsc, context), iid.getId(), rev, includeOlderRevisions);
    return zsc.createElement(MailConstants.PURGE_REVISION_RESPONSE);
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId)

Aggregations

ItemId (com.zimbra.cs.service.util.ItemId)327 Mailbox (com.zimbra.cs.mailbox.Mailbox)243 OperationContext (com.zimbra.cs.mailbox.OperationContext)231 Account (com.zimbra.cs.account.Account)219 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)189 Message (com.zimbra.cs.mailbox.Message)185 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)168 Test (org.junit.Test)161 Element (com.zimbra.common.soap.Element)81 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)80 MimeMessage (javax.mail.internet.MimeMessage)65 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)58 ServiceException (com.zimbra.common.service.ServiceException)52 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)47 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)45 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)44 SyntaxException (org.apache.jsieve.exception.SyntaxException)42 Folder (com.zimbra.cs.mailbox.Folder)33 ArrayList (java.util.ArrayList)30 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)29