Search in sources :

Example 6 with SalesforceException

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

the class AbstractRestProcessor method processUpdateSobject.

private void processUpdateSobject(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String sObjectName;
    // determine parameters from input AbstractSObject
    final AbstractSObjectBase sObjectBase = exchange.getIn().getBody(AbstractSObjectBase.class);
    String sObjectId;
    if (sObjectBase != null) {
        sObjectName = sObjectBase.getClass().getSimpleName();
        // remember the sObject Id
        sObjectId = sObjectBase.getId();
        // clear base object fields, which cannot be updated
        sObjectBase.clearBaseFields();
    } else {
        sObjectName = getParameter(SOBJECT_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL);
        sObjectId = getParameter(SOBJECT_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
    }
    final String finalsObjectId = sObjectId;
    restClient.updateSObject(sObjectName, sObjectId, getRequestStream(exchange), new RestClient.ResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException exception) {
            processResponse(exchange, response, exception, callback);
            restoreFields(exchange, sObjectBase, finalsObjectId, null, null);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) AbstractSObjectBase(org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase) InputStream(java.io.InputStream) RestClient(org.apache.camel.component.salesforce.internal.client.RestClient) DefaultRestClient(org.apache.camel.component.salesforce.internal.client.DefaultRestClient)

Example 7 with SalesforceException

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

the class AbstractRestProcessor method processApproval.

final void processApproval(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    final TypeConverter converter = exchange.getContext().getTypeConverter();
    final ApprovalRequest approvalRequestFromHeader = getParameter(SalesforceEndpointConfig.APPROVAL, exchange, IGNORE_BODY, IS_OPTIONAL, ApprovalRequest.class);
    final boolean requestGivenInHeader = approvalRequestFromHeader != null;
    // find if there is a ApprovalRequest as `approval` in the message header
    final ApprovalRequest approvalHeader = Optional.ofNullable(approvalRequestFromHeader).orElse(new ApprovalRequest());
    final Message incomingMessage = exchange.getIn();
    final Map<String, Object> incomingHeaders = incomingMessage.getHeaders();
    final boolean requestGivenInParametersInHeader = processApprovalHeaderValues(approvalHeader, incomingHeaders);
    final boolean nothingInheader = !requestGivenInHeader && !requestGivenInParametersInHeader;
    final Object approvalBody = incomingMessage.getBody();
    final boolean bodyIsIterable = approvalBody instanceof Iterable;
    final boolean bodyIsIterableButEmpty = bodyIsIterable && !((Iterable) approvalBody).iterator().hasNext();
    // body contains nothing of interest if it's null, holds an empty iterable or cannot be converted to
    // ApprovalRequest
    final boolean nothingInBody = !(approvalBody != null && !bodyIsIterableButEmpty);
    // we found nothing in the headers or the body
    if (nothingInheader && nothingInBody) {
        throw new SalesforceException("Missing " + SalesforceEndpointConfig.APPROVAL + " parameter in header or ApprovalRequest or List of ApprovalRequests body", 0);
    }
    // let's try to resolve the request body to send
    final ApprovalRequests requestsBody;
    if (nothingInBody) {
        // nothing in body use the header values only
        requestsBody = new ApprovalRequests(approvalHeader);
    } else if (bodyIsIterable) {
        // multiple ApprovalRequests are found
        final Iterable<?> approvalRequests = (Iterable<?>) approvalBody;
        // use header values as template and apply them to the body
        final List<ApprovalRequest> requests = StreamSupport.stream(approvalRequests.spliterator(), false).map(value -> converter.convertTo(ApprovalRequest.class, value)).map(request -> request.applyTemplate(approvalHeader)).collect(Collectors.toList());
        requestsBody = new ApprovalRequests(requests);
    } else {
        // we've looked at the body, and are expecting to see something resembling ApprovalRequest in there
        // but lets see if that is so
        final ApprovalRequest given = converter.tryConvertTo(ApprovalRequest.class, approvalBody);
        final ApprovalRequest request = Optional.ofNullable(given).orElse(new ApprovalRequest()).applyTemplate(approvalHeader);
        requestsBody = new ApprovalRequests(request);
    }
    final InputStream request = getRequestStream(requestsBody);
    restClient.approval(request, (response, exception) -> processResponse(exchange, response, exception, callback));
}
Also used : SOBJECT_CLASS(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_CLASS) SOBJECT_EXT_ID_VALUE(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_EXT_ID_VALUE) Message(org.apache.camel.Message) ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) SOBJECT_BLOB_FIELD_NAME(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_BLOB_FIELD_NAME) SOBJECT_ID(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_ID) SOBJECT_SEARCH(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_SEARCH) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) APEX_URL(org.apache.camel.component.salesforce.SalesforceEndpointConfig.APEX_URL) SOBJECT_FIELDS(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_FIELDS) Matcher(java.util.regex.Matcher) SOBJECT_NAME(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_NAME) SalesforceEndpoint(org.apache.camel.component.salesforce.SalesforceEndpoint) Map(java.util.Map) SOBJECT_QUERY(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_QUERY) StreamSupport(java.util.stream.StreamSupport) APEX_QUERY_PARAM_PREFIX(org.apache.camel.component.salesforce.SalesforceEndpointConfig.APEX_QUERY_PARAM_PREFIX) Method(java.lang.reflect.Method) PayloadFormat(org.apache.camel.component.salesforce.internal.PayloadFormat) ServiceHelper(org.apache.camel.util.ServiceHelper) AbstractSObjectBase(org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase) RestClient(org.apache.camel.component.salesforce.internal.client.RestClient) AsyncCallback(org.apache.camel.AsyncCallback) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) APEX_METHOD(org.apache.camel.component.salesforce.SalesforceEndpointConfig.APEX_METHOD) URLEncoder(java.net.URLEncoder) List(java.util.List) TypeConverter(org.apache.camel.TypeConverter) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) SOBJECT_EXT_ID_NAME(org.apache.camel.component.salesforce.SalesforceEndpointConfig.SOBJECT_EXT_ID_NAME) DefaultRestClient(org.apache.camel.component.salesforce.internal.client.DefaultRestClient) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SalesforceEndpointConfig(org.apache.camel.component.salesforce.SalesforceEndpointConfig) API_VERSION(org.apache.camel.component.salesforce.SalesforceEndpointConfig.API_VERSION) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) Message(org.apache.camel.Message) InputStream(java.io.InputStream) ApprovalRequest(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequest) TypeConverter(org.apache.camel.TypeConverter) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ApprovalRequests(org.apache.camel.component.salesforce.api.dto.approval.ApprovalRequests) List(java.util.List)

Example 8 with SalesforceException

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

the class AbstractRestProcessor method setResponseClass.

private void setResponseClass(Exchange exchange, String sObjectName) throws SalesforceException {
    Class<?> sObjectClass;
    if (sObjectName != null) {
        // lookup class from class map
        sObjectClass = classMap.get(sObjectName);
        if (null == sObjectClass) {
            throw new SalesforceException(String.format("No class found for SObject %s", sObjectName), null);
        }
    } else {
        // use custom response class property
        final String className = getParameter(SOBJECT_CLASS, exchange, IGNORE_BODY, NOT_OPTIONAL);
        try {
            sObjectClass = endpoint.getComponent().getCamelContext().getClassResolver().resolveMandatoryClass(className);
        } catch (ClassNotFoundException e) {
            throw new SalesforceException(String.format("SObject class not found %s, %s", className, e.getMessage()), e);
        }
    }
    exchange.setProperty(RESPONSE_CLASS, sObjectClass);
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException)

Example 9 with SalesforceException

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

the class AbstractRestProcessor method processUpsertSobject.

private void processUpsertSobject(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String sObjectName;
    String sObjectExtIdValue;
    final String sObjectExtIdName = getParameter(SOBJECT_EXT_ID_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL);
    // determine parameters from input AbstractSObject
    Object oldValue = null;
    final AbstractSObjectBase sObjectBase = exchange.getIn().getBody(AbstractSObjectBase.class);
    if (sObjectBase != null) {
        sObjectName = sObjectBase.getClass().getSimpleName();
        oldValue = getAndClearPropertyValue(sObjectBase, sObjectExtIdName);
        sObjectExtIdValue = oldValue.toString();
        // clear base object fields, which cannot be updated
        sObjectBase.clearBaseFields();
    } else {
        sObjectName = getParameter(SOBJECT_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL);
        sObjectExtIdValue = getParameter(SOBJECT_EXT_ID_VALUE, exchange, IGNORE_BODY, NOT_OPTIONAL);
    }
    final Object finalOldValue = oldValue;
    restClient.upsertSObject(sObjectName, sObjectExtIdName, sObjectExtIdValue, getRequestStream(exchange), new RestClient.ResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException exception) {
            processResponse(exchange, response, exception, callback);
            restoreFields(exchange, sObjectBase, null, sObjectExtIdName, finalOldValue);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) AbstractSObjectBase(org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase) InputStream(java.io.InputStream) RestClient(org.apache.camel.component.salesforce.internal.client.RestClient) DefaultRestClient(org.apache.camel.component.salesforce.internal.client.DefaultRestClient)

Example 10 with SalesforceException

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

the class BulkApiProcessor method processGetJob.

private void processGetJob(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    JobInfo jobBody;
    jobBody = exchange.getIn().getBody(JobInfo.class);
    String jobId;
    if (jobBody != null) {
        jobId = jobBody.getId();
    } else {
        jobId = getParameter(JOB_ID, exchange, USE_BODY, NOT_OPTIONAL);
    }
    bulkClient.getJob(jobId, new BulkApiClient.JobInfoResponseCallback() {

        @Override
        public void onResponse(JobInfo jobInfo, SalesforceException ex) {
            processResponse(exchange, jobInfo, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) JobInfo(org.apache.camel.component.salesforce.api.dto.bulk.JobInfo) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient)

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