Search in sources :

Example 11 with SalesforceException

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

the class BulkApiProcessor method processGetResults.

private void processGetResults(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String jobId;
    BatchInfo batchBody;
    String batchId;
    batchBody = exchange.getIn().getBody(BatchInfo.class);
    if (batchBody != null) {
        jobId = batchBody.getJobId();
        batchId = batchBody.getId();
    } else {
        jobId = getParameter(JOB_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
        batchId = getParameter(BATCH_ID, exchange, USE_BODY, NOT_OPTIONAL);
    }
    bulkClient.getResults(jobId, batchId, new BulkApiClient.StreamResponseCallback() {

        @Override
        public void onResponse(InputStream inputStream, SalesforceException ex) {
            // read the result stream into a StreamCache temp file
            // ensures the connection is read
            StreamCache body = null;
            if (inputStream != null) {
                try {
                    body = StreamCacheConverter.convertToStreamCache(inputStream, exchange);
                } catch (IOException e) {
                    String msg = "Error retrieving batch results: " + e.getMessage();
                    ex = new SalesforceException(msg, e);
                } finally {
                    // close the input stream to release the Http connection
                    try {
                        inputStream.close();
                    } catch (IOException ignore) {
                    }
                }
            }
            processResponse(exchange, body, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) StreamCache(org.apache.camel.StreamCache) InputStream(java.io.InputStream) IOException(java.io.IOException) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Example 12 with SalesforceException

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

the class CompositeApiProcessor method processCompositeTreeResponse.

void processCompositeTreeResponse(final Exchange exchange, final Optional<SObjectTreeResponse> responseBody, final SalesforceException exception, final AsyncCallback callback) {
    try {
        if (!responseBody.isPresent()) {
            exchange.setException(exception);
        } else {
            final Message in = exchange.getIn();
            final Message out = exchange.getOut();
            final SObjectTree tree = in.getBody(SObjectTree.class);
            final SObjectTreeResponse response = responseBody.get();
            final boolean hasErrors = response.hasErrors();
            for (final ReferenceId referenceId : response.getResults()) {
                tree.setIdFor(referenceId.getReferenceId(), referenceId.getId());
                if (hasErrors) {
                    tree.setErrorFor(referenceId.getReferenceId(), referenceId.getErrors());
                }
            }
            if (hasErrors) {
                final SalesforceException withErrors = new SalesforceException(response.getAllErrors(), exception.getStatusCode(), exception);
                exchange.setException(withErrors);
            }
            out.copyFromWithNewBody(in, tree);
        }
    } finally {
        // notify callback that exchange is done
        callback.done(false);
    }
}
Also used : SObjectTreeResponse(org.apache.camel.component.salesforce.api.dto.composite.SObjectTreeResponse) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Message(org.apache.camel.Message) ReferenceId(org.apache.camel.component.salesforce.api.dto.composite.ReferenceId) SObjectTree(org.apache.camel.component.salesforce.api.dto.composite.SObjectTree)

Example 13 with SalesforceException

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

the class XmlRestProcessor method getRequestStream.

protected InputStream getRequestStream(Exchange exchange) throws SalesforceException {
    try {
        // get request stream from In message
        Message in = exchange.getIn();
        InputStream request = in.getBody(InputStream.class);
        if (request == null) {
            AbstractDTOBase dto = in.getBody(AbstractDTOBase.class);
            if (dto != null) {
                // marshall the DTO
                request = getRequestStream(dto);
            } else {
                // if all else fails, get body as String
                final String body = in.getBody(String.class);
                if (null == body) {
                    String msg = "Unsupported request message body " + (in.getBody() == null ? null : in.getBody().getClass());
                    throw new SalesforceException(msg, null);
                } else {
                    request = new ByteArrayInputStream(body.getBytes(StringUtil.__UTF8));
                }
            }
        }
        return request;
    } catch (XStreamException e) {
        String msg = "Error marshaling request: " + e.getMessage();
        throw new SalesforceException(msg, e);
    } catch (UnsupportedEncodingException e) {
        String msg = "Error marshaling request: " + e.getMessage();
        throw new SalesforceException(msg, e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) XStreamException(com.thoughtworks.xstream.XStreamException) Message(org.apache.camel.Message) AbstractDTOBase(org.apache.camel.component.salesforce.api.dto.AbstractDTOBase) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with SalesforceException

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

the class PushTopicHelper method createTopic.

private void createTopic() throws CamelException {
    final PushTopic topic = new PushTopic();
    topic.setName(topicName);
    topic.setApiVersion(Double.valueOf(config.getApiVersion()));
    topic.setQuery(config.getSObjectQuery());
    topic.setDescription("Topic created by Camel Salesforce component");
    topic.setNotifyForFields(config.getNotifyForFields());
    if (preApi29) {
        topic.setNotifyForOperations(config.getNotifyForOperations());
    } else {
        topic.setNotifyForOperationCreate(config.getNotifyForOperationCreate());
        topic.setNotifyForOperationDelete(config.getNotifyForOperationDelete());
        topic.setNotifyForOperationUndelete(config.getNotifyForOperationUndelete());
        topic.setNotifyForOperationUpdate(config.getNotifyForOperationUpdate());
    }
    LOG.info("Creating Topic {}: {}", topicName, topic);
    final SyncResponseCallback callback = new SyncResponseCallback();
    try {
        restClient.createSObject(PUSH_TOPIC_OBJECT_NAME, new ByteArrayInputStream(OBJECT_MAPPER.writeValueAsBytes(topic)), callback);
        if (!callback.await(API_TIMEOUT, TimeUnit.SECONDS)) {
            throw new SalesforceException("API call timeout!", null);
        }
        final SalesforceException callbackException = callback.getException();
        if (callbackException != null) {
            throw callbackException;
        }
        CreateSObjectResult result = OBJECT_MAPPER.readValue(callback.getResponse(), CreateSObjectResult.class);
        if (!result.getSuccess()) {
            final SalesforceException salesforceException = new SalesforceException(result.getErrors(), HttpStatus.BAD_REQUEST_400);
            throw new CamelException(String.format("Error creating Topic %s: %s", topicName, result.getErrors()), salesforceException);
        }
    } catch (SalesforceException e) {
        throw new CamelException(String.format("Error creating Topic %s: %s", topicName, e.getMessage()), e);
    } catch (IOException e) {
        throw new CamelException(String.format("Un-marshaling error creating Topic %s: %s", topicName, e.getMessage()), e);
    } catch (InterruptedException e) {
        throw new CamelException(String.format("Un-marshaling error creating Topic %s: %s", topicName, e.getMessage()), e);
    } finally {
        if (callback.getResponse() != null) {
            try {
                callback.getResponse().close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : PushTopic(org.apache.camel.component.salesforce.internal.dto.PushTopic) QueryRecordsPushTopic(org.apache.camel.component.salesforce.internal.dto.QueryRecordsPushTopic) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) CreateSObjectResult(org.apache.camel.component.salesforce.api.dto.CreateSObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CamelException(org.apache.camel.CamelException) IOException(java.io.IOException) SyncResponseCallback(org.apache.camel.component.salesforce.internal.client.SyncResponseCallback)

Example 15 with SalesforceException

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

the class AbstractRestProcessor method getApexUrl.

private String getApexUrl(Exchange exchange) throws SalesforceException {
    final String apexUrl = getParameter(APEX_URL, exchange, IGNORE_BODY, NOT_OPTIONAL);
    final Matcher matcher = URL_TEMPLATE.matcher(apexUrl);
    StringBuilder result = new StringBuilder();
    int start = 0;
    while (matcher.find()) {
        // append part before parameter template
        result.append(apexUrl.substring(start, matcher.start()));
        start = matcher.end();
        // append template value from exchange header
        final String parameterName = matcher.group(1);
        final Object value = exchange.getIn().getHeader(parameterName);
        if (value == null) {
            throw new IllegalArgumentException("Missing APEX URL template header " + parameterName);
        }
        try {
            result.append(URLEncoder.encode(String.valueOf(value), "UTF-8").replaceAll("\\+", "%20"));
        } catch (UnsupportedEncodingException e) {
            throw new SalesforceException("Unexpected error: " + e.getMessage(), e);
        }
    }
    if (start != 0) {
        // append remaining URL
        result.append(apexUrl.substring(start));
        final String resolvedUrl = result.toString();
        log.debug("Resolved APEX URL {} to {}", apexUrl, resolvedUrl);
        return resolvedUrl;
    }
    return apexUrl;
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Matcher(java.util.regex.Matcher) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SalesforceEndpoint(org.apache.camel.component.salesforce.SalesforceEndpoint)

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