Search in sources :

Example 1 with SalesforceException

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

the class SubscriptionHelper method subscribe.

public void subscribe(final String topicName, final SalesforceConsumer consumer) {
    // create subscription for consumer
    final String channelName = getChannelName(topicName);
    setupReplay((SalesforceEndpoint) consumer.getEndpoint());
    // channel message listener
    LOG.info("Subscribing to channel {}...", channelName);
    final ClientSessionChannel.MessageListener listener = new ClientSessionChannel.MessageListener() {

        @Override
        public void onMessage(ClientSessionChannel channel, Message message) {
            LOG.debug("Received Message: {}", message);
            // convert CometD message to Camel Message
            consumer.processMessage(channel, message);
        }
    };
    final ClientSessionChannel clientChannel = client.getChannel(channelName);
    // listener for subscription
    final ClientSessionChannel.MessageListener subscriptionListener = new ClientSessionChannel.MessageListener() {

        public void onMessage(ClientSessionChannel channel, Message message) {
            LOG.debug("[CHANNEL:META_SUBSCRIBE]: {}", message);
            final String subscribedChannelName = message.get(SUBSCRIPTION_FIELD).toString();
            if (channelName.equals(subscribedChannelName)) {
                if (!message.isSuccessful()) {
                    String error = (String) message.get(ERROR_FIELD);
                    if (error == null) {
                        error = "Missing error message";
                    }
                    Exception failure = getFailure(message);
                    String msg = String.format("Error subscribing to %s: %s", topicName, failure != null ? failure.getMessage() : error);
                    consumer.handleException(msg, new SalesforceException(msg, failure));
                } else {
                    // remember subscription
                    LOG.info("Subscribed to channel {}", subscribedChannelName);
                    listenerMap.put(consumer, listener);
                }
                // remove this subscription listener
                client.getChannel(META_SUBSCRIBE).removeListener(this);
            }
        }
    };
    client.getChannel(META_SUBSCRIBE).addListener(subscriptionListener);
    // subscribe asynchronously
    clientChannel.subscribe(listener);
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Message(org.cometd.bayeux.Message) ClientSessionChannel(org.cometd.bayeux.client.ClientSessionChannel) CamelException(org.apache.camel.CamelException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException)

Example 2 with SalesforceException

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

the class DefaultRestClient method query.

@Override
public void query(String soqlQuery, ResponseCallback callback) {
    try {
        String encodedQuery = urlEncode(soqlQuery);
        final Request get = getRequest(HttpMethod.GET, versionUrl() + "query/?q=" + encodedQuery);
        // requires authorization token
        setAccessToken(get);
        doHttpRequest(get, new DelegatingClientCallback(callback));
    } catch (UnsupportedEncodingException e) {
        String msg = "Unexpected error: " + e.getMessage();
        callback.onResponse(null, new SalesforceException(msg, e));
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Request(org.eclipse.jetty.client.api.Request) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with SalesforceException

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

the class AbstractRestProcessor method processGetBlobField.

private void processGetBlobField(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String sObjectName;
    // get blob field name
    final String sObjectBlobFieldName = getParameter(SOBJECT_BLOB_FIELD_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL);
    // determine parameters from input AbstractSObject
    final AbstractSObjectBase sObjectBase = exchange.getIn().getBody(AbstractSObjectBase.class);
    String sObjectIdValue;
    if (sObjectBase != null) {
        sObjectName = sObjectBase.getClass().getSimpleName();
        sObjectIdValue = sObjectBase.getId();
    } else {
        sObjectName = getParameter(SOBJECT_NAME, exchange, IGNORE_BODY, NOT_OPTIONAL);
        sObjectIdValue = getParameter(SOBJECT_ID, exchange, USE_BODY, NOT_OPTIONAL);
    }
    final String sObjectId = sObjectIdValue;
    restClient.getBlobField(sObjectName, sObjectId, sObjectBlobFieldName, new RestClient.ResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException exception) {
            processResponse(exchange, response, exception, callback);
            restoreFields(exchange, sObjectBase, sObjectId, 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 4 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 5 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)

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