Search in sources :

Example 66 with Account

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

the class CalendarCollection method handlePost.

@Override
public void handlePost(DavContext ctxt) throws DavException, IOException, ServiceException {
    Provisioning prov = Provisioning.getInstance();
    DavResource rs = null;
    try {
        String user = ctxt.getUser();
        Account account = prov.get(AccountBy.name, user);
        if (account == null) {
            // Anti-account name harvesting.
            ZimbraLog.dav.info("Failing POST to Calendar - no such account '%s'", user);
            throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
        }
        List<Invite> invites = uploadToInvites(ctxt, account);
        String uid = findEventUid(invites);
        rs = createItemFromInvites(ctxt, account, uid + ".ics", invites, false);
        if (rs.isNewlyCreated()) {
            ctxt.getResponse().setHeader("Location", rs.getHref());
            ctxt.setStatus(HttpServletResponse.SC_CREATED);
        } else {
            ctxt.setStatus(HttpServletResponse.SC_NO_CONTENT);
        }
        if (rs.hasEtag()) {
            ctxt.getResponse().setHeader(DavProtocol.HEADER_ETAG, rs.getEtag());
            ctxt.getResponse().setHeader(ETagHeaderFilter.ZIMBRA_ETAG_HEADER, rs.getEtag());
        }
    } catch (ServiceException e) {
        if (e.getCode().equals(ServiceException.FORBIDDEN)) {
            throw new DavException(e.getMessage(), HttpServletResponse.SC_FORBIDDEN, e);
        } else {
            throw new DavException("cannot create icalendar item", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) DavException(com.zimbra.cs.dav.DavException) Provisioning(com.zimbra.cs.account.Provisioning) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 67 with Account

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

the class DavResource method getTextContent.

protected String getTextContent(DavContext ctxt) throws IOException {
    StringBuilder buf = new StringBuilder();
    buf.append("Request\n\n");
    buf.append("\tAuthenticated user:\t").append(ctxt.getAuthAccount().getName()).append("\n");
    buf.append("\tCurrent date:\t\t").append(new Date(System.currentTimeMillis())).append("\n");
    buf.append("\nResource\n\n");
    buf.append("\tName:\t\t\t").append(getName()).append("\n");
    buf.append("\tPath:\t\t\t").append(getUri()).append("\n");
    buf.append("\tDate:\t\t\t").append(getLastModifiedDate()).append("\n");
    try {
        Provisioning prov = Provisioning.getInstance();
        Account account = prov.get(Key.AccountBy.name, getOwner());
        buf.append("\tOwner account name:\t").append(account.getName()).append("\n");
    } catch (ServiceException se) {
    }
    buf.append("\nProperties\n\n");
    buf.append(getPropertiesAsText(ctxt));
    return buf.toString();
}
Also used : Account(com.zimbra.cs.account.Account) ServiceException(com.zimbra.common.service.ServiceException) Date(java.util.Date) Provisioning(com.zimbra.cs.account.Provisioning)

Example 68 with Account

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

the class MailItemResource method getZMailbox.

private static ZMailbox getZMailbox(DavContext ctxt, Collection col) throws ServiceException {
    AuthToken authToken = AuthProvider.getAuthToken(ctxt.getAuthAccount());
    Account acct = Provisioning.getInstance().getAccountById(col.getItemId().getAccountId());
    ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(acct));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(acct.getId());
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    return ZMailbox.getMailbox(zoptions);
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) AuthToken(com.zimbra.cs.account.AuthToken)

Example 69 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 70 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)

Aggregations

Account (com.zimbra.cs.account.Account)1244 Test (org.junit.Test)520 Mailbox (com.zimbra.cs.mailbox.Mailbox)389 OperationContext (com.zimbra.cs.mailbox.OperationContext)292 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)274 Message (com.zimbra.cs.mailbox.Message)255 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)242 Provisioning (com.zimbra.cs.account.Provisioning)222 Element (com.zimbra.common.soap.Element)220 ItemId (com.zimbra.cs.service.util.ItemId)219 ServiceException (com.zimbra.common.service.ServiceException)201 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)158 HashMap (java.util.HashMap)154 Domain (com.zimbra.cs.account.Domain)145 MimeMessage (javax.mail.internet.MimeMessage)104 GuestAccount (com.zimbra.cs.account.GuestAccount)98 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)92 AccountServiceException (com.zimbra.cs.account.AccountServiceException)77 ZMailbox (com.zimbra.client.ZMailbox)72 Header (javax.mail.Header)67