Search in sources :

Example 6 with MailboxManager

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

the class ModifyPrefsTest method init.

@BeforeClass
public static void init() throws Exception {
    MailboxTestUtil.initServer();
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = Maps.newHashMap();
    prov.createDomain("zimbra.com", attrs);
    attrs = Maps.newHashMap();
    prov.createAccount("test@zimbra.com", "secret", attrs);
    MailboxManager.setInstance(new MailboxManager() {

        @Override
        protected Mailbox instantiateMailbox(MailboxData data) {
            return new Mailbox(data) {

                @Override
                public MailSender getMailSender() {
                    return new MailSender() {

                        @Override
                        protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) throws SafeMessagingException, IOException {
                            try {
                                return Arrays.asList(getRecipients(mm));
                            } catch (Exception e) {
                                return Collections.emptyList();
                            }
                        }
                    };
                }
            };
        }
    });
    L10nUtil.setMsgClassLoader("../store-conf/conf/msgs");
}
Also used : MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) MailSender(com.zimbra.cs.mailbox.MailSender) IOException(java.io.IOException) Provisioning(com.zimbra.cs.account.Provisioning) IOException(java.io.IOException) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Collection(java.util.Collection) BeforeClass(org.junit.BeforeClass)

Example 7 with MailboxManager

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

the class CreateDataSource method doZMGAppProvisioningIfReq.

private static synchronized boolean doZMGAppProvisioningIfReq(ZimbraSoapContext zsc, Provisioning prov, Element eDataSource, DataSourceType type) throws ServiceException {
    String acctId = zsc.getAuthtokenAccountId();
    AuthToken authToken = zsc.getAuthToken();
    if (authToken.isZMGAppBootstrap() && prov.getAccountById(acctId) == null) {
        // test the data source to make sure it is a valid one
        TestDataSource.testDataSourceConnection(prov, eDataSource, type, null);
        Account acct = prov.autoProvZMGAppAccount(acctId, authToken.getDigest());
        MailboxManager mailboxManager = MailboxManager.getInstance();
        if (mailboxManager.getMailboxByAccountId(acctId, false) == null) {
            try {
                mailboxManager.createMailbox(null, acct);
            } catch (ServiceException e) {
                // Rollback account creation
                prov.deleteAccount(acctId);
                throw e;
            }
        }
        return true;
    }
    return false;
}
Also used : Account(com.zimbra.cs.account.Account) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) ServiceException(com.zimbra.common.service.ServiceException) AuthToken(com.zimbra.cs.account.AuthToken)

Example 8 with MailboxManager

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

the class SomeAccountsWaitSet method addAccounts.

synchronized List<WaitSetError> addAccounts(List<WaitSetAccount> wsas) {
    List<WaitSetError> errors = new ArrayList<WaitSetError>();
    for (WaitSetAccount wsa : wsas) {
        if (!mSessions.containsKey(wsa.getAccountId())) {
            // add the account to our session list
            mSessions.put(wsa.getAccountId(), wsa);
            // create the Session, if necessary, to listen to the requested mailbox
            try {
                // if there is a sync token, then we need to check to see if the
                // token is up-to-date...which means we have to fetch the mailbox.  Otherwise,
                // we don't have to fetch the mailbox.
                MailboxManager.FetchMode fetchMode = MailboxManager.FetchMode.AUTOCREATE;
                if (wsa.getLastKnownSyncToken() == null)
                    fetchMode = MailboxManager.FetchMode.ONLY_IF_CACHED;
                //
                // THIS CALL MIGHT REGISTER THE SESSION (via the MailboxManager notification --> mailboxLoaded() callback!
                //
                Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(wsa.getAccountId(), fetchMode);
                if (mbox != null) {
                    WaitSetError error = initializeWaitSetSession(wsa, mbox);
                    if (error != null) {
                        errors.add(error);
                    }
                }
            } catch (ServiceException e) {
                if (e.getCode() == AccountServiceException.NO_SUCH_ACCOUNT) {
                    errors.add(new WaitSetError(wsa.getAccountId(), WaitSetError.Type.NO_SUCH_ACCOUNT));
                } else if (e.getCode() == ServiceException.WRONG_HOST) {
                    errors.add(new WaitSetError(wsa.getAccountId(), WaitSetError.Type.WRONG_HOST_FOR_ACCOUNT));
                } else {
                    errors.add(new WaitSetError(wsa.getAccountId(), WaitSetError.Type.ERROR_LOADING_MAILBOX));
                }
                mSessions.remove(wsa);
            }
        } else {
            errors.add(new WaitSetError(wsa.getAccountId(), WaitSetError.Type.ALREADY_IN_SET_DURING_ADD));
        }
    }
    return errors;
}
Also used : MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) FetchMode(com.zimbra.cs.mailbox.MailboxManager.FetchMode) ArrayList(java.util.ArrayList)

Example 9 with MailboxManager

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

the class GetMiniCal method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    Account authAcct = getAuthenticatedAccount(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    Element response = getResponseElement(zsc);
    long rangeStart = request.getAttributeLong(MailConstants.A_CAL_START_TIME);
    long rangeEnd = request.getAttributeLong(MailConstants.A_CAL_END_TIME);
    List<ItemId> folderIids = new ArrayList<ItemId>();
    for (Iterator<Element> foldersIter = request.elementIterator(MailConstants.E_FOLDER); foldersIter.hasNext(); ) {
        Element fElem = foldersIter.next();
        ItemId iidFolder = new ItemId(fElem.getAttribute(MailConstants.A_ID), zsc);
        folderIids.add(iidFolder);
    }
    ICalTimeZone tz = parseTimeZone(request);
    if (tz == null)
        // requestor's time zone, not mailbox owner's
        tz = Util.getAccountTimeZone(authAcct);
    TreeSet<String> busyDates = new TreeSet<String>();
    Provisioning prov = Provisioning.getInstance();
    MailboxManager mboxMgr = MailboxManager.getInstance();
    Server localServer = prov.getLocalServer();
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    Map<ItemId, Resolved> resolved = resolveMountpoints(octxt, mbox, folderIids);
    Map<ItemId, ItemId> /* requested iid */
    reverseMap = new HashMap<ItemId, ItemId>();
    for (Map.Entry<ItemId, Resolved> entry : resolved.entrySet()) {
        ItemId requestedIid = entry.getKey();
        Resolved res = entry.getValue();
        if (res.error == null) {
            reverseMap.put(res.iid, requestedIid);
        } else {
            addError(response, ifmt.formatItemId(requestedIid), res.error.getCode(), res.error.getMessage());
        }
    }
    Map<Server, Map<String, List<Integer>>> /* folder ids */
    groupedByServer = Search.groupByServer(groupFoldersByAccount(resolved));
    // Look up in calendar cache first.
    if (LC.calendar_cache_enabled.booleanValue()) {
        CalSummaryCache calCache = CalendarCacheManager.getInstance().getSummaryCache();
        Calendar cal = new GregorianCalendar(tz);
        for (Iterator<Map.Entry<Server, Map<String, List<Integer>>>> serverIter = groupedByServer.entrySet().iterator(); serverIter.hasNext(); ) {
            Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry = serverIter.next();
            Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
            // for each account
            for (Iterator<Map.Entry<String, List<Integer>>> acctIter = accountFolders.entrySet().iterator(); acctIter.hasNext(); ) {
                Map.Entry<String, List<Integer>> acctEntry = acctIter.next();
                String acctId = acctEntry.getKey();
                List<Integer> folderIds = acctEntry.getValue();
                // for each folder
                for (Iterator<Integer> iterFolderId = folderIds.iterator(); iterFolderId.hasNext(); ) {
                    int folderId = iterFolderId.next();
                    try {
                        CalendarDataResult result = calCache.getCalendarSummary(octxt, acctId, folderId, MailItem.Type.APPOINTMENT, rangeStart, rangeEnd, true);
                        if (result != null) {
                            // Found data in cache.
                            iterFolderId.remove();
                            addBusyDates(cal, result.data, rangeStart, rangeEnd, busyDates);
                        }
                    } catch (ServiceException e) {
                        iterFolderId.remove();
                        ItemId iid = new ItemId(acctId, folderId);
                        // Error must mention folder id requested by client.
                        ItemId reqIid = reverseMap.get(iid);
                        if (reqIid != null) {
                            ZimbraLog.calendar.warn("Error accessing calendar folder " + ifmt.formatItemId(reqIid), e);
                            addError(response, ifmt.formatItemId(reqIid), e.getCode(), e.getMessage());
                        } else {
                            ZimbraLog.calendar.warn("Error accessing calendar folder; resolved id=" + ifmt.formatItemId(iid) + " (missing reverse mapping)", e);
                            addError(response, ifmt.formatItemId(iid), e.getCode(), e.getMessage());
                        }
                    }
                }
                if (folderIds.isEmpty())
                    acctIter.remove();
            }
            if (accountFolders.isEmpty())
                serverIter.remove();
        }
    }
    // For any remaining calendars, we have to get the data the hard way.
    for (Map.Entry<Server, Map<String, List<Integer>>> serverMapEntry : groupedByServer.entrySet()) {
        Server server = serverMapEntry.getKey();
        Map<String, List<Integer>> accountFolders = serverMapEntry.getValue();
        if (server.equals(localServer)) {
            // local server
            for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
                String acctId = entry.getKey();
                List<Integer> folderIds = entry.getValue();
                Account targetAcct = prov.get(AccountBy.id, acctId);
                if (targetAcct == null) {
                    ZimbraLog.calendar.warn("Skipping unknown account " + acctId + " during minical search");
                    continue;
                }
                Mailbox targetMbox = mboxMgr.getMailboxByAccount(targetAcct);
                for (int folderId : folderIds) {
                    try {
                        doLocalFolder(octxt, tz, targetMbox, folderId, rangeStart, rangeEnd, busyDates);
                    } catch (ServiceException e) {
                        ItemId iid = new ItemId(acctId, folderId);
                        // Error must mention folder id requested by client.
                        ItemId reqIid = reverseMap.get(iid);
                        if (reqIid != null) {
                            ZimbraLog.calendar.warn("Error accessing calendar folder " + ifmt.formatItemId(reqIid), e);
                            addError(response, ifmt.formatItemId(reqIid), e.getCode(), e.getMessage());
                        } else {
                            ZimbraLog.calendar.warn("Error accessing calendar folder; resolved id=" + ifmt.formatItemId(iid) + " (missing reverse mapping)", e);
                            addError(response, ifmt.formatItemId(iid), e.getCode(), e.getMessage());
                        }
                    }
                }
            }
        } else {
            // remote server
            // mail service soap requests want to see a target account
            String nominalTargetAcctId = null;
            List<String> folderList = new ArrayList<String>();
            for (Map.Entry<String, List<Integer>> entry : accountFolders.entrySet()) {
                String acctId = entry.getKey();
                if (nominalTargetAcctId == null)
                    nominalTargetAcctId = acctId;
                ItemIdFormatter ifmtRemote = new ItemIdFormatter(authAcct.getId(), acctId, false);
                List<Integer> folderIds = entry.getValue();
                for (int folderId : folderIds) {
                    folderList.add(ifmtRemote.formatItemId(folderId));
                }
            }
            doRemoteFolders(zsc, nominalTargetAcctId, folderList, rangeStart, rangeEnd, busyDates, response, reverseMap, ifmt);
        }
    }
    for (String datestamp : busyDates) {
        Element dateElem = response.addElement(MailConstants.E_CAL_MINICAL_DATE);
        dateElem.setText(datestamp);
    }
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) CalendarDataResult(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache.CalendarDataResult) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) OperationContext(com.zimbra.cs.mailbox.OperationContext) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) HashMap(java.util.HashMap) Map(java.util.Map) IcalXmlStrMap(com.zimbra.cs.mailbox.calendar.IcalXmlStrMap) ICalTimeZone(com.zimbra.common.calendar.ICalTimeZone) CalSummaryCache(com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache)

Example 10 with MailboxManager

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

the class IndexEditor method reIndexAll.

public void reIndexAll() {
    MailboxManager mmgr;
    try {
        mmgr = MailboxManager.getInstance();
    } catch (ServiceException e) {
        ZimbraLog.index.error("could not retrieve mailbox manager; aborting reindex", e);
        return;
    }
    int[] ids = mmgr.getMailboxIds();
    for (int i = 0; i < ids.length; i++) {
        mLog.info("Mailbox " + ids[i] + "\n");
        try {
            Mailbox mbx = mmgr.getMailboxById(ids[i]);
            mbx.index.startReIndex();
        } catch (ServiceException e) {
            mLog.info("Exception ReIndexing " + ids[i], e);
        }
    }
}
Also used : MailboxManager(com.zimbra.cs.mailbox.MailboxManager) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox)

Aggregations

MailboxManager (com.zimbra.cs.mailbox.MailboxManager)10 Mailbox (com.zimbra.cs.mailbox.Mailbox)9 ServiceException (com.zimbra.common.service.ServiceException)7 Provisioning (com.zimbra.cs.account.Provisioning)6 MailSender (com.zimbra.cs.mailbox.MailSender)4 MailboxData (com.zimbra.cs.mailbox.Mailbox.MailboxData)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 MimeMessage (javax.mail.internet.MimeMessage)4 Account (com.zimbra.cs.account.Account)3 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)3 ZMailbox (com.zimbra.client.ZMailbox)2 Server (com.zimbra.cs.account.Server)2 FetchMode (com.zimbra.cs.mailbox.MailboxManager.FetchMode)2 CalSummaryCache (com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache)2 CalendarDataResult (com.zimbra.cs.mailbox.calendar.cache.CalSummaryCache.CalendarDataResult)2 ItemId (com.zimbra.cs.service.util.ItemId)2 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)2 IOException (java.io.IOException)2