Search in sources :

Example 76 with SalesforceException

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);
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters) Test(org.junit.Test)

Example 77 with SalesforceException

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);
                            }
                        }
                    }
                }
            }
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SalesforceConsumer(org.apache.camel.component.salesforce.SalesforceConsumer) SalesforceHttpClient(org.apache.camel.component.salesforce.SalesforceHttpClient) CamelException(org.apache.camel.CamelException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException)

Example 78 with SalesforceException

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);
}
Also used : PropertyPair(io.syndesis.connector.support.verifier.api.PropertyPair) MetaData(org.apache.camel.component.extension.MetaDataExtension.MetaData) HashMap(java.util.HashMap) ComponentMetadataRetrieval(io.syndesis.connector.support.verifier.api.ComponentMetadataRetrieval) HashSet(java.util.HashSet) SimpleTypeSchema(com.fasterxml.jackson.module.jsonSchema.types.SimpleTypeSchema) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonUtils(org.apache.camel.component.salesforce.api.utils.JsonUtils) CamelContext(org.apache.camel.CamelContext) DataShape(io.syndesis.common.model.DataShape) SyndesisMetadata(io.syndesis.connector.support.verifier.api.SyndesisMetadata) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) DataShapeKinds(io.syndesis.common.model.DataShapeKinds) List(java.util.List) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Entry(java.util.Map.Entry) SalesforceEndpointConfig(org.apache.camel.component.salesforce.SalesforceEndpointConfig) Collections(java.util.Collections) Json(io.syndesis.common.util.Json) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SyndesisServerException(io.syndesis.common.util.SyndesisServerException)

Example 79 with SalesforceException

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));
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Message(org.apache.camel.Message) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)79 InputStream (java.io.InputStream)35 Request (org.eclipse.jetty.client.api.Request)25 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)12 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)12 DefaultRestClient (org.apache.camel.component.salesforce.internal.client.DefaultRestClient)12 IOException (java.io.IOException)11 BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)11 RestClient (org.apache.camel.component.salesforce.internal.client.RestClient)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 JobInfo (org.apache.camel.component.salesforce.api.dto.bulk.JobInfo)10 AbstractSObjectBase (org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase)9 HashMap (java.util.HashMap)7 CamelException (org.apache.camel.CamelException)7 Message (org.apache.camel.Message)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 List (java.util.List)5 Map (java.util.Map)5 CreateSObjectResult (org.apache.camel.component.salesforce.api.dto.CreateSObjectResult)3 SyncResponseCallback (org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)3