use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class XmlRestProcessor method processResponse.
@Override
protected void processResponse(Exchange exchange, InputStream responseEntity, SalesforceException exception, AsyncCallback callback) {
final XStream localXStream = xStream.get();
try {
// do we need to un-marshal a response
if (responseEntity != null) {
final Class<?> responseClass = exchange.getProperty(RESPONSE_CLASS, Class.class);
Object response;
if (responseClass != null) {
// its ok to call this multiple times, as xstream ignores duplicate calls
localXStream.processAnnotations(responseClass);
final String responseAlias = exchange.getProperty(RESPONSE_ALIAS, String.class);
if (responseAlias != null) {
// extremely dirty, need to flush entire cache if its holding on to an old alias!!!
final CachingMapper mapper = (CachingMapper) localXStream.getMapper();
try {
if (mapper.realClass(responseAlias) != responseClass) {
mapper.flushCache();
}
} catch (CannotResolveClassException ignore) {
// recent XStream versions add a ClassNotFoundException to cache
mapper.flushCache();
}
localXStream.alias(responseAlias, responseClass);
}
response = responseClass.newInstance();
localXStream.fromXML(responseEntity, response);
} else {
// return the response as a stream, for getBlobField
response = responseEntity;
}
exchange.getOut().setBody(response);
} else {
exchange.setException(exception);
}
// copy headers and attachments
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
exchange.getOut().getAttachmentObjects().putAll(exchange.getIn().getAttachmentObjects());
} catch (XStreamException e) {
String msg = "Error parsing XML response: " + e.getMessage();
exchange.setException(new SalesforceException(msg, e));
} catch (Exception e) {
String msg = "Error creating XML response: " + e.getMessage();
exchange.setException(new SalesforceException(msg, e));
} finally {
// cleanup temporary exchange headers
exchange.removeProperty(RESPONSE_CLASS);
exchange.removeProperty(RESPONSE_ALIAS);
// consume response entity
if (responseEntity != null) {
try {
responseEntity.close();
} catch (IOException ignored) {
}
}
// 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.
@Override
protected InputStream getRequestStream(final Object object) throws SalesforceException {
final XStream localXStream = xStream.get();
// first process annotations on the class, for things like alias, etc.
localXStream.processAnnotations(object.getClass());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
// make sure we write the XML with the right encoding
try {
localXStream.toXML(object, new OutputStreamWriter(out, StringUtil.__UTF8));
} catch (UnsupportedEncodingException e) {
String msg = "Error marshaling request: " + e.getMessage();
throw new SalesforceException(msg, e);
}
return new ByteArrayInputStream(out.toByteArray());
}
use of org.apache.camel.component.salesforce.api.SalesforceException 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.api.SalesforceException 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.api.SalesforceException in project camel by apache.
the class SubscriptionHelper method doStart.
@Override
protected void doStart() throws Exception {
// create CometD client
this.client = createClient(component);
// reset all error conditions
handshakeError = null;
handshakeException = null;
connectError = null;
connectException = null;
// listener for handshake error or exception
if (handshakeListener == null) {
// first start
handshakeListener = new ClientSessionChannel.MessageListener() {
public void onMessage(ClientSessionChannel channel, Message message) {
LOG.debug("[CHANNEL:META_HANDSHAKE]: {}", message);
if (!message.isSuccessful()) {
LOG.warn("Handshake failure: {}", message);
handshakeError = (String) message.get(ERROR_FIELD);
handshakeException = getFailure(message);
if (handshakeError != null) {
// refresh oauth token, if it's a 401 error
if (handshakeError.startsWith("401::")) {
try {
LOG.info("Refreshing OAuth token...");
session.login(session.getAccessToken());
LOG.info("Refreshed OAuth token for re-handshake");
} catch (SalesforceException e) {
LOG.error("Error renewing OAuth token on 401 error: " + e.getMessage(), e);
}
}
}
// restart if handshake fails for any reason
restartClient();
} else if (!listenerMap.isEmpty()) {
reconnecting = true;
}
}
};
}
client.getChannel(META_HANDSHAKE).addListener(handshakeListener);
// listener for connect error
if (connectListener == null) {
connectListener = new ClientSessionChannel.MessageListener() {
public void onMessage(ClientSessionChannel channel, Message message) {
LOG.debug("[CHANNEL:META_CONNECT]: {}", message);
if (!message.isSuccessful()) {
LOG.warn("Connect failure: {}", message);
connectError = (String) message.get(ERROR_FIELD);
connectException = getFailure(message);
} else if (reconnecting) {
reconnecting = false;
LOG.debug("Refreshing subscriptions to {} channels on reconnect", listenerMap.size());
// reconnected to Salesforce, subscribe to existing channels
final Map<SalesforceConsumer, ClientSessionChannel.MessageListener> map = new HashMap<SalesforceConsumer, ClientSessionChannel.MessageListener>();
map.putAll(listenerMap);
listenerMap.clear();
for (Map.Entry<SalesforceConsumer, ClientSessionChannel.MessageListener> entry : map.entrySet()) {
final SalesforceConsumer consumer = entry.getKey();
final String topicName = consumer.getTopicName();
subscribe(topicName, consumer);
}
}
}
};
}
client.getChannel(META_CONNECT).addListener(connectListener);
// handle fatal disconnects by reconnecting asynchronously
if (disconnectListener == null) {
disconnectListener = new ClientSessionChannel.MessageListener() {
@Override
public void onMessage(ClientSessionChannel clientSessionChannel, Message message) {
restartClient();
}
};
}
client.getChannel(META_DISCONNECT).addListener(disconnectListener);
// connect to Salesforce cometd endpoint
client.handshake();
final long waitMs = MILLISECONDS.convert(CONNECT_TIMEOUT, SECONDS);
if (!client.waitFor(waitMs, BayeuxClient.State.CONNECTED)) {
if (handshakeException != null) {
throw new CamelException(String.format("Exception during HANDSHAKE: %s", handshakeException.getMessage()), handshakeException);
} else if (handshakeError != null) {
throw new CamelException(String.format("Error during HANDSHAKE: %s", handshakeError));
} else if (connectException != null) {
throw new CamelException(String.format("Exception during CONNECT: %s", connectException.getMessage()), connectException);
} else if (connectError != null) {
throw new CamelException(String.format("Error during CONNECT: %s", connectError));
} else {
throw new CamelException(String.format("Handshake request timeout after %s seconds", CONNECT_TIMEOUT));
}
}
}
Aggregations