Search in sources :

Example 21 with MailServiceException

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);
    }
}
Also used : HashMap(java.util.HashMap) SearchFolder(com.zimbra.cs.mailbox.SearchFolder) Folder(com.zimbra.cs.mailbox.Folder) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Charset(java.nio.charset.Charset) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ExportPeriodNotSpecifiedException(com.zimbra.cs.mailbox.MailServiceException.ExportPeriodNotSpecifiedException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ExportPeriodTooLongException(com.zimbra.cs.mailbox.MailServiceException.ExportPeriodTooLongException) UserServletException(com.zimbra.cs.service.UserServletException) LinkedList(java.util.LinkedList) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) ItemData(com.zimbra.cs.service.util.ItemData)

Example 22 with MailServiceException

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);
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Server(com.zimbra.cs.account.Server) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

MailServiceException (com.zimbra.cs.mailbox.MailServiceException)22 Mailbox (com.zimbra.cs.mailbox.Mailbox)18 OperationContext (com.zimbra.cs.mailbox.OperationContext)6 Folder (com.zimbra.cs.mailbox.Folder)4 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)4 ItemId (com.zimbra.cs.service.util.ItemId)4 ServiceException (com.zimbra.common.service.ServiceException)3 MailItem (com.zimbra.cs.mailbox.MailItem)3 ExportPeriodNotSpecifiedException (com.zimbra.cs.mailbox.MailServiceException.ExportPeriodNotSpecifiedException)3 ExportPeriodTooLongException (com.zimbra.cs.mailbox.MailServiceException.ExportPeriodTooLongException)3 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)3 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)3 UserServletException (com.zimbra.cs.service.UserServletException)3 IOException (java.io.IOException)3 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)2 Contact (com.zimbra.cs.mailbox.Contact)2 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)2 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)2 MailSender (com.zimbra.cs.mailbox.MailSender)2 SetCalendarItemData (com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData)2