Search in sources :

Example 21 with ItemIdFormatter

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

the class ListDocumentRevisions 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 doc = request.getElement(MailConstants.E_DOC);
    String id = doc.getAttribute(MailConstants.A_ID);
    int version = (int) doc.getAttributeLong(MailConstants.A_VERSION, -1);
    int count = (int) doc.getAttributeLong(MailConstants.A_COUNT, 1);
    Element response = zsc.createElement(MailConstants.LIST_DOCUMENT_REVISIONS_RESPONSE);
    Document item;
    ItemId iid = new ItemId(id, zsc);
    item = mbox.getDocumentById(octxt, iid.getId());
    if (version < 0) {
        version = item.getVersion();
    }
    MailItem.Type type = item.getType();
    HashSet<Account> accounts = new HashSet<Account>();
    Provisioning prov = Provisioning.getInstance();
    while (version > 0 && count > 0) {
        item = (Document) mbox.getItemRevision(octxt, iid.getId(), type, version);
        if (item != null) {
            ToXML.encodeDocument(response, ifmt, octxt, item);
            Account a = prov.getAccountByName(item.getCreator());
            if (a != null)
                accounts.add(a);
        }
        version--;
        count--;
    }
    for (Account a : accounts) {
        Element user = response.addElement(MailConstants.A_USER);
        user.addAttribute(MailConstants.A_ID, a.getId());
        user.addAttribute(MailConstants.A_EMAIL, a.getName());
        user.addAttribute(MailConstants.A_NAME, a.getDisplayName());
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) HashSet(java.util.HashSet)

Example 22 with ItemIdFormatter

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

the class TagAction 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);
    Element action = request.getElement(MailConstants.E_ACTION);
    String opAttr = action.getAttribute(MailConstants.A_OPERATION).toLowerCase();
    String operation = getOperation(opAttr);
    if (operation.equals(OP_TAG) || operation.equals(OP_FLAG)) {
        throw ServiceException.INVALID_REQUEST("cannot tag/flag a tag", null);
    }
    if (!TAG_ACTIONS.contains(operation)) {
        throw ServiceException.INVALID_REQUEST("invalid operation on tag: " + opAttr, null);
    }
    String tn = action.getAttribute(MailConstants.A_TAG_NAMES, null);
    if (tn != null) {
        // switch tag names to tag IDs, because that's what ItemAction expects
        StringBuilder tagids = new StringBuilder();
        for (String name : TagUtil.decodeTags(tn)) {
            tagids.append(tagids.length() == 0 ? "" : ",").append(mbox.getTagByName(octxt, name).getId());
        }
        action.addAttribute(MailConstants.A_ID, tagids.toString());
    }
    String successes;
    if (operation.equals(OP_RETENTION_POLICY)) {
        ItemId iid = new ItemId(action.getAttribute(MailConstants.A_ID), zsc);
        RetentionPolicy rp = new RetentionPolicy(action.getElement(MailConstants.E_RETENTION_POLICY));
        mbox.setRetentionPolicy(octxt, iid.getId(), MailItem.Type.TAG, rp);
        successes = new ItemIdFormatter(zsc).formatItemId(iid);
    } else {
        successes = handleCommon(context, request, opAttr, MailItem.Type.TAG);
    }
    Element response = zsc.createElement(MailConstants.TAG_ACTION_RESPONSE);
    Element result = response.addUniqueElement(MailConstants.E_ACTION);
    result.addAttribute(MailConstants.A_ID, successes);
    result.addAttribute(MailConstants.A_TAG_NAMES, tn);
    result.addAttribute(MailConstants.A_OPERATION, opAttr);
    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) ItemId(com.zimbra.cs.service.util.ItemId) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Example 23 with ItemIdFormatter

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

the class SetCalendarItem 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);
    int flags = Flag.toBitmask(request.getAttribute(MailConstants.A_FLAGS, null));
    String[] tags = TagUtil.parseTags(request, mbox, octxt);
    mbox.lock.lock();
    try {
        int defaultFolder = getItemType() == MailItem.Type.TASK ? Mailbox.ID_FOLDER_TASKS : Mailbox.ID_FOLDER_CALENDAR;
        String defaultFolderStr = Integer.toString(defaultFolder);
        String folderIdStr = request.getAttribute(MailConstants.A_FOLDER, defaultFolderStr);
        ItemId iidFolder = new ItemId(folderIdStr, zsc);
        Folder folder = mbox.getFolderById(octxt, iidFolder.getId());
        SetCalendarItemParseResult parsed = parseSetAppointmentRequest(request, zsc, octxt, folder, getItemType(), false);
        CalendarItem calItem = mbox.setCalendarItem(octxt, iidFolder.getId(), flags, tags, parsed.defaultInv, parsed.exceptions, parsed.replies, parsed.nextAlarm);
        Element response = getResponseElement(zsc);
        if (parsed.defaultInv != null) {
            response.addElement(MailConstants.A_DEFAULT).addAttribute(MailConstants.A_ID, ifmt.formatItemId(parsed.defaultInv.invite.getMailItemId()));
        }
        if (parsed.exceptions != null) {
            for (SetCalendarItemData cur : parsed.exceptions) {
                Element e = response.addElement(MailConstants.E_CAL_EXCEPT);
                e.addAttribute(MailConstants.A_CAL_RECURRENCE_ID, cur.invite.getRecurId().toString());
                e.addAttribute(MailConstants.A_ID, ifmt.formatItemId(cur.invite.getMailItemId()));
            }
        }
        String itemId = ifmt.formatItemId(calItem == null ? 0 : calItem.getId());
        response.addAttribute(MailConstants.A_CAL_ID, itemId);
        try {
            Element inv = request.getElement(MailConstants.A_DEFAULT).getElement(MailConstants.E_MSG).getElement(MailConstants.E_INVITE);
            Element comp = inv.getOptionalElement(MailConstants.E_INVITE_COMPONENT);
            if (comp != null) {
                inv = comp;
            }
            String reqCalItemId = inv.getAttribute(MailConstants.A_CAL_ID);
            String uid = inv.getAttribute(MailConstants.A_UID);
            //new or same as requested, or Outlook and case-insensitive equal
            boolean uidSame = (calItem == null || (calItem.getUid() == null && uid == null) || (calItem.getUid() != null && (calItem.getUid().equals(uid) || (Invite.isOutlookUid(calItem.getUid()) && calItem.getUid().equalsIgnoreCase(uid)))));
            if (ZimbraLog.calendar.isInfoEnabled()) {
                StringBuilder logBuf = new StringBuilder();
                if (!reqCalItemId.equals(itemId)) {
                    logBuf.append("Mapped requested id ").append(reqCalItemId).append(" -> ").append(itemId);
                }
                if (!uidSame) {
                    logBuf.append(" ?? requested UID ").append(uid).append(" differs from mapped ").append(calItem.getUid());
                    ZimbraLog.calendar.warn(logBuf.toString());
                } else if (logBuf.length() > 0) {
                    ZimbraLog.calendar.info(logBuf.toString());
                }
            }
            assert (uidSame);
        } catch (ServiceException se) {
        //one of the elements we wanted to use doesn't exist; ignore; no log/assertion possible
        }
        if (!parsed.isTodo)
            // for backward compat
            response.addAttribute(MailConstants.A_APPT_ID_DEPRECATE_ME, itemId);
        return response;
    } finally {
        mbox.lock.release();
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) 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) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 24 with ItemIdFormatter

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

the class SetCustomMetadata 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 meta = request.getElement(MailConstants.E_METADATA);
    String section = meta.getAttribute(MailConstants.A_SECTION);
    section = section.trim();
    if (section.length() == 0 || section.length() > 36)
        throw ServiceException.INVALID_REQUEST("invalid length for custom metadata section name", null);
    CustomMetadata custom = new CustomMetadata(section);
    for (Element.KeyValuePair kvp : meta.listKeyValuePairs()) custom.put(kvp.getKey(), kvp.getValue());
    ItemId iid = new ItemId(request.getAttribute(MailConstants.A_ID), zsc);
    mbox.setCustomData(octxt, iid.getId(), MailItem.Type.UNKNOWN, custom);
    Element response = zsc.createElement(MailConstants.SET_METADATA_RESPONSE);
    response.addAttribute(MailConstants.A_ID, ifmt.formatItemId(iid));
    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) ItemId(com.zimbra.cs.service.util.ItemId) CustomMetadata(com.zimbra.cs.mailbox.MailItem.CustomMetadata)

Example 25 with ItemIdFormatter

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

the class SnoozeCalendarItemAlarm method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    String acctId = mbox.getAccountId();
    Account authAcct = getAuthenticatedAccount(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element response = getResponseElement(zsc);
    for (String calItemElement : sCalItemElems) {
        for (Iterator<Element> iter = request.elementIterator(calItemElement); iter.hasNext(); ) {
            Element calItemElem = iter.next();
            ItemId iid = new ItemId(calItemElem.getAttribute(MailConstants.A_ID), zsc);
            long snoozeUntil = calItemElem.getAttributeLong(MailConstants.A_CAL_ALARM_SNOOZE_UNTIL);
            // trace logging
            ZimbraLog.calendar.info("<SnoozeCalendarItemAlarm> id=%s,until=%d", iid.toString(), snoozeUntil);
            // mailbox for this calendar item; not necessarily same as requested mailbox
            Mailbox ciMbox = null;
            String ciAcctId = iid.getAccountId();
            if (ciAcctId == null || ciAcctId.equals(acctId)) {
                ciMbox = mbox;
            } else {
                // Let's see if the account is a Desktop account.
                if (AccountUtil.isZDesktopLocalAccount(zsc.getAuthtokenAccountId()))
                    ciMbox = MailboxManager.getInstance().getMailboxByAccountId(ciAcctId, false);
                if (ciMbox == null)
                    throw MailServiceException.PERM_DENIED("cannot snooze alarms of a shared calendar");
            }
            int calItemId = iid.getId();
            ItemIdFormatter ifmt = new ItemIdFormatter(authAcct.getId(), acctId, false);
            try {
                ciMbox.snoozeCalendarItemAlarm(octxt, calItemId, snoozeUntil);
                CalendarItem calItem = ciMbox.getCalendarItemById(octxt, calItemId);
                Element calItemRespElem;
                if (calItem instanceof Appointment)
                    calItemRespElem = response.addElement(MailConstants.E_APPOINTMENT);
                else
                    calItemRespElem = response.addElement(MailConstants.E_TASK);
                calItemRespElem.addAttribute(MailConstants.A_CAL_ID, iid.toString(ifmt));
                boolean hidePrivate = !calItem.allowPrivateAccess(authAcct, zsc.isUsingAdminPrivileges());
                boolean showAll = !hidePrivate || calItem.isPublic();
                if (showAll) {
                    AlarmData alarmData = calItem.getAlarmData();
                    if (alarmData != null)
                        ToXML.encodeAlarmData(calItemRespElem, calItem, alarmData);
                }
            } catch (NoSuchItemException nsie) {
                //item must not exist in db anymore.
                //this can happen if an alarm is snoozed while a big sync is happening which deletes the item (e.g. bug 48560)
                //since item is not in db, it has effectively been snoozed; return ok and no further alarms
                Element calItemRespElem = response.addElement(calItemElement);
                calItemRespElem.addAttribute(MailConstants.A_CAL_ID, iid.toString(ifmt));
            }
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Appointment(com.zimbra.cs.mailbox.Appointment) Account(com.zimbra.cs.account.Account) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) AlarmData(com.zimbra.cs.mailbox.CalendarItem.AlarmData)

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