Search in sources :

Example 31 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class AddressbookCollection 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 Addressbook - no such account '%s'", user);
            throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
        }
        VCard vcard = AddressObject.uploadToVCard(ctxt);
        String baseName = new StringBuilder(vcard.uid).append(AddressObject.VCARD_EXTENSION).toString();
        rs = UrlNamespace.getResourceAt(ctxt, ctxt.getUser(), relativeUrlForChild(ctxt.getUser(), baseName));
        if (rs != null) {
            // name based on uid already taken - choose another.
            baseName = new StringBuilder(UUIDUtil.generateUUID()).append(AddressObject.VCARD_EXTENSION).toString();
        }
        rs = AddressObject.create(ctxt, baseName, this, vcard, 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 vcard 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) VCard(com.zimbra.cs.service.formatter.VCard) Provisioning(com.zimbra.cs.account.Provisioning)

Example 32 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class CalendarCollection method createItem.

/**
     * Creates an appointment sent in PUT request in this calendar.
     * @param name - basename (last component) of the path - i.e. the name within this collection
     */
@Override
public DavResource createItem(DavContext ctxt, String name) throws DavException, IOException {
    Provisioning prov = Provisioning.getInstance();
    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);
        return createItemFromInvites(ctxt, account, name, invites, true);
    } 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 33 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class Collection method mkCol.

public Collection mkCol(DavContext ctxt, String name, MailItem.Type view) throws DavException {
    try {
        Mailbox mbox = getMailbox(ctxt);
        Folder.FolderOptions fopt = new Folder.FolderOptions().setDefaultView(view);
        Folder f = mbox.createFolder(ctxt.getOperationContext(), name, mId, fopt);
        return (Collection) UrlNamespace.getResourceFromMailItem(ctxt, f);
    } catch (ServiceException e) {
        if (e.getCode().equals(MailServiceException.ALREADY_EXISTS))
            throw new DavException("item already exists", HttpServletResponse.SC_CONFLICT, e);
        else if (e.getCode().equals(ServiceException.PERM_DENIED))
            throw new DavException("permission denied", HttpServletResponse.SC_FORBIDDEN, e);
        else
            throw new DavException("can't create", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavException(com.zimbra.cs.dav.DavException) Folder(com.zimbra.cs.mailbox.Folder)

Example 34 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class Collection method delete.

@Override
public void delete(DavContext ctxt) throws DavException {
    String user = ctxt.getUser();
    String path = ctxt.getPath();
    if (user == null || path == null)
        throw new DavException("invalid uri", HttpServletResponse.SC_NOT_FOUND, null);
    try {
        Mailbox mbox = getMailbox(ctxt);
        mbox.delete(ctxt.getOperationContext(), mId, type);
    } catch (ServiceException e) {
        throw new DavException("cannot get item", HttpServletResponse.SC_NOT_FOUND, e);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavException(com.zimbra.cs.dav.DavException)

Example 35 with DavException

use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.

the class DavServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ZimbraLog.clearContext();
    addRemoteIpToLoggingContext(req);
    ZimbraLog.addUserAgentToContext(req.getHeader(DavProtocol.HEADER_USER_AGENT));
    //bug fix - send 400 for Range requests
    String rangeHeader = req.getHeader(DavProtocol.HEADER_RANGE);
    if (null != rangeHeader) {
        sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "Range header not supported", null, Level.debug);
        return;
    }
    RequestType rtype = getAllowedRequestType(req);
    ZimbraLog.dav.debug("Allowable request types %s", rtype);
    if (rtype == RequestType.none) {
        sendError(resp, HttpServletResponse.SC_NOT_ACCEPTABLE, "Not an allowed request type", null, Level.debug);
        return;
    }
    logRequestInfo(req);
    Account authUser = null;
    DavContext ctxt;
    try {
        AuthToken at = AuthProvider.getAuthToken(req, false);
        if (at != null && (at.isExpired() || !at.isRegistered())) {
            at = null;
        }
        if (at != null && (rtype == RequestType.both || rtype == RequestType.authtoken)) {
            authUser = Provisioning.getInstance().get(AccountBy.id, at.getAccountId());
        } else if (at == null && (rtype == RequestType.both || rtype == RequestType.password)) {
            AuthUtil.AuthResult result = AuthUtil.basicAuthRequest(req, resp, true, this);
            if (result.sendErrorCalled) {
                logResponseInfo(resp);
                return;
            }
            authUser = result.authorizedAccount;
        }
        if (authUser == null) {
            try {
                sendError(resp, HttpServletResponse.SC_UNAUTHORIZED, "Authentication failed", null, Level.debug);
            } catch (Exception e) {
            }
            return;
        }
        ZimbraLog.addToContext(ZimbraLog.C_ANAME, authUser.getName());
        ctxt = new DavContext(req, resp, authUser);
    } catch (AuthTokenException e) {
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error getting authenticated user", e);
        return;
    } catch (ServiceException e) {
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error getting authenticated user", e);
        return;
    }
    DavMethod method = sMethods.get(req.getMethod());
    if (method == null) {
        setAllowHeader(resp);
        sendError(resp, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Not an allowed method", null, Level.debug);
        return;
    }
    long t0 = System.currentTimeMillis();
    CacheStates cache = null;
    try {
        if (ZimbraLog.dav.isDebugEnabled()) {
            try {
                Upload upload = ctxt.getUpload();
                if (upload.getSize() > 0 && upload.getContentType().startsWith("text")) {
                    if (ZimbraLog.dav.isDebugEnabled()) {
                        StringBuilder logMsg = new StringBuilder("REQUEST\n").append(new String(ByteUtil.readInput(upload.getInputStream(), -1, 20480), "UTF-8"));
                        ZimbraLog.dav.debug(logMsg.toString());
                    }
                }
            } catch (DavException de) {
                throw de;
            } catch (Exception e) {
                ZimbraLog.dav.debug("ouch", e);
            }
        }
        cache = checkCachedResponse(ctxt, authUser);
        if (!ctxt.isResponseSent() && !isProxyRequest(ctxt, method)) {
            method.checkPrecondition(ctxt);
            method.handle(ctxt);
            method.checkPostcondition(ctxt);
            if (!ctxt.isResponseSent()) {
                resp.setStatus(ctxt.getStatus());
            }
        }
        if (!ctxt.isResponseSent()) {
            logResponseInfo(resp);
        }
    } catch (DavException e) {
        if (e.getCause() instanceof MailServiceException.NoSuchItemException || e.getStatus() == HttpServletResponse.SC_NOT_FOUND)
            ZimbraLog.dav.info(ctxt.getUri() + " not found");
        else if (e.getStatus() == HttpServletResponse.SC_MOVED_TEMPORARILY || e.getStatus() == HttpServletResponse.SC_MOVED_PERMANENTLY)
            ZimbraLog.dav.info("sending redirect");
        try {
            if (e.isStatusSet()) {
                resp.setStatus(e.getStatus());
                if (e.hasErrorMessage())
                    e.writeErrorMsg(resp.getOutputStream());
                if (ZimbraLog.dav.isDebugEnabled()) {
                    ZimbraLog.dav.info("sending http error %d because: %s", e.getStatus(), e.getMessage(), e);
                } else {
                    ZimbraLog.dav.info("sending http error %d because: %s", e.getStatus(), e.getMessage());
                }
                if (e.getCause() != null)
                    ZimbraLog.dav.debug("exception: ", e.getCause());
            } else {
                sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error handling method " + method.getName(), e);
            }
        } catch (IllegalStateException ise) {
            ZimbraLog.dav.debug("can't write error msg", ise);
        }
    } catch (ServiceException e) {
        if (e instanceof MailServiceException.NoSuchItemException) {
            sendError(resp, HttpServletResponse.SC_NOT_FOUND, ctxt.getUri() + " not found", null, Level.info);
            return;
        }
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error handling method " + method.getName(), e);
    } catch (Exception e) {
        try {
            sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error handling method " + method.getName(), e);
        } catch (Exception ex) {
        }
    } finally {
        long t1 = System.currentTimeMillis();
        ZimbraLog.dav.info("DavServlet operation " + method.getName() + " to " + req.getPathInfo() + " (depth: " + ctxt.getDepth().name() + ") finished in " + (t1 - t0) + "ms");
        if (cache != null)
            cacheCleanUp(ctxt, cache);
        ctxt.cleanup();
    }
}
Also used : Account(com.zimbra.cs.account.Account) DavException(com.zimbra.cs.dav.DavException) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) ServletException(javax.servlet.ServletException) XmlParseException(com.zimbra.common.soap.XmlParseException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) DavException(com.zimbra.cs.dav.DavException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) AuthToken(com.zimbra.cs.account.AuthToken) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavContext(com.zimbra.cs.dav.DavContext)

Aggregations

DavException (com.zimbra.cs.dav.DavException)67 ServiceException (com.zimbra.common.service.ServiceException)27 Element (org.dom4j.Element)25 Account (com.zimbra.cs.account.Account)18 Mailbox (com.zimbra.cs.mailbox.Mailbox)18 DavResource (com.zimbra.cs.dav.resource.DavResource)15 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)15 ArrayList (java.util.ArrayList)14 Provisioning (com.zimbra.cs.account.Provisioning)11 Document (org.dom4j.Document)9 DavResponse (com.zimbra.cs.dav.service.DavResponse)8 MailItem (com.zimbra.cs.mailbox.MailItem)8 Invite (com.zimbra.cs.mailbox.calendar.Invite)7 ZMailbox (com.zimbra.client.ZMailbox)6 RequestProp (com.zimbra.cs.dav.DavContext.RequestProp)6 IOException (java.io.IOException)6 Collection (com.zimbra.cs.dav.resource.Collection)5 Folder (com.zimbra.cs.mailbox.Folder)5 CalendarCollection (com.zimbra.cs.dav.resource.CalendarCollection)4 ZProperty (com.zimbra.common.calendar.ZCalendar.ZProperty)3