Search in sources :

Example 16 with ZMailbox

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

the class MailItemResource method getZMailbox.

private static ZMailbox getZMailbox(DavContext ctxt, Collection col) throws ServiceException {
    AuthToken authToken = AuthProvider.getAuthToken(ctxt.getAuthAccount());
    Account acct = Provisioning.getInstance().getAccountById(col.getItemId().getAccountId());
    ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(acct));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(acct.getId());
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
    if (zmbx != null) {
        zmbx.setName(acct.getName());
    /* need this when logging in using another user's auth */
    }
    return zmbx;
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) AuthToken(com.zimbra.cs.account.AuthToken)

Example 17 with ZMailbox

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

the class MailItemResource method deleteDestinationItem.

private void deleteDestinationItem(DavContext ctxt, Collection dest, int id) throws ServiceException, DavException {
    Mailbox mbox = getMailbox(ctxt);
    if (dest.getItemId().belongsTo(mbox)) {
        mbox.delete(ctxt.getOperationContext(), id, MailItem.Type.UNKNOWN, null);
    } else {
        ZMailbox zmbx = getZMailbox(ctxt, dest);
        ItemId itemId = new ItemId(dest.getItemId().getAccountId(), id);
        zmbx.deleteItem(itemId.toString(), null);
    }
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ItemId(com.zimbra.cs.service.util.ItemId)

Example 18 with ZMailbox

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

the class RemoteCollection method getRemoteMailbox.

static ZMailbox getRemoteMailbox(ZAuthToken zat, String ownerId) throws ServiceException {
    Account target = Provisioning.getInstance().get(Key.AccountBy.id, ownerId);
    if (target == null)
        return null;
    ZMailbox.Options zoptions = new ZMailbox.Options(zat, AccountUtil.getSoapUri(target));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(ownerId);
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
    if (zmbx != null) {
        zmbx.setName(target.getName());
    /* need this when logging in using another user's auth */
    }
    return zmbx;
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox)

Example 19 with ZMailbox

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

the class FilterUtil method addMessage.

/**
 * Adds a message to the given folder.  Handles both local folders and mountpoints.
 * @return the id of the new message, or <tt>null</tt> if it was a duplicate
 */
public static ItemId addMessage(DeliveryContext context, Mailbox mbox, ParsedMessage pm, String recipient, String folderPath, boolean noICal, int flags, String[] tags, int convId, OperationContext octxt) throws ServiceException {
    // Do initial lookup.
    Pair<Folder, String> folderAndPath = mbox.getFolderByPathLongestMatch(octxt, Mailbox.ID_FOLDER_USER_ROOT, folderPath);
    Folder folder = folderAndPath.getFirst();
    String remainingPath = folderAndPath.getSecond();
    ZimbraLog.filter.debug("Attempting to file to %s, remainingPath=%s.", folder, remainingPath);
    if (folder instanceof Mountpoint) {
        Mountpoint mountpoint = (Mountpoint) folder;
        ZMailbox remoteMbox = getRemoteZMailbox(mbox, mountpoint);
        // Look up remote folder.
        String remoteAccountId = mountpoint.getOwnerId();
        ItemId id = mountpoint.getTarget();
        ZFolder remoteFolder = remoteMbox.getFolderById(id.toString());
        if (remoteFolder != null) {
            if (remainingPath != null) {
                remoteFolder = remoteFolder.getSubFolderByPath(remainingPath);
                if (remoteFolder == null) {
                    String msg = String.format("Subfolder %s of mountpoint %s does not exist.", remainingPath, mountpoint.getName());
                    throw ServiceException.FAILURE(msg, null);
                }
            }
        }
        // File to remote folder.
        if (remoteFolder != null) {
            byte[] content;
            try {
                content = pm.getRawData();
            } catch (Exception e) {
                throw ServiceException.FAILURE("Unable to get message content", e);
            }
            String msgId = remoteMbox.addMessage(remoteFolder.getId(), com.zimbra.cs.mailbox.Flag.toString(flags), null, 0, content, false);
            return new ItemId(msgId, remoteAccountId);
        } else {
            String msg = String.format("Unable to find remote folder %s for mountpoint %s.", remainingPath, mountpoint.getName());
            throw ServiceException.FAILURE(msg, null);
        }
    } else {
        if (!StringUtil.isNullOrEmpty(remainingPath)) {
            // Only part of the folder path matched.  Auto-create the remaining path.
            ZimbraLog.filter.info("Could not find folder %s.  Automatically creating it.", folderPath);
            folder = mbox.createFolder(octxt, folderPath, new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
        }
        try {
            DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(noICal);
            dopt.setFlags(flags).setTags(tags).setConversationId(convId).setRecipientEmail(recipient);
            Message msg = mbox.addMessage(octxt, pm, dopt, context);
            if (msg == null) {
                return null;
            } else {
                return new ItemId(msg);
            }
        } catch (IOException e) {
            throw ServiceException.FAILURE("Unable to add message", e);
        }
    }
}
Also used : ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Message(com.zimbra.cs.mailbox.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) Folder(com.zimbra.cs.mailbox.Folder) ZFolder(com.zimbra.client.ZFolder) ItemId(com.zimbra.cs.service.util.ItemId) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) SyntaxException(org.apache.jsieve.exception.SyntaxException) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions)

Example 20 with ZMailbox

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

the class ImapCredentials method getImapMailboxStore.

protected ImapMailboxStore getImapMailboxStore() throws ServiceException {
    if (mStore != null) {
        return mStore;
    }
    if (mIsLocal && !LC.imap_always_use_remote_store.booleanValue() && ImapDaemon.isRunningImapInsideMailboxd()) {
        ZimbraLog.imap.debug("ImapCredentials returning local mailbox store for %s", mAccountId);
        return new LocalImapMailboxStore(MailboxManager.getInstance().getMailboxByAccountId(mAccountId));
    }
    try {
        Account acct = getAccount();
        ZMailbox.Options options = new ZMailbox.Options(AuthProvider.getAuthToken(acct).getEncoded(), AccountUtil.getSoapUri(acct));
        /* getting by ID avoids failed GetInfo SOAP requests trying to determine ID before auth setup. */
        options.setTargetAccountBy(AccountBy.id);
        options.setTargetAccount(acct.getId());
        options.setNoSession(false);
        options.setUserAgent("zclient-imap", SystemUtil.getProductVersion());
        options.setNotificationFormat(NotificationFormat.IMAP);
        options.setAlwaysRefreshFolders(true);
        ZMailbox store = ZMailbox.getMailbox(options);
        store.setAccountId(acct.getId());
        store.setName(acct.getName());
        store.setAuthName(acct.getName());
        mStore = ImapMailboxStore.get(store, mAccountId);
        ZimbraLog.imap.debug("Registering listener with ZMailbox for '%s' [id=%s]", acct.getName(), mAccountId);
        store.addEventHandler(zMailboxEventHandler);
        return mStore;
    } catch (AuthTokenException ate) {
        throw ServiceException.FAILURE("error generating auth token", ate);
    }
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) AuthTokenException(com.zimbra.cs.account.AuthTokenException)

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