Search in sources :

Example 41 with DataSource

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

the class GalSearchControl method generateLocalResourceSearchQuery.

private boolean generateLocalResourceSearchQuery(Account galAcct) throws ServiceException {
    String query = mParams.getQuery();
    StringBuilder searchQuery = new StringBuilder();
    if (!Strings.isNullOrEmpty(query)) {
        searchQuery.append("contact:\"");
        // escape quotes
        searchQuery.append(query.replace("\"", "\\\""));
        searchQuery.append("\" AND");
    }
    searchQuery.append(" #zimbraAccountCalendarUserType:RESOURCE");
    for (DataSource ds : galAcct.getAllDataSources()) {
        if (ds.getType() != DataSourceType.gal)
            continue;
        String galType = ds.getAttr(Provisioning.A_zimbraGalType);
        if (galType.compareTo("ldap") == 0)
            continue;
        searchQuery.append(" AND (");
        searchQuery.append(" inid:").append(ds.getFolderId());
        searchQuery.append(")");
        ZimbraLog.gal.debug("query: " + searchQuery.toString());
        mParams.parseSearchParams(mParams.getRequest(), searchQuery.toString());
        return true;
    }
    return false;
}
Also used : DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource)

Example 42 with DataSource

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

the class ModifyDataSource method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    Account account = getRequestedAccount(zsc);
    if (!canModifyOptions(zsc, account))
        throw ServiceException.PERM_DENIED("can not modify options");
    // boolean wipeOutOldData = false;
    Element eDataSource = CreateDataSource.getDataSourceElement(request);
    DataSourceType type = DataSourceType.fromString(eDataSource.getName());
    String id = eDataSource.getAttribute(MailConstants.A_ID);
    DataSource ds = prov.get(account, Key.DataSourceBy.id, id);
    if (ds == null) {
        throw ServiceException.INVALID_REQUEST("Unable to find data source with id=" + id, null);
    }
    Map<String, Object> dsAttrs = new HashMap<String, Object>();
    String value = eDataSource.getAttribute(MailConstants.A_NAME, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceName, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_IS_ENABLED, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_IS_ENABLED)));
    }
    value = eDataSource.getAttribute(MailConstants.A_FOLDER, null);
    if (value != null) {
        Mailbox mbox = getRequestedMailbox(zsc);
        CreateDataSource.validateFolderId(account, mbox, eDataSource, type);
        dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_HOST, null);
    if (value != null && !value.equals(ds.getHost())) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceHost, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_PORT, null);
    if (value != null)
        dsAttrs.put(Provisioning.A_zimbraDataSourcePort, value);
    value = eDataSource.getAttribute(MailConstants.A_DS_CONNECTION_TYPE, null);
    if (value != null)
        dsAttrs.put(Provisioning.A_zimbraDataSourceConnectionType, value);
    value = eDataSource.getAttribute(MailConstants.A_DS_USERNAME, null);
    if (value != null && !value.equals(ds.getUsername())) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceUsername, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_PASSWORD, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourcePassword, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_OAUTH_TOKEN, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthToken, value);
        dsAttrs.put(Provisioning.A_zimbraDataSourceAuthMechanism, ZAttrProvisioning.DataSourceAuthMechanism.XOAUTH2.name());
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_ID, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientId, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_SECRET, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientSecret, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshToken, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN_URL, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshTokenUrl, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_LEAVE_ON_SERVER, null);
    if (value != null) {
        if (type != DataSourceType.pop3) {
            String msg = String.format("%s only allowed for %s data sources", MailConstants.A_DS_LEAVE_ON_SERVER, MailConstants.E_DS_POP3);
            throw ServiceException.INVALID_REQUEST(msg, null);
        }
        boolean newValue = eDataSource.getAttributeBool(MailConstants.A_DS_LEAVE_ON_SERVER);
        if (newValue != ds.leaveOnServer()) {
            dsAttrs.put(Provisioning.A_zimbraDataSourceLeaveOnServer, LdapUtil.getLdapBooleanString(newValue));
            Mailbox mbox = getRequestedMailbox(zsc);
            DbPop3Message.deleteUids(mbox, ds.getId());
            DbDataSource.deleteAllMappings(ds);
        }
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_POLLING_INTERVAL, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourcePollingInterval, value);
    }
    // import class
    String importClass = eDataSource.getAttribute(MailConstants.A_DS_IMPORT_CLASS, DataSourceManager.getDefaultImportClass(type));
    if (importClass != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceImportClassName, importClass);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_ENABLED, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_SMTP_ENABLED)));
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_HOST, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpHost, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PORT, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpPort, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_CONNECTION_TYPE, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpConnectionType, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_AUTH_REQUIRED, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthRequired, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_SMTP_AUTH_REQUIRED)));
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_USERNAME, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthUsername, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PASSWORD, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthPassword, value);
    }
    if (eDataSource.getAttributeBool(MailConstants.A_DS_TEST, false)) {
        TestDataSource.testDataSourceConnection(prov, eDataSource, type, account);
    }
    processCommonOptionalAttrs(account, dsAttrs, eDataSource);
    prov.modifyDataSource(account, id, dsAttrs);
    Element response = zsc.createElement(MailConstants.MODIFY_DATA_SOURCE_RESPONSE);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) DataSourceType(com.zimbra.soap.admin.type.DataSourceType) ZAttrProvisioning(com.zimbra.common.account.ZAttrProvisioning) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource) DbDataSource(com.zimbra.cs.db.DbDataSource)

Example 43 with DataSource

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

the class TestDataSource method testDataSource.

static String testDataSource(Provisioning prov, Account account, Element eDataSource, DataSourceType type) throws ServiceException {
    Map<String, Object> testAttrs = new HashMap<String, Object>();
    String testId = "TestId";
    String id = eDataSource.getAttribute(MailConstants.A_ID, null);
    String password = null;
    if (id != null) {
        // Testing existing data source
        DataSource dsOrig = prov.get(account, DataSourceBy.id, id);
        Map<String, Object> origAttrs = dsOrig.getAttrs();
        for (String key : origAttrs.keySet()) {
            if (key.equals(Provisioning.A_zimbraDataSourcePassword)) {
                password = dsOrig.getDecryptedPassword();
            } else {
                testAttrs.put(key, dsOrig.getAttr(key));
            }
        }
    }
    // Get values from SOAP request.  If testing an existing data source,
    // values in the request override the persisted values.
    String value = eDataSource.getAttribute(MailConstants.A_DS_HOST, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceHost, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_PORT, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourcePort, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CONNECTION_TYPE, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceConnectionType, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_USERNAME, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceUsername, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_PASSWORD, null);
    if (value != null) {
        password = value;
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_LEAVE_ON_SERVER, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceLeaveOnServer, value.toUpperCase());
    }
    value = eDataSource.getAttribute(MailConstants.A_FOLDER, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceFolderId, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_OAUTH_TOKEN, null);
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceOAuthToken, DataSource.encryptData(testId, value));
        testAttrs.put(Provisioning.A_zimbraDataSourceAuthMechanism, DataSourceAuthMechanism.XOAUTH2.name());
    }
    if (password != null) {
        // Password has to be encrypted explicitly since this is a temporary object.
        // The current implementation of LdapDataSource doesn't perform encryption until
        // the DataSource is saved.
        testAttrs.put(Provisioning.A_zimbraDataSourcePassword, DataSource.encryptData(testId, password));
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PASSWORD, null);
    if (value != null) {
        // Again password has to be encrypted explicitly.
        testAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthPassword, DataSource.encryptData(testId, value));
    }
    // import class
    value = eDataSource.getAttribute(MailConstants.A_DS_IMPORT_CLASS, DataSourceManager.getDefaultImportClass(type));
    if (value != null) {
        testAttrs.put(Provisioning.A_zimbraDataSourceImportClassName, value);
    }
    // SMTP attributes
    CreateDataSource.processSmtpAttrs(testAttrs, eDataSource, true, testId);
    // Common optional attributes
    ModifyDataSource.processCommonOptionalAttrs(account, testAttrs, eDataSource);
    // Perform test and assemble response
    DataSource ds = new DataSource(account, type, "Test", testId, testAttrs, null);
    String error = null;
    try {
        DataSourceManager.test(ds);
    } catch (ServiceException x) {
        Throwable t = SystemUtil.getInnermostException(x);
        error = t.getMessage();
        if (error == null) {
            error = "datasource test failed";
        }
    }
    return error;
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) DataSource(com.zimbra.cs.account.DataSource)

Example 44 with DataSource

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

the class CreateDataSource method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    String id = request.getAttribute(AdminConstants.E_ID);
    Account account = prov.get(AccountBy.id, id, zsc.getAuthToken());
    if (account == null)
        throw AccountServiceException.NO_SUCH_ACCOUNT(id);
    checkAdminLoginAsRight(zsc, prov, account);
    Element dsEl = request.getElement(AccountConstants.E_DATA_SOURCE);
    Map<String, Object> attrs = AdminService.getAttrs(dsEl);
    DataSourceType type = DataSourceType.fromString(dsEl.getAttribute(AccountConstants.A_TYPE));
    // Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
    if (isDomainAdminOnly(zsc)) {
        // yuck, can't really integrate into AdminDocumentHandler methods cleanly
        // have to check separately here
        AttributeClass klass = ModifyDataSource.getAttributeClassFromType(type);
        checkModifyAttrs(zsc, klass, attrs);
    }
    String name = dsEl.getAttribute(AccountConstants.A_NAME);
    DataSource ds = Provisioning.getInstance().createDataSource(account, type, name, attrs);
    Element response = zsc.createElement(AdminConstants.CREATE_DATA_SOURCE_RESPONSE);
    com.zimbra.cs.service.account.ToXML.encodeDataSource(response, ds);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) DataSourceType(com.zimbra.soap.admin.type.DataSourceType) AttributeClass(com.zimbra.cs.account.AttributeClass) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource)

Example 45 with DataSource

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

the class CreateDataSource method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    Element eDataSource = getDataSourceElement(request);
    DataSourceType type = DataSourceType.fromString(eDataSource.getName());
    boolean isAppProvisioned = doZMGAppProvisioningIfReq(zsc, prov, eDataSource, type);
    Account account = getRequestedAccount(zsc);
    if (eDataSource.getAttributeBool(MailConstants.A_DS_TEST, false) && !isAppProvisioned) {
        TestDataSource.testDataSourceConnection(prov, eDataSource, type, account);
    }
    if (!canModifyOptions(zsc, account))
        throw ServiceException.PERM_DENIED("can not modify options");
    Mailbox mbox = getRequestedMailbox(zsc);
    // Create the data source
    String folderIdStr = eDataSource.getAttribute(MailConstants.A_FOLDER);
    int folderId = Integer.valueOf(folderIdStr);
    String name = eDataSource.getAttribute(MailConstants.A_NAME);
    boolean returnFolderId = false;
    if (folderId == -1) {
        folderId = mbox.createFolder(null, "/" + getUniqueDSFolderName(mbox, name), new Folder.FolderOptions()).getId();
        folderIdStr = String.valueOf(folderId);
        returnFolderId = true;
    } else {
        validateFolderId(account, mbox, eDataSource, type);
    }
    Map<String, Object> dsAttrs = new HashMap<>();
    // Common attributes
    dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, folderIdStr);
    dsAttrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_IS_ENABLED)));
    dsAttrs.put(Provisioning.A_zimbraDataSourceImportOnly, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_IS_IMPORTONLY, false)));
    dsAttrs.put(Provisioning.A_zimbraDataSourceHost, eDataSource.getAttribute(MailConstants.A_DS_HOST, null));
    dsAttrs.put(Provisioning.A_zimbraDataSourcePort, eDataSource.getAttribute(MailConstants.A_DS_PORT, null));
    dsAttrs.put(Provisioning.A_zimbraDataSourceConnectionType, eDataSource.getAttribute(MailConstants.A_DS_CONNECTION_TYPE, prov.getConfig().getDataSourceConnectionTypeAsString()));
    dsAttrs.put(Provisioning.A_zimbraDataSourceUsername, eDataSource.getAttribute(MailConstants.A_DS_USERNAME, null));
    String value = eDataSource.getAttribute(MailConstants.A_DS_PASSWORD, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourcePassword, value);
    }
    // type
    dsAttrs.put(Provisioning.A_zimbraDataSourceType, type.toString());
    // import class
    String importClass = eDataSource.getAttribute(MailConstants.A_DS_IMPORT_CLASS, DataSourceManager.getDefaultImportClass(type));
    if (importClass != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceImportClassName, importClass);
    }
    // SMTP attributes
    processSmtpAttrs(dsAttrs, eDataSource, false, null);
    // Common optional attributes
    ModifyDataSource.processCommonOptionalAttrs(account, dsAttrs, eDataSource);
    // POP3-specific attributes
    if (type == DataSourceType.pop3) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceLeaveOnServer, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_LEAVE_ON_SERVER, true)));
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_OAUTH_TOKEN, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthToken, value);
        dsAttrs.put(Provisioning.A_zimbraDataSourceAuthMechanism, DataSourceAuthMechanism.XOAUTH2.name());
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_ID, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientId, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_SECRET, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientSecret, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshToken, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN_URL, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshTokenUrl, value);
    }
    DataSource ds;
    try {
        ds = prov.createDataSource(account, type, name, dsAttrs);
        if (type == DataSourceType.imap) {
            DataSourceListner.createDataSource(account, ds);
        }
    } catch (ServiceException e) {
        if (returnFolderId) {
            // we should delete the auto-created folder
            mbox.delete(null, folderId, MailItem.Type.FOLDER);
        }
        throw e;
    }
    ZimbraLog.addDataSourceNameToContext(ds.getName());
    // Assemble response
    Element response = zsc.createElement(MailConstants.CREATE_DATA_SOURCE_RESPONSE);
    eDataSource = response.addElement(type.toString());
    eDataSource.addAttribute(MailConstants.A_ID, ds.getId());
    if (returnFolderId) {
        eDataSource.addAttribute(MailConstants.A_FOLDER, folderId);
    }
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) Folder(com.zimbra.cs.mailbox.Folder) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DataSourceType(com.zimbra.soap.admin.type.DataSourceType)

Aggregations

DataSource (com.zimbra.cs.account.DataSource)71 Account (com.zimbra.cs.account.Account)29 HashMap (java.util.HashMap)21 Provisioning (com.zimbra.cs.account.Provisioning)18 DbDataSource (com.zimbra.cs.db.DbDataSource)18 ServiceException (com.zimbra.common.service.ServiceException)15 Element (com.zimbra.common.soap.Element)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)11 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)11 ArrayList (java.util.ArrayList)11 ZDataSource (com.zimbra.client.ZDataSource)8 AccountServiceException (com.zimbra.cs.account.AccountServiceException)7 Folder (com.zimbra.cs.mailbox.Folder)7 DataSourceType (com.zimbra.soap.admin.type.DataSourceType)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)6 ZMailbox (com.zimbra.client.ZMailbox)5 LdapDataSource (com.zimbra.cs.account.ldap.entry.LdapDataSource)5 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)5