use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class Search method runSimpleAppointmentQuery.
private static void runSimpleAppointmentQuery(Element parent, SearchParams params, OperationContext octxt, ZimbraSoapContext zsc, Account authAcct, Mailbox mbox, List<String> folderIdStrs) throws ServiceException {
Set<MailItem.Type> types = params.getTypes();
MailItem.Type type = types.size() == 1 ? Iterables.getOnlyElement(types) : MailItem.Type.APPOINTMENT;
if (params.getSortBy() != null) {
parent.addAttribute(MailConstants.A_SORTBY, params.getSortBy().toString());
}
parent.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset());
parent.addAttribute(MailConstants.A_QUERY_MORE, false);
List<ItemId> folderIids = new ArrayList<ItemId>(folderIdStrs.size());
for (String folderIdStr : folderIdStrs) {
folderIids.add(new ItemId(folderIdStr, zsc));
}
Provisioning prov = Provisioning.getInstance();
MailboxManager mboxMgr = MailboxManager.getInstance();
Server localServer = prov.getLocalServer();
Map<Server, Map<String, List<Integer>>> /* folder ids */
groupedByServer = groupByServer(ItemId.groupFoldersByAccount(octxt, mbox, folderIids));
// Look up in calendar cache first.
if (LC.calendar_cache_enabled.booleanValue()) {
CalSummaryCache calCache = CalendarCacheManager.getInstance().getSummaryCache();
long rangeStart = params.getCalItemExpandStart();
long rangeEnd = params.getCalItemExpandEnd();
for (Iterator<Map.Entry<Server, Map<String, List<Integer>>>> serverIter = groupedByServer.entrySet().iterator(); serverIter.hasNext(); ) {
Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry = serverIter.next();
Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
// for each account
for (Iterator<Map.Entry<String, List<Integer>>> acctIter = accountFolders.entrySet().iterator(); acctIter.hasNext(); ) {
Map.Entry<String, List<Integer>> acctEntry = acctIter.next();
String acctId = acctEntry.getKey();
List<Integer> folderIds = acctEntry.getValue();
// Setup ItemIdFormatter appropriate for this folder which might not be in the authed account
// but also take note of presense of <noqualify/> in SOAP context
ItemIdFormatter ifmt = new ItemIdFormatter(authAcct.getId(), acctId, zsc.wantsUnqualifiedIds());
// for each folder
for (Iterator<Integer> iterFolderId = folderIds.iterator(); iterFolderId.hasNext(); ) {
int folderId = iterFolderId.next();
try {
CalendarDataResult result = calCache.getCalendarSummary(octxt, acctId, folderId, type, rangeStart, rangeEnd, true);
if (result != null) {
// Found data in cache.
iterFolderId.remove();
addCalendarDataToResponse(parent, zsc, ifmt, result.data, result.allowPrivateAccess);
}
} catch (ServiceException e) {
String ecode = e.getCode();
if (ecode.equals(ServiceException.PERM_DENIED)) {
// share permission was revoked
ZimbraLog.calendar.warn("Ignoring permission error during calendar search of folder %s", ifmt.formatItemId(folderId), e);
} else if (ecode.equals(MailServiceException.NO_SUCH_FOLDER)) {
// shared calendar folder was deleted by the owner
ZimbraLog.calendar.warn("Ignoring deleted calendar folder %s", ifmt.formatItemId(folderId));
} else {
throw e;
}
iterFolderId.remove();
}
}
if (folderIds.isEmpty())
acctIter.remove();
}
if (accountFolders.isEmpty())
serverIter.remove();
}
}
// For any remaining calendars, we have to get the data the hard way.
for (Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry : groupedByServer.entrySet()) {
Server server = serverMapEntry.getKey();
Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
if (server.equals(localServer)) {
// local server
for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
String acctId = entry.getKey();
List<Integer> folderIds = entry.getValue();
if (folderIds.isEmpty())
continue;
Account targetAcct = prov.get(AccountBy.id, acctId);
if (targetAcct == null) {
ZimbraLog.calendar.warn("Skipping unknown account " + acctId + " during calendar search");
continue;
}
Mailbox targetMbox = mboxMgr.getMailboxByAccount(targetAcct);
searchLocalAccountCalendars(parent, params, octxt, zsc, authAcct, targetMbox, folderIds, type);
}
} else {
// remote server
searchRemoteAccountCalendars(parent, params, zsc, authAcct, accountFolders);
}
}
}
use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class SendMsg method handle.
public Element handle(Element request, Map<String, Object> context, QName respQname) throws ServiceException {
try {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Mailbox mbox = getRequestedMailbox(zsc);
AccountUtil.checkQuotaWhenSendMail(mbox);
OperationContext octxt = getOperationContext(zsc, context);
ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
// <m>
Element msgElem = request.getElement(MailConstants.E_MSG);
// check to see whether the entire message has been uploaded under separate cover
String attachId = msgElem.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
boolean needCalendarSentByFixup = request.getAttributeBool(MailConstants.A_NEED_CALENDAR_SENTBY_FIXUP, false);
boolean isCalendarForward = request.getAttributeBool(MailConstants.A_IS_CALENDAR_FORWARD, false);
boolean noSaveToSent = request.getAttributeBool(MailConstants.A_NO_SAVE_TO_SENT, false);
boolean fetchSavedMsg = request.getAttributeBool(MailConstants.A_FETCH_SAVED_MSG, false);
String origId = msgElem.getAttribute(MailConstants.A_ORIG_ID, null);
ItemId iidOrigId = origId == null ? null : new ItemId(origId, zsc);
String replyType = msgElem.getAttribute(MailConstants.A_REPLY_TYPE, MailSender.MSGTYPE_REPLY);
String identityId = msgElem.getAttribute(MailConstants.A_IDENTITY_ID, null);
String dataSourceId = msgElem.getAttribute(MailConstants.A_DATASOURCE_ID, null);
String draftId = msgElem.getAttribute(MailConstants.A_DRAFT_ID, null);
ItemId iidDraft = draftId == null ? null : new ItemId(draftId, zsc);
boolean sendFromDraft = msgElem.getAttributeBool(MailConstants.A_SEND_FROM_DRAFT, false);
SendState state = SendState.NEW;
ItemId savedMsgId = null;
Pair<String, ItemId> sendRecord = null;
// get the "send uid" and check that this isn't a retry of a pending send
String sendUid = request.getAttribute(MailConstants.A_SEND_UID, null);
if (sendUid != null) {
long delay = MAX_IN_FLIGHT_DELAY_MSECS;
do {
if (state == SendState.PENDING) {
try {
delay -= RETRY_CHECK_PERIOD_MSECS;
Thread.sleep(RETRY_CHECK_PERIOD_MSECS);
} catch (InterruptedException ie) {
}
}
Pair<SendState, Pair<String, ItemId>> result = findPendingSend(mbox.getId(), sendUid);
state = result.getFirst();
sendRecord = result.getSecond();
} while (state == SendState.PENDING && delay >= 0);
}
if (state == SendState.SENT) {
// message successfully sent by another thread
savedMsgId = sendRecord.getSecond();
} else if (state == SendState.PENDING) {
// tired of waiting for another thread to complete the send
throw MailServiceException.TRY_AGAIN("message send already in progress: " + sendUid);
} else if (state == SendState.NEW) {
MimeMessageData mimeData = new MimeMessageData();
try {
// holds return data about the MimeMessage
MimeMessage mm;
if (attachId != null) {
mm = parseUploadedMessage(zsc, attachId, mimeData, needCalendarSentByFixup);
} else if (iidDraft != null && sendFromDraft) {
Message msg = mbox.getMessageById(octxt, iidDraft.getId());
mm = msg.getMimeMessage(false);
} else {
mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, null, mimeData);
}
savedMsgId = doSendMessage(octxt, mbox, mm, mimeData.uploads, iidOrigId, replyType, identityId, dataSourceId, noSaveToSent, needCalendarSentByFixup, isCalendarForward);
// a null ItemId makes the send appear to still be PENDING)
if (savedMsgId == null) {
savedMsgId = NO_MESSAGE_SAVED_TO_SENT;
}
// and record it in the table in case the client retries the send
if (sendRecord != null) {
sendRecord.setSecond(savedMsgId);
}
} catch (ServiceException e) {
clearPendingSend(mbox.getId(), sendRecord);
throw e;
} catch (RuntimeException re) {
clearPendingSend(mbox.getId(), sendRecord);
throw re;
} finally {
// purge the messages fetched from other servers.
if (mimeData.fetches != null) {
FileUploadServlet.deleteUploads(mimeData.fetches);
}
}
}
if (iidDraft != null) {
deleteDraft(iidDraft, octxt, mbox, zsc);
}
Element response = zsc.createElement(respQname);
if (savedMsgId != null && savedMsgId != NO_MESSAGE_SAVED_TO_SENT && savedMsgId.getId() > 0) {
if (fetchSavedMsg) {
Message msg = GetMsg.getMsg(octxt, mbox, savedMsgId, false);
ToXML.encodeMessageAsMP(response, ifmt, octxt, msg, null, 0, true, true, null, false, false, LC.mime_encode_missing_blob.booleanValue(), MsgContent.both);
} else {
Element respElement = response.addElement(MailConstants.E_MSG);
respElement.addAttribute(MailConstants.A_ID, ifmt.formatItemId(savedMsgId));
}
} else {
response.addElement(MailConstants.E_MSG);
}
return response;
} finally {
processor = null;
}
}
use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class SendMsg method doSendMessage.
public static ItemId doSendMessage(OperationContext oc, Mailbox mbox, MimeMessage mm, List<Upload> uploads, ItemId origMsgId, String replyType, String identityId, String dataSourceId, boolean noSaveToSent, boolean needCalendarSentByFixup, boolean isCalendarForward) throws ServiceException {
boolean isCalendarMessage = false;
ItemId id;
OutlookICalendarFixupMimeVisitor mv = null;
if (needCalendarSentByFixup || isCalendarForward) {
mv = new OutlookICalendarFixupMimeVisitor(mbox.getAccount(), mbox).needFixup(needCalendarSentByFixup).setIsCalendarForward(isCalendarForward);
try {
mv.accept(mm);
} catch (MessagingException e) {
throw ServiceException.PARSE_ERROR("Error while fixing up SendMsg for SENT-BY", e);
}
isCalendarMessage = mv.isCalendarMessage();
}
MailSender sender;
if (dataSourceId == null) {
sender = isCalendarMessage ? CalendarMailSender.getCalendarMailSender(mbox) : mbox.getMailSender();
if (noSaveToSent) {
id = sender.sendMimeMessage(oc, mbox, false, mm, uploads, origMsgId, replyType, null, false, processor);
} else {
id = sender.sendMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType, identityId, false, processor);
}
} else {
com.zimbra.cs.account.DataSource dataSource = mbox.getAccount().getDataSourceById(dataSourceId);
if (dataSource == null) {
throw ServiceException.INVALID_REQUEST("No data source with id " + dataSourceId, null);
}
if (!dataSource.isSmtpEnabled()) {
throw ServiceException.INVALID_REQUEST("Data source SMTP is not enabled", null);
}
sender = mbox.getDataSourceMailSender(dataSource, isCalendarMessage);
id = sender.sendDataSourceMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType);
}
// Send Calendar Forward Invitation notification if applicable
try {
if (isCalendarMessage && isCalendarForward && mv.getOriginalInvite() != null) {
ZOrganizer org = mv.getOriginalInvite().getOrganizer();
if (org != null) {
String orgAddress = org.getAddress();
Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
if (orgAccount != null && !orgAccount.getId().equals(mbox.getAccount().getId())) {
MimeMessage notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(mbox.getAccount(), orgAccount, orgAddress, mm.getAllRecipients(), mv.getOriginalInvite());
CalendarMailSender.sendPartial(oc, mbox, notifyMimeMsg, null, null, null, null, false, true);
}
}
}
} catch (Exception e) {
ZimbraLog.soap.warn("Ignoring error while sending Calendar Invitation Forward Notification", e);
}
return id;
}
use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class ModifySearchFolder 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_SEARCH);
ItemId iid = new ItemId(t.getAttribute(MailConstants.A_ID), zsc);
String query = t.getAttribute(MailConstants.A_QUERY);
String types = t.getAttribute(MailConstants.A_SEARCH_TYPES, null);
String sort = t.getAttribute(MailConstants.A_SORTBY, null);
mbox.modifySearchFolder(octxt, iid.getId(), query, types, sort);
SearchFolder search = mbox.getSearchFolderById(octxt, iid.getId());
Element response = zsc.createElement(MailConstants.MODIFY_SEARCH_FOLDER_RESPONSE);
ToXML.encodeSearchFolder(response, ifmt, search);
return response;
}
use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.
the class PurgeRevision method handle.
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Element revisionElem = request.getElement(MailConstants.E_REVISION);
ItemId iid = new ItemId(revisionElem.getAttribute(MailConstants.A_ID), zsc);
int rev = (int) revisionElem.getAttributeLong(MailConstants.A_VERSION);
boolean includeOlderRevisions = revisionElem.getAttributeBool(MailConstants.A_INCLUDE_OLDER_REVISIONS, false);
Mailbox mbox = getRequestedMailbox(zsc);
mbox.purgeRevision(getOperationContext(zsc, context), iid.getId(), rev, includeOlderRevisions);
return zsc.createElement(MailConstants.PURGE_REVISION_RESPONSE);
}
Aggregations