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