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);
}
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));
}
}
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);
}
});
}
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);
}
});
}
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));
}
Aggregations