Search in sources :

Example 1 with Argument

use of com.zimbra.common.service.ServiceException.Argument in project zm-mailbox by Zimbra.

the class MailUtil method populateFailureDeliveryMessageFields.

public static void populateFailureDeliveryMessageFields(MimeMessage failedDeliverymm, String subject, String to, List<Argument> addressArgs, InternetAddress iAddr) throws MessagingException, UnsupportedEncodingException {
    failedDeliverymm.setSubject("Send Partial Failure Notice");
    failedDeliverymm.setSentDate(new Date());
    failedDeliverymm.setFrom(iAddr);
    failedDeliverymm.setRecipient(RecipientType.TO, new JavaMailInternetAddress(to));
    StringBuilder text = new StringBuilder();
    String sentDate = new SimpleDateFormat("MMMMM d, yyyy").format(new Date());
    String sentTime = new SimpleDateFormat("h:mm a").format(new Date());
    text.append("Your message \"" + subject + "\" sent on " + sentDate + " at " + sentTime + " " + TimeZone.getDefault().getDisplayName() + " " + "could not be delivered to one or more recipients.\n\n" + "For further assistance, please send mail to postmaster.\n\n");
    List<String> invalidAddrs = new ArrayList<String>();
    List<String> unsentAddrs = new ArrayList<String>();
    if (addressArgs != null) {
        for (Argument arg : addressArgs) {
            if (arg.name != null && arg.value != null && arg.value.length() > 0) {
                if (arg.name.equals("invalid"))
                    invalidAddrs.add(arg.value);
                else if (arg.name.equals("unsent"))
                    unsentAddrs.add(arg.value);
            }
        }
    }
    for (String addr : invalidAddrs) text.append(addr + ":" + "Invalid Address\n");
    for (String addr : unsentAddrs) text.append(addr + ":" + "Unsent Address\n");
    failedDeliverymm.setText(text.toString());
    failedDeliverymm.saveChanges();
}
Also used : Argument(com.zimbra.common.service.ServiceException.Argument) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with Argument

use of com.zimbra.common.service.ServiceException.Argument in project zm-mailbox by Zimbra.

the class MailItemResource method moveORcopyWithOverwrite.

public void moveORcopyWithOverwrite(DavContext ctxt, Collection dest, String newName, boolean deleteOriginal) throws DavException {
    try {
        if (deleteOriginal)
            move(ctxt, dest, newName);
        else
            copy(ctxt, dest, newName);
    } catch (DavException e) {
        if (e.getStatus() == HttpServletResponse.SC_PRECONDITION_FAILED) {
            // in case of name conflict, delete the existing mail item and
            // attempt the move operation again.
            // return if the error is not ALREADY_EXISTS
            ServiceException se = (ServiceException) e.getCause();
            int id = 0;
            try {
                if (se.getCode().equals(MailServiceException.ALREADY_EXISTS) == false)
                    throw e;
                else {
                    // get the conflicting item-id
                    if (se instanceof SoapFaultException) {
                        // destination belongs other mailbox.
                        String itemIdStr = ((SoapFaultException) se).getArgumentValue("id");
                        ItemId itemId = new ItemId(itemIdStr, dest.getItemId().getAccountId());
                        id = itemId.getId();
                    } else {
                        // destination belongs to same mailbox.
                        String name = null;
                        for (Argument arg : se.getArgs()) {
                            if (arg.name != null && arg.value != null && arg.value.length() > 0) {
                                if (arg.name.equals("name"))
                                    name = arg.value;
                            /* commented out since the exception is giving wrong itemId for copy.
                                       If the the item is conflicting with an existing item we want the
                                       id of the existing item. But, the exception has the proposed id of
                                       the new item which does not exist yet.
                                     else if (arg.mName.equals("itemId"))
                                        id = Integer.parseInt(arg.mValue);
                                     */
                            }
                        }
                        if (id <= 0) {
                            if (name == null && !deleteOriginal) {
                                // in case of copy get the id from source name since we don't support copy with rename.
                                name = ctxt.getItem();
                            }
                            if (name != null) {
                                Mailbox mbox = getMailbox(ctxt);
                                MailItem item = mbox.getItemByPath(ctxt.getOperationContext(), name, dest.getId());
                                id = item.getId();
                            } else
                                throw e;
                        }
                    }
                }
                deleteDestinationItem(ctxt, dest, id);
            } catch (ServiceException se1) {
                throw new DavException("cannot move/copy item", HttpServletResponse.SC_FORBIDDEN, se1);
            }
            if (deleteOriginal)
                move(ctxt, dest, newName);
            else
                copy(ctxt, dest, newName);
        } else {
            throw e;
        }
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Argument(com.zimbra.common.service.ServiceException.Argument) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) DavException(com.zimbra.cs.dav.DavException) ItemId(com.zimbra.cs.service.util.ItemId) SoapFaultException(com.zimbra.common.soap.SoapFaultException)

Example 3 with Argument

use of com.zimbra.common.service.ServiceException.Argument in project zm-mailbox by Zimbra.

the class SaveDocument method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    OperationContext octxt = getOperationContext(zsc, context);
    Element docElem = request.getElement(MailConstants.E_DOC);
    Doc doc = null;
    Element response = null;
    boolean success = false;
    Mailbox mbox = null;
    int folderId = 0;
    try {
        String explicitName = docElem.getAttribute(MailConstants.A_NAME, null);
        String explicitCtype = docElem.getAttribute(MailConstants.A_CONTENT_TYPE, null);
        // bug 37180, extract the filename from the path (for IE). IE sends the full path.
        if (explicitName != null) {
            try {
                explicitName = explicitName.replaceAll("\\\\", "/");
                explicitName = explicitName.substring(explicitName.lastIndexOf("/") + 1);
            } catch (Exception e) {
            // Do nothing
            }
        }
        String description = docElem.getAttribute(MailConstants.A_DESC, null);
        ItemId fid = new ItemId(docElem.getAttribute(MailConstants.A_FOLDER, DEFAULT_DOCUMENT_FOLDER), zsc);
        folderId = fid.getId();
        String id = docElem.getAttribute(MailConstants.A_ID, null);
        int itemId = id == null ? 0 : new ItemId(id, zsc).getId();
        int ver = (int) docElem.getAttributeLong(MailConstants.A_VERSION, 0);
        mbox = getRequestedMailbox(zsc);
        Element attElem = docElem.getOptionalElement(MailConstants.E_UPLOAD);
        Element msgElem = docElem.getOptionalElement(MailConstants.E_MSG);
        Element docRevElem = docElem.getOptionalElement(MailConstants.E_DOC);
        if (attElem != null) {
            String aid = attElem.getAttribute(MailConstants.A_ID, null);
            doc = getUploadedDoc(aid, zsc, explicitName, explicitCtype, description);
        } else if (msgElem != null) {
            String part = msgElem.getAttribute(MailConstants.A_PART);
            ItemId iid = new ItemId(msgElem.getAttribute(MailConstants.A_ID), zsc);
            doc = fetchMimePart(octxt, zsc.getAuthToken(), iid, part, explicitName, explicitCtype, description);
        } else if (docRevElem != null) {
            ItemId iid = new ItemId(docRevElem.getAttribute(MailConstants.A_ID), zsc);
            int revSource = (int) docRevElem.getAttributeLong(MailConstants.A_VERSION, 0);
            Account sourceAccount = Provisioning.getInstance().getAccountById(iid.getAccountId());
            if (sourceAccount.getId().equals(zsc.getRequestedAccountId())) {
                Document docRev;
                if (revSource == 0) {
                    docRev = mbox.getDocumentById(octxt, iid.getId());
                } else {
                    docRev = (Document) mbox.getItemRevision(octxt, iid.getId(), MailItem.Type.DOCUMENT, revSource);
                }
                doc = new Doc(docRev);
            } else {
                doc = new Doc(zsc.getAuthToken(), sourceAccount, iid.getId(), revSource);
            }
            // the content from another document
            if (ver != 0) {
                doc.name = null;
            }
        } else {
            String inlineContent = docElem.getAttribute(MailConstants.E_CONTENT);
            doc = new Doc(inlineContent, explicitName, explicitCtype, description);
        }
        // set content-type based on file extension.
        setDocContentType(doc);
        Document docItem = null;
        InputStream is = null;
        try {
            is = doc.getInputStream();
        } catch (IOException e) {
            throw ServiceException.FAILURE("can't save document", e);
        }
        if (itemId == 0) {
            // create a new page
            docItem = createDocument(doc, zsc, octxt, mbox, docElem, is, folderId, MailItem.Type.DOCUMENT);
        } else {
            // add a new revision
            docItem = mbox.getDocumentById(octxt, itemId);
            if (docItem.getVersion() != ver) {
                throw MailServiceException.MODIFY_CONFLICT(new Argument(MailConstants.A_NAME, doc.name, Argument.Type.STR), new Argument(MailConstants.A_ID, itemId, Argument.Type.IID), new Argument(MailConstants.A_VERSION, docItem.getVersion(), Argument.Type.NUM));
            }
            String name = docItem.getName();
            if (doc.name != null) {
                name = doc.name;
            }
            boolean descEnabled = docElem.getAttributeBool(MailConstants.A_DESC_ENABLED, docItem.isDescriptionEnabled());
            docItem = mbox.addDocumentRevision(octxt, itemId, getAuthor(zsc), name, doc.description, descEnabled, is);
        }
        response = zsc.createElement(MailConstants.SAVE_DOCUMENT_RESPONSE);
        Element m = response.addElement(MailConstants.E_DOC);
        m.addAttribute(MailConstants.A_ID, new ItemIdFormatter(zsc).formatItemId(docItem));
        m.addAttribute(MailConstants.A_VERSION, docItem.getVersion());
        m.addAttribute(MailConstants.A_NAME, docItem.getName());
        success = true;
    } catch (ServiceException e) {
        if (e.getCode().equals(MailServiceException.ALREADY_EXISTS)) {
            MailItem item = null;
            if (mbox != null && folderId != 0) {
                item = mbox.getItemByPath(octxt, doc.name, folderId);
            }
            if (item != null && item instanceof Document) {
                // name clash with another Document
                throw MailServiceException.ALREADY_EXISTS("name " + doc.name + " in folder " + folderId, doc.name, item.getId(), ((Document) item).getVersion());
            } else if (item != null) {
                // name clash with a folder
                throw MailServiceException.ALREADY_EXISTS("name " + doc.name + " in folder " + folderId, doc.name, item.getId());
            }
        }
        throw e;
    } finally {
        if (success && doc != null) {
            doc.cleanup();
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) InternalArgument(com.zimbra.common.service.ServiceException.InternalArgument) Argument(com.zimbra.common.service.ServiceException.Argument) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(com.zimbra.common.soap.Element) IOException(java.io.IOException) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Document(com.zimbra.cs.mailbox.Document) ItemId(com.zimbra.cs.service.util.ItemId) MessagingException(javax.mail.MessagingException) HttpException(org.apache.http.HttpException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 4 with Argument

use of com.zimbra.common.service.ServiceException.Argument in project zm-mailbox by Zimbra.

the class TestMailSender method validateException.

private void validateException(MailServiceException e, String expectedCode, String invalidRecipient, String errorSubstring) {
    Assert.assertEquals(expectedCode, e.getCode());
    if (errorSubstring != null) {
        Assert.assertTrue("Error did not contain '" + errorSubstring + "': " + e.getMessage(), e.getMessage().contains(errorSubstring));
    }
    boolean foundRecipient = false;
    for (Argument arg : e.getArgs()) {
        if (arg.name.equals("invalid")) {
            Assert.assertEquals(invalidRecipient, arg.value);
            foundRecipient = true;
        }
    }
    Assert.assertTrue(foundRecipient);
}
Also used : Argument(com.zimbra.common.service.ServiceException.Argument)

Example 5 with Argument

use of com.zimbra.common.service.ServiceException.Argument in project zm-mailbox by Zimbra.

the class SoapFaultExceptionTest method soapFaultArgs.

/**
 * Confirms that SOAP fault arguments are marshalled to and from elements.
 */
@Test
public void soapFaultArgs() throws Exception {
    // Create original exception.
    String msg = "Something bad happened.";
    Argument howBad = new Argument("howBad", "Really bad", Argument.Type.STR);
    Argument howManyTimes = new Argument("howManyTimes", "2", Argument.Type.NUM);
    String accountId = UUID.randomUUID().toString();
    String itemId = accountId + ":123";
    Argument itemIdArg = new Argument("itemId", itemId, Argument.Type.IID);
    Argument accountIdArg = new Argument("accountId", accountId, Argument.Type.ACCTID);
    String code = "mail:SOMETHING_BAD";
    List<Argument> args = Lists.newArrayList(howBad, howManyTimes, itemIdArg, accountIdArg);
    TestException te = new TestException(msg, code, args);
    // Marshal and unmarshal to a SOAP 1.2 fault.
    Element fault = SoapProtocol.Soap12.soapFault(te);
    Element detail = fault.getElement(Soap12Protocol.DETAIL);
    SoapFaultException sfe = new SoapFaultException(msg, detail, false, fault);
    // Compare.
    Assert.assertEquals(msg, sfe.getMessage());
    Assert.assertEquals(code, sfe.getCode());
    Assert.assertEquals(args, sfe.getArgs());
    // Marshal and unmarshal to a SOAP 1.1 fault.
    fault = SoapProtocol.Soap11.soapFault(te);
    detail = fault.getElement(Soap11Protocol.DETAIL);
    sfe = new SoapFaultException(msg, detail, false, fault);
    // Compare.
    Assert.assertEquals(msg, sfe.getMessage());
    Assert.assertEquals(code, sfe.getCode());
    Assert.assertEquals(args, sfe.getArgs());
    // Marshal and unmarshal to a SOAP JS fault.
    fault = SoapProtocol.SoapJS.soapFault(te);
    detail = fault.getElement(SoapJSProtocol.DETAIL);
    sfe = new SoapFaultException(msg, detail, false, fault);
    // Compare.
    Assert.assertEquals(msg, sfe.getMessage());
    Assert.assertEquals(code, sfe.getCode());
    Assert.assertEquals(args, sfe.getArgs());
}
Also used : Argument(com.zimbra.common.service.ServiceException.Argument) Test(org.junit.Test)

Aggregations

Argument (com.zimbra.common.service.ServiceException.Argument)7 ServiceException (com.zimbra.common.service.ServiceException)2 MailItem (com.zimbra.cs.mailbox.MailItem)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 ItemId (com.zimbra.cs.service.util.ItemId)2 Test (org.junit.Test)2 ZMailbox (com.zimbra.client.ZMailbox)1 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)1 InternalArgument (com.zimbra.common.service.ServiceException.InternalArgument)1 Element (com.zimbra.common.soap.Element)1 SoapFaultException (com.zimbra.common.soap.SoapFaultException)1 Account (com.zimbra.cs.account.Account)1 DavException (com.zimbra.cs.dav.DavException)1 Document (com.zimbra.cs.mailbox.Document)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)1 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1