Search in sources :

Example 41 with ItemIdFormatter

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

the class CreateCalendarItemException method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account acct = getRequestedAccount(zsc);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    // proxy handling
    Element msgElem = request.getElement(MailConstants.E_MSG);
    String folderStr = msgElem.getAttribute(MailConstants.A_FOLDER, null);
    ItemId iid = new ItemId(request.getAttribute(MailConstants.A_ID), zsc);
    if (!iid.belongsTo(acct)) {
        // Proxy it.
        if (folderStr != null) {
            // make sure that the folder ID is fully qualified
            ItemId folderFQ = new ItemId(folderStr, zsc);
            msgElem.addAttribute(MailConstants.A_FOLDER, folderFQ.toString());
        }
        return proxyRequest(request, context, iid.getAccountId());
    }
    // Check if moving to a different mailbox.
    boolean isInterMboxMove = false;
    ItemId iidFolder = null;
    if (folderStr != null) {
        iidFolder = new ItemId(folderStr, zsc);
        isInterMboxMove = !iidFolder.belongsTo(mbox);
    }
    int compNum = (int) request.getAttributeLong(MailConstants.E_INVITE_COMPONENT);
    MailSendQueue sendQueue = new MailSendQueue();
    Element response = getResponseElement(zsc);
    mbox.lock.lock();
    try {
        CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
        if (calItem == null)
            throw MailServiceException.NO_SUCH_CALITEM(iid.getId(), " for CreateCalendarItemExceptionRequest(" + iid + "," + compNum + ")");
        // Reject the request if calendar item is under trash or is being moved to trash.
        if (calItem.inTrash())
            throw ServiceException.INVALID_REQUEST("cannot modify a calendar item under trash", null);
        if (!isInterMboxMove && iidFolder != null) {
            if (iidFolder.getId() != calItem.getFolderId()) {
                Folder destFolder = mbox.getFolderById(octxt, iidFolder.getId());
                if (destFolder.inTrash())
                    throw ServiceException.INVALID_REQUEST("cannot combine with a move to trash", null);
            }
        }
        // Conflict detection.  Do it only if requested by client.  (for backward compat)
        int modSeq = (int) request.getAttributeLong(MailConstants.A_MODIFIED_SEQUENCE, 0);
        int revision = (int) request.getAttributeLong(MailConstants.A_REVISION, 0);
        if (modSeq != 0 && revision != 0 && (modSeq < calItem.getModifiedSequence() || revision < calItem.getSavedSequence()))
            throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
        Invite inv = calItem.getInvite(iid.getSubpartId(), compNum);
        if (inv == null)
            throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
        if (inv.hasRecurId())
            throw MailServiceException.INVITE_OUT_OF_DATE("Invite id=" + ifmt.formatItemId(iid) + " comp=" + compNum + " is not the default invite");
        if (!calItem.isRecurring())
            throw ServiceException.INVALID_REQUEST("CalendarItem " + calItem.getId() + " is not a recurring calendar item", null);
        CreateCalendarItemExceptionInviteParser parser = new CreateCalendarItemExceptionInviteParser(calItem.getUid(), inv, sendQueue);
        CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
        dat.mDontNotifyAttendees = isInterMboxMove;
        int folderId = calItem.getFolderId();
        if (!isInterMboxMove && iidFolder != null)
            folderId = iidFolder.getId();
        // trace logging
        if (!dat.mInvite.hasRecurId())
            ZimbraLog.calendar.info("<CreateCalendarItemException> id=%d, folderId=%d, subject=\"%s\", UID=%s", iid.getId(), folderId, dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid());
        else
            ZimbraLog.calendar.info("<CreateCalendarItemException> id=%d, folderId=%d, subject=\"%s\", UID=%s, recurId=%s", iid.getId(), folderId, dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid(), dat.mInvite.getRecurId().getDtZ());
        boolean hasRecipients;
        try {
            Address[] rcpts = dat.mMm.getAllRecipients();
            hasRecipients = rcpts != null && rcpts.length > 0;
        } catch (MessagingException e) {
            throw ServiceException.FAILURE("Checking recipients of outgoing msg ", e);
        }
        // If we are sending this to other people, then we MUST be the organizer!
        if (!dat.mInvite.isOrganizer() && hasRecipients)
            throw MailServiceException.MUST_BE_ORGANIZER("CreateCalendarItemException");
        if (!dat.mInvite.isOrganizer()) {
            // neverSent is always false for attendee users.
            dat.mInvite.setNeverSent(false);
        } else if (!dat.mInvite.hasOtherAttendees()) {
            // neverSent is always false for appointments without attendees.
            dat.mInvite.setNeverSent(false);
        } else if (hasRecipients) {
            // neverSent is set to false when attendees are notified.
            dat.mInvite.setNeverSent(false);
        } else {
            // This is the case of organizer saving an invite with attendees, but without sending the notification.
            // Set neverSent to false, but only if it isn't already set to true on the series.
            // !series.isNeverSent() ? false : true ==> series.isNeverSent()
            dat.mInvite.setNeverSent(inv.isNeverSent());
        }
        boolean forceSend = request.getAttributeBool(MailConstants.A_CAL_FORCESEND, true);
        sendCalendarMessage(zsc, octxt, folderId, acct, mbox, dat, response, true, forceSend, sendQueue);
        boolean echo = request.getAttributeBool(MailConstants.A_CAL_ECHO, false);
        if (echo && dat.mAddInvData != null) {
            int maxSize = (int) request.getAttributeLong(MailConstants.A_MAX_INLINED_LENGTH, 0);
            boolean wantHTML = request.getAttributeBool(MailConstants.A_WANT_HTML, false);
            boolean neuter = request.getAttributeBool(MailConstants.A_NEUTER, true);
            echoAddedInvite(response, ifmt, octxt, mbox, dat.mAddInvData, maxSize, wantHTML, neuter);
        }
    } finally {
        mbox.lock.release();
        sendQueue.send();
    }
    // Inter-mailbox move if necessary.
    if (isInterMboxMove) {
        CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
        List<Integer> ids = new ArrayList<Integer>(1);
        ids.add(calItem.getId());
        ItemActionHelper.MOVE(octxt, mbox, zsc.getResponseProtocol(), ids, calItem.getType(), null, iidFolder);
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Address(javax.mail.Address) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) MessagingException(javax.mail.MessagingException) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 42 with ItemIdFormatter

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

the class CreateCalendarItem method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account acct = getRequestedAccount(zsc);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    // <M>
    Element msgElem = request.getElement(MailConstants.E_MSG);
    CreateCalendarItemInviteParser parser = new CreateCalendarItemInviteParser();
    CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
    int defaultFolder = dat.mInvite.isTodo() ? Mailbox.ID_FOLDER_TASKS : Mailbox.ID_FOLDER_CALENDAR;
    String defaultFolderStr = Integer.toString(defaultFolder);
    String folderIdStr = msgElem.getAttribute(MailConstants.A_FOLDER, defaultFolderStr);
    ItemId iidFolder = new ItemId(folderIdStr, zsc);
    // Don't allow creating in Trash folder/subfolder.  We don't want to invite attendees to an appointment in trash.
    Folder folder = mbox.getFolderById(octxt, iidFolder.getId());
    if (folder.inTrash())
        throw ServiceException.INVALID_REQUEST("cannot create a calendar item under trash", null);
    // trace logging
    if (!dat.mInvite.hasRecurId())
        ZimbraLog.calendar.info("<CreateCalendarItem> folderId=%d, subject=\"%s\", UID=%s", iidFolder.getId(), dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid());
    else
        ZimbraLog.calendar.info("<CreateCalendarItem> folderId=%d, subject=\"%s\", UID=%s, recurId=%s", iidFolder.getId(), dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid(), dat.mInvite.getRecurId().getDtZ());
    Element response = getResponseElement(zsc);
    boolean hasRecipients;
    try {
        Address[] rcpts = dat.mMm.getAllRecipients();
        hasRecipients = rcpts != null && rcpts.length > 0;
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("Checking recipients of outgoing msg ", e);
    }
    // If we are sending this to other people, then we MUST be the organizer!
    if (!dat.mInvite.isOrganizer() && hasRecipients)
        throw MailServiceException.MUST_BE_ORGANIZER("CreateCalendarItem");
    if (!dat.mInvite.isOrganizer()) {
        // neverSent is always false for attendee users.
        dat.mInvite.setNeverSent(false);
    } else if (!dat.mInvite.hasOtherAttendees()) {
        // neverSent is always false for appointments without attendees.
        dat.mInvite.setNeverSent(false);
    } else if (hasRecipients) {
        // neverSent is set to false when attendees are notified.
        dat.mInvite.setNeverSent(false);
    } else {
        // This is the case of organizer saving an invite with attendees, but without sending the notification.
        dat.mInvite.setNeverSent(true);
    }
    boolean forceSend = request.getAttributeBool(MailConstants.A_CAL_FORCESEND, true);
    MailSendQueue sendQueue = new MailSendQueue();
    try {
        sendCalendarMessage(zsc, octxt, iidFolder.getId(), acct, mbox, dat, response, true, forceSend, sendQueue);
    } finally {
        sendQueue.send();
    }
    boolean echo = request.getAttributeBool(MailConstants.A_CAL_ECHO, false);
    if (echo && dat.mAddInvData != null) {
        ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
        int maxSize = (int) request.getAttributeLong(MailConstants.A_MAX_INLINED_LENGTH, 0);
        boolean wantHTML = request.getAttributeBool(MailConstants.A_WANT_HTML, false);
        boolean neuter = request.getAttributeBool(MailConstants.A_NEUTER, true);
        echoAddedInvite(response, ifmt, octxt, mbox, dat.mAddInvData, maxSize, wantHTML, neuter);
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Address(javax.mail.Address) MessagingException(javax.mail.MessagingException) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 43 with ItemIdFormatter

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

the class CreateFolder method handle.

@Override
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_FOLDER);
    String name = t.getAttribute(MailConstants.A_NAME);
    String view = t.getAttribute(MailConstants.A_DEFAULT_VIEW, null);
    String flags = t.getAttribute(MailConstants.A_FLAGS, null);
    byte color = (byte) t.getAttributeLong(MailConstants.A_COLOR, MailItem.DEFAULT_COLOR);
    String rgb = t.getAttribute(MailConstants.A_RGB, null);
    String url = t.getAttribute(MailConstants.A_URL, null);
    String folderId = t.getAttribute(MailConstants.A_FOLDER, null);
    ItemId iidParent = folderId != null ? new ItemId(folderId, zsc) : null;
    boolean fetchIfExists = t.getAttributeBool(MailConstants.A_FETCH_IF_EXISTS, false);
    boolean syncToUrl = t.getAttributeBool(MailConstants.A_SYNC, true);
    ACL acl = FolderAction.parseACL(t.getOptionalElement(MailConstants.E_ACL), MailItem.Type.of(view), mbox.getAccount());
    Folder folder;
    boolean alreadyExisted = false;
    try {
        Folder.FolderOptions fopt = new Folder.FolderOptions();
        fopt.setColor(rgb != null ? new Color(rgb) : new Color(color)).setUrl(url);
        fopt.setDefaultView(MailItem.Type.of(view)).setFlags(Flag.toBitmask(flags));
        if (iidParent != null) {
            folder = mbox.createFolder(octxt, name, iidParent.getId(), fopt);
        } else {
            folder = mbox.createFolder(octxt, name, fopt);
        }
        if (!folder.getUrl().equals("") && syncToUrl) {
            try {
                mbox.synchronizeFolder(octxt, folder.getId());
            } catch (ServiceException e) {
                // if the synchronization fails, roll back the folder create
                rollbackFolder(folder);
                throw e;
            } catch (RuntimeException e) {
                // if the synchronization fails, roll back the folder create
                rollbackFolder(folder);
                throw ServiceException.FAILURE("could not synchronize with remote feed", e);
            }
        }
    } catch (ServiceException se) {
        if (se.getCode() == MailServiceException.ALREADY_EXISTS && fetchIfExists) {
            if (iidParent != null) {
                folder = mbox.getFolderByName(octxt, iidParent.getId(), name);
            } else {
                folder = mbox.getFolderByPath(octxt, name);
            }
            alreadyExisted = true;
        } else {
            throw se;
        }
    }
    // set the folder ACL as a separate operation, when appropriate
    if (acl != null && !alreadyExisted) {
        try {
            mbox.setPermissions(octxt, folder.getId(), acl);
        } catch (ServiceException e) {
            try {
                // roll back folder creation
                mbox.delete(null, folder.getId(), MailItem.Type.FOLDER);
            } catch (ServiceException nse) {
                ZimbraLog.soap.warn("error ignored while rolling back folder create", nse);
            }
            throw e;
        }
    }
    Element response = zsc.createElement(MailConstants.CREATE_FOLDER_RESPONSE);
    if (folder != null)
        ToXML.encodeFolder(response, ifmt, octxt, folder);
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) Color(com.zimbra.common.mailbox.Color) ACL(com.zimbra.cs.mailbox.ACL) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 44 with ItemIdFormatter

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

the class CreateNote 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_NOTE);
    ItemId iidFolder = new ItemId(t.getAttribute(MailConstants.A_FOLDER), zsc);
    String content = t.getAttribute(MailConstants.E_CONTENT);
    byte color = (byte) t.getAttributeLong(MailConstants.A_COLOR, MailItem.DEFAULT_COLOR);
    String strBounds = t.getAttribute(MailConstants.A_BOUNDS, null);
    Rectangle bounds = new Rectangle(strBounds);
    Note note = mbox.createNote(octxt, content, bounds, color, iidFolder.getId());
    Element response = zsc.createElement(MailConstants.CREATE_NOTE_RESPONSE);
    if (note != null)
        ToXML.encodeNote(response, ifmt, octxt, note);
    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) Note(com.zimbra.cs.mailbox.Note) Rectangle(com.zimbra.cs.mailbox.Note.Rectangle) ItemId(com.zimbra.cs.service.util.ItemId)

Example 45 with ItemIdFormatter

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

the class CreateSearchFolder method handle.

@Override
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);
    String name = t.getAttribute(MailConstants.A_NAME);
    String query = t.getAttribute(MailConstants.A_QUERY);
    String types = t.getAttribute(MailConstants.A_SEARCH_TYPES, null);
    String sort = t.getAttribute(MailConstants.A_SORTBY, null);
    String flags = t.getAttribute(MailConstants.A_FLAGS, null);
    byte color = (byte) t.getAttributeLong(MailConstants.A_COLOR, MailItem.DEFAULT_COLOR);
    String rgb = t.getAttribute(MailConstants.A_RGB, null);
    Color itemColor = rgb != null ? new Color(rgb) : new Color(color);
    ItemId iidParent = new ItemId(t.getAttribute(MailConstants.A_FOLDER), zsc);
    SearchFolder search = mbox.createSearchFolder(octxt, iidParent.getId(), name, query, types, sort, Flag.toBitmask(flags), itemColor);
    Element response = zsc.createElement(MailConstants.CREATE_SEARCH_FOLDER_RESPONSE);
    if (search != null)
        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) Color(com.zimbra.common.mailbox.Color) SearchFolder(com.zimbra.cs.mailbox.SearchFolder) ItemId(com.zimbra.cs.service.util.ItemId)

Aggregations

ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)63 Element (com.zimbra.common.soap.Element)56 Mailbox (com.zimbra.cs.mailbox.Mailbox)48 OperationContext (com.zimbra.cs.mailbox.OperationContext)46 ItemId (com.zimbra.cs.service.util.ItemId)44 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)44 Account (com.zimbra.cs.account.Account)21 ServiceException (com.zimbra.common.service.ServiceException)16 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)14 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)11 Folder (com.zimbra.cs.mailbox.Folder)11 Message (com.zimbra.cs.mailbox.Message)11 HashMap (java.util.HashMap)11 MailItem (com.zimbra.cs.mailbox.MailItem)10 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)8 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 MessagingException (javax.mail.MessagingException)7 MimeMessage (javax.mail.internet.MimeMessage)7