Search in sources :

Example 41 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class GetAllConfig method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    Config config = prov.getConfig();
    AdminAccessControl aac = checkRight(zsc, context, config, AdminRight.PR_ALWAYS_ALLOW);
    Element response = zsc.createElement(AdminConstants.GET_ALL_CONFIG_RESPONSE);
    encodeConfig(response, config, null, aac.getAttrRightChecker(config));
    return response;
}
Also used : ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Config(com.zimbra.cs.account.Config) Element(com.zimbra.common.soap.Element) Provisioning(com.zimbra.cs.account.Provisioning)

Example 42 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class ExportAndDeleteItems method handle.

@Override
public Element handle(Element requst, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
    // Parse request.
    ExportAndDeleteItemsRequest req = zsc.elementToJaxb(requst);
    ExportAndDeleteMailboxSpec mailbox = req.getMailbox();
    if (mailbox == null) {
        throw ServiceException.INVALID_REQUEST("empty mbox id", null);
    }
    Mailbox mbox = MailboxManager.getInstance().getMailboxById(mailbox.getId());
    Multimap<Integer, Integer> idRevs = HashMultimap.create();
    for (ExportAndDeleteItemSpec item : mailbox.getItems()) {
        idRevs.put(item.getId(), item.getVersion());
    }
    String dirPath = req.getExportDir();
    String prefix = req.getExportFilenamePrefix();
    // Lock the mailbox, to make sure that another thread doesn't modify the items we're exporting/deleting.
    mbox.lock.lock();
    try {
        DbConnection conn = null;
        try {
            conn = DbPool.getConnection();
            if (dirPath != null) {
                File exportDir = new File(dirPath);
                if (!exportDir.isDirectory()) {
                    DbPool.quietClose(conn);
                    throw ServiceException.INVALID_REQUEST(dirPath + " is not a directory", null);
                }
                String filePath = makePath(dirPath, DbMailItem.TABLE_MAIL_ITEM, prefix);
                export(conn, mbox, DbMailItem.TABLE_MAIL_ITEM, "id", idRevs, filePath);
                filePath = makePath(dirPath, DbMailItem.TABLE_MAIL_ITEM_DUMPSTER, prefix);
                export(conn, mbox, DbMailItem.TABLE_MAIL_ITEM_DUMPSTER, "id", idRevs, filePath);
                filePath = makePath(dirPath, DbMailItem.TABLE_REVISION, prefix);
                export(conn, mbox, DbMailItem.TABLE_REVISION, "item_id", idRevs, filePath);
                filePath = makePath(dirPath, DbMailItem.TABLE_REVISION_DUMPSTER, prefix);
                export(conn, mbox, DbMailItem.TABLE_REVISION_DUMPSTER, "item_id", idRevs, filePath);
                filePath = makePath(dirPath, DbMailItem.TABLE_APPOINTMENT, prefix);
                export(conn, mbox, DbMailItem.TABLE_APPOINTMENT, "item_id", idRevs, filePath);
                filePath = makePath(dirPath, DbMailItem.TABLE_APPOINTMENT_DUMPSTER, prefix);
                export(conn, mbox, DbMailItem.TABLE_APPOINTMENT_DUMPSTER, "item_id", idRevs, filePath);
            }
            // delete item from mail_item and revision table
            for (Integer itemId : idRevs.keySet()) {
                Collection<Integer> revs = idRevs.get(itemId);
                for (int rev : revs) {
                    if (rev == 0) {
                        // delete all revisions to make sure we delete all blobs
                        List<MailItem> list = null;
                        try {
                            list = mbox.getAllRevisions(null, itemId, MailItem.Type.UNKNOWN);
                        } catch (NoSuchItemException ex) {
                            // exception happens when we try to delete a mail_item which is already in mail_item_dumpster
                            continue;
                        }
                        for (MailItem item : list) {
                            if (item.getType() == MailItem.Type.DOCUMENT) {
                                mbox.purgeRevision(null, itemId, item.getVersion(), false);
                            }
                        }
                        mbox.delete(null, itemId, MailItem.Type.UNKNOWN, null);
                        break;
                    } else if (!revs.contains(0)) {
                        try {
                            mbox.purgeRevision(null, itemId, rev, false);
                        } catch (NoSuchItemException ex) {
                            // exception happens when we try to delete a revision which is already in revision_dumpster
                            continue;
                        }
                    }
                }
            }
            // Delete items from mail_item_dumpster & revision_dumpster tables just
            // incase moved to dumpster tables
            DbBlobConsistency.delete(conn, mbox, idRevs);
        } finally {
            conn.commit();
            DbPool.quietClose(conn);
        }
    } finally {
        mbox.lock.release();
    }
    return zsc.createElement(AdminConstants.EXPORT_AND_DELETE_ITEMS_RESPONSE);
}
Also used : ExportAndDeleteItemSpec(com.zimbra.soap.admin.type.ExportAndDeleteItemSpec) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) MailItem(com.zimbra.cs.mailbox.MailItem) DbMailItem(com.zimbra.cs.db.DbMailItem) ExportAndDeleteItemsRequest(com.zimbra.soap.admin.message.ExportAndDeleteItemsRequest) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ExportAndDeleteMailboxSpec(com.zimbra.soap.admin.type.ExportAndDeleteMailboxSpec) File(java.io.File)

Example 43 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class CreateGalSyncAccount method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    CreateGalSyncAccountRequest cgaRequest = zsc.elementToJaxb(request);
    String name = cgaRequest.getName();
    String domainStr = cgaRequest.getDomain();
    GalMode type = cgaRequest.getType();
    AccountSelector acctSelector = cgaRequest.getAccount();
    AccountBy acctBy = acctSelector.getBy();
    String acctValue = acctSelector.getKey();
    String password = cgaRequest.getPassword();
    String folder = cgaRequest.getFolder();
    String mailHost = cgaRequest.getMailHost();
    Domain domain = prov.getDomainByName(domainStr);
    if (domain == null) {
        throw AccountServiceException.NO_SUCH_DOMAIN(domainStr);
    }
    Account account = null;
    try {
        account = prov.get(acctBy.toKeyAccountBy(), acctValue, zsc.getAuthToken());
    } catch (ServiceException se) {
        ZimbraLog.gal.warn("error checking GalSyncAccount", se);
    }
    // create the system account if not already exists.
    if (account == null) {
        if (acctBy != AccountBy.name) {
            throw AccountServiceException.NO_SUCH_ACCOUNT(acctValue);
        }
        // there should be one gal sync account per domain per mailhost
        for (String acctId : domain.getGalAccountId()) {
            Account acct = prov.getAccountById(acctId);
            if ((acct != null) && (acct.getMailHost().equals(mailHost))) {
                throw AccountServiceException.ACCOUNT_EXISTS(acct.getName());
            }
        }
        // XXX revisit
        checkDomainRightByEmail(zsc, acctValue, Admin.R_createAccount);
        Map<String, Object> accountAttrs = new HashMap<String, Object>();
        StringUtil.addToMultiMap(accountAttrs, Provisioning.A_zimbraIsSystemResource, LdapConstants.LDAP_TRUE);
        StringUtil.addToMultiMap(accountAttrs, Provisioning.A_zimbraIsSystemAccount, LdapConstants.LDAP_TRUE);
        StringUtil.addToMultiMap(accountAttrs, Provisioning.A_zimbraHideInGal, LdapConstants.LDAP_TRUE);
        StringUtil.addToMultiMap(accountAttrs, Provisioning.A_zimbraContactMaxNumEntries, "0");
        StringUtil.addToMultiMap(accountAttrs, Provisioning.A_zimbraMailHost, mailHost);
        checkSetAttrsOnCreate(zsc, TargetType.account, acctValue, accountAttrs);
        account = prov.createAccount(acctValue, password, accountAttrs);
    }
    if (!Provisioning.onLocalServer(account)) {
        String host = account.getMailHost();
        Server server = prov.getServerByName(host);
        return proxyRequest(request, context, server);
    }
    addDataSource(request, zsc, account, domain, folder, name, type);
    Element response = zsc.createElement(AdminConstants.CREATE_GAL_SYNC_ACCOUNT_RESPONSE);
    ToXML.encodeAccount(response, account, false, emptySet, null);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) GalMode(com.zimbra.soap.admin.type.GalMode) Server(com.zimbra.cs.account.Server) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) AccountSelector(com.zimbra.soap.type.AccountSelector) Provisioning(com.zimbra.cs.account.Provisioning) CreateGalSyncAccountRequest(com.zimbra.soap.admin.message.CreateGalSyncAccountRequest) AccountBy(com.zimbra.soap.type.AccountBy) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Domain(com.zimbra.cs.account.Domain)

Example 44 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class CreateSystemRetentionPolicy method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    CreateSystemRetentionPolicyRequest req = zsc.elementToJaxb(request);
    Provisioning prov = Provisioning.getInstance();
    // assume default retention policy to be set in globalConfig (for backward compatibility)
    Entry entry = prov.getConfig();
    // check if cos is specified
    CosSelector cosSelector = req.getCos();
    if (cosSelector != null) {
        entry = prov.get(Key.CosBy.fromString(cosSelector.getBy().name()), cosSelector.getKey());
        if (entry == null)
            throw AccountServiceException.NO_SUCH_COS(cosSelector.getKey());
    }
    // check right
    checkSetRight(entry, zsc, context, this);
    Policy keep = req.getKeepPolicy();
    Policy purge = req.getPurgePolicy();
    if (keep == null && purge == null) {
        throw ServiceException.INVALID_REQUEST("No keep or purge policy specified.", null);
    }
    if (keep != null && purge != null) {
        throw ServiceException.INVALID_REQUEST("Cannot specify both keep and purge policy.", null);
    }
    Policy newPolicy;
    if (keep != null) {
        newPolicy = RetentionPolicyManager.getInstance().createSystemKeepPolicy(entry, keep.getName(), keep.getLifetime());
    } else {
        newPolicy = RetentionPolicyManager.getInstance().createSystemPurgePolicy(entry, purge.getName(), purge.getLifetime());
    }
    CreateSystemRetentionPolicyResponse res = new CreateSystemRetentionPolicyResponse(newPolicy);
    return zsc.jaxbToElement(res);
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) Entry(com.zimbra.cs.account.Entry) CreateSystemRetentionPolicyRequest(com.zimbra.soap.admin.message.CreateSystemRetentionPolicyRequest) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) CosSelector(com.zimbra.soap.admin.type.CosSelector) CreateSystemRetentionPolicyResponse(com.zimbra.soap.admin.message.CreateSystemRetentionPolicyResponse) Provisioning(com.zimbra.cs.account.Provisioning)

Example 45 with ZimbraSoapContext

use of com.zimbra.soap.ZimbraSoapContext in project zm-mailbox by Zimbra.

the class AdminDocumentHandler method proxyIfNecessary.

@Override
protected Element proxyIfNecessary(Element request, Map<String, Object> context) throws ServiceException {
    // if we've explicitly been told to execute here, don't proxy
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    if (zsc.getProxyTarget() != null)
        return null;
    try {
        Provisioning prov = Provisioning.getInstance();
        Provisioning.Reasons reasons = new Provisioning.Reasons();
        // check whether we need to proxy to the home server of a target account
        String[] xpath = getProxiedAccountPath();
        String acctId = (xpath != null ? getXPath(request, xpath) : null);
        if (acctId != null) {
            Account acct = getAccount(prov, AccountBy.id, acctId, zsc.getAuthToken());
            if (acct != null && !Provisioning.onLocalServer(acct, reasons)) {
                ZimbraLog.soap.info("Proxying request: ProxiedAccountPath=%s reason: %s", Joiner.on("/").join(xpath), reasons.getReason());
                return proxyRequest(request, context, acctId);
            }
        }
        xpath = getProxiedAccountElementPath();
        Element acctElt = (xpath != null ? getXPathElement(request, xpath) : null);
        if (acctElt != null) {
            Account acct = getAccount(prov, AccountBy.fromString(acctElt.getAttribute(AdminConstants.A_BY)), acctElt.getText(), zsc.getAuthToken());
            if (acct != null && !Provisioning.onLocalServer(acct, reasons)) {
                ZimbraLog.soap.info("Proxying request: ProxiedAccountElementPath=%s acctElt=%s reason: %s", Joiner.on("/").join(xpath), acctElt.toString(), reasons.getReason());
                return proxyRequest(request, context, acct.getId());
            }
        }
        // check whether we need to proxy to the home server of a target calendar resource
        xpath = getProxiedResourcePath();
        String rsrcId = (xpath != null ? getXPath(request, xpath) : null);
        if (rsrcId != null) {
            CalendarResource rsrc = getCalendarResource(prov, Key.CalendarResourceBy.id, rsrcId);
            if (rsrc != null && !Provisioning.onLocalServer(rsrc, reasons)) {
                ZimbraLog.soap.info("Proxying request: ProxiedResourcePath=%s rsrcId=%s reason: %s", Joiner.on("/").join(xpath), rsrcId, reasons.getReason());
                return proxyRequest(request, context, rsrcId);
            }
        }
        xpath = getProxiedResourceElementPath();
        Element resourceElt = (xpath != null ? getXPathElement(request, xpath) : null);
        if (resourceElt != null) {
            CalendarResource rsrc = getCalendarResource(prov, Key.CalendarResourceBy.fromString(resourceElt.getAttribute(AdminConstants.A_BY)), resourceElt.getText());
            if (rsrc != null && !Provisioning.onLocalServer(rsrc, reasons)) {
                ZimbraLog.soap.info("Proxying request: ProxiedResourceElementPath=%s resourceElt=%s reason: %s", Joiner.on("/").join(xpath), resourceElt.toString(), reasons.getReason());
                return proxyRequest(request, context, rsrc.getId());
            }
        }
        // check whether we need to proxy to a target server
        xpath = getProxiedServerPath();
        String serverId = (xpath != null ? getXPath(request, xpath) : null);
        if (serverId != null) {
            Server server = prov.get(Key.ServerBy.id, serverId);
            if (server != null && !getLocalHostId().equalsIgnoreCase(server.getId())) {
                ZimbraLog.soap.info("Proxying request: ProxiedServerPath=%s serverId=%s server=%s reason: server ID=%s != localHostId=%s", Joiner.on("/").join(xpath), serverId, server.getName(), server.getId(), getLocalHostId());
                return proxyRequest(request, context, server);
            }
        }
        return null;
    } catch (ServiceException e) {
        // if something went wrong proxying the request, just execute it locally
        if (ServiceException.PROXY_ERROR.equals(e.getCode()))
            return null;
        // but if it's a real error, it's a real error
        throw e;
    }
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) CalendarResource(com.zimbra.cs.account.CalendarResource) Provisioning(com.zimbra.cs.account.Provisioning)

Aggregations

ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)410 Element (com.zimbra.common.soap.Element)322 Account (com.zimbra.cs.account.Account)175 Provisioning (com.zimbra.cs.account.Provisioning)164 Mailbox (com.zimbra.cs.mailbox.Mailbox)126 OperationContext (com.zimbra.cs.mailbox.OperationContext)95 ItemId (com.zimbra.cs.service.util.ItemId)60 Server (com.zimbra.cs.account.Server)48 ServiceException (com.zimbra.common.service.ServiceException)43 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)43 HashMap (java.util.HashMap)42 Domain (com.zimbra.cs.account.Domain)34 ArrayList (java.util.ArrayList)27 HashSet (java.util.HashSet)26 IOException (java.io.IOException)24 Test (org.junit.Test)20 Message (com.zimbra.cs.mailbox.Message)18 Group (com.zimbra.cs.account.Group)17 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)17 MimeMessage (javax.mail.internet.MimeMessage)16