use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class NativeFormatter method saveDocument.
private void saveDocument(Blob blob, UserServletContext context, String contentType, Folder folder, String filename, InputStream is) throws IOException, ServiceException, UserServletException {
Mailbox mbox = folder.getMailbox();
MailItem item = null;
String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
ParsedDocument pd = null;
try {
if (contentType == null) {
contentType = MimeDetect.getMimeDetect().detect(filename);
if (contentType == null)
contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
}
pd = new ParsedDocument(blob, filename, contentType, System.currentTimeMillis(), creator, context.req.getHeader("X-Zimbra-Description"), true);
item = mbox.getItemByPath(context.opContext, filename, folder.getId());
// XXX: should we just overwrite here instead?
if (!(item instanceof Document))
throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "cannot overwrite existing object at that path");
// scan upload for viruses
StringBuffer info = new StringBuffer();
UploadScanner.Result result = UploadScanner.acceptStream(is, info);
if (result == UploadScanner.REJECT)
throw MailServiceException.UPLOAD_REJECTED(filename, info.toString());
if (result == UploadScanner.ERROR)
throw MailServiceException.SCAN_ERROR(filename);
item = mbox.addDocumentRevision(context.opContext, item.getId(), pd);
} catch (NoSuchItemException nsie) {
item = mbox.createDocument(context.opContext, folder.getId(), pd, MailItem.Type.DOCUMENT, 0);
}
sendZimbraHeaders(context, context.resp, item);
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException 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;
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class ToXML method encodeMessageAsMP.
/** Encodes a Message object into <m> element with <mp> elements for
* message body.
* @param parent The Element to add the new <tt><m></tt> to.
* @param ifmt The formatter to sue when serializing item ids.
* @param msg The Message to serialize.
* @param part If non-null, serialize this message/rfc822 subpart of
* the Message instead of the Message itself.
* @param maxSize TODO
* @param wantHTML <tt>true</tt> to prefer HTML parts as the "body",
* <tt>false</tt> to prefer text/plain parts.
* @param neuter Whether to rename "src" attributes on HTML <img> tags.
* @param headers Extra message headers to include in the returned element.
* @param serializeType If <tt>false</tt>, always serializes as an
* <tt><m></tt> element.
* @return The newly-created <tt><m></tt> Element, which has already
* been added as a child to the passed-in <tt>parent</tt>.
* @throws ServiceException */
public static Element encodeMessageAsMP(Element parent, ItemIdFormatter ifmt, OperationContext octxt, Message msg, String part, int maxSize, boolean wantHTML, boolean neuter, Set<String> headers, boolean serializeType, boolean wantExpandGroupInfo, boolean encodeMissingBlobs, MsgContent wantContent) throws ServiceException {
Mailbox mbox = msg.getMailbox();
int changeId = msg.getSavedSequence();
while (true) {
try {
return encodeMessageAsMP(parent, ifmt, octxt, msg, part, maxSize, wantHTML, neuter, headers, serializeType, wantExpandGroupInfo, false, encodeMissingBlobs, wantContent);
} catch (ServiceException e) {
// (this case generally means that the blob backing the MimeMessage disappeared halfway through)
try {
msg = mbox.getMessageById(octxt, msg.getId());
if (msg.getSavedSequence() != changeId) {
// if a draft was re-saved and we failed because the old blob was deleted
// out from under us, just fetch the new MimeMessage and try again
changeId = msg.getSavedSequence();
ZimbraLog.soap.info("caught message content change while serializing; will retry");
continue;
}
} catch (NoSuchItemException nsie) {
// the message has been deleted, so don't include draft data in the response
throw nsie;
}
// we're kinda screwed here -- we weren't able to write the message structure and it's not clear what went wrong.
// best we can do now is send back what we got and apologize.
ZimbraLog.soap.warn("could not serialize full message structure in response", e);
return encodeMessageAsMP(parent, ifmt, octxt, msg, part, maxSize, wantHTML, neuter, headers, serializeType, wantExpandGroupInfo, true, wantContent);
}
}
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class ToXML method encodeCalendarItemSummary.
/**
* Encode the metadata for a calendar item.
*
* The content for the calendar item is a big multipart/digest containing each
* invite in the calendar item as a sub-mimepart -- it can be retreived from the content
* servlet:
* http://servername/service/content/get?id=<calItemId>
*
* The client can ALSO request just the content for each individual invite using a
* compound item-id request:
* http://servername/service/content/get?id=<calItemId>-<invite-mail-item-id>
*
* DO NOT use the raw invite-mail-item-id to fetch the content: since the invite is a
* standard mail-message it can be deleted by the user at any time!
*/
public static Element encodeCalendarItemSummary(Element parent, ItemIdFormatter ifmt, OperationContext octxt, CalendarItem calItem, int fields, boolean includeInvites, boolean includeContent) throws ServiceException {
final int MAX_RETRIES = LC.calendar_item_get_max_retries.intValue();
Element elem = null;
Mailbox mbox = calItem.getMailbox();
int changeId = calItem.getSavedSequence();
int numTries = 0;
while (numTries < MAX_RETRIES) {
numTries++;
if (calItem instanceof Appointment) {
elem = parent.addElement(MailConstants.E_APPOINTMENT);
} else {
elem = parent.addElement(MailConstants.E_TASK);
}
try {
setCalendarItemFields(elem, ifmt, octxt, calItem, fields, includeInvites, includeContent, true);
if (needToOutput(fields, Change.METADATA)) {
encodeAllCustomMetadata(elem, calItem, fields);
}
return elem;
} catch (ServiceException e) {
// Problem writing the structure to the response
// (this case generally means that the blob backing the Calendar item disappeared halfway through)
elem.detach();
elem = null;
try {
calItem = mbox.getCalendarItemById(octxt, calItem.getId());
if (calItem.getSavedSequence() != changeId) {
// just fetch the new item and try again
changeId = calItem.getSavedSequence();
ZimbraLog.soap.info("caught calendar item content change while serializing; will retry");
continue;
}
} catch (NoSuchItemException nsie) {
// the message has been deleted, so don't include data in the response
throw nsie;
}
// Were unable to write calendar item structure & not clear what went wrong. Try again a few times
// in case it was related to underlying modifications
ZimbraLog.soap.warn("Could not serialize full calendar item structure in response", e);
}
}
// Still failing after several retries...
throw NoSuchItemException.NO_SUCH_CALITEM("Problem encoding calendar item. Maybe corrupt");
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class DismissCalendarItemAlarm 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 dismissedAt = calItemElem.getAttributeLong(MailConstants.A_CAL_ALARM_DISMISSED_AT);
// trace logging
ZimbraLog.calendar.info("<DismissCalendarItemAlarm> id=%s, at=%d", iid.toString(), dismissedAt);
// 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 dismiss alarms of a shared calendar");
}
int calItemId = iid.getId();
ItemIdFormatter ifmt = new ItemIdFormatter(authAcct.getId(), acctId, false);
try {
ciMbox.dismissCalendarItemAlarm(octxt, calItemId, dismissedAt);
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 dismissed while a big sync is happening which deletes the item (e.g. bug 48560)
//since item is not in db, it has effectively been dismissed; return ok and no further alarms
Element calItemRespElem = response.addElement(calItemElement);
calItemRespElem.addAttribute(MailConstants.A_CAL_ID, iid.toString(ifmt));
}
}
}
return response;
}
Aggregations