Search in sources :

Example 11 with ConnectorConfig

use of com.sforce.ws.ConnectorConfig in project incubator-gobblin by apache.

the class SalesforceExtractor method bulkApiLogin.

/**
 * Login to salesforce
 * @return login status
 */
public boolean bulkApiLogin() throws Exception {
    log.info("Authenticating salesforce bulk api");
    boolean success = false;
    String hostName = this.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_HOST_NAME);
    String apiVersion = this.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_VERSION);
    if (Strings.isNullOrEmpty(apiVersion)) {
        apiVersion = "29.0";
    }
    String soapAuthEndPoint = hostName + SALESFORCE_SOAP_SERVICE + "/" + apiVersion;
    try {
        ConnectorConfig partnerConfig = new ConnectorConfig();
        if (super.workUnitState.contains(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL) && !super.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL).isEmpty()) {
            partnerConfig.setProxy(super.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL), super.workUnitState.getPropAsInt(ConfigurationKeys.SOURCE_CONN_USE_PROXY_PORT));
        }
        String accessToken = sfConnector.getAccessToken();
        if (accessToken == null) {
            boolean isConnectSuccess = sfConnector.connect();
            if (isConnectSuccess) {
                accessToken = sfConnector.getAccessToken();
            }
        }
        if (accessToken != null) {
            String serviceEndpoint = sfConnector.getInstanceUrl() + SALESFORCE_SOAP_SERVICE + "/" + apiVersion;
            partnerConfig.setSessionId(accessToken);
            partnerConfig.setServiceEndpoint(serviceEndpoint);
        } else {
            String securityToken = this.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_SECURITY_TOKEN);
            String password = PasswordManager.getInstance(this.workUnitState).readPassword(this.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_PASSWORD));
            partnerConfig.setUsername(this.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME));
            partnerConfig.setPassword(password + securityToken);
        }
        partnerConfig.setAuthEndpoint(soapAuthEndPoint);
        new PartnerConnection(partnerConfig);
        String soapEndpoint = partnerConfig.getServiceEndpoint();
        String restEndpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("Soap/")) + "async/" + apiVersion;
        ConnectorConfig config = new ConnectorConfig();
        config.setSessionId(partnerConfig.getSessionId());
        config.setRestEndpoint(restEndpoint);
        config.setCompression(true);
        config.setTraceFile("traceLogs.txt");
        config.setTraceMessage(false);
        config.setPrettyPrintXml(true);
        if (super.workUnitState.contains(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL) && !super.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL).isEmpty()) {
            config.setProxy(super.workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USE_PROXY_URL), super.workUnitState.getPropAsInt(ConfigurationKeys.SOURCE_CONN_USE_PROXY_PORT));
        }
        this.bulkConnection = new BulkConnection(config);
        success = true;
    } catch (RuntimeException e) {
        throw new RuntimeException("Failed to connect to salesforce bulk api; error - " + e, e);
    }
    return success;
}
Also used : ConnectorConfig(com.sforce.ws.ConnectorConfig) PartnerConnection(com.sforce.soap.partner.PartnerConnection) BulkConnection(com.sforce.async.BulkConnection)

Example 12 with ConnectorConfig

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

the class SalesforceSourceOrSink method connectBulk.

protected BulkConnection connectBulk(ConnectorConfig config) throws ComponentException {
    final SalesforceConnectionProperties connProps = getConnectionProperties();
    /*
         * When PartnerConnection is instantiated, a login is implicitly executed and, if successful, a valid session id is
         * stored in the ConnectorConfig instance. Use this key to initialize a BulkConnection:
         */
    ConnectorConfig bulkConfig = new ConnectorConfig();
    setProxy(bulkConfig);
    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();
    // 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(connProps.needCompression.getValue());
    bulkConfig.setTraceMessage(connProps.httpTraceMessage.getValue());
    bulkConfig.setValidateSchema(false);
    try {
        return new BulkConnection(bulkConfig);
    } catch (AsyncApiException e) {
        throw new ComponentException(e);
    }
}
Also used : SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) ConnectorConfig(com.sforce.ws.ConnectorConfig) ComponentException(org.talend.components.api.exception.ComponentException) BulkConnection(com.sforce.async.BulkConnection) AsyncApiException(com.sforce.async.AsyncApiException)

Example 13 with ConnectorConfig

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

the class SalesforceSourceOrSinkTestIT method testSalesForcePasswordExpired.

@Test(expected = ConnectionException.class)
public void testSalesForcePasswordExpired() throws ConnectionException {
    SalesforceSourceOrSink salesforceSourceOrSink = new SalesforceSourceOrSink();
    TSalesforceInputProperties properties = (TSalesforceInputProperties) new TSalesforceInputProperties(null).init();
    salesforceSourceOrSink.initialize(null, properties);
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(StringUtils.strip(USER_ID_EXPIRED, "\""));
    String password = StringUtils.strip(PASSWORD_EXPIRED, "\"");
    String securityKey = StringUtils.strip(SECURITY_KEY_EXPIRED, "\"");
    if (StringUtils.isNotEmpty(securityKey)) {
        password = password + securityKey;
    }
    config.setPassword(password);
    PartnerConnection connection = null;
    try {
        connection = salesforceSourceOrSink.doConnection(config, true);
    } catch (LoginFault ex) {
        Assert.fail("Must be an exception related to expired password, not the Login Fault.");
    } finally {
        if (null != connection) {
            connection.logout();
        }
    }
}
Also used : ConnectorConfig(com.sforce.ws.ConnectorConfig) PartnerConnection(com.sforce.soap.partner.PartnerConnection) LoginFault(com.sforce.soap.partner.fault.LoginFault) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) Test(org.junit.Test)

Example 14 with ConnectorConfig

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

the class SalesforceDataprepSource method connect.

ConnectionHolder connect(RuntimeContainer container) throws IOException {
    SalesforceRuntimeCommon.enableTLSv11AndTLSv12ForJava7();
    final ConnectionHolder ch = new ConnectionHolder();
    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(datastore.userId.getValue());
    String password = datastore.password.getValue();
    String securityKey = datastore.securityKey.getValue();
    if (!StringUtils.isEmpty(securityKey)) {
        password = password + securityKey;
    }
    config.setPassword(password);
    // 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);
            // 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;
        }
    });
    config.setConnectionTimeout(timeout);
    // This should only be false when doing debugging.
    config.setCompression(true);
    config.setUseChunkedPost(true);
    config.setValidateSchema(false);
    try {
        ch.connection = doConnection(config);
        ch.bulkConnection = connectBulk(ch.connection.getConfig());
        return ch;
    } catch (ConnectionException e) {
        throw new IOException(e);
    }
}
Also used : SessionRenewer(com.sforce.ws.SessionRenewer) ConnectorConfig(com.sforce.ws.ConnectorConfig) PartnerConnection(com.sforce.soap.partner.PartnerConnection) QName(javax.xml.namespace.QName) IOException(java.io.IOException) ConnectionException(com.sforce.ws.ConnectionException) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 15 with ConnectorConfig

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

the class SalesforceRuntimeCommonTest method testRenewSession.

@Test
public void testRenewSession() throws Exception {
    SessionRenewer sessionRenewer = mock(SessionRenewer.class);
    ConnectorConfig connectorConfig = new ConnectorConfig();
    connectorConfig.setSessionRenewer(sessionRenewer);
    SalesforceRuntimeCommon.renewSession(connectorConfig);
    verify(sessionRenewer, only()).renewSession(eq(connectorConfig));
}
Also used : SessionRenewer(com.sforce.ws.SessionRenewer) ConnectorConfig(com.sforce.ws.ConnectorConfig) Test(org.junit.Test)

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