use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class RestApiIntegrationTest method testRetryFailure.
@Test
public void testRetryFailure() throws Exception {
final SalesforceComponent sf = context().getComponent("salesforce", SalesforceComponent.class);
final String accessToken = sf.getSession().getAccessToken();
final SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setSslContext(new SSLContextParameters().createSSLContext(context));
final HttpClient httpClient = new HttpClient(sslContextFactory);
httpClient.setConnectTimeout(60000);
httpClient.start();
final String uri = sf.getLoginConfig().getLoginUrl() + "/services/oauth2/revoke?token=" + accessToken;
final Request logoutGet = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(1, TimeUnit.MINUTES);
final ContentResponse response = logoutGet.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
// set component config to bad password to cause relogin attempts to fail
final String password = sf.getLoginConfig().getPassword();
sf.getLoginConfig().setPassword("bad_password");
try {
testGetGlobalObjects();
fail("Expected CamelExecutionException!");
} catch (final CamelExecutionException e) {
if (e.getCause() instanceof SalesforceException) {
final SalesforceException cause = (SalesforceException) e.getCause();
assertEquals("Expected 400 on authentication retry failure", HttpStatus.BAD_REQUEST_400, cause.getStatusCode());
} else {
fail("Expected SalesforceException!");
}
} finally {
// reset password and retries to allow other tests to pass
sf.getLoginConfig().setPassword(password);
}
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class SubscriptionHelper method restartClient.
// launch an async task to restart
private void restartClient() {
// launch a new restart command
final SalesforceHttpClient httpClient = component.getConfig().getHttpClient();
httpClient.getExecutor().execute(new Runnable() {
@Override
public void run() {
LOG.info("Restarting on unexpected disconnect from Salesforce...");
boolean abort = false;
// wait for disconnect
LOG.debug("Waiting to disconnect...");
while (!client.isDisconnected()) {
try {
Thread.sleep(DISCONNECT_INTERVAL);
} catch (InterruptedException e) {
LOG.error("Aborting restart on interrupt!");
abort = true;
}
}
if (!abort) {
// update restart attempt backoff
final long backoff = restartBackoff.getAndAdd(backoffIncrement);
if (backoff > maxBackoff) {
LOG.error("Restart aborted after exceeding {} msecs backoff", maxBackoff);
abort = true;
} else {
// pause before restart attempt
LOG.debug("Pausing for {} msecs before restart attempt", backoff);
try {
Thread.sleep(backoff);
} catch (InterruptedException e) {
LOG.error("Aborting restart on interrupt!");
abort = true;
}
}
if (!abort) {
Exception lastError = new SalesforceException("Unknown error", null);
try {
// reset client
doStop();
// register listeners and restart
doStart();
} catch (Exception e) {
LOG.error("Error restarting: " + e.getMessage(), e);
lastError = e;
}
if (client.isHandshook()) {
LOG.info("Successfully restarted!");
// reset backoff interval
restartBackoff.set(client.getBackoffIncrement());
} else {
LOG.error("Failed to restart after pausing for {} msecs", backoff);
if ((backoff + backoffIncrement) > maxBackoff) {
// notify all consumers
String abortMsg = "Aborting restart attempt due to: " + lastError.getMessage();
SalesforceException ex = new SalesforceException(abortMsg, lastError);
for (SalesforceConsumer consumer : listenerMap.keySet()) {
consumer.handleException(abortMsg, ex);
}
}
}
}
}
}
});
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project syndesis by syndesisio.
the class SalesforceMetadataRetrieval method handle.
@Override
public RuntimeException handle(final Exception e) {
Throwable current = e;
while (current != null && current != current.getCause() && !(current instanceof SalesforceException)) {
current = current.getCause();
}
if (current instanceof SalesforceException) {
final SalesforceException salesforceException = (SalesforceException) current;
final String message = salesforceException.getErrors().stream().map(error -> error.getErrorCode() + ": " + error.getMessage()).collect(Collectors.joining(". "));
return new SyndesisServerException("Salesforce: " + message, e);
}
if (e instanceof RuntimeException) {
return (RuntimeException) e;
}
return new SyndesisServerException("Salesforce", e);
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project syndesis by syndesisio.
the class ForUpdateCustomizer method beforeProducer.
public void beforeProducer(final Exchange exchange) throws IOException {
// parse input json and extract Id field
final Message in = exchange.getIn();
final String body = in.getBody(String.class);
if (body == null) {
return;
}
final ObjectNode node = (ObjectNode) Json.reader().readTree(body);
final JsonNode idProperty = node.remove(idPropertyName);
if (idProperty == null) {
exchange.setException(new SalesforceException("Missing option value for Id or " + SalesforceEndpointConfig.SOBJECT_EXT_ID_NAME, 404));
return;
}
final String idValue = idProperty.textValue();
if ("Id".equals(idPropertyName)) {
in.setHeader(SalesforceEndpointConfig.SOBJECT_ID, idValue);
} else {
in.setHeader(SalesforceEndpointConfig.SOBJECT_EXT_ID_VALUE, idValue);
}
// base fields are not allowed to be updated
clearBaseFields(node);
// update input json
in.setBody(Json.writer().writeValueAsString(node));
}
Aggregations