use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.
the class RuleRewriter method createFolderIfNecessary.
private void createFolderIfNecessary(String path, String ruleName) throws ServiceException {
Pair<Folder, String> folderAndRemotePath = mMailbox.getFolderByPathLongestMatch(null, Mailbox.ID_FOLDER_USER_ROOT, path);
Folder folder = folderAndRemotePath.getFirst();
String remotePath = folderAndRemotePath.getSecond();
if (StringUtil.isNullOrEmpty(remotePath)) {
remotePath = null;
}
if (folder instanceof Mountpoint && remotePath != null) {
// Create remote folder path
Mountpoint mountpoint = (Mountpoint) folder;
ZimbraLog.filter.info("Creating folder %s in remote folder %s for rule %s.", remotePath, folder.getPath(), ruleName);
ZMailbox remoteMbox = FilterUtil.getRemoteZMailbox(mMailbox, (Mountpoint) folder);
ItemId id = mountpoint.getTarget();
ZFolder parent = remoteMbox.getFolderById(id.toString());
if (parent == null) {
String msg = String.format("Could not find folder with id %d in remote mailbox %s.", mountpoint.getRemoteId(), mountpoint.getOwnerId());
throw ServiceException.FAILURE(msg, null);
}
String[] pathElements = remotePath.split(ZMailbox.PATH_SEPARATOR);
for (String folderName : pathElements) {
if (!StringUtil.isNullOrEmpty(folderName)) {
ZFolder currentFolder = parent.getSubFolderByPath(folderName);
if (currentFolder != null) {
parent = currentFolder;
} else {
parent = remoteMbox.createFolder(parent.getId(), folderName, ZFolder.View.message, null, null, null);
}
}
}
} else if (remotePath != null) {
// Create local folder path
ZimbraLog.filter.info("Creating folder %s for rule %s.", path, ruleName);
mMailbox.createFolder(null, path, new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
}
}
use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.
the class ZimbraQuery method handleLocalPermissionChecks.
/**
* For the local targets:
* - exclude all the not-visible folders from the query
* - look at all the text-operations and figure out if private appointments need to be excluded
*/
private static UnionQueryOperation handleLocalPermissionChecks(UnionQueryOperation union, Set<Folder> visibleFolders, boolean allowPrivateAccess) {
// iterate backwards so we can remove/add w/o screwing iteration
for (int i = union.operations.size() - 1; i >= 0; i--) {
QueryOperation op = union.operations.get(i);
Set<QueryTarget> targets = op.getQueryTargets();
// during the optimize() step
assert (QueryTarget.getExplicitTargetCount(targets) <= 1);
if (!QueryTarget.hasExternalTarget(targets)) {
// local target
if (!allowPrivateAccess)
op.depthFirstRecurse(new excludePrivateCalendarItems());
if (visibleFolders != null) {
if (visibleFolders.isEmpty()) {
union.operations.remove(i);
ZimbraLog.search.debug("Query changed to NULL_QUERY_OPERATION, no visible folders");
union.operations.add(i, new NoResultsQueryOperation());
} else {
union.operations.remove(i);
// build a "and (in:visible1 or in:visible2 or in:visible3...)" query tree here!
IntersectionQueryOperation intersect = new IntersectionQueryOperation();
intersect.addQueryOp(op);
UnionQueryOperation newUnion = new UnionQueryOperation();
intersect.addQueryOp(newUnion);
// if one or more target folders are specified, use those which are visible.
Set<Folder> targetFolders = null;
if (op instanceof DBQueryOperation) {
DBQueryOperation dbOp = (DBQueryOperation) op;
targetFolders = dbOp.getTargetFolders();
}
for (Folder folder : visibleFolders) {
// exclude remote folders
if (!(folder instanceof Mountpoint) || ((Mountpoint) folder).isLocal()) {
if (targetFolders != null && targetFolders.size() > 0 && !targetFolders.contains(folder)) {
// don't bother searching other visible folders if the query asked for specific folders...
continue;
}
DBQueryOperation newOp = new DBQueryOperation();
newUnion.add(newOp);
newOp.addInFolder(folder, true);
}
}
union.operations.add(i, intersect);
}
}
}
}
return union;
}
use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.
the class HtmlFormatter method dispatchJspRest.
static void dispatchJspRest(Servlet servlet, UserServletContext context) throws ServiceException, ServletException, IOException {
AuthToken auth = null;
long expiration = System.currentTimeMillis() + AUTH_EXPIRATION;
if (context.basicAuthHappened) {
Account acc = context.getAuthAccount();
if (acc instanceof GuestAccount) {
auth = AuthToken.getAuthToken(acc.getId(), acc.getName(), null, ((GuestAccount) acc).getDigest(), expiration);
} else {
auth = AuthProvider.getAuthToken(context.getAuthAccount(), expiration);
}
} else if (context.cookieAuthHappened) {
auth = UserServlet.getAuthTokenFromCookie(context.req, context.resp, true);
} else {
auth = AuthToken.getAuthToken(GuestAccount.GUID_PUBLIC, null, null, null, expiration);
}
if (auth != null && context.targetAccount != null && context.targetAccount != context.getAuthAccount()) {
auth.setProxyAuthToken(Provisioning.getInstance().getProxyAuthToken(context.targetAccount.getId(), null));
}
String authString = null;
try {
if (auth != null)
authString = auth.getEncoded();
} catch (AuthTokenException e) {
throw new ServletException("error generating the authToken", e);
}
Account targetAccount = context.targetAccount;
MailItem targetItem = context.target;
String uri = (String) context.req.getAttribute("requestedPath");
if (targetItem instanceof Mountpoint && ((Mountpoint) targetItem).getDefaultView() != MailItem.Type.APPOINTMENT) {
Mountpoint mp = (Mountpoint) targetItem;
Provisioning prov = Provisioning.getInstance();
targetAccount = prov.getAccountById(mp.getOwnerId());
Pair<Header[], HttpInputStream> remoteItem = UserServlet.getRemoteResourceAsStream((auth == null) ? null : auth.toZAuthToken(), mp.getTarget(), context.extraPath);
remoteItem.getSecond().close();
String remoteItemId = null;
String remoteItemType = null;
String remoteItemName = null;
String remoteItemPath = null;
for (Header h : remoteItem.getFirst()) if (h.getName().compareToIgnoreCase("X-Zimbra-ItemId") == 0)
remoteItemId = h.getValue();
else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemType") == 0)
remoteItemType = h.getValue();
else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemName") == 0)
remoteItemName = h.getValue();
else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemPath") == 0)
remoteItemPath = h.getValue();
context.req.setAttribute(ATTR_TARGET_ITEM_ID, remoteItemId);
context.req.setAttribute(ATTR_TARGET_ITEM_TYPE, remoteItemType);
context.req.setAttribute(ATTR_TARGET_ITEM_NAME, remoteItemName);
context.req.setAttribute(ATTR_TARGET_ITEM_PATH, remoteItemPath);
context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, mp.getColor());
context.req.setAttribute(ATTR_TARGET_ITEM_VIEW, mp.getDefaultView().toByte());
targetItem = null;
}
context.req.setAttribute(ATTR_INTERNAL_DISPATCH, "yes");
context.req.setAttribute(ATTR_REQUEST_URI, uri != null ? uri : context.req.getRequestURI());
context.req.setAttribute(ATTR_AUTH_TOKEN, authString);
context.req.setAttribute(ATTR_CSRF_ENABLED, auth.isCsrfTokenEnabled());
if (targetAccount != null) {
context.req.setAttribute(ATTR_TARGET_ACCOUNT_NAME, targetAccount.getName());
context.req.setAttribute(ATTR_TARGET_ACCOUNT_ID, targetAccount.getId());
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_TIME_ZONE, targetAccount.getAttr(Provisioning.A_zimbraPrefTimeZoneId));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_SKIN, targetAccount.getAttr(Provisioning.A_zimbraPrefSkin));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_LOCALE, targetAccount.getAttr(Provisioning.A_zimbraPrefLocale));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_FIRST_DAY_OF_WEEK, targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarFirstDayOfWeek));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_START, targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarDayHourStart));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_END, targetAccount.getAttr(Provisioning.A_zimbraPrefCalendarDayHourEnd));
} else {
// Useful when faking results - e.g. FREEBUSY html view for non-existent account
if (context.fakeTarget != null) {
context.req.setAttribute(ATTR_TARGET_ACCOUNT_NAME, context.fakeTarget.getAccount());
}
com.zimbra.cs.account.Cos defaultCos = Provisioning.getInstance().get(com.zimbra.common.account.Key.CosBy.name, Provisioning.DEFAULT_COS_NAME);
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_TIME_ZONE, defaultCos.getAttr(Provisioning.A_zimbraPrefTimeZoneId));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_SKIN, defaultCos.getAttr(Provisioning.A_zimbraPrefSkin));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_LOCALE, defaultCos.getAttr(Provisioning.A_zimbraPrefLocale));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_FIRST_DAY_OF_WEEK, defaultCos.getAttr(Provisioning.A_zimbraPrefCalendarFirstDayOfWeek));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_START, defaultCos.getAttr(Provisioning.A_zimbraPrefCalendarDayHourStart));
context.req.setAttribute(ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_END, defaultCos.getAttr(Provisioning.A_zimbraPrefCalendarDayHourEnd));
}
if (targetItem != null) {
context.req.setAttribute(ATTR_TARGET_ITEM_ID, targetItem.getId());
context.req.setAttribute(ATTR_TARGET_ITEM_PATH, targetItem.getPath());
context.req.setAttribute(ATTR_TARGET_ITEM_NAME, targetItem.getName());
context.req.setAttribute(ATTR_TARGET_ITEM_TYPE, targetItem.getType().toString());
context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, targetItem.getColor());
if (targetItem instanceof Folder) {
context.req.setAttribute(ATTR_TARGET_ITEM_VIEW, ((Folder) targetItem).getDefaultView().toString());
}
context.req.setAttribute(ATTR_TARGET_ACTION, context.getAction());
context.req.setAttribute(ATTR_TARGET_BODYPART, context.getBodypart());
context.req.setAttribute(ATTR_TARGET_COLOR, context.getColor());
context.req.setAttribute(ATTR_TARGET_DATE, context.getDate());
context.req.setAttribute(ATTR_TARGET_EX_COMP_NUM, context.getExCompNum());
context.req.setAttribute(ATTR_TARGET_EX_INV_ID, context.getExInvId());
context.req.setAttribute(ATTR_TARGET_FMT, context.getFmt());
context.req.setAttribute(ATTR_TARGET_FOLDER_IDS, context.getFolderIds());
context.req.setAttribute(ATTR_TARGET_IM_ID, context.getImId());
context.req.setAttribute(ATTR_TARGET_IM_PART, context.getImPart());
context.req.setAttribute(ATTR_TARGET_IM_XIM, context.getImXim());
context.req.setAttribute(ATTR_TARGET_INST_DURATION, context.getInstDuration());
context.req.setAttribute(ATTR_TARGET_INST_START_TIME, context.getInstStartTime());
context.req.setAttribute(ATTR_TARGET_INV_COMP_NUM, context.getInvCompNum());
context.req.setAttribute(ATTR_TARGET_INV_ID, context.getInvId());
context.req.setAttribute(ATTR_TARGET_NOTOOLBAR, context.getNotoolbar());
context.req.setAttribute(ATTR_TARGET_NUMDAYS, context.getNumdays());
context.req.setAttribute(ATTR_TARGET_PSTAT, context.getPstat());
context.req.setAttribute(ATTR_TARGET_REFRESH, context.getRefresh());
context.req.setAttribute(ATTR_TARGET_SKIN, context.getSkin());
context.req.setAttribute(ATTR_TARGET_SQ, context.getSq());
context.req.setAttribute(ATTR_TARGET_TZ, context.getTz());
context.req.setAttribute(ATTR_TARGET_USE_INSTANCE, context.getUseInstance());
context.req.setAttribute(ATTR_TARGET_XIM, context.getXim());
context.req.setAttribute(ATTR_TARGET_VIEW, context.getView());
} else {
context.req.setAttribute(ATTR_TARGET_ITEM_COLOR, Color.getMappedColor(null));
}
if (context.fakeTarget != null) {
// Override to avoid address harvesting
context.req.setAttribute(ATTR_TARGET_ITEM_PATH, context.fakeTarget.getPath());
context.req.setAttribute(ATTR_TARGET_ITEM_NAME, context.fakeTarget.getName());
}
String mailUrl = PATH_MAIN_CONTEXT;
if (WebSplitUtil.isZimbraServiceSplitEnabled()) {
mailUrl = Provisioning.getInstance().getLocalServer().getWebClientURL() + PATH_JSP_REST_PAGE;
HttpClient httpclient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient().build();
/*
* Retest the code with POST to check whether it works
HttpPost postMethod = new HttpPost(mailUrl);
Enumeration<String> attributeNames = context.req.getAttributeNames();
List<Part> parts = new ArrayList<Part>();
while(attributeNames.hasMoreElements())
{
String attrName = (String) attributeNames.nextElement();
String attrValue = context.req.getAttribute(attrName).toString();
Part part = new StringPart(attrName, attrValue);
parts.add(part);
}
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams()));
HttpClientUtil.executeMethod(httpclient, postMethod);
ByteUtil.copy(postMethod.getResponseBodyAsStream(), true, context.resp.getOutputStream(), true);
*/
Enumeration<String> attributeNames = context.req.getAttributeNames();
StringBuilder sb = new StringBuilder(mailUrl);
sb.append("?");
while (attributeNames.hasMoreElements()) {
String attrName = attributeNames.nextElement();
String attrValue = context.req.getAttribute(attrName).toString();
sb.append(attrName).append("=").append(HttpUtil.urlEscape(attrValue)).append("&");
}
HttpGet postMethod = new HttpGet(sb.toString());
postMethod.setHeader("Accept-Language", context.getLocale().getLanguage());
HttpResponse httpResp;
try {
httpResp = HttpClientUtil.executeMethod(httpclient, postMethod);
ByteUtil.copy(httpResp.getEntity().getContent(), true, context.resp.getOutputStream(), false);
} catch (HttpException e) {
throw new ServletException("error executing the request.", e);
}
} else {
try {
mailUrl = Provisioning.getInstance().getLocalServer().getMailURL();
} catch (Exception e) {
}
ServletContext targetContext = servlet.getServletConfig().getServletContext().getContext(mailUrl);
RequestDispatcher dispatcher = targetContext.getRequestDispatcher(PATH_JSP_REST_PAGE);
dispatcher.forward(context.req, context.resp);
}
}
use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.
the class CreateMountpoint method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Mailbox mbox = getRequestedMailbox(zsc);
OperationContext octxt = getOperationContext(zsc, context);
ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
Element t = request.getElement(MailConstants.E_MOUNT);
String name = t.getAttribute(MailConstants.A_NAME);
String view = t.getAttribute(MailConstants.A_DEFAULT_VIEW, null);
String flags = t.getAttribute(MailConstants.A_FLAGS, null);
byte color = (byte) t.getAttributeLong(MailConstants.A_COLOR, MailItem.DEFAULT_COLOR);
String rgb = t.getAttribute(MailConstants.A_RGB, null);
ItemId iidParent = new ItemId(t.getAttribute(MailConstants.A_FOLDER), zsc);
boolean fetchIfExists = t.getAttributeBool(MailConstants.A_FETCH_IF_EXISTS, false);
boolean reminderEnabled = t.getAttributeBool(MailConstants.A_REMINDER, false);
Account target = null;
String ownerId = t.getAttribute(MailConstants.A_ZIMBRA_ID, null);
if (ownerId == null) {
String ownerName = t.getAttribute(MailConstants.A_OWNER_NAME);
target = Provisioning.getInstance().get(AccountBy.name, ownerName, zsc.getAuthToken());
// prevent directory harvest attack, mask no such account as permission denied
if (target == null)
throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
ownerId = target.getId();
if (ownerId.equalsIgnoreCase(zsc.getRequestedAccountId()))
throw ServiceException.INVALID_REQUEST("cannot mount your own folder", null);
}
Element remote = fetchRemoteFolder(zsc, context, ownerId, (int) t.getAttributeLong(MailConstants.A_REMOTE_ID, -1), t.getAttribute(MailConstants.A_PATH, null));
int remoteId = new ItemId(remote.getAttribute(MailConstants.A_ID), zsc).getId();
String remoteUuid = remote.getAttribute(MailConstants.A_UUID, null);
if (view == null)
view = remote.getAttribute(MailConstants.A_DEFAULT_VIEW, null);
Mountpoint mpt;
try {
Color itemColor = rgb != null ? new Color(rgb) : new Color(color);
mpt = mbox.createMountpoint(octxt, iidParent.getId(), name, ownerId, remoteId, remoteUuid, MailItem.Type.of(view), Flag.toBitmask(flags), itemColor, reminderEnabled);
} catch (ServiceException se) {
if (se.getCode() == MailServiceException.ALREADY_EXISTS && fetchIfExists) {
Folder folder = mbox.getFolderByName(octxt, iidParent.getId(), name);
if (folder instanceof Mountpoint)
mpt = (Mountpoint) folder;
else
throw se;
} else {
throw se;
}
}
Element response = zsc.createElement(MailConstants.CREATE_MOUNTPOINT_RESPONSE);
if (mpt != null) {
Element eMount = ToXML.encodeMountpoint(response, ifmt, octxt, mpt);
// transfer folder counts and subfolders to the serialized mountpoint from the serialized target folder
ToXML.transferMountpointContents(eMount, remote);
}
return response;
}
use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.
the class GetFolder method encodeFolderNode.
private static Element encodeFolderNode(FolderNode node, Element parent, ItemIdFormatter ifmt, OperationContext octxt, boolean exposeAclAccessKey, int depth, MailItem.Type view, boolean traverse, List<ExpandableMountpoint> mounts) throws ServiceException {
Element eFolder;
Folder folder = node.mFolder;
if (folder != null) {
eFolder = ToXML.encodeFolder(parent, ifmt, octxt, folder, ToXML.NOTIFY_FIELDS, exposeAclAccessKey);
// if requested, fetch contents of mountpoints
if (traverse && mounts != null && folder instanceof Mountpoint && folder.getMailbox().hasFullAccess(octxt)) {
mounts.add(new ExpandableMountpoint(eFolder, (Mountpoint) folder, depth));
}
} else {
eFolder = parent.addNonUniqueElement(MailConstants.E_FOLDER).addAttribute(MailConstants.A_ID, ifmt.formatItemId(node.mId)).addAttribute(MailConstants.A_NAME, node.mName);
}
if (depth == 0) {
return eFolder;
}
int remainingDepth = depth > 0 ? depth - 1 : depth;
for (FolderNode subNode : node.mSubfolders) {
encodeFolderNode(subNode, eFolder, ifmt, octxt, exposeAclAccessKey, remainingDepth, view, traverse, mounts);
}
return eFolder;
}
Aggregations