Search in sources :

Example 1 with UserServletException

use of com.zimbra.cs.service.UserServletException in project zm-mailbox by Zimbra.

the class AuthUtil method basicAuthRequest.

public static Account basicAuthRequest(HttpServletRequest req, boolean allowGuest, boolean isDav) throws IOException, ServiceException, UserServletException {
    String auth = req.getHeader(HTTP_AUTH_HEADER);
    // TODO: more liberal parsing of Authorization value...
    if (auth == null || !auth.startsWith("Basic ")) {
        throw new UserServletException(HttpServletResponse.SC_UNAUTHORIZED, "must authenticate");
    }
    // 6 comes from "Basic ".length();
    String userPass = new String(Base64.decodeBase64(auth.substring(6).getBytes()), "UTF-8");
    int loc = userPass.indexOf(":");
    if (loc == -1) {
        throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "invalid basic auth credentials");
    }
    String userPassedIn = userPass.substring(0, loc);
    String user = userPassedIn;
    String pass = userPass.substring(loc + 1);
    Provisioning prov = Provisioning.getInstance();
    if (user.indexOf('@') == -1) {
        String host = HttpUtil.getVirtualHost(req);
        if (host != null) {
            Domain d = prov.get(Key.DomainBy.virtualHostname, host.toLowerCase());
            if (d != null)
                user += "@" + d.getName();
        }
    }
    Account acct = prov.get(AccountBy.name, user);
    if (acct == null) {
        if (allowGuest) {
            return new GuestAccount(user, pass);
        }
        throw new UserServletException(HttpServletResponse.SC_UNAUTHORIZED, "invalid username/password");
    }
    try {
        Map<String, Object> authCtxt = new HashMap<String, Object>();
        authCtxt.put(AuthContext.AC_ORIGINATING_CLIENT_IP, ZimbraServlet.getOrigIp(req));
        authCtxt.put(AuthContext.AC_REMOTE_IP, ZimbraServlet.getClientIp(req));
        authCtxt.put(AuthContext.AC_ACCOUNT_NAME_PASSEDIN, userPassedIn);
        authCtxt.put(AuthContext.AC_USER_AGENT, req.getHeader("User-Agent"));
        Protocol proto = isDav ? Protocol.http_dav : Protocol.http_basic;
        prov.authAccount(acct, pass, proto, authCtxt);
    } catch (ServiceException se) {
        throw new UserServletException(HttpServletResponse.SC_UNAUTHORIZED, "invalid username/password");
    }
    return acct;
}
Also used : GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) UserServletException(com.zimbra.cs.service.UserServletException) Domain(com.zimbra.cs.account.Domain) Protocol(com.zimbra.cs.account.auth.AuthContext.Protocol) Provisioning(com.zimbra.cs.account.Provisioning)

Example 2 with UserServletException

use of com.zimbra.cs.service.UserServletException in project zm-mailbox by Zimbra.

the class NativeFormatter method saveDocument.

private void saveDocument(Blob blob, UserServletContext context, String contentType, Folder folder, String filename, InputStream is) throws IOException, ServiceException, UserServletException {
    Mailbox mbox = folder.getMailbox();
    MailItem item = null;
    String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
    ParsedDocument pd = null;
    try {
        if (contentType == null) {
            contentType = MimeDetect.getMimeDetect().detect(filename);
            if (contentType == null)
                contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
        }
        pd = new ParsedDocument(blob, filename, contentType, System.currentTimeMillis(), creator, context.req.getHeader("X-Zimbra-Description"), true);
        item = mbox.getItemByPath(context.opContext, filename, folder.getId());
        // XXX: should we just overwrite here instead?
        if (!(item instanceof Document))
            throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "cannot overwrite existing object at that path");
        // scan upload for viruses
        StringBuffer info = new StringBuffer();
        UploadScanner.Result result = UploadScanner.acceptStream(is, info);
        if (result == UploadScanner.REJECT)
            throw MailServiceException.UPLOAD_REJECTED(filename, info.toString());
        if (result == UploadScanner.ERROR)
            throw MailServiceException.SCAN_ERROR(filename);
        item = mbox.addDocumentRevision(context.opContext, item.getId(), pd);
    } catch (NoSuchItemException nsie) {
        item = mbox.createDocument(context.opContext, folder.getId(), pd, MailItem.Type.DOCUMENT, 0);
    }
    sendZimbraHeaders(context, context.resp, item);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) UserServletException(com.zimbra.cs.service.UserServletException) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Document(com.zimbra.cs.mailbox.Document) UploadScanner(com.zimbra.cs.service.mail.UploadScanner) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)

Example 3 with UserServletException

use of com.zimbra.cs.service.UserServletException in project zm-mailbox by Zimbra.

the class NativeFormatter method saveCallback.

@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws IOException, ServiceException, UserServletException {
    if (filename == null) {
        Mailbox mbox = folder.getMailbox();
        try {
            ParsedMessage pm = new ParsedMessage(context.getPostBody(), mbox.attachmentsIndexingEnabled());
            DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(true);
            mbox.addMessage(context.opContext, pm, dopt, null);
            return;
        } catch (ServiceException e) {
            throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "error parsing message");
        }
    }
    InputStream is = context.getRequestInputStream();
    try {
        Blob blob = StoreManager.getInstance().storeIncoming(is);
        saveDocument(blob, context, contentType, folder, filename, is);
    } finally {
        is.close();
    }
}
Also used : Blob(com.zimbra.cs.store.Blob) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) UserServletException(com.zimbra.cs.service.UserServletException) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions)

Example 4 with UserServletException

use of com.zimbra.cs.service.UserServletException in project zm-mailbox by Zimbra.

the class SyncFormatter method saveCallback.

// FIXME: need to support tags, flags, date, etc...
@Override
public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename) throws IOException, ServiceException, UserServletException {
    byte[] body = context.getPostBody();
    try {
        Mailbox mbox = folder.getMailbox();
        ParsedMessage pm = new ParsedMessage(body, mbox.attachmentsIndexingEnabled());
        DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(true);
        mbox.addMessage(context.opContext, pm, dopt, null);
    } catch (ServiceException e) {
        throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, "error parsing message");
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) UserServletException(com.zimbra.cs.service.UserServletException) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions)

Example 5 with UserServletException

use of com.zimbra.cs.service.UserServletException in project zm-mailbox by Zimbra.

the class Formatter method save.

public final void save(UserServletContext context, String contentType, Folder folder, String filename) throws UserServletException, IOException, ServletException, ServiceException {
    try {
        saveStarted(context);
        if (context.targetMailbox != null) {
            context.targetMailbox.suspendIndexing();
        }
        saveCallback(context, contentType, folder, filename);
        updateClient(context, null);
    } catch (UserServletException e) {
        throw new UserServletException(e.getHttpStatusCode(), e.getMessage(), e);
    } catch (Exception e) {
        updateClient(context, e);
        saveEnded(context);
    } finally {
        if (context.targetMailbox != null) {
            try {
                context.targetMailbox.resumeIndexingAndDrainDeferred();
            } catch (Exception e) {
                context.targetMailbox.resumeIndexing();
            }
        }
    }
}
Also used : UserServletException(com.zimbra.cs.service.UserServletException) ServletException(javax.servlet.ServletException) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) ConversionUnsupportedException(com.zimbra.cs.convert.ConversionUnsupportedException) UserServletException(com.zimbra.cs.service.UserServletException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Aggregations

UserServletException (com.zimbra.cs.service.UserServletException)12 ServiceException (com.zimbra.common.service.ServiceException)8 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)6 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)5 ServletException (javax.servlet.ServletException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 IOException (java.io.IOException)4 ConversionUnsupportedException (com.zimbra.cs.convert.ConversionUnsupportedException)3 MailItem (com.zimbra.cs.mailbox.MailItem)3 PushbackInputStream (java.io.PushbackInputStream)3 GuestAccount (com.zimbra.cs.account.GuestAccount)2 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)2 Document (com.zimbra.cs.mailbox.Document)2 ParsedDocument (com.zimbra.cs.mime.ParsedDocument)2 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 UploadScanner (com.zimbra.cs.service.mail.UploadScanner)2 Blob (com.zimbra.cs.store.Blob)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Charset (java.nio.charset.Charset)2