Search in sources :

Example 1 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class CreateWaitSet method staticHandle.

public static Element staticHandle(DocumentHandler handler, Element request, Map<String, Object> context, Element response) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    String defInterestStr = request.getAttribute(MailConstants.A_DEFTYPES);
    Set<MailItem.Type> defaultInterests = WaitSetRequest.parseInterestStr(defInterestStr, EnumSet.noneOf(MailItem.Type.class));
    boolean adminAllowed = zsc.getAuthToken().isAdmin();
    boolean allAccts = request.getAttributeBool(MailConstants.A_ALL_ACCOUNTS, false);
    if (allAccts) {
        WaitSetMgr.checkRightForAllAccounts(zsc);
    }
    List<WaitSetAccount> add = WaitSetRequest.parseAddUpdateAccounts(zsc, request.getOptionalElement(MailConstants.E_WAITSET_ADD), defaultInterests);
    // workaround for 27480: load the mailboxes NOW, before we grab the waitset lock
    List<Mailbox> referencedMailboxes = new ArrayList<Mailbox>();
    for (WaitSetAccount acct : add) {
        try {
            MailboxManager.FetchMode fetchMode = MailboxManager.FetchMode.AUTOCREATE;
            Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(acct.getAccountId(), fetchMode);
            referencedMailboxes.add(mbox);
        } catch (ServiceException e) {
            ZimbraLog.session.debug("Caught exception preloading mailbox for waitset", e);
        }
    }
    Pair<String, List<WaitSetError>> result = WaitSetMgr.create(zsc.getRequestedAccountId(), adminAllowed, defaultInterests, allAccts, add);
    String wsId = result.getFirst();
    List<WaitSetError> errors = result.getSecond();
    response.addAttribute(MailConstants.A_WAITSET_ID, wsId);
    response.addAttribute(MailConstants.A_DEFTYPES, WaitSetRequest.interestToStr(defaultInterests));
    response.addAttribute(MailConstants.A_SEQ, 0);
    WaitSetRequest.encodeErrors(response, errors);
    return response;
}
Also used : WaitSetAccount(com.zimbra.cs.session.WaitSetAccount) ArrayList(java.util.ArrayList) WaitSetError(com.zimbra.cs.session.WaitSetError) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class WaitSetRequest method staticHandle.

public static Element staticHandle(Element request, Map<String, Object> context, Element response, boolean adminAllowed) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    HttpServletRequest servletRequest = (HttpServletRequest) context.get(SoapServlet.SERVLET_REQUEST);
    String waitSetId = request.getAttribute(MailConstants.A_WAITSET_ID);
    String lastKnownSeqNo = request.getAttribute(MailConstants.A_SEQ);
    boolean block = request.getAttributeBool(MailConstants.A_BLOCK, false);
    Callback cb = (Callback) servletRequest.getAttribute(VARS_ATTR_NAME);
    if (cb == null) {
        // Initial
        Continuation continuation = ContinuationSupport.getContinuation(servletRequest);
        cb = new Callback();
        cb.continuationResume = new ResumeContinuationListener(continuation);
        servletRequest.setAttribute(VARS_ATTR_NAME, cb);
        String defInterestStr = null;
        if (waitSetId.startsWith(WaitSetMgr.ALL_ACCOUNTS_ID_PREFIX)) {
            WaitSetMgr.checkRightForAllAccounts(zsc);
            // default interest types required for "All" waitsets
            defInterestStr = request.getAttribute(MailConstants.A_DEFTYPES);
            Set<MailItem.Type> defaultInterests = WaitSetRequest.parseInterestStr(defInterestStr, EnumSet.noneOf(MailItem.Type.class));
            cb.ws = WaitSetMgr.lookupOrCreateForAllAccts(zsc.getRequestedAccountId(), waitSetId, defaultInterests, lastKnownSeqNo);
        } else {
            cb.ws = WaitSetMgr.lookup(waitSetId);
        }
        if (cb.ws == null)
            throw AdminServiceException.NO_SUCH_WAITSET(waitSetId);
        WaitSetMgr.checkRightForOwnerAccount(cb.ws, zsc.getRequestedAccountId());
        List<WaitSetAccount> add = parseAddUpdateAccounts(zsc, request.getOptionalElement(MailConstants.E_WAITSET_ADD), cb.ws.getDefaultInterest());
        List<WaitSetAccount> update = parseAddUpdateAccounts(zsc, request.getOptionalElement(MailConstants.E_WAITSET_UPDATE), cb.ws.getDefaultInterest());
        List<String> remove = parseRemoveAccounts(zsc, request.getOptionalElement(MailConstants.E_WAITSET_REMOVE));
        ///////////////////
        // workaround for 27480: load the mailboxes NOW, before we grab the waitset lock
        List<Mailbox> referencedMailboxes = new ArrayList<Mailbox>();
        for (WaitSetAccount acct : add) {
            try {
                Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(acct.getAccountId(), MailboxManager.FetchMode.AUTOCREATE);
                referencedMailboxes.add(mbox);
            } catch (ServiceException e) {
                ZimbraLog.session.debug("Caught exception preloading mailbox for waitset", e);
            }
        }
        for (WaitSetAccount acct : update) {
            try {
                Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(acct.getAccountId(), MailboxManager.FetchMode.AUTOCREATE);
                referencedMailboxes.add(mbox);
            } catch (ServiceException e) {
                ZimbraLog.session.debug("Caught exception preloading mailbox for waitset", e);
            }
        }
        // the server in a very fast loop (they should be using the 'block' mode)
        try {
            Thread.sleep(INITIAL_SLEEP_TIME_MILLIS);
        } catch (InterruptedException ex) {
        }
        cb.errors.addAll(cb.ws.removeAccounts(remove));
        synchronized (cb.ws) {
            // bug 28190: always grab the WS lock before the CB lock.
            synchronized (cb) {
                cb.errors.addAll(cb.ws.doWait(cb, lastKnownSeqNo, add, update));
                // the ws until we release the cb lock!
                if (cb.completed)
                    block = false;
            }
        }
        if (block) {
            // bit.
            try {
                Thread.sleep(NODATA_SLEEP_TIME_MILLIS);
            } catch (InterruptedException ex) {
            }
            synchronized (cb) {
                if (!cb.completed) {
                    // don't wait if it completed right away
                    long timeout = getTimeoutMillis(request, adminAllowed);
                    if (ZimbraLog.soap.isTraceEnabled())
                        ZimbraLog.soap.trace("Suspending <WaitSetRequest> for %dms", timeout);
                    cb.continuationResume.suspendAndUndispatch(timeout);
                }
            }
        }
    }
    // if we got here, then we did *not* execute a jetty RetryContinuation,
    // soooo, we'll fall through and finish up at the bottom
    // clear the
    cb.ws.doneWaiting();
    response.addAttribute(MailConstants.A_WAITSET_ID, waitSetId);
    if (cb.canceled) {
        response.addAttribute(MailConstants.A_CANCELED, true);
    } else if (cb.completed) {
        response.addAttribute(MailConstants.A_SEQ, cb.seqNo);
        for (String s : cb.signalledAccounts) {
            Element saElt = response.addElement(MailConstants.E_A);
            saElt.addAttribute(MailConstants.A_ID, s);
        }
    } else {
        // timed out....they should try again
        response.addAttribute(MailConstants.A_SEQ, lastKnownSeqNo);
    }
    encodeErrors(response, cb.errors);
    return response;
}
Also used : Continuation(org.eclipse.jetty.continuation.Continuation) WaitSetAccount(com.zimbra.cs.session.WaitSetAccount) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) ResumeContinuationListener(com.zimbra.cs.servlet.continuation.ResumeContinuationListener) HttpServletRequest(javax.servlet.http.HttpServletRequest) WaitSetCallback(com.zimbra.cs.session.WaitSetCallback) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) AdminServiceException(com.zimbra.cs.service.admin.AdminServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 3 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class ParseMimeMessageTest method staleReference.

@Test
public void staleReference() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // first, create the original draft
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
    Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
    Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
    el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    attach = el.addElement(MailConstants.E_ATTACH);
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // then, create a new draft that references one of the original draft's attachments
    attach.detach();
    (attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
    mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    // delete the draft itself and then try to save the new draft
    mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
    Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // check that the attachment's content is present and correct
    MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
    Assert.assertEquals("2 parts in draft", 2, multi.getCount());
    Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 4 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class ParseMimeMessageTest method parseMimeMsgSoap.

@Test
public void parseMimeMsgSoap() throws Exception {
    Element el = new Element.JSONElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, "dinner appt");
    el.addUniqueElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "foo bar");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    OperationContext octxt = new OperationContext(acct);
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    Assert.assertEquals("text/plain; charset=utf-8", mm.getContentType());
    Assert.assertEquals("dinner appt", mm.getSubject());
    Assert.assertEquals("rcpt@zimbra.com", mm.getHeader("To", ","));
    Assert.assertEquals("7bit", mm.getHeader("Content-Transfer-Encoding", ","));
    Assert.assertEquals("foo bar", mm.getContent());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) MimeMessage(javax.mail.internet.MimeMessage) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Test(org.junit.Test)

Example 5 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class ParseMimeMessageTest method attachZimbraDocument.

@Test
public void attachZimbraDocument() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("test123".getBytes()));
    Element el = new Element.JSONElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, "attach message");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    el.addElement(MailConstants.E_ATTACH).addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    MimeBodyPart part = (MimeBodyPart) mmp.getBodyPart(1);
    Assert.assertEquals(MimeConstants.CT_TEXT_HTML, new ContentType(part.getContentType()).getContentType());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ContentType(com.zimbra.common.mime.ContentType) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Aggregations

ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)410 Element (com.zimbra.common.soap.Element)322 Account (com.zimbra.cs.account.Account)175 Provisioning (com.zimbra.cs.account.Provisioning)164 Mailbox (com.zimbra.cs.mailbox.Mailbox)126 OperationContext (com.zimbra.cs.mailbox.OperationContext)95 ItemId (com.zimbra.cs.service.util.ItemId)60 Server (com.zimbra.cs.account.Server)48 ServiceException (com.zimbra.common.service.ServiceException)43 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)43 HashMap (java.util.HashMap)42 Domain (com.zimbra.cs.account.Domain)34 ArrayList (java.util.ArrayList)27 HashSet (java.util.HashSet)26 IOException (java.io.IOException)24 Test (org.junit.Test)20 Message (com.zimbra.cs.mailbox.Message)18 Group (com.zimbra.cs.account.Group)17 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)17 MimeMessage (javax.mail.internet.MimeMessage)16