use of org.apache.camel.component.salesforce.internal.client.SyncResponseCallback 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.internal.client.SyncResponseCallback in project camel by apache.
the class PushTopicHelper method updateTopic.
private void updateTopic(String topicId) throws CamelException {
final String query = config.getSObjectQuery();
LOG.info("Updating Topic {} with Query [{}]", topicName, query);
final SyncResponseCallback callback = new SyncResponseCallback();
try {
// update the query, notifyForFields and notifyForOperations fields
final PushTopic topic = new PushTopic();
topic.setQuery(query);
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());
}
restClient.updateSObject("PushTopic", topicId, 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;
}
} catch (SalesforceException e) {
throw new CamelException(String.format("Error updating topic %s with query [%s] : %s", topicName, query, e.getMessage()), e);
} catch (InterruptedException e) {
// reset interrupt status
Thread.currentThread().interrupt();
throw new CamelException(String.format("Error updating topic %s with query [%s] : %s", topicName, query, e.getMessage()), e);
} catch (IOException e) {
throw new CamelException(String.format("Error updating topic %s with query [%s] : %s", topicName, query, e.getMessage()), e);
} finally {
if (callback.getResponse() != null) {
try {
callback.getResponse().close();
} catch (IOException ignore) {
}
}
}
}
use of org.apache.camel.component.salesforce.internal.client.SyncResponseCallback in project camel by apache.
the class PushTopicHelper method createOrUpdateTopic.
public void createOrUpdateTopic() throws CamelException {
final String query = config.getSObjectQuery();
final SyncResponseCallback callback = new SyncResponseCallback();
// lookup Topic first
try {
// use SOQL to lookup Topic, since Name is not an external ID!!!
restClient.query("SELECT Id, Name, Query, ApiVersion, IsActive, " + "NotifyForFields, NotifyForOperations, NotifyForOperationCreate, " + "NotifyForOperationDelete, NotifyForOperationUndelete, " + "NotifyForOperationUpdate, Description " + "FROM PushTopic WHERE Name = '" + topicName + "'", 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;
}
QueryRecordsPushTopic records = OBJECT_MAPPER.readValue(callback.getResponse(), QueryRecordsPushTopic.class);
if (records.getTotalSize() == 1) {
PushTopic topic = records.getRecords().get(0);
LOG.info("Found existing topic {}: {}", topicName, topic);
// check if we need to update topic
final boolean notifyOperationsChanged;
if (preApi29) {
notifyOperationsChanged = notEquals(config.getNotifyForOperations(), topic.getNotifyForOperations());
} else {
notifyOperationsChanged = notEquals(config.getNotifyForOperationCreate(), topic.getNotifyForOperationCreate()) || notEquals(config.getNotifyForOperationDelete(), topic.getNotifyForOperationDelete()) || notEquals(config.getNotifyForOperationUndelete(), topic.getNotifyForOperationUndelete()) || notEquals(config.getNotifyForOperationUpdate(), topic.getNotifyForOperationUpdate());
}
if (!query.equals(topic.getQuery()) || notEquals(config.getNotifyForFields(), topic.getNotifyForFields()) || notifyOperationsChanged) {
if (!config.isUpdateTopic()) {
String msg = "Query doesn't match existing Topic and updateTopic is set to false";
throw new CamelException(msg);
}
// otherwise update the topic
updateTopic(topic.getId());
}
} else {
createTopic();
}
} catch (SalesforceException e) {
throw new CamelException(String.format("Error retrieving Topic %s: %s", topicName, e.getMessage()), e);
} catch (IOException e) {
throw new CamelException(String.format("Un-marshaling error retrieving Topic %s: %s", topicName, e.getMessage()), e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CamelException(String.format("Un-marshaling error retrieving Topic %s: %s", topicName, e.getMessage()), e);
} finally {
// close stream to close HttpConnection
if (callback.getResponse() != null) {
try {
callback.getResponse().close();
} catch (IOException e) {
// ignore
}
}
}
}
use of org.apache.camel.component.salesforce.internal.client.SyncResponseCallback in project camel by apache.
the class CamelSalesforceMojo method execute.
/**
* Execute the mojo to generate SObject DTOs
*
* @throws MojoExecutionException
*/
public void execute() throws MojoExecutionException {
engine = createVelocityEngine();
// make sure we can load both templates
if (!engine.resourceExists(SOBJECT_POJO_VM) || !engine.resourceExists(SOBJECT_QUERY_RECORDS_VM) || !engine.resourceExists(SOBJECT_POJO_OPTIONAL_VM) || !engine.resourceExists(SOBJECT_QUERY_RECORDS_OPTIONAL_VM)) {
throw new MojoExecutionException("Velocity templates not found");
}
// connect to Salesforce
final SalesforceHttpClient httpClient = createHttpClient();
final SalesforceSession session = httpClient.getSession();
getLog().info("Salesforce login...");
try {
session.login(null);
} catch (SalesforceException e) {
String msg = "Salesforce login error " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
getLog().info("Salesforce login successful");
// create rest client
RestClient restClient;
try {
restClient = new DefaultRestClient(httpClient, version, PayloadFormat.JSON, session);
// remember to start the active client object
((DefaultRestClient) restClient).start();
} catch (Exception e) {
final String msg = "Unexpected exception creating Rest client: " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
try {
// use Jackson json
final ObjectMapper mapper = JsonUtils.createObjectMapper();
// call getGlobalObjects to get all SObjects
final Set<String> objectNames = new TreeSet<String>();
final SyncResponseCallback callback = new SyncResponseCallback();
try {
getLog().info("Getting Salesforce Objects...");
restClient.getGlobalObjects(callback);
if (!callback.await(responseTimeout, TimeUnit.MILLISECONDS)) {
throw new MojoExecutionException("Timeout waiting for getGlobalObjects!");
}
final SalesforceException ex = callback.getException();
if (ex != null) {
throw ex;
}
final GlobalObjects globalObjects = mapper.readValue(callback.getResponse(), GlobalObjects.class);
// create a list of object names
for (SObject sObject : globalObjects.getSobjects()) {
objectNames.add(sObject.getName());
}
} catch (Exception e) {
String msg = "Error getting global Objects: " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
// check if we are generating POJOs for all objects or not
if ((includes != null && includes.length > 0) || (excludes != null && excludes.length > 0) || ObjectHelper.isNotEmpty(includePattern) || ObjectHelper.isNotEmpty(excludePattern)) {
filterObjectNames(objectNames);
} else {
getLog().warn(String.format("Generating Java classes for all %s Objects, this may take a while...", objectNames.size()));
}
// for every accepted name, get SObject description
final Set<SObjectDescription> descriptions = new HashSet<SObjectDescription>();
getLog().info("Retrieving Object descriptions...");
for (String name : objectNames) {
try {
callback.reset();
restClient.getDescription(name, callback);
if (!callback.await(responseTimeout, TimeUnit.MILLISECONDS)) {
throw new MojoExecutionException("Timeout waiting for getDescription for sObject " + name);
}
final SalesforceException ex = callback.getException();
if (ex != null) {
throw ex;
}
descriptions.add(mapper.readValue(callback.getResponse(), SObjectDescription.class));
} catch (Exception e) {
String msg = "Error getting SObject description for '" + name + "': " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
}
// validate package name
if (!packageName.matches(PACKAGE_NAME_PATTERN)) {
throw new MojoExecutionException("Invalid package name " + packageName);
}
if (outputDirectory.getAbsolutePath().contains("$")) {
outputDirectory = new File("generated-sources/camel-salesforce");
}
final File pkgDir = new File(outputDirectory, packageName.trim().replace('.', File.separatorChar));
if (!pkgDir.exists()) {
if (!pkgDir.mkdirs()) {
throw new MojoExecutionException("Unable to create " + pkgDir);
}
}
getLog().info("Generating Java Classes...");
// generate POJOs for every object description
final GeneratorUtility utility = new GeneratorUtility(useStringsForPicklists);
// should we provide a flag to control timestamp generation?
final String generatedDate = new Date().toString();
for (SObjectDescription description : descriptions) {
try {
processDescription(pkgDir, description, utility, generatedDate);
} catch (IOException e) {
throw new MojoExecutionException("Unable to generate source files for: " + description.getName(), e);
}
}
getLog().info(String.format("Successfully generated %s Java Classes", descriptions.size() * 2));
} finally {
// remember to stop the client
try {
((DefaultRestClient) restClient).stop();
} catch (Exception ignore) {
}
// Salesforce session stop
try {
session.stop();
} catch (Exception ignore) {
}
// release HttpConnections
try {
httpClient.stop();
} catch (Exception ignore) {
}
}
}
Aggregations