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);
}
}
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);
}
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);
}
}
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));
}
Aggregations