Search in sources :

Example 6 with ConnectorConfig

use of com.sforce.ws.ConnectorConfig in project components by Talend.

the class SalesforceDataprepSource method connectBulk.

protected BulkConnection connectBulk(ConnectorConfig config) throws ComponentException {
    /*
         * When PartnerConnection is instantiated, a login is implicitly executed and, if successful, a valid session is
         * stored in the ConnectorConfig instance. Use this key to initialize a BulkConnection:
         */
    ConnectorConfig bulkConfig = new ConnectorConfig();
    bulkConfig.setSessionId(config.getSessionId());
    // For session renew
    bulkConfig.setSessionRenewer(config.getSessionRenewer());
    bulkConfig.setUsername(config.getUsername());
    bulkConfig.setPassword(config.getPassword());
    /*
         * The endpoint for the Bulk API service is the same as for the normal SOAP uri until the /Soap/ part. From here
         * it's '/async/versionNumber'
         */
    String soapEndpoint = config.getServiceEndpoint();
    // set it by a default property file
    // Service endpoint should be like this:
    // https://ap1.salesforce.com/services/Soap/u/37.0/00D90000000eSq3
    String apiVersion = soapEndpoint.substring(soapEndpoint.lastIndexOf("/services/Soap/u/") + 17);
    apiVersion = apiVersion.substring(0, apiVersion.indexOf("/"));
    String restEndpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("Soap/")) + "async/" + apiVersion;
    bulkConfig.setRestEndpoint(restEndpoint);
    // This should only be false when doing debugging.
    bulkConfig.setCompression(true);
    bulkConfig.setTraceMessage(false);
    bulkConfig.setValidateSchema(false);
    try {
        return new BulkConnection(bulkConfig);
    } catch (AsyncApiException e) {
        throw new ComponentException(e);
    }
}
Also used : ConnectorConfig(com.sforce.ws.ConnectorConfig) ComponentException(org.talend.components.api.exception.ComponentException) BulkConnection(com.sforce.async.BulkConnection) AsyncApiException(com.sforce.async.AsyncApiException)

Example 7 with ConnectorConfig

use of com.sforce.ws.ConnectorConfig in project components by Talend.

the class SalesforceSourceOrSink method connect.

protected ConnectionHolder connect(RuntimeContainer container) throws IOException {
    SalesforceRuntimeCommon.enableTLSv11AndTLSv12ForJava7();
    final ConnectionHolder ch = new ConnectionHolder();
    SalesforceConnectionProperties connProps = properties.getConnectionProperties();
    String refComponentId = connProps.getReferencedComponentId();
    // Using another component's connection
    if (refComponentId != null) {
        // In a runtime container
        if (container != null) {
            // PartnerConnection must be created. BulkConnection is optional.
            PartnerConnection connection = (PartnerConnection) container.getComponentData(refComponentId, KEY_CONNECTION);
            if (connection == null) {
                throw new IOException("Referenced component: " + refComponentId + " not connected");
            }
            ch.connection = connection;
            ch.bulkConnection = (BulkConnection) container.getComponentData(refComponentId, KEY_CONNECTION_BULK);
            return ch;
        }
        // Design time
        connProps = connProps.getReferencedConnectionProperties();
    }
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(StringUtils.strip(connProps.userPassword.userId.getStringValue(), "\""));
    String password = StringUtils.strip(connProps.userPassword.password.getStringValue(), "\"");
    String securityKey = StringUtils.strip(connProps.userPassword.securityKey.getStringValue(), "\"");
    if (StringUtils.isNotEmpty(securityKey)) {
        password = password + securityKey;
    }
    config.setPassword(password);
    setProxy(config);
    // Notes on how to test this
    // http://thysmichels.com/2014/02/15/salesforce-wsc-partner-connection-session-renew-when-session-timeout/
    config.setSessionRenewer(new SessionRenewer() {

        @Override
        public SessionRenewalHeader renewSession(ConnectorConfig connectorConfig) throws ConnectionException {
            LOG.debug("renewing session...");
            SessionRenewalHeader header = new SessionRenewalHeader();
            connectorConfig.setSessionId(null);
            PartnerConnection connection = doConnection(connectorConfig, true);
            // update the connection session header
            ch.connection.setSessionHeader(connection.getSessionHeader().getSessionId());
            header.name = new QName("urn:partner.soap.sforce.com", "SessionHeader");
            header.headerElement = connection.getSessionHeader();
            LOG.debug("session renewed!");
            return header;
        }
    });
    if (connProps.timeout.getValue() > 0) {
        config.setConnectionTimeout(connProps.timeout.getValue());
    }
    config.setCompression(connProps.needCompression.getValue());
    config.setUseChunkedPost(connProps.httpChunked.getValue());
    config.setValidateSchema(false);
    try {
        // Get session from session file or new connection
        if (isReuseSession()) {
            Properties properties = getSessionProperties();
            if (properties != null) {
                this.sessionId = properties.getProperty(SalesforceConstant.SESSION_ID);
                this.serviceEndPoint = properties.getProperty(SalesforceConstant.SERVICE_ENDPOINT);
            }
        }
        if (this.sessionId != null && this.serviceEndPoint != null) {
            ch.connection = doConnection(config, false);
        } else {
            ch.connection = doConnection(config, true);
        }
        if (ch.connection != null) {
            String clientId = connProps.clientId.getStringValue();
            if (clientId != null) {
                // Need the test.
                ch.connection.setCallOptions(clientId, null);
            }
        }
        if (connProps.bulkConnection.getValue()) {
            ch.bulkConnection = connectBulk(ch.connection.getConfig());
        }
        if (container != null) {
            container.setComponentData(container.getCurrentComponentId(), KEY_CONNECTION, ch.connection);
            if (ch.bulkConnection != null) {
                container.setComponentData(container.getCurrentComponentId(), KEY_CONNECTION_BULK, ch.bulkConnection);
            }
        }
        return ch;
    } catch (ConnectionException e) {
        throw new IOException(e);
    }
}
Also used : SessionRenewer(com.sforce.ws.SessionRenewer) SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) PartnerConnection(com.sforce.soap.partner.PartnerConnection) ConnectorConfig(com.sforce.ws.ConnectorConfig) QName(javax.xml.namespace.QName) IOException(java.io.IOException) SalesforceProvideConnectionProperties(org.talend.components.salesforce.SalesforceProvideConnectionProperties) Properties(java.util.Properties) ComponentProperties(org.talend.components.api.properties.ComponentProperties) SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) ConnectionException(com.sforce.ws.ConnectionException) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 8 with ConnectorConfig

use of com.sforce.ws.ConnectorConfig in project components by Talend.

the class SalesforceRuntimeTestUtil method login.

private void login(String endpoint) throws ConnectionException {
    ConnectorConfig config = new ConnectorConfig();
    config.setAuthEndpoint(endpoint);
    config.setUsername(username);
    config.setPassword(password + securityKey);
    config.setConnectionTimeout(60000);
    config.setUseChunkedPost(true);
    partnerConnection = new PartnerConnection(config);
}
Also used : ConnectorConfig(com.sforce.ws.ConnectorConfig) PartnerConnection(com.sforce.soap.partner.PartnerConnection)

Example 9 with ConnectorConfig

use of com.sforce.ws.ConnectorConfig in project gitblit by gitblit.

the class SalesforceAuthProvider method authenticate.

@Override
public UserModel authenticate(String username, char[] password) {
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(username);
    config.setPassword(new String(password));
    try {
        PartnerConnection connection = Connector.newConnection(config);
        GetUserInfoResult info = connection.getUserInfo();
        String org = settings.getString(Keys.realm.salesforce.orgId, "0").trim();
        if (!org.equals("0")) {
            if (!org.equals(info.getOrganizationId())) {
                logger.warn("Access attempted by user of an invalid org: " + info.getUserName() + ", org: " + info.getOrganizationName() + "(" + info.getOrganizationId() + ")");
                return null;
            }
        }
        logger.info("Authenticated user " + info.getUserName() + " using org " + info.getOrganizationName() + "(" + info.getOrganizationId() + ")");
        String simpleUsername = getSimpleUsername(info);
        UserModel user = null;
        synchronized (this) {
            user = userManager.getUserModel(simpleUsername);
            if (user == null) {
                user = new UserModel(simpleUsername);
            }
            setCookie(user);
            setUserAttributes(user, info);
            updateUser(user);
        }
        return user;
    } catch (ConnectionException e) {
        logger.error("Failed to authenticate", e);
    }
    return null;
}
Also used : UserModel(com.gitblit.models.UserModel) ConnectorConfig(com.sforce.ws.ConnectorConfig) PartnerConnection(com.sforce.soap.partner.PartnerConnection) GetUserInfoResult(com.sforce.soap.partner.GetUserInfoResult) ConnectionException(com.sforce.ws.ConnectionException)

Example 10 with ConnectorConfig

use of com.sforce.ws.ConnectorConfig in project tdi-studio-se by Talend.

the class SforceBasicBulkConnection method init.

private void init() throws Exception {
    config = new ConnectorConfig();
    setProxyToConnection(config);
    // This should only be false when doing debugging.
    config.setCompression(needCompression);
    // Set this to true to see HTTP requests and responses on stdout
    config.setTraceMessage(needTraceMessage);
    renewSession();
    connection = new BulkConnection(config);
}
Also used : ConnectorConfig(com.sforce.ws.ConnectorConfig) BulkConnection(com.sforce.async.BulkConnection)

Aggregations

ConnectorConfig (com.sforce.ws.ConnectorConfig)20 PartnerConnection (com.sforce.soap.partner.PartnerConnection)12 BulkConnection (com.sforce.async.BulkConnection)5 ConnectionException (com.sforce.ws.ConnectionException)5 Test (org.junit.Test)4 SessionRenewer (com.sforce.ws.SessionRenewer)3 IOException (java.io.IOException)3 KettleException (org.pentaho.di.core.exception.KettleException)3 AsyncApiException (com.sforce.async.AsyncApiException)2 GetUserInfoResult (com.sforce.soap.partner.GetUserInfoResult)2 LoginResult (com.sforce.soap.partner.LoginResult)2 LoginFault (com.sforce.soap.partner.fault.LoginFault)2 QName (javax.xml.namespace.QName)2 Matchers.anyString (org.mockito.Matchers.anyString)2 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)2 ComponentException (org.talend.components.api.exception.ComponentException)2 SalesforceConnectionProperties (org.talend.components.salesforce.SalesforceConnectionProperties)2 ConnectionHolder (org.talend.components.salesforce.runtime.common.ConnectionHolder)2 UserModel (com.gitblit.models.UserModel)1 GetServerTimestampResult (com.sforce.soap.partner.GetServerTimestampResult)1