Search in sources :

Example 41 with Metadata

use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.

the class SetMailboxMetadata 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 meta = request.getElement(MailConstants.E_METADATA);
    String section = meta.getAttribute(MailConstants.A_SECTION);
    section = section.trim();
    if (section.length() == 0 || section.length() > 36 || section.indexOf(':') < 1)
        throw ServiceException.INVALID_REQUEST("invalid mailbox metadata section name", null);
    //will throw IllegalArgumentException if not known
    SectionNames.valueOf(section.substring(0, section.indexOf(':')));
    Metadata metadata = isModify() ? mbox.getConfig(octxt, section) : null;
    List<Element.KeyValuePair> keyvals = meta.listKeyValuePairs();
    if (!keyvals.isEmpty()) {
        metadata = metadata != null ? metadata : new Metadata();
        for (Element.KeyValuePair kvp : keyvals) {
            String key = kvp.getKey();
            if (key == null || key.equals(""))
                throw ServiceException.INVALID_REQUEST("empty key not allowed", null);
            String val = kvp.getValue();
            if (val == null || val.equals("")) {
                if (isModify())
                    metadata.remove(key);
                else
                    throw ServiceException.INVALID_REQUEST("empty value not allowed", null);
            } else {
                metadata.put(kvp.getKey(), kvp.getValue());
            }
        }
        if (metadata.isEmpty())
            metadata = null;
        else if (metadata.toString().length() > TOTAL_METADATA_LIMIT)
            throw MailServiceException.TOO_MUCH_METADATA(TOTAL_METADATA_LIMIT);
    } else if (isModify()) {
        throw ServiceException.INVALID_REQUEST("empty key/value set not allowed", null);
    }
    mbox.setConfig(octxt, section, metadata);
    Element response = zsc.createElement(getResponseName());
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Metadata(com.zimbra.cs.mailbox.Metadata)

Example 42 with Metadata

use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.

the class CalDavDataImport method pushModify.

private void pushModify(MailItem mitem) throws ServiceException, IOException, DavException {
    int itemId = mitem.getId();
    DataSource ds = getDataSource();
    DataSourceItem item = DbDataSource.getMapping(ds, itemId);
    boolean isCreate = false;
    if (item.remoteId == null) {
        // new item
        item.md = new Metadata();
        item.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
        item.remoteId = createTargetUrl(mitem);
        item.folderId = mitem.getFolderId();
        isCreate = true;
    }
    String type = item.md.get(METADATA_KEY_TYPE);
    if (METADATA_TYPE_FOLDER.equals(type)) {
        if (mitem.getType() != MailItem.Type.FOLDER) {
            ZimbraLog.datasource.warn("pushModify: item type doesn't match in metadata for item %d", itemId);
            return;
        }
    // detect and push rename
    } else if (METADATA_TYPE_APPOINTMENT.equals(type)) {
        if (mitem.getType() != MailItem.Type.APPOINTMENT) {
            ZimbraLog.datasource.warn("pushModify: item type doesn't match in metadata for item %d", itemId);
            return;
        }
        // push modified appt
        ZimbraLog.datasource.debug("pushModify: sending appointment %s", item.remoteId);
        String etag = putAppointment((CalendarItem) mitem, item);
        if (etag == null) {
            Appointment appt = mClient.getEtag(item.remoteId);
            etag = appt.etag;
        }
        item.md.put(METADATA_KEY_ETAG, etag);
        if (isCreate) {
            DbDataSource.addMapping(ds, item);
        } else {
            DbDataSource.updateMapping(ds, item);
        }
    } else {
        ZimbraLog.datasource.warn("pushModify: unrecognized item type for %d: %s", itemId, type);
        return;
    }
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Appointment(com.zimbra.cs.dav.client.CalDavClient.Appointment) Metadata(com.zimbra.cs.mailbox.Metadata) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource)

Example 43 with Metadata

use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.

the class CalDavDataImport method applyRemoteItem.

private MailItem applyRemoteItem(RemoteItem remoteItem, Folder where) throws ServiceException, IOException {
    if (!(remoteItem instanceof RemoteCalendarItem)) {
        ZimbraLog.datasource.warn("applyRemoteItem: not a calendar item: %s", remoteItem);
        return null;
    }
    RemoteCalendarItem item = (RemoteCalendarItem) remoteItem;
    DataSource ds = getDataSource();
    DataSourceItem dsItem = DbDataSource.getReverseMapping(ds, item.href);
    OperationContext octxt = new OperationContext(mbox);
    MailItem mi = null;
    boolean isStale = false;
    boolean isCreate = false;
    if (dsItem.md == null && item.status != Status.deleted) {
        dsItem.md = new Metadata();
        dsItem.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
    }
    if (dsItem.itemId == 0) {
        isStale = true;
        isCreate = true;
    } else {
        String etag = dsItem.md.get(METADATA_KEY_ETAG, null);
        try {
            mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
        } catch (MailServiceException.NoSuchItemException se) {
            ZimbraLog.datasource.warn("applyRemoteItem: calendar item not found: ", remoteItem);
        }
        if (item.etag == null) {
            ZimbraLog.datasource.warn("No Etag returned for item %s", item.href);
            isStale = true;
        } else if (etag == null) {
            ZimbraLog.datasource.warn("Empty etag for item %d", dsItem.itemId);
            isStale = true;
        } else {
            isStale = !item.etag.equals(etag);
        }
        if (mi == null)
            isStale = true;
    }
    if (item.status == Status.deleted) {
        ZimbraLog.datasource.debug("Deleting appointment %s", item.href);
        try {
            mi = mbox.getItemById(octxt, item.itemId, MailItem.Type.UNKNOWN);
        } catch (NoSuchItemException se) {
            mi = null;
        }
        try {
            mbox.delete(octxt, item.itemId, MailItem.Type.UNKNOWN);
        } catch (ServiceException se) {
            ZimbraLog.datasource.warn("Error deleting remotely deleted item %d (%s)", item.itemId, dsItem.remoteId);
        }
    } else if (isStale) {
        ZimbraLog.datasource.debug("Updating stale appointment %s", item.href);
        ZCalendar.ZVCalendar vcalendar;
        SetCalendarItemData main = new SetCalendarItemData();
        SetCalendarItemData[] exceptions = null;
        CalDavClient client = null;
        try {
            client = getClient();
        } catch (DavException e) {
            throw ServiceException.FAILURE("error creating CalDAV client", e);
        }
        Appointment appt = client.getCalendarData(new Appointment(item.href, item.etag));
        if (appt.data == null) {
            ZimbraLog.datasource.warn("No appointment found at " + item.href);
            return null;
        }
        dsItem.md.put(METADATA_KEY_ETAG, appt.etag);
        try {
            vcalendar = ZCalendar.ZCalendarBuilder.build(appt.data);
            List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, vcalendar, true);
            if (invites.size() > 1)
                exceptions = new SetCalendarItemData[invites.size() - 1];
            int pos = 0;
            boolean first = true;
            for (Invite i : invites) {
                if (first) {
                    main.invite = i;
                    first = false;
                } else {
                    SetCalendarItemData scid = new SetCalendarItemData();
                    scid.invite = i;
                    exceptions[pos++] = scid;
                }
            }
        } catch (Exception e) {
            ZimbraLog.datasource.warn("Error parsing appointment ", e);
            return null;
        }
        mi = mbox.setCalendarItem(octxt, where.getId(), 0, null, main, exceptions, null, CalendarItem.NEXT_ALARM_KEEP_CURRENT);
        dsItem.itemId = mi.getId();
        dsItem.folderId = mi.getFolderId();
        if (isCreate) {
            DbDataSource.addMapping(ds, dsItem);
        } else {
            DbDataSource.updateMapping(ds, dsItem);
        }
    } else {
        ZimbraLog.datasource.debug("Appointment up to date %s", item.href);
        try {
            mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
        } catch (NoSuchItemException se) {
            // item not found.  delete the mapping so it can be downloaded again if needed.
            ArrayList<Integer> deletedIds = new ArrayList<Integer>();
            deletedIds.add(dsItem.itemId);
            DbDataSource.deleteMappings(ds, deletedIds);
        }
    }
    return mi;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Appointment(com.zimbra.cs.dav.client.CalDavClient.Appointment) DavException(com.zimbra.cs.dav.DavException) Metadata(com.zimbra.cs.mailbox.Metadata) ArrayList(java.util.ArrayList) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalDavClient(com.zimbra.cs.dav.client.CalDavClient) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) DavException(com.zimbra.cs.dav.DavException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) MailItem(com.zimbra.cs.mailbox.MailItem) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ArrayList(java.util.ArrayList) List(java.util.List) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 44 with Metadata

use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.

the class Alarm method decodeMetadata.

/**
     * Create an Alarm from Metadata.  Return value may be null.
     * @param meta
     * @return
     * @throws ServiceException
     * @throws ParseException
     */
public static Alarm decodeMetadata(Metadata meta) throws ServiceException {
    Action action = expandAction(meta.get(FN_ACTION));
    if (!actionAllowed(action))
        return null;
    TriggerType tt = expandTriggerType(meta.get(FN_TRIGGER_TYPE));
    TriggerRelated triggerRelated = null;
    ParsedDuration triggerRelative = null;
    ParsedDateTime triggerAbsolute = null;
    if (TriggerType.ABSOLUTE.equals(tt)) {
        try {
            triggerAbsolute = ParsedDateTime.parseUtcOnly(meta.get(FN_TRIGGER_ABSOLUTE));
        } catch (ParseException e) {
            throw ServiceException.FAILURE("Error parsing metadata for alarm", e);
        }
    } else {
        triggerRelative = ParsedDuration.parse(meta.get(FN_TRIGGER_RELATIVE));
        triggerRelated = expandTriggerRelated(meta.get(FN_TRIGGER_RELATED, null));
    }
    ParsedDuration repeatDuration = null;
    int repeatCount = 0;
    String val = meta.get(FN_REPEAT_DURATION, null);
    if (val != null) {
        repeatDuration = ParsedDuration.parse(val);
        repeatCount = (int) meta.getLong(FN_REPEAT_COUNT, 0);
    }
    String description = meta.get(FN_DESCRIPTION, null);
    String summary = meta.get(FN_SUMMARY, null);
    Attach attach = null;
    Metadata metaAttach = meta.getMap(FN_ATTACH, true);
    if (metaAttach != null)
        attach = Util.decodeAttachFromMetadata(metaAttach);
    int numAts = (int) meta.getLong(FN_NUM_ATTENDEES, 0);
    List<ZAttendee> attendees = new ArrayList<ZAttendee>(numAts);
    for (int i = 0; i < numAts; i++) {
        try {
            Metadata metaAttendee = meta.getMap(FN_ATTENDEE + i, true);
            if (metaAttendee != null)
                attendees.add(new ZAttendee(metaAttendee));
        } catch (ServiceException e) {
            ZimbraLog.calendar.warn("Problem decoding attendee " + i + " in ALARM ");
        }
    }
    Alarm alarm = new Alarm(action, tt, triggerRelated, triggerRelative, triggerAbsolute, repeatDuration, repeatCount, description, summary, attach, attendees, Util.decodeXPropsFromMetadata(meta));
    return alarm;
}
Also used : ParsedDuration(com.zimbra.common.calendar.ParsedDuration) Attach(com.zimbra.common.calendar.Attach) CalendarAttach(com.zimbra.soap.mail.type.CalendarAttach) Metadata(com.zimbra.cs.mailbox.Metadata) ArrayList(java.util.ArrayList) ServiceException(com.zimbra.common.service.ServiceException) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) ParseException(java.text.ParseException)

Example 45 with Metadata

use of com.zimbra.cs.mailbox.Metadata in project zm-mailbox by Zimbra.

the class CalendarUser method encodeMetadata.

public Metadata encodeMetadata() {
    Metadata meta = new Metadata();
    meta.put(FN_ADDRESS, mAddress);
    meta.put(FN_CN, mCn);
    meta.put(FN_SENTBY, mSentBy);
    meta.put(FN_DIR, mDir);
    meta.put(FN_LANGUAGE, mLanguage);
    if (mXParams.size() > 0)
        Util.encodeXParamsAsMetadata(meta, xparamsIterator());
    return meta;
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata)

Aggregations

Metadata (com.zimbra.cs.mailbox.Metadata)60 Mailbox (com.zimbra.cs.mailbox.Mailbox)16 ArrayList (java.util.ArrayList)15 ServiceException (com.zimbra.common.service.ServiceException)13 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)9 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)8 PreparedStatement (java.sql.PreparedStatement)8 ResultSet (java.sql.ResultSet)8 OperationContext (com.zimbra.cs.mailbox.OperationContext)7 ICalTimeZone (com.zimbra.common.calendar.ICalTimeZone)6 MailItem (com.zimbra.cs.mailbox.MailItem)5 HashMap (java.util.HashMap)5 ZParameter (com.zimbra.common.calendar.ZCalendar.ZParameter)4 DataSource (com.zimbra.cs.account.DataSource)4 DbDataSource (com.zimbra.cs.db.DbDataSource)4 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 ParsedDateTime (com.zimbra.common.calendar.ParsedDateTime)3