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;
}
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);
}
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();
}
}
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");
}
}
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();
}
}
}
}
Aggregations