Search in sources :

Example 41 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class DavServlet method checkCachedResponse.

private CacheStates checkCachedResponse(DavContext ctxt, Account authUser) throws IOException, DavException, ServiceException {
    CacheStates cache = new CacheStates();
    // Are we running with cache enabled, and is this a cachable CalDAV ctag request?
    if (cache.ctagCacheEnabled && isCtagRequest(ctxt)) {
        cache.ctagResponseCache = CalendarCacheManager.getInstance().getCtagResponseCache();
        cache.gzipAccepted = ctxt.isGzipAccepted();
        String targetUser = ctxt.getUser();
        Account targetAcct = Provisioning.getInstance().get(AccountBy.name, targetUser);
        boolean ownAcct = targetAcct != null && targetAcct.getId().equals(authUser.getId());
        String parentPath = ctxt.getPath();
        KnownUserAgent knownUA = ctxt.getKnownUserAgent();
        // Use cache only when requesting own account and User-Agent and path are well-defined.
        if (ownAcct && knownUA != null && parentPath != null) {
            AccountKey accountKey = new AccountKey(targetAcct.getId());
            AccountCtags allCtagsData = CalendarCacheManager.getInstance().getCtags(accountKey);
            // We can't use cache if it doesn't have data for this user.
            if (allCtagsData != null) {
                boolean validRoot = true;
                int rootFolderId = Mailbox.ID_FOLDER_USER_ROOT;
                if (!"/".equals(parentPath)) {
                    CtagInfo calInfoRoot = allCtagsData.getByPath(parentPath);
                    if (calInfoRoot != null)
                        rootFolderId = calInfoRoot.getId();
                    else
                        validRoot = false;
                }
                if (validRoot) {
                    // Is there a previously cached response?
                    cache.ctagCacheKey = new CtagResponseCacheKey(targetAcct.getId(), knownUA.toString(), rootFolderId);
                    CtagResponseCacheValue ctagResponse = cache.ctagResponseCache.get(cache.ctagCacheKey);
                    if (ctagResponse != null) {
                        // Found a cached response.  Let's check if it's stale.
                        // 1. If calendar list has been updated since, cached response is no good.
                        String currentCalListVer = allCtagsData.getVersion();
                        if (currentCalListVer.equals(ctagResponse.getVersion())) {
                            // 2. We have to examine ctags of individual calendars.
                            boolean cacheHit = true;
                            Map<Integer, String> oldCtags = ctagResponse.getCtags();
                            // We're good if ctags from before are unchanged.
                            for (Map.Entry<Integer, String> entry : oldCtags.entrySet()) {
                                int calFolderId = entry.getKey();
                                String ctag = entry.getValue();
                                CtagInfo calInfoCurr = allCtagsData.getById(calFolderId);
                                if (calInfoCurr == null) {
                                    // Just a sanity check.  The cal list version check should have
                                    // already taken care of added/removed calendars.
                                    cacheHit = false;
                                    break;
                                }
                                if (!ctag.equals(calInfoCurr.getCtag())) {
                                    // A calendar has been modified.  Stale!
                                    cacheHit = false;
                                    break;
                                }
                            }
                            if (cacheHit) {
                                ZimbraLog.dav.debug("CTAG REQUEST CACHE HIT");
                                // All good.  Send cached response.
                                ctxt.setStatus(DavProtocol.STATUS_MULTI_STATUS);
                                HttpServletResponse response = ctxt.getResponse();
                                response.setStatus(ctxt.getStatus());
                                response.setContentType(DavProtocol.DAV_CONTENT_TYPE);
                                byte[] respData = ctagResponse.getResponseBody();
                                response.setContentLength(ctagResponse.getRawLength());
                                byte[] unzipped = null;
                                if (ZimbraLog.dav.isDebugEnabled() || (ctagResponse.isGzipped() && !cache.gzipAccepted)) {
                                    if (ctagResponse.isGzipped()) {
                                        ByteArrayInputStream bais = new ByteArrayInputStream(respData);
                                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                        GZIPInputStream gzis = null;
                                        try {
                                            gzis = new GZIPInputStream(bais, respData.length);
                                            ByteUtil.copy(gzis, false, baos, true);
                                        } finally {
                                            ByteUtil.closeStream(gzis);
                                        }
                                        unzipped = baos.toByteArray();
                                    } else {
                                        unzipped = respData;
                                    }
                                    if (ZimbraLog.dav.isDebugEnabled()) {
                                        ZimbraLog.dav.debug("RESPONSE:\n" + new String(unzipped, "UTF-8"));
                                    }
                                }
                                if (!ctagResponse.isGzipped()) {
                                    response.getOutputStream().write(respData);
                                } else {
                                    if (cache.gzipAccepted) {
                                        response.addHeader(DavProtocol.HEADER_CONTENT_ENCODING, DavProtocol.ENCODING_GZIP);
                                        response.getOutputStream().write(respData);
                                    } else {
                                        assert (unzipped != null);
                                        response.getOutputStream().write(unzipped);
                                    }
                                }
                                // Tell the context the response has been sent.
                                ctxt.responseSent();
                            }
                        }
                    }
                    if (!ctxt.isResponseSent()) {
                        // Cache miss, or cached response is stale.  We're gonna have to generate the
                        // response the hard way.  Capture a snapshot of current state of calendars
                        // to attach to the response to be cached later.
                        cache.cacheThisCtagResponse = true;
                        cache.acctVerSnapshot = allCtagsData.getVersion();
                        cache.ctagsSnapshot = new HashMap<Integer, String>();
                        Collection<CtagInfo> childCals = allCtagsData.getChildren(rootFolderId);
                        if (rootFolderId != Mailbox.ID_FOLDER_USER_ROOT) {
                            CtagInfo ctagRoot = allCtagsData.getById(rootFolderId);
                            if (ctagRoot != null)
                                cache.ctagsSnapshot.put(rootFolderId, ctagRoot.getCtag());
                        }
                        for (CtagInfo calInfo : childCals) {
                            cache.ctagsSnapshot.put(calInfo.getId(), calInfo.getCtag());
                        }
                    }
                }
            }
            if (!ctxt.isResponseSent())
                ZimbraLog.dav.debug("CTAG REQUEST CACHE MISS");
        }
    }
    return cache;
}
Also used : Account(com.zimbra.cs.account.Account) AccountCtags(com.zimbra.cs.mailbox.calendar.cache.AccountCtags) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CtagResponseCacheKey(com.zimbra.cs.mailbox.calendar.cache.CtagResponseCache.CtagResponseCacheKey) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) CtagInfo(com.zimbra.cs.mailbox.calendar.cache.CtagInfo) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) KnownUserAgent(com.zimbra.cs.dav.DavContext.KnownUserAgent) AccountKey(com.zimbra.cs.mailbox.calendar.cache.AccountKey) Map(java.util.Map) HashMap(java.util.HashMap) CtagResponseCacheValue(com.zimbra.cs.mailbox.calendar.cache.CtagResponseCache.CtagResponseCacheValue)

Example 42 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class AclReports method getAclPrincipals.

private ArrayList<DavResource> getAclPrincipals(DavContext ctxt) throws DavException, ServiceException {
    ArrayList<DavResource> ret = new ArrayList<DavResource>();
    DavResource res = ctxt.getRequestedResource();
    if (!(res instanceof MailItemResource))
        return ret;
    List<Ace> aces = ((MailItemResource) res).getAce(ctxt);
    Provisioning prov = Provisioning.getInstance();
    for (Ace ace : aces) {
        if (ace.hasHref()) {
            Account acct = prov.get(Key.AccountBy.id, ace.getZimbraId());
            if (acct != null)
                ret.add(UrlNamespace.getPrincipal(ctxt, acct));
        }
    }
    return ret;
}
Also used : Account(com.zimbra.cs.account.Account) Ace(com.zimbra.cs.dav.property.Acl.Ace) DavResource(com.zimbra.cs.dav.resource.DavResource) MailItemResource(com.zimbra.cs.dav.resource.MailItemResource) ArrayList(java.util.ArrayList) Provisioning(com.zimbra.cs.account.Provisioning)

Example 43 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class ImapHandler method doLISTRIGHTS.

private boolean doLISTRIGHTS(String tag, ImapPath path, String principal) throws IOException {
    if (!checkState(tag, State.AUTHENTICATED)) {
        return true;
    }
    boolean isOwner = false;
    try {
        if (!"anyone".equals(principal)) {
            Account acct = Provisioning.getInstance().get(Key.AccountBy.name, principal);
            if (acct == null) {
                throw AccountServiceException.NO_SUCH_ACCOUNT(principal);
            }
            isOwner = path.belongsTo(acct.getId());
        }
        // as a side effect, path.getFolderRights() checks for the existence of the target folder
        if ((path.getFolderRights() & ACL.RIGHT_ADMIN) == 0) {
            throw ServiceException.PERM_DENIED("you must have admin privileges to perform LISTRIGHTS");
        }
    } catch (ServiceException e) {
        if (e.getCode().equals(ServiceException.PERM_DENIED)) {
            ZimbraLog.imap.info("LISTRIGHTS failed: permission denied on folder: %s", path);
        } else if (e.getCode().equals(MailServiceException.NO_SUCH_FOLDER)) {
            ZimbraLog.imap.info("LISTRIGHTS failed: no such folder: %s", path);
        } else if (e.getCode().equals(AccountServiceException.NO_SUCH_ACCOUNT)) {
            ZimbraLog.imap.info("LISTRIGHTS failed: no such account: %s", principal);
        } else {
            ZimbraLog.imap.warn("LISTRIGHTS failed", e);
        }
        sendNO(tag, "LISTRIGHTS failed");
        return canContinue(e);
    }
    if (isOwner) {
        sendUntagged("LISTRIGHTS " + path.asUtf7String() + " \"" + principal + "\" " + IMAP_CONCATENATED_RIGHTS);
    } else {
        sendUntagged("LISTRIGHTS " + path.asUtf7String() + " \"" + principal + "\" \"\" " + IMAP_DELIMITED_RIGHTS);
    }
    sendNotifications(true, false);
    sendOK(tag, "LISTRIGHTS completed");
    return true;
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 44 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class ImapPath method isEquivalent.

public boolean isEquivalent(ImapPath other) {
    if (!mPath.equalsIgnoreCase(other.mPath)) {
        return false;
    } else if (mOwner == other.mOwner || (mOwner != null && mOwner.equalsIgnoreCase(other.mOwner))) {
        return true;
    }
    try {
        Account acct = getOwnerAccount();
        Account otheracct = other.getOwnerAccount();
        return (acct == null || otheracct == null ? false : acct.getId().equalsIgnoreCase(otheracct.getId()));
    } catch (ServiceException e) {
        return false;
    }
}
Also used : Account(com.zimbra.cs.account.Account) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 45 with Account

use of com.zimbra.cs.account.Account in project zm-mailbox by Zimbra.

the class ImapPath method setupMailboxStoreForTarget.

private ImapMailboxStore setupMailboxStoreForTarget(Account target, ItemId iidRemote) throws ServiceException {
    ImapMailboxStore imapMailboxStore = null;
    // if both target and owner are on local server and using local imap
    if (Provisioning.onLocalServer(target) && onLocalServer()) {
        try {
            MailboxStore mbox = MailboxManager.getInstance().getMailboxByAccount(target);
            imapMailboxStore = ImapMailboxStore.get(mbox, target.getId());
        } catch (ServiceException se) {
            ZimbraLog.imap.debug("Unexpected exception", se);
        }
    } else {
        Account acct = mCredentials == null ? null : Provisioning.getInstance().get(AccountBy.id, mCredentials.getAccountId());
        if (acct == null) {
            return null;
        }
        try {
            ZMailbox zmbx = getZMailboxForAccount(target);
            ZFolder zfolder = zmbx.getFolderById(iidRemote.toString(mCredentials.getAccountId()));
            if (zfolder == null) {
                return null;
            }
            imapMailboxStore = ImapMailboxStore.get(zmbx);
        } catch (ServiceException e) {
            ZimbraLog.imap.debug("Unexpected exception", e);
        }
    }
    return imapMailboxStore;
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) MailboxStore(com.zimbra.common.mailbox.MailboxStore) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZFolder(com.zimbra.client.ZFolder)

Aggregations

Account (com.zimbra.cs.account.Account)1444 Test (org.junit.Test)691 Mailbox (com.zimbra.cs.mailbox.Mailbox)468 OperationContext (com.zimbra.cs.mailbox.OperationContext)354 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)331 Message (com.zimbra.cs.mailbox.Message)315 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)294 Element (com.zimbra.common.soap.Element)280 ServiceException (com.zimbra.common.service.ServiceException)261 ItemId (com.zimbra.cs.service.util.ItemId)248 Provisioning (com.zimbra.cs.account.Provisioning)234 HashMap (java.util.HashMap)179 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)175 Domain (com.zimbra.cs.account.Domain)149 MimeMessage (javax.mail.internet.MimeMessage)125 GuestAccount (com.zimbra.cs.account.GuestAccount)107 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)100 SyntaxException (org.apache.jsieve.exception.SyntaxException)90 AccountServiceException (com.zimbra.cs.account.AccountServiceException)87 Header (javax.mail.Header)84