Search in sources :

Example 41 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class RestApiIntegrationTest method testStatus400.

@Test
public void testStatus400() throws Exception {
    // get test merchandise
    // note that the header value overrides sObjectFields in endpoint
    final Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject", testId, "sObjectFields", "Description__c,Price__c", Merchandise__c.class);
    assertNotNull(merchandise);
    assertNotNull(merchandise.getPrice__c());
    assertNull(merchandise.getTotal_Inventory__c());
    merchandise.clearBaseFields();
    // required field Total_Inventory__c is missing
    CreateSObjectResult result = null;
    try {
        result = template().requestBody("direct:createSObject", merchandise, CreateSObjectResult.class);
        fail("Expected SalesforceException with statusCode 400");
    } catch (final CamelExecutionException e) {
        assertTrue(e.getCause() instanceof SalesforceException);
        assertTrue(e.getCause().getCause() instanceof SalesforceException);
        final SalesforceException cause = (SalesforceException) e.getCause().getCause();
        assertEquals(400, cause.getStatusCode());
        assertEquals(1, cause.getErrors().size());
        assertEquals("[Total_Inventory__c]", cause.getErrors().get(0).getFields().toString());
    } finally {
        // delete the clone if created
        if (result != null) {
            template().requestBody("direct:deleteSObject", result.getId());
        }
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) CreateSObjectResult(org.apache.camel.component.salesforce.api.dto.CreateSObjectResult) Merchandise__c(org.apache.camel.component.salesforce.dto.generated.Merchandise__c) Test(org.junit.Test)

Example 42 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class RestApiIntegrationTest method testStatus300.

@Test
public void testStatus300() throws Exception {
    // get test merchandise
    // note that the header value overrides sObjectFields in endpoint
    final Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject", testId, "sObjectFields", "Name,Description__c,Price__c,Total_Inventory__c", Merchandise__c.class);
    assertNotNull(merchandise);
    assertNotNull(merchandise.getName());
    assertNotNull(merchandise.getPrice__c());
    assertNotNull(merchandise.getTotal_Inventory__c());
    CreateSObjectResult result = null;
    try {
        merchandise.clearBaseFields();
        result = template().requestBody("direct:createSObject", merchandise, CreateSObjectResult.class);
        assertNotNull(result);
        assertNotNull(result.getId());
        // note that the request SObject overrides settings on the endpoint for LineItem__c
        try {
            template().requestBody("direct:getSObjectWithId", merchandise, Merchandise__c.class);
            fail("Expected SalesforceException with statusCode 300");
        } catch (final CamelExecutionException e) {
            assertTrue(e.getCause() instanceof SalesforceException);
            assertTrue(e.getCause().getCause() instanceof SalesforceMultipleChoicesException);
            final SalesforceMultipleChoicesException cause = (SalesforceMultipleChoicesException) e.getCause().getCause();
            assertEquals(300, cause.getStatusCode());
            final List<String> choices = cause.getChoices();
            assertNotNull(choices);
            assertFalse(choices.isEmpty());
        }
    } finally {
        // delete the test clone
        if (result != null) {
            template().requestBody("direct:deleteSObject", result.getId());
        }
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) CreateSObjectResult(org.apache.camel.component.salesforce.api.dto.CreateSObjectResult) Merchandise__c(org.apache.camel.component.salesforce.dto.generated.Merchandise__c) SalesforceMultipleChoicesException(org.apache.camel.component.salesforce.api.SalesforceMultipleChoicesException) List(java.util.List) Test(org.junit.Test)

Example 43 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class SalesforceSession method login.

public synchronized String login(String oldToken) throws SalesforceException {
    // this way there's always a single valid session
    if ((accessToken == null) || accessToken.equals(oldToken)) {
        // try revoking the old access token before creating a new one
        accessToken = oldToken;
        if (accessToken != null) {
            try {
                logout();
            } catch (SalesforceException e) {
                LOG.warn("Error revoking old access token: " + e.getMessage(), e);
            }
            accessToken = null;
        }
        // login to Salesforce and get session id
        final Request loginPost = getLoginRequest(null);
        try {
            final ContentResponse loginResponse = loginPost.send();
            parseLoginResponse(loginResponse, loginResponse.getContentAsString());
        } catch (InterruptedException e) {
            throw new SalesforceException("Login error: " + e.getMessage(), e);
        } catch (TimeoutException e) {
            throw new SalesforceException("Login request timeout: " + e.getMessage(), e);
        } catch (ExecutionException e) {
            throw new SalesforceException("Unexpected login error: " + e.getCause().getMessage(), e.getCause());
        }
    }
    return accessToken;
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 44 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class AbstractClientBase method doHttpRequest.

protected void doHttpRequest(final Request request, final ClientResponseCallback callback) {
    // Highly memory inefficient,
    // but buffer the request content to allow it to be replayed for authentication retries
    final ContentProvider content = request.getContent();
    if (content instanceof InputStreamContentProvider) {
        final List<ByteBuffer> buffers = new ArrayList<>();
        for (ByteBuffer buffer : content) {
            buffers.add(buffer);
        }
        request.content(new ByteBufferContentProvider(buffers.toArray(new ByteBuffer[buffers.size()])));
        buffers.clear();
    }
    // execute the request
    request.send(new BufferingResponseListener(httpClient.getMaxContentLength()) {

        @Override
        public void onComplete(Result result) {
            Response response = result.getResponse();
            if (result.isFailed()) {
                // Failure!!!
                // including Salesforce errors reported as exception from SalesforceSecurityHandler
                Throwable failure = result.getFailure();
                if (failure instanceof SalesforceException) {
                    callback.onResponse(null, (SalesforceException) failure);
                } else {
                    final String msg = String.format("Unexpected error {%s:%s} executing {%s:%s}", response.getStatus(), response.getReason(), request.getMethod(), request.getURI());
                    callback.onResponse(null, new SalesforceException(msg, response.getStatus(), failure));
                }
            } else {
                // HTTP error status
                final int status = response.getStatus();
                SalesforceHttpRequest request = (SalesforceHttpRequest) ((SalesforceHttpRequest) result.getRequest()).getConversation().getAttribute(SalesforceSecurityHandler.AUTHENTICATION_REQUEST_ATTRIBUTE);
                if (status == HttpStatus.BAD_REQUEST_400 && request != null) {
                    // parse login error
                    ContentResponse contentResponse = new HttpContentResponse(response, getContent(), getMediaType(), getEncoding());
                    try {
                        session.parseLoginResponse(contentResponse, getContentAsString());
                        final String msg = String.format("Unexpected Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
                        callback.onResponse(null, new SalesforceException(msg, null));
                    } catch (SalesforceException e) {
                        final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
                        callback.onResponse(null, new SalesforceException(msg, response.getStatus(), e));
                    }
                } else if (status < HttpStatus.OK_200 || status >= HttpStatus.MULTIPLE_CHOICES_300) {
                    // Salesforce HTTP failure!
                    request = (SalesforceHttpRequest) result.getRequest();
                    final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
                    final SalesforceException cause = createRestException(response, getContentAsInputStream());
                    // for APIs that return body on status 400, such as Composite API we need content as well
                    callback.onResponse(getContentAsInputStream(), new SalesforceException(msg, response.getStatus(), cause));
                } else {
                    // Success!!!
                    callback.onResponse(getContentAsInputStream(), null);
                }
            }
        }

        @Override
        public InputStream getContentAsInputStream() {
            if (getContent().length == 0) {
                return null;
            }
            return super.getContentAsInputStream();
        }
    });
}
Also used : HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener)

Example 45 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class DefaultAnalyticsApiClient method getReportResults.

@Override
public void getReportResults(String reportId, String instanceId, final ReportResultsResponseCallback callback) {
    final Request request = getRequest(HttpMethod.GET, reportInstancesUrl(reportId, instanceId));
    doHttpRequest(request, new ClientResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException ex) {
            AsyncReportResults reportResults = null;
            try {
                reportResults = unmarshalResponse(response, request, AsyncReportResults.class);
            } catch (SalesforceException e) {
                ex = e;
            }
            callback.onResponse(reportResults, ex);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) InputStream(java.io.InputStream) AsyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.AsyncReportResults) Request(org.eclipse.jetty.client.api.Request)

Aggregations

SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)77 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 CamelException (org.apache.camel.CamelException)7 HashMap (java.util.HashMap)6 Message (org.apache.camel.Message)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 List (java.util.List)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3