use of com.zimbra.cs.mailbox.MailServiceException in project zm-mailbox by Zimbra.
the class ArchiveFormatter method saveCallback.
@Override
public void saveCallback(UserServletContext context, String contentType, Folder fldr, String file) throws IOException, ServiceException {
// Disable the jetty timeout
disableJettyTimeout(context);
Exception ex = null;
ItemData id = null;
FolderDigestInfo digestInfo = new FolderDigestInfo(context.opContext);
List<ServiceException> errs = new LinkedList<ServiceException>();
List<Folder> flist;
Map<Object, Folder> fmap = new HashMap<Object, Folder>();
Map<Integer, Integer> idMap = new HashMap<Integer, Integer>();
long last = System.currentTimeMillis();
String types = context.getTypesString();
String resolve = context.params.get(PARAM_RESOLVE);
String subfolder = context.params.get("subfolder");
String timestamp = context.params.get("timestamp");
String timeout = context.params.get("timeout");
try {
ArchiveInputStream ais;
int[] ids = null;
long interval = 45 * 1000;
Resolve r = resolve == null ? Resolve.Skip : Resolve.valueOf(resolve.substring(0, 1).toUpperCase() + resolve.substring(1).toLowerCase());
if (timeout != null) {
interval = Long.parseLong(timeout);
}
Set<MailItem.Type> searchTypes = null;
if (context.reqListIds != null) {
ids = context.reqListIds.clone();
Arrays.sort(ids);
}
if (!Strings.isNullOrEmpty(types)) {
try {
searchTypes = MailItem.Type.setOf(types);
} catch (IllegalArgumentException e) {
throw MailServiceException.INVALID_TYPE(e.getMessage());
}
searchTypes.remove(MailItem.Type.CONVERSATION);
}
Charset charset = context.getCharset();
try {
ais = getInputStream(context, charset.name());
} catch (Exception e) {
String filename = context.params.get(UserServlet.UPLOAD_NAME);
throw FormatterServiceException.INVALID_FORMAT(filename == null ? "unknown" : filename);
}
if (!Strings.isNullOrEmpty(subfolder)) {
fldr = createPath(context, fmap, fldr.getPath() + subfolder, Folder.Type.UNKNOWN);
}
flist = fldr.getSubfolderHierarchy();
if (r == Resolve.Reset) {
for (Folder f : flist) {
if (context.targetMailbox.isImmutableSystemFolder(f.getId()))
continue;
try {
List<Integer> delIds;
/* TODO Uncomment when bug 76892 is fixed.
if (System.currentTimeMillis() - last > interval) {
updateClient(context, true);
last = System.currentTimeMillis();
}
*/
if (searchTypes == null) {
delIds = context.targetMailbox.listItemIds(context.opContext, MailItem.Type.UNKNOWN, f.getId());
} else {
delIds = context.targetMailbox.getItemIds(context.opContext, f.getId()).getIds(searchTypes);
}
if (delIds == null)
continue;
int[] delIdsArray = new int[delIds.size()];
int i = 0;
for (Integer del : delIds) {
if (del >= Mailbox.FIRST_USER_ID) {
delIdsArray[i++] = del;
}
}
while (i < delIds.size()) {
delIdsArray[i++] = Mailbox.ID_AUTO_INCREMENT;
}
context.targetMailbox.delete(context.opContext, delIdsArray, MailItem.Type.UNKNOWN, null);
} catch (MailServiceException e) {
if (e.getCode() != MailServiceException.NO_SUCH_FOLDER) {
r = Resolve.Replace;
addError(errs, e);
}
} catch (Exception e) {
r = Resolve.Replace;
addError(errs, FormatterServiceException.UNKNOWN_ERROR(f.getName(), e));
}
}
context.targetMailbox.purge(MailItem.Type.UNKNOWN);
flist = fldr.getSubfolderHierarchy();
}
for (Folder f : flist) {
fmap.put(f.getId(), f);
fmap.put(f.getPath(), f);
}
try {
ArchiveInputEntry aie;
Boolean meta = false;
while ((aie = ais.getNextEntry()) != null) {
/* TODO Uncomment when bug 76892 is fixed.
if (System.currentTimeMillis() - last > interval) {
updateClient(context, true);
last = System.currentTimeMillis();
}
*/
if (aie.getName().startsWith("__MACOSX/")) {
continue;
} else if (aie.getName().endsWith(".meta")) {
meta = true;
if (id != null) {
addItem(context, fldr, fmap, digestInfo, idMap, ids, searchTypes, r, id, ais, null, errs);
}
try {
id = new ItemData(readArchiveEntry(ais, aie));
} catch (Exception e) {
addError(errs, FormatterServiceException.INVALID_FORMAT(aie.getName()));
}
continue;
} else if (aie.getName().endsWith(".err")) {
addError(errs, FormatterServiceException.MISMATCHED_SIZE(aie.getName()));
} else if (id == null) {
if (meta) {
addError(errs, FormatterServiceException.MISSING_META(aie.getName()));
} else {
addData(context, fldr, fmap, searchTypes, r, timestamp == null || !timestamp.equals("0"), ais, aie, errs);
}
} else if ((aie.getType() != 0 && id.ud.type != aie.getType()) || (id.ud.getBlobDigest() != null && aie.getSize() != -1 && id.ud.size != aie.getSize())) {
addError(errs, FormatterServiceException.MISMATCHED_META(aie.getName()));
} else {
addItem(context, fldr, fmap, digestInfo, idMap, ids, searchTypes, r, id, ais, aie, errs);
}
id = null;
}
if (id != null) {
addItem(context, fldr, fmap, digestInfo, idMap, ids, searchTypes, r, id, ais, null, errs);
}
} catch (Exception e) {
if (id == null) {
addError(errs, FormatterServiceException.UNKNOWN_ERROR(e));
} else {
addError(errs, FormatterServiceException.UNKNOWN_ERROR(id.path, e));
}
id = null;
} finally {
if (ais != null) {
ais.close();
}
contacts.clear();
}
} catch (Exception e) {
ex = e;
}
try {
updateClient(context, ex, errs);
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw ServiceException.FAILURE("Archive formatter failure", e);
}
}
use of com.zimbra.cs.mailbox.MailServiceException in project zm-mailbox by Zimbra.
the class TestMailSender method testRejectRecipient.
@Test
public void testRejectRecipient() throws Exception {
String errorMsg = "Sender address rejected: User unknown in relay recipient table";
String bogusAddress = TestUtil.getAddress("bogus");
startDummySmtpServer(bogusAddress, errorMsg);
Server server = Provisioning.getInstance().getLocalServer();
server.setSmtpPort(TEST_SMTP_PORT);
String content = TestUtil.getTestMessage(NAME_PREFIX + " testRejectSender", bogusAddress, SENDER_NAME, null);
MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
Mailbox mbox = TestUtil.getMailbox(SENDER_NAME);
// Test reject first recipient, get partial send value from LDAP.
boolean sendFailed = false;
server.setSmtpSendPartial(false);
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test reject first recipient, set partial send value explicitly.
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
server.setSmtpSendPartial(true);
MailSender sender = mbox.getMailSender().setSendPartial(false);
try {
sender.sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test reject second recipient, get partial send value from LDAP.
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
String validAddress = TestUtil.getAddress(RECIPIENT_NAME);
InternetAddress[] recipients = new InternetAddress[2];
recipients[0] = new JavaMailInternetAddress(validAddress);
recipients[1] = new JavaMailInternetAddress(bogusAddress);
msg.setRecipients(MimeMessage.RecipientType.TO, recipients);
server.setSmtpSendPartial(false);
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test partial send, get value from LDAP.
startDummySmtpServer(bogusAddress, errorMsg);
server.setSmtpSendPartial(true);
sendFailed = false;
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test partial send, specify value explicitly.
server.setSmtpSendPartial(false);
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
sender = mbox.getMailSender().setSendPartial(true);
try {
sender.sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
// Don't check error message. JavaMail does not give us the SMTP protocol error in the
// partial send case.
validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
}
Aggregations