use of com.zimbra.cs.mailbox.Conversation in project zm-mailbox by Zimbra.
the class GetConv 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 econv = request.getElement(MailConstants.E_CONV);
ItemId iid = new ItemId(econv.getAttribute(MailConstants.A_ID), zsc);
SearchParams params = new SearchParams();
params.setInlineRule(ExpandResults.valueOf(econv.getAttribute(MailConstants.A_FETCH, null), zsc));
if (params.getInlineRule() != ExpandResults.NONE) {
params.setWantHtml(econv.getAttributeBool(MailConstants.A_WANT_HTML, false));
params.setMaxInlinedLength((int) econv.getAttributeLong(MailConstants.A_MAX_INLINED_LENGTH, -1));
params.setWantExpandGroupInfo(econv.getAttributeBool(MailConstants.A_NEED_EXP, false));
for (Element eHdr : econv.listElements(MailConstants.A_HEADER)) {
params.addInlinedHeader(eHdr.getAttribute(MailConstants.A_ATTRIBUTE_NAME));
}
}
Conversation conv = mbox.getConversationById(octxt, iid.getId());
if (conv == null) {
throw MailServiceException.NO_SUCH_CONV(iid.getId());
}
List<Message> msgs = mbox.getMessagesByConversation(octxt, conv.getId(), SortBy.DATE_ASC, -1);
if (msgs.isEmpty() && zsc.isDelegatedRequest()) {
throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
}
Element response = zsc.createElement(MailConstants.GET_CONV_RESPONSE);
ToXML.encodeConversation(response, ifmt, octxt, conv, msgs, params);
return response;
}
use of com.zimbra.cs.mailbox.Conversation in project zm-mailbox by Zimbra.
the class DbMailItem method setFolder.
public static void setFolder(MailItem item, Folder folder) throws ServiceException {
Mailbox mbox = item.getMailbox();
if (mbox != folder.getMailbox()) {
throw MailServiceException.WRONG_MAILBOX();
}
checkNamingConstraint(mbox, folder.getId(), item.getName(), item.getId());
DbConnection conn = mbox.getOperationConnection();
PreparedStatement stmt = null;
try {
String imapRenumber = mbox.isTrackingImap() ? ", imap_id = CASE WHEN imap_id IS NULL THEN NULL ELSE 0 END" : "";
int pos = 1;
boolean hasIndexId = false;
if (item instanceof Folder) {
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET parent_id = ?, folder_id = ?, prev_folders = ?, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
stmt.setInt(pos++, folder.getId());
} else if (item instanceof Conversation && !(item instanceof VirtualConversation)) {
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET folder_id = ?, prev_folders = ?, mod_metadata = ?, change_date = ?" + imapRenumber + " WHERE " + IN_THIS_MAILBOX_AND + "parent_id = ?");
} else {
// set the indexId, in case it changed (moving items out of junk can trigger an index ID change)
hasIndexId = true;
stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET folder_id = ?, prev_folders = ?, index_id = ?, mod_metadata = ?, change_date = ? " + imapRenumber + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
}
stmt.setInt(pos++, folder.getId());
int modseq = mbox.getOperationChangeID();
String prevFolders = findPrevFolders(item, modseq);
stmt.setString(pos++, prevFolders);
item.getUnderlyingData().setPrevFolders(prevFolders);
if (hasIndexId) {
if (item.getIndexStatus() == MailItem.IndexStatus.NO) {
stmt.setNull(pos++, Types.INTEGER);
} else {
stmt.setInt(pos++, item.getIndexId());
}
}
stmt.setInt(pos++, modseq);
stmt.setInt(pos++, mbox.getOperationTimestamp());
pos = setMailboxId(stmt, mbox, pos);
stmt.setInt(pos++, item instanceof VirtualConversation ? ((VirtualConversation) item).getMessageId() : item.getId());
stmt.executeUpdate();
} catch (SQLException e) {
// catch item_id uniqueness constraint violation and return failure
if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
throw MailServiceException.ALREADY_EXISTS(item.getName(), e);
} else {
throw ServiceException.FAILURE("writing new folder data for item " + item.getId(), e);
}
} finally {
DbPool.closeStatement(stmt);
}
}
use of com.zimbra.cs.mailbox.Conversation in project zm-mailbox by Zimbra.
the class DataSourcePurge method purgeConversation.
private void purgeConversation(OperationContext octxt, PurgeableConv conv) throws ServiceException {
if (conv.isMsg()) {
Message message = mbox.getMessageById(null, conv.getId());
ZimbraLog.datasource.info(String.format("purging message %d", conv.getId()));
purgeMailItem(octxt, conv.getDataSourceId(), message, Type.MESSAGE);
} else {
Conversation conversation = mbox.getConversationById(null, conv.getId());
ZimbraLog.datasource.info(String.format("purging conversation %d", conv.getId()));
purgeMailItem(octxt, conv.getDataSourceId(), conversation, Type.CONVERSATION);
}
}
use of com.zimbra.cs.mailbox.Conversation in project zm-mailbox by Zimbra.
the class SearchConv 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);
SearchConvRequest req = JaxbUtil.elementToJaxb(request);
boolean nest = ZmBoolean.toBool(req.getNestMessages(), false);
Account acct = getRequestedAccount(zsc);
SearchParams params = SearchParams.parse(req, zsc, acct.getPrefMailInitialSearch());
// append (conv:(convid)) onto the beginning of the queryStr
ItemId cid = new ItemId(req.getConversationId(), zsc);
params.setQueryString("conv:\"" + cid.toString(ifmt) + "\" (" + params.getQueryString() + ')');
// force to group-by-message
params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
Element response = null;
if (cid.belongsTo(mbox)) {
// local
ZimbraQueryResults results = mbox.index.search(zsc.getResponseProtocol(), octxt, params);
try {
response = zsc.createElement(MailConstants.SEARCH_CONV_RESPONSE);
response.addAttribute(MailConstants.A_QUERY_OFFSET, Integer.toString(params.getOffset()));
SortBy sort = results.getSortBy();
response.addAttribute(MailConstants.A_SORTBY, sort.toString());
List<Message> msgs = mbox.getMessagesByConversation(octxt, cid.getId(), sort, -1);
if (msgs.isEmpty() && zsc.isDelegatedRequest()) {
throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
}
// filter out IMAP \Deleted messages from the message lists
Conversation conv = mbox.getConversationById(octxt, cid.getId());
if (conv.isTagged(Flag.FlagInfo.DELETED)) {
List<Message> raw = msgs;
msgs = new ArrayList<Message>();
for (Message msg : raw) {
if (!msg.isTagged(Flag.FlagInfo.DELETED)) {
msgs.add(msg);
}
}
}
Element container = nest ? ToXML.encodeConversationSummary(response, ifmt, octxt, conv, CONVERSATION_FIELD_MASK) : response;
SearchResponse builder = new SearchResponse(zsc, octxt, container, params);
builder.setAllRead(conv.getUnreadCount() == 0);
boolean more = putHits(octxt, ifmt, builder, msgs, results, params, conv);
response.addAttribute(MailConstants.A_QUERY_MORE, more);
// call me AFTER putHits since some of the <info> is generated by the getting of the hits!
builder.add(results.getResultInfo());
} finally {
Closeables.closeQuietly(results);
}
return response;
} else {
// remote
try {
Element proxyRequest = zsc.createElement(MailConstants.SEARCH_CONV_REQUEST);
Account target = Provisioning.getInstance().get(AccountBy.id, cid.getAccountId(), zsc.getAuthToken());
if (target != null) {
params.setInlineRule(params.getInlineRule().toLegacyExpandResults(target.getServer()));
}
params.encodeParams(proxyRequest);
proxyRequest.addAttribute(MailConstants.A_NEST_MESSAGES, nest);
proxyRequest.addAttribute(MailConstants.A_CONV_ID, cid.toString());
// okay, lets run the search through the query parser -- this has the side-effect of
// re-writing the query in a format that is OK to proxy to the other server -- since the
// query has an "AND conv:remote-conv-id" part, the query parser will figure out the right
// format for us. TODO somehow make this functionality a bit more exposed in the
// ZimbraQuery APIs...
String rewrittenQueryString = mbox.getRewrittenQueryString(octxt, params);
proxyRequest.addAttribute(MailConstants.E_QUERY, rewrittenQueryString, Element.Disposition.CONTENT);
// proxy to remote account
response = proxyRequest(proxyRequest, context, target.getId());
return response.detach();
} catch (SoapFaultException e) {
throw ServiceException.FAILURE("SoapFaultException: ", e);
}
}
}
use of com.zimbra.cs.mailbox.Conversation in project zm-mailbox by Zimbra.
the class ItemActionHelper method executeRemote.
private void executeRemote() throws ServiceException, IOException {
Account target = Provisioning.getInstance().get(Key.AccountBy.id, mIidFolder.getAccountId());
AuthToken at = getAuthToken();
String pxyAuthToken = Provisioning.onLocalServer(target) ? null : at.getProxyAuthToken();
ZAuthToken zat = null;
if (pxyAuthToken == null) {
zat = at.toZAuthToken();
zat.resetProxyAuthToken();
} else {
zat = new ZAuthToken(pxyAuthToken);
}
ZMailbox.Options zoptions = new ZMailbox.Options(zat, AccountUtil.getSoapUri(target));
zoptions.setNoSession(true);
zoptions.setTargetAccount(target.getId());
zoptions.setTargetAccountBy(Key.AccountBy.id);
ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
// check for mountpoints before going any further...
ZFolder zfolder = zmbx.getFolderById(mIidFolder.toString(mAuthenticatedAccount));
if (zfolder instanceof ZMountpoint) {
ItemId iidTarget = new ItemId(((ZMountpoint) zfolder).getCanonicalRemoteId(), mAuthenticatedAccount.getId());
if (!mIidFolder.equals(iidTarget)) {
mIidFolder = iidTarget;
if (++mHopCount > com.zimbra.soap.ZimbraSoapContext.MAX_HOP_COUNT)
throw MailServiceException.TOO_MANY_HOPS(mIidRequestedFolder);
schedule();
return;
}
}
boolean deleteOriginal = mOperation != Op.COPY;
String folderStr = mIidFolder.toString();
mCreatedIds = new ArrayList<String>(itemIds.length);
boolean toSpam = mIidFolder.getId() == Mailbox.ID_FOLDER_SPAM;
boolean toMailbox = !toSpam && mIidFolder.getId() != Mailbox.ID_FOLDER_TRASH;
for (MailItem item : mMailbox.getItemById(mOpCtxt, itemIds, type)) {
if (item == null) {
continue;
}
List<Message> msgs = null;
if (item instanceof Conversation) {
msgs = mMailbox.getMessagesByConversation(mOpCtxt, item.getId(), SortBy.DATE_ASC, -1);
}
if (deleteOriginal) {
if (msgs != null) {
// determine which of the conversation's component messages are actually able to be moved
boolean permDenied = false;
for (Iterator<Message> it = msgs.iterator(); it.hasNext(); ) {
Message msg = it.next();
if (!TargetConstraint.checkItem(mTargetConstraint, msg)) {
it.remove();
} else if (!canDelete(msg)) {
it.remove();
permDenied = true;
}
}
// stop here if no messages would be moved...
if (msgs.isEmpty()) {
if (permDenied) {
throw ServiceException.PERM_DENIED("cannot delete any messages in " + item.getType() + " " + item.getId());
}
// all messages were excluded by the TargetConstraint, so there's no failure...
continue;
}
} else {
if (!canDelete(item)) {
throw ServiceException.PERM_DENIED("cannot delete existing copy of " + item.getType() + " " + item.getId());
}
}
}
boolean fromSpam = item.inSpam();
if ((fromSpam && toMailbox) || (!fromSpam && toSpam)) {
try {
Folder dest = mMailbox.getFolderById(mOpCtxt, mIidFolder.getId());
SpamReport report = new SpamReport(toSpam, "remote " + mOperation, dest.getPath());
Folder source = mMailbox.getFolderById(mOpCtxt, item.getFolderId());
report.setSourceFolderPath(source.getPath());
report.setDestAccountName(target.getName());
SpamHandler.getInstance().handle(mOpCtxt, mMailbox, item.getId(), item.getType(), report);
} catch (OutOfMemoryError e) {
Zimbra.halt("out of memory", e);
} catch (Throwable t) {
ZimbraLog.mailop.info("could not train spam filter: " + new ItemId(item).toString(), t);
}
}
// since we can't apply tags to a remote object, hardwiring "tags" to null below...
String flags = (mOperation == Op.UPDATE && mFlags != null ? mFlags : item.getFlagString());
String name = ((mOperation == Op.RENAME || mOperation == Op.UPDATE) && mName != null ? mName : item.getName());
String createdId = null;
InputStream in = null;
switch(item.getType()) {
case CONTACT:
Contact ct = (Contact) item;
Map<String, ZMailbox.ZAttachmentInfo> attachments = new HashMap<String, ZMailbox.ZAttachmentInfo>();
for (Contact.Attachment att : ct.getAttachments()) {
String attachmentId = zmbx.uploadAttachment(att.getFilename(), att.getContent(), att.getContentType(), 0);
ZMailbox.ZAttachmentInfo info = new ZMailbox.ZAttachmentInfo().setAttachmentId(attachmentId);
attachments.put(att.getName(), info);
}
Map<String, String> fields = ct.getFields();
Map<String, String> members = new HashMap<String, String>();
for (String key : fields.keySet()) {
if (ContactConstants.A_groupMember.equals(key)) {
String memberEncoded = fields.get(key);
ContactGroup group = ContactGroup.init(memberEncoded);
for (Member m : group.getMembers()) {
members.put(m.getValue(), m.getType().getSoapEncoded());
}
break;
}
}
fields.remove(ContactConstants.A_groupMember);
ZContact contact = zmbx.createContact(folderStr, null, fields, attachments, members);
createdId = contact.getId();
mCreatedIds.add(createdId);
break;
case MESSAGE:
try {
in = StoreManager.getInstance().getContent(item.getBlob());
createdId = zmbx.addMessage(folderStr, flags, null, item.getDate(), in, item.getSize(), true);
} finally {
ByteUtil.closeStream(in);
}
mCreatedIds.add(createdId);
break;
case VIRTUAL_CONVERSATION:
case CONVERSATION:
for (Message msg : msgs) {
flags = (mOperation == Op.UPDATE && mFlags != null ? mFlags : msg.getFlagString());
try {
in = StoreManager.getInstance().getContent(msg.getBlob());
createdId = zmbx.addMessage(folderStr, flags, null, msg.getDate(), in, msg.getSize(), true);
} finally {
ByteUtil.closeStream(in);
}
mCreatedIds.add(createdId);
}
break;
case DOCUMENT:
Document doc = (Document) item;
SoapHttpTransport transport = new SoapHttpTransport(zoptions.getUri());
try {
in = StoreManager.getInstance().getContent(doc.getBlob());
String uploadId = zmbx.uploadContentAsStream(name, in, doc.getContentType(), doc.getSize(), 4000, true);
// instead of using convenience method from ZMailbox
// we need to hand marshall the request and set the
// response protocol explicitly to what was requested
// from the client.
Element req = new XMLElement(MailConstants.SAVE_DOCUMENT_REQUEST);
Element edoc = req.addUniqueElement(MailConstants.E_DOC);
edoc.addAttribute(MailConstants.A_NAME, name);
edoc.addAttribute(MailConstants.A_FOLDER, folderStr);
edoc.addAttribute(MailConstants.A_FLAGS, flags);
Element upload = edoc.addElement(MailConstants.E_UPLOAD);
upload.addAttribute(MailConstants.A_ID, uploadId);
transport.setResponseProtocol(mResponseProtocol);
transport.setAuthToken(zat);
Element response = transport.invoke(req);
createdId = response.getElement(MailConstants.E_DOC).getAttribute(MailConstants.A_ID);
} finally {
ByteUtil.closeStream(in);
transport.shutdown();
}
mCreatedIds.add(createdId);
break;
case APPOINTMENT:
case TASK:
CalendarItem cal = (CalendarItem) item;
// private calendar item may not be moved by non-owner unless permission was granted
if (!cal.isPublic()) {
boolean asAdmin = mOpCtxt != null ? mOpCtxt.isUsingAdminPrivileges() : false;
if (!cal.allowPrivateAccess(mAuthenticatedAccount, asAdmin))
throw ServiceException.PERM_DENIED("you do not have permission to move/copy a private calendar item from the current folder/mailbox");
}
// Move the item to remote mailbox using SetAppointmentRequest/SetTaskRequest.
QName qname = (item.getType() == MailItem.Type.TASK ? MailConstants.SET_TASK_REQUEST : MailConstants.SET_APPOINTMENT_REQUEST);
Element request = new Element.XMLElement(qname).addAttribute(MailConstants.A_FOLDER, folderStr).addAttribute(MailConstants.A_FLAGS, flags);
ToXML.encodeAlarmTimes(request, cal);
Invite invDefault = cal.getDefaultInviteOrNull();
// Takeover as organizer if we're doing a MOVE and source mailbox is the organizer.
// Don't takeover in a COPY operation.
boolean takeoverAsOrganizer = false;
boolean blockMove = false;
if (Op.MOVE.equals(mOperation)) {
Invite inv = invDefault;
if (inv == null) {
// no default invite; let's use the first invite
Invite[] invs = cal.getInvites();
if (invs != null && invs.length > 0)
inv = invs[0];
}
takeoverAsOrganizer = inv != null && inv.isOrganizer();
blockMove = takeoverAsOrganizer && inv.hasOtherAttendees();
}
if (blockMove) {
throw MailServiceException.INVALID_REQUEST("This operation requires change of organizer and it is not permitted", null);
}
if (invDefault != null) {
addCalendarPart(request.addUniqueElement(MailConstants.A_DEFAULT), cal, invDefault, zmbx, target, takeoverAsOrganizer);
}
for (Invite inv : cal.getInvites()) {
if (inv == null || inv == invDefault)
continue;
String elem = inv.isCancel() ? MailConstants.E_CAL_CANCEL : MailConstants.E_CAL_EXCEPT;
addCalendarPart(request.addElement(elem), cal, inv, zmbx, target, takeoverAsOrganizer);
}
ToXML.encodeCalendarReplies(request, cal);
createdId = zmbx.invoke(request).getAttribute(MailConstants.A_CAL_ID);
mCreatedIds.add(createdId);
break;
default:
throw MailServiceException.CANNOT_COPY(item.getId());
}
try {
if (deleteOriginal && !mIdFormatter.formatItemId(item).equals(createdId)) {
if (msgs == null) {
mMailbox.delete(mOpCtxt, item.getId(), item.getType());
} else {
for (Message msg : msgs) mMailbox.delete(mOpCtxt, msg.getId(), msg.getType());
}
}
} catch (ServiceException e) {
if (e.getCode() != ServiceException.PERM_DENIED)
throw e;
// something funky happened permissions-wise between the getEffectivePermissions check and here...
ZimbraLog.misc.info("could not delete original item " + item.getId() + "; treating operation as a copy instead");
}
}
}
Aggregations