use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.
the class GetContacts 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);
boolean sync = request.getAttributeBool(MailConstants.A_SYNC, false);
boolean derefContactGroupMember = request.getAttributeBool(MailConstants.A_DEREF_CONTACT_GROUP_MEMBER, false);
String folderIdStr = request.getAttribute(MailConstants.A_FOLDER, null);
int folderId = ALL_FOLDERS;
if (folderIdStr != null) {
ItemId iidFolder = new ItemId(folderIdStr, zsc);
if (iidFolder.belongsTo(mbox))
folderId = iidFolder.getId();
else
throw ServiceException.FAILURE("Got remote folderId: " + folderIdStr + " but did not proxy", null);
}
SortBy sort = SortBy.of(request.getAttribute(MailConstants.A_SORTBY, null));
if (sort == null) {
sort = SortBy.NONE;
}
ArrayList<String> attrs = null;
ArrayList<String> memberAttrs = null;
ArrayList<ItemId> ids = null;
for (Element e : request.listElements()) {
if (e.getName().equals(MailConstants.E_ATTRIBUTE)) {
String name = e.getAttribute(MailConstants.A_ATTRIBUTE_NAME);
if (attrs == null)
attrs = new ArrayList<String>();
attrs.add(name);
} else if (e.getName().equals(MailConstants.E_CONTACT_GROUP_MEMBER_ATTRIBUTE)) {
String name = e.getAttribute(MailConstants.A_ATTRIBUTE_NAME);
if (memberAttrs == null)
memberAttrs = new ArrayList<String>();
memberAttrs.add(name);
} else if (e.getName().equals(MailConstants.E_CONTACT)) {
String idStr = e.getAttribute(MailConstants.A_ID);
String[] targets = idStr.split(",");
for (String target : targets) {
ItemId iid = new ItemId(target, zsc);
if (ids == null)
ids = new ArrayList<ItemId>();
ids.add(iid);
}
// remove it from the request, so we can re-use the request for proxying below
e.detach();
}
}
long maxMembers = DEFAULT_MAX_MEMBERS;
boolean returnHiddenAttrs = false;
if (attrs == null) {
returnHiddenAttrs = request.getAttributeBool(MailConstants.A_RETURN_HIDDEN_ATTRS, false);
maxMembers = request.getAttributeLong(MailConstants.A_MAX_MEMBERS, DEFAULT_MAX_MEMBERS);
}
boolean returnCertInfo = request.getAttributeBool(MailConstants.A_RETURN_CERT_INFO, false);
Element response = zsc.createElement(MailConstants.GET_CONTACTS_RESPONSE);
// want to return modified date only on sync-related requests
int fields = ToXML.NOTIFY_FIELDS;
if (sync) {
fields |= Change.CONFLICT;
}
// for perf reason, derefContactGroupMember is not supported in this mode
if (derefContactGroupMember) {
if (ids == null) {
throw ServiceException.INVALID_REQUEST(MailConstants.A_DEREF_CONTACT_GROUP_MEMBER + " is supported only when specific contact ids are specified", null);
}
}
if (ids != null) {
ArrayList<Integer> local = new ArrayList<Integer>();
HashMap<String, StringBuffer> remote = new HashMap<String, StringBuffer>();
partitionItems(zsc, ids, local, remote);
if (remote.size() > 0) {
if (folderId > 0)
throw ServiceException.INVALID_REQUEST("Cannot specify a folder with mixed-mailbox items", null);
List<Element> responses = proxyRemote(request, remote, context);
for (Element e : responses) response.addElement(e);
}
if (local.size() > 0) {
boolean migrateDlist = CreateContact.needToMigrateDlist(zsc);
for (int id : local) {
Contact con = mbox.getContactById(octxt, id);
if (con != null && (folderId == ALL_FOLDERS || folderId == con.getFolderId())) {
ContactGroup contactGroup = null;
String migratedDlist = null;
if (migrateDlist) {
ContactGroup cg = ContactGroup.init(con, false);
if (cg != null) {
migratedDlist = cg.migrateToDlist(con.getMailbox(), octxt);
}
} else if (derefContactGroupMember) {
contactGroup = ContactGroup.init(con, false);
if (contactGroup != null) {
contactGroup.derefAllMembers(con.getMailbox(), octxt, zsc.getResponseProtocol());
}
}
ToXML.encodeContact(response, ifmt, octxt, con, contactGroup, memberAttrs, false, attrs, fields, migratedDlist, returnHiddenAttrs, maxMembers, returnCertInfo);
}
}
}
} else {
for (Contact con : mbox.getContactList(octxt, folderId, sort)) {
if (con != null) {
ToXML.encodeContact(response, ifmt, octxt, con, null, null, false, attrs, fields, null, returnHiddenAttrs, maxMembers, returnCertInfo);
}
}
}
return response;
}
use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.
the class GetCustomMetadata 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);
ItemId iid = new ItemId(request.getAttribute(MailConstants.A_ID), zsc);
MailItem item = mbox.getItemById(octxt, iid.getId(), MailItem.Type.UNKNOWN);
CustomMetadata custom = item.getCustomData(section);
Element response = zsc.createElement(MailConstants.GET_METADATA_RESPONSE);
response.addAttribute(MailConstants.A_ID, ifmt.formatItemId(item));
ToXML.encodeCustomMetadata(response, custom);
return response;
}
use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.
the class GetItem 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 target = request.getElement(MailConstants.E_ITEM);
String id = target.getAttribute(MailConstants.A_ID, null);
String folderStr = target.getAttribute(MailConstants.A_FOLDER, null);
String path = target.getAttribute(MailConstants.A_PATH, target.getAttribute(MailConstants.A_NAME, null));
if (folderStr != null && path == null)
throw ServiceException.INVALID_REQUEST("missing required attribute: " + MailConstants.A_PATH, null);
MailItem item;
if (id != null) {
// get item by id
item = mbox.getItemById(octxt, new ItemId(id, zsc).getId(), MailItem.Type.UNKNOWN);
} else if (path != null) {
// get item by name within containing folder id (from root by default)
int folderId = folderStr == null ? Mailbox.ID_FOLDER_USER_ROOT : new ItemId(folderStr, zsc).getId();
try {
item = mbox.getItemByPath(octxt, path, folderId);
} catch (MailServiceException.NoSuchItemException nsie) {
Pair<Folder, String> match = mbox.getFolderByPathLongestMatch(octxt, folderId, path);
if (match.getFirst() instanceof Mountpoint) {
Mountpoint mpt = (Mountpoint) match.getFirst();
target.addAttribute(MailConstants.A_FOLDER, mpt.getRemoteId()).addAttribute(MailConstants.A_PATH, match.getSecond());
return proxyRequest(request, context, mpt.getOwnerId());
}
throw nsie;
}
} else {
throw ServiceException.INVALID_REQUEST("must specify 'id' or 'path'", null);
}
Element response = zsc.createElement(MailConstants.GET_ITEM_RESPONSE);
ToXML.encodeItem(response, ifmt, octxt, item, ToXML.NOTIFY_FIELDS);
return response;
}
use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.
the class GetFolder 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);
ItemId iid;
Element eFolder = request.getOptionalElement(MailConstants.E_FOLDER);
if (eFolder != null) {
String uuid = eFolder.getAttribute(MailConstants.A_UUID, null);
if (uuid != null) {
Folder folder = mbox.getFolderByUuid(octxt, uuid);
iid = new ItemId(folder);
} else {
iid = new ItemId(eFolder.getAttribute(MailConstants.A_FOLDER, DEFAULT_FOLDER_ID), zsc);
}
String path = eFolder.getAttribute(MailConstants.A_PATH, null);
if (path != null) {
Pair<Folder, String> resolved = mbox.getFolderByPathLongestMatch(octxt, iid.getId(), path);
Folder folder = resolved.getFirst();
String overflow = resolved.getSecond();
if (overflow == null) {
iid = new ItemId(folder);
} else if (folder instanceof Mountpoint) {
// path crosses a mountpoint; update request and proxy to target mailbox
ItemId iidTarget = ((Mountpoint) folder).getTarget();
eFolder.addAttribute(MailConstants.A_FOLDER, iidTarget.toString()).addAttribute(MailConstants.A_PATH, overflow);
return proxyRequest(request, context, new ItemId(folder), iidTarget);
} else {
throw MailServiceException.NO_SUCH_FOLDER(path);
}
}
} else {
iid = new ItemId(DEFAULT_FOLDER_ID, zsc);
}
int depth = (int) request.getAttributeLong(MailConstants.A_FOLDER_DEPTH, -1);
boolean traverse = request.getAttributeBool(MailConstants.A_TRAVERSE, false);
boolean visible = request.getAttributeBool(MailConstants.A_VISIBLE, false);
boolean needGranteeName = request.getAttributeBool(MailConstants.A_NEED_GRANTEE_NAME, true);
String v = request.getAttribute(MailConstants.A_DEFAULT_VIEW, null);
MailItem.Type view = v == null ? null : MailItem.Type.of(v);
FolderNode rootnode = filterByView(mbox.getFolderTree(octxt, iid, visible), view);
Element response = zsc.createElement(MailConstants.GET_FOLDER_RESPONSE);
if (rootnode != null) {
if (needGranteeName) {
OperationContextData.addGranteeNames(octxt, rootnode);
} else {
OperationContextData.setNeedGranteeName(octxt, false);
}
List<ExpandableMountpoint> mounts = Lists.newArrayList();
Element folderRoot = encodeFolderNode(rootnode, response, ifmt, octxt, true, depth, view, traverse, mounts);
if (rootnode.mFolder != null && rootnode.mFolder instanceof Mountpoint) {
mounts.add(new ExpandableMountpoint(folderRoot, (Mountpoint) rootnode.mFolder, depth));
}
for (ExpandableMountpoint empt : mounts) {
expandMountpoint(empt, request, context);
}
}
return response;
}
use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.
the class GetNote 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 enote = request.getElement(MailConstants.E_NOTE);
ItemId iid = new ItemId(enote.getAttribute(MailConstants.A_ID), zsc);
Note note = mbox.getNoteById(octxt, iid.getId());
if (note == null)
throw MailServiceException.NO_SUCH_NOTE(iid.getId());
Element response = zsc.createElement(MailConstants.GET_NOTE_RESPONSE);
ToXML.encodeNote(response, ifmt, octxt, note);
return response;
}
Aggregations