Search in sources :

Example 11 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class ImapSessionManager method waitForWaitSetNotifications.

private void waitForWaitSetNotifications(ImapMailboxStore imapStore, FolderStore folder, ImapRemoteSession irs) {
    ZMailbox sessMboxStore = (ZMailbox) irs.getMailbox();
    if (sessMboxStore.isUsingSession()) {
        return;
    /* can rely on SOAP notifications */
    }
    LastChange zmboxLastKnownChange = sessMboxStore.getLastChange();
    if (irs.getLastKnownChangeId() >= zmboxLastKnownChange.getId()) {
        return;
    /* WaitSet based notifications at least as current as anything we've done */
    }
    long start = System.currentTimeMillis();
    ImapServerListener svrListener = imapStore.getServerListener(folder.getFolderItemIdentifier());
    if (svrListener == null) {
        return;
    }
    // Should be rare to have to wait for much of this time
    long timeout = LC.imap_max_time_to_wait_for_catchup_millis.longValue();
    if (zmboxLastKnownChange.getTime() != 0) {
        if (start - zmboxLastKnownChange.getTime() >= timeout) {
            ZimbraLog.imap.debug("ImapSessionManager.waitForWaitSetNotifications known a long time");
            return;
        }
        timeout = timeout - (start - zmboxLastKnownChange.getTime());
    }
    CountDownLatch doneSignal = new CountDownLatch(1);
    svrListener.addNotifyWhenCaughtUp(imapStore.getAccountId(), zmboxLastKnownChange.getId(), doneSignal);
    try {
        doneSignal.await(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ie) {
    }
    ZimbraLog.imap.debug("ImapSessionManager.waitForWaitSetNotifications folder=%s mboxCID=%s irsCID=%s timeout=%s %s", folder, zmboxLastKnownChange, irs.getLastKnownChangeId(), timeout, ZimbraLog.elapsedTime(start, System.currentTimeMillis()));
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) LastChange(com.zimbra.client.ZMailbox.LastChange) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class ImapSessionManager method getSessionForFolder.

/**
 * Choose the listener which knows about the most recent change, preferring ones associated with
 * a ZMailbox which is using a session, so that we know that our cache will be up to date to that
 * change in that case.
 */
private static ImapListener getSessionForFolder(int folderId, List<ImapListener> sessionList) {
    List<ImapListener> listeners = getSessionsForFolder(folderId, sessionList);
    if (listeners.isEmpty()) {
        return null;
    }
    int lastKnownChangeId = -1;
    ImapListener bestListener = null;
    boolean usingSession = false;
    for (ImapListener i4listener : listeners) {
        if (i4listener instanceof ImapRemoteSession) {
            ImapRemoteSession irs = (ImapRemoteSession) i4listener;
            ZMailbox zmbox = (ZMailbox) irs.getMailbox();
            int zmLastKnownChangeId = zmbox.getLastChangeID();
            if (zmLastKnownChangeId > lastKnownChangeId) {
                bestListener = i4listener;
                usingSession = zmbox.isUsingSession();
            } else if ((zmLastKnownChangeId == lastKnownChangeId) && !usingSession) {
                bestListener = i4listener;
                usingSession = zmbox.isUsingSession();
            }
        }
    }
    return bestListener != null ? bestListener : listeners.get(0);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox)

Example 13 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class Mailbox method processICalReplies.

private void processICalReplies(OperationContext octxt, ZVCalendar cal, String sender) throws ServiceException {
    // Reply from Outlook will usually have PRODID set to the following:
    // 
    // Outlook2007+ZCO: PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN
    // Outlook2010+ZCO: PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN
    // Outlook20xx+Exchange: PRODID:Microsoft Exchange Server 2007
    // (if Exchange is Exchange 2007; Exchange 2010 probably works similarly)
    // 
    // Lowest common denominator is "Microsoft" substring.
    String prodId = cal.getPropVal(ICalTok.PRODID, null);
    boolean fromOutlook = prodId != null && prodId.toLowerCase().contains("microsoft");
    AccountAddressMatcher acctMatcher = new AccountAddressMatcher(getAccount());
    List<Invite> components = Invite.createFromCalendar(getAccount(), null, cal, false);
    for (Invite inv : components) {
        String orgAddress;
        if (inv.hasOrganizer()) {
            ZOrganizer org = inv.getOrganizer();
            orgAddress = org.getAddress();
        } else {
            ZimbraLog.calendar.warn("No ORGANIZER found in REPLY.  Assuming current mailbox.");
            orgAddress = getAccount().getName();
        }
        if (acctMatcher.matches(orgAddress)) {
            // RECURRENCE-ID.
            if (fromOutlook && !inv.isAllDayEvent() && inv.hasRecurId()) {
                RecurId rid = inv.getRecurId();
                if (rid.getDt() != null && rid.getDt().hasZeroTime()) {
                    CalendarItem calItem = getCalendarItemByUid(octxt, inv.getUid());
                    if (calItem != null) {
                        Invite seriesInv = calItem.getDefaultInviteOrNull();
                        if (seriesInv != null) {
                            ParsedDateTime seriesDtStart = seriesInv.getStartTime();
                            if (seriesDtStart != null) {
                                ParsedDateTime fixedDt = seriesDtStart.cloneWithNewDate(rid.getDt());
                                RecurId fixedRid = new RecurId(fixedDt, rid.getRange());
                                ZimbraLog.calendar.debug("Fixed up invalid RECURRENCE-ID with zero time; before=[%s], after=[%s]", rid, fixedRid);
                                inv.setRecurId(fixedRid);
                            }
                        }
                    }
                }
            }
            processICalReply(octxt, inv, sender);
        } else {
            Account orgAccount = inv.getOrganizerAccount();
            // Unknown organizer
            if (orgAccount == null) {
                ZimbraLog.calendar.warn("Unknown organizer " + orgAddress + " in REPLY");
                continue;
            }
            if (Provisioning.onLocalServer(orgAccount)) {
                // Run in the context of organizer's mailbox.
                Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(orgAccount);
                OperationContext orgOctxt = new OperationContext(mbox);
                mbox.processICalReply(orgOctxt, inv, sender);
            } else {
                // Organizer's mailbox is on a remote server.
                String uri = AccountUtil.getSoapUri(orgAccount);
                if (uri == null) {
                    ZimbraLog.calendar.warn("Unable to determine URI for organizer account %s", orgAddress);
                    continue;
                }
                try {
                    // TODO: Get the iCalendar data from the
                    // MIME part since we already have it.
                    String ical;
                    StringWriter sr = null;
                    try {
                        sr = new StringWriter();
                        inv.setMethod(ICalTok.REPLY.toString());
                        inv.newToICalendar(true).toICalendar(sr);
                        ical = sr.toString();
                    } finally {
                        if (sr != null) {
                            sr.close();
                        }
                    }
                    Options options = new Options();
                    AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(getAuthToken(octxt));
                    options.setAuthToken(authToken.toZAuthToken());
                    options.setTargetAccount(orgAccount.getName());
                    options.setTargetAccountBy(AccountBy.name);
                    options.setUri(uri);
                    options.setNoSession(true);
                    ZMailbox zmbox = ZMailbox.getMailbox(options);
                    zmbox.setAccountId(orgAccount.getId());
                    zmbox.iCalReply(ical, sender);
                } catch (IOException e) {
                    throw ServiceException.FAILURE("Error while posting REPLY to organizer mailbox host", e);
                }
            }
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) ParsedMessageOptions(com.zimbra.cs.mime.ParsedMessageOptions) Options(com.zimbra.client.ZMailbox.Options) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) RecurId(com.zimbra.cs.mailbox.calendar.RecurId) IOException(java.io.IOException) SetCalendarItem(com.zimbra.cs.redolog.op.SetCalendarItem) ZMailbox(com.zimbra.client.ZMailbox) RenameMailbox(com.zimbra.cs.redolog.op.RenameMailbox) DbMailbox(com.zimbra.cs.db.DbMailbox) CreateMailbox(com.zimbra.cs.redolog.op.CreateMailbox) ZMailbox(com.zimbra.client.ZMailbox) DeleteMailbox(com.zimbra.cs.redolog.op.DeleteMailbox) StringWriter(java.io.StringWriter) AccountAddressMatcher(com.zimbra.cs.util.AccountUtil.AccountAddressMatcher) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ParsedDateTime(com.zimbra.common.calendar.ParsedDateTime) CreateInvite(com.zimbra.cs.redolog.op.CreateInvite) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 14 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class Mailbox method refreshMountpoint.

/**
 * Updates the remote owner and item id stored in the mountpoint to match the current location of the
 * target folder.  The target folder is identified by the remote UUID stored in the mountpoint's metadata.
 * @param octxt
 * @param mountpointId item id of the Mountpoint
 * @return
 * @throws ServiceException
 */
public Mountpoint refreshMountpoint(OperationContext octxt, int mountpointId) throws ServiceException {
    Mountpoint mp = getMountpointById(octxt, mountpointId);
    Provisioning prov = Provisioning.getInstance();
    ShareLocator shloc = prov.getShareLocatorById(mp.getRemoteUuid());
    if (shloc == null || mp.getOwnerId().equalsIgnoreCase(shloc.getShareOwnerAccountId())) {
        // Share apparently did not move.
        return mp;
    }
    // Look up remote folder by UUID to discover the new numeric id.
    Account shareOwner = Provisioning.getInstance().get(Key.AccountBy.id, shloc.getShareOwnerAccountId());
    AuthToken at = AuthToken.getCsrfUnsecuredAuthToken(octxt.getAuthToken());
    String pxyAuthToken = Provisioning.onLocalServer(shareOwner) ? null : at.getProxyAuthToken();
    ZAuthToken zat = null;
    if (pxyAuthToken == null) {
        zat = at.toZAuthToken();
        zat.resetProxyAuthToken();
    } else {
        zat = new ZAuthToken(pxyAuthToken);
    }
    ZMailbox.Options zoptions = new ZMailbox.Options(zat, AccountUtil.getSoapUri(shareOwner));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(shareOwner.getId());
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
    zmbx.setName(shareOwner.getName());
    /* need this when logging in using another user's auth */
    ZFolder zfolder = zmbx.getFolderByUuid(shloc.getUuid());
    if (zfolder != null) {
        ItemId fid = new ItemId(zfolder.getId(), shareOwner.getId());
        return refreshMountpoint(octxt, mountpointId, shareOwner.getId(), fid.getId());
    } else {
        return null;
    }
}
Also used : Account(com.zimbra.cs.account.Account) ParsedMessageOptions(com.zimbra.cs.mime.ParsedMessageOptions) Options(com.zimbra.client.ZMailbox.Options) ZMailbox(com.zimbra.client.ZMailbox) Options(com.zimbra.client.ZMailbox.Options) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ZFolder(com.zimbra.client.ZFolder) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) ShareLocator(com.zimbra.cs.account.ShareLocator)

Example 15 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class Mailbox method getRemoteCalItemByUID.

public com.zimbra.soap.mail.type.CalendarItemInfo getRemoteCalItemByUID(Account ownerAccount, String uid, boolean includeInvites, boolean includeContent) throws ServiceException {
    Options options = new Options();
    AuthToken authToken = AuthToken.getCsrfUnsecuredAuthToken(getAuthToken(getOperationContext()));
    options.setAuthToken(authToken.toZAuthToken());
    options.setTargetAccount(getAccount().getName());
    options.setTargetAccountBy(AccountBy.name);
    options.setUri(AccountUtil.getSoapUri(ownerAccount));
    options.setNoSession(true);
    ZMailbox zmbox = ZMailbox.getMailbox(options);
    try {
        return zmbox.getRemoteCalItemByUID(ownerAccount.getId(), uid, includeInvites, includeContent);
    } catch (ServiceException e) {
        String exceptionCode = e.getCode();
        if (exceptionCode.equals(AccountServiceException.NO_SUCH_ACCOUNT) || exceptionCode.equals(MailServiceException.NO_SUCH_CALITEM)) {
            ZimbraLog.calendar.debug("Either remote acct or calendar item not found [%s]", exceptionCode);
        } else {
            ZimbraLog.calendar.debug("Unexpected exception thrown when getting remote calendar item - ignoring", e);
        }
        return null;
    }
}
Also used : ParsedMessageOptions(com.zimbra.cs.mime.ParsedMessageOptions) Options(com.zimbra.client.ZMailbox.Options) ZMailbox(com.zimbra.client.ZMailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken)

Aggregations

ZMailbox (com.zimbra.client.ZMailbox)383 Test (org.junit.Test)288 ZFolder (com.zimbra.client.ZFolder)90 ZMessage (com.zimbra.client.ZMessage)82 Mailbox (com.zimbra.cs.mailbox.Mailbox)61 Account (com.zimbra.cs.account.Account)60 ServiceException (com.zimbra.common.service.ServiceException)55 ArrayList (java.util.ArrayList)38 IOException (java.io.IOException)35 MessageData (com.zimbra.cs.mailclient.imap.MessageData)28 ZMountpoint (com.zimbra.client.ZMountpoint)26 Message (com.zimbra.cs.mailbox.Message)24 HashMap (java.util.HashMap)23 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)21 ZTag (com.zimbra.client.ZTag)21 SoapFaultException (com.zimbra.common.soap.SoapFaultException)21 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)21 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)20 HttpClient (org.apache.http.client.HttpClient)19 ZSearchParams (com.zimbra.client.ZSearchParams)18