Search in sources :

Example 1 with SessionRenewer

use of com.sforce.ws.SessionRenewer 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 2 with SessionRenewer

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

the class SalesforceRuntimeCommon method renewSession.

/**
 * This is for Buck connection session renew It can't called automatically with current force-wsc api
 */
public static void renewSession(ConnectorConfig config) throws ConnectionException {
    LOG.debug("renew session bulk connection");
    SessionRenewer renewer = config.getSessionRenewer();
    renewer.renewSession(config);
}
Also used : SessionRenewer(com.sforce.ws.SessionRenewer)

Example 3 with SessionRenewer

use of com.sforce.ws.SessionRenewer 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 4 with SessionRenewer

use of com.sforce.ws.SessionRenewer 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

SessionRenewer (com.sforce.ws.SessionRenewer)4 ConnectorConfig (com.sforce.ws.ConnectorConfig)3 PartnerConnection (com.sforce.soap.partner.PartnerConnection)2 ConnectionException (com.sforce.ws.ConnectionException)2 IOException (java.io.IOException)2 QName (javax.xml.namespace.QName)2 ConnectionHolder (org.talend.components.salesforce.runtime.common.ConnectionHolder)2 Properties (java.util.Properties)1 Test (org.junit.Test)1 ComponentProperties (org.talend.components.api.properties.ComponentProperties)1 SalesforceConnectionProperties (org.talend.components.salesforce.SalesforceConnectionProperties)1 SalesforceProvideConnectionProperties (org.talend.components.salesforce.SalesforceProvideConnectionProperties)1