use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class RestApiIntegrationTest method testStatus400.
@Test
public void testStatus400() throws Exception {
// get test merchandise
// note that the header value overrides sObjectFields in endpoint
final Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject", testId, "sObjectFields", "Description__c,Price__c", Merchandise__c.class);
assertNotNull(merchandise);
assertNotNull(merchandise.getPrice__c());
assertNull(merchandise.getTotal_Inventory__c());
merchandise.clearBaseFields();
// required field Total_Inventory__c is missing
CreateSObjectResult result = null;
try {
result = template().requestBody("direct:createSObject", merchandise, CreateSObjectResult.class);
fail("Expected SalesforceException with statusCode 400");
} catch (final CamelExecutionException e) {
assertTrue(e.getCause() instanceof SalesforceException);
assertTrue(e.getCause().getCause() instanceof SalesforceException);
final SalesforceException cause = (SalesforceException) e.getCause().getCause();
assertEquals(400, cause.getStatusCode());
assertEquals(1, cause.getErrors().size());
assertEquals("[Total_Inventory__c]", cause.getErrors().get(0).getFields().toString());
} finally {
// delete the clone if created
if (result != null) {
template().requestBody("direct:deleteSObject", result.getId());
}
}
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class RestApiIntegrationTest method testStatus300.
@Test
public void testStatus300() throws Exception {
// get test merchandise
// note that the header value overrides sObjectFields in endpoint
final Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject", testId, "sObjectFields", "Name,Description__c,Price__c,Total_Inventory__c", Merchandise__c.class);
assertNotNull(merchandise);
assertNotNull(merchandise.getName());
assertNotNull(merchandise.getPrice__c());
assertNotNull(merchandise.getTotal_Inventory__c());
CreateSObjectResult result = null;
try {
merchandise.clearBaseFields();
result = template().requestBody("direct:createSObject", merchandise, CreateSObjectResult.class);
assertNotNull(result);
assertNotNull(result.getId());
// note that the request SObject overrides settings on the endpoint for LineItem__c
try {
template().requestBody("direct:getSObjectWithId", merchandise, Merchandise__c.class);
fail("Expected SalesforceException with statusCode 300");
} catch (final CamelExecutionException e) {
assertTrue(e.getCause() instanceof SalesforceException);
assertTrue(e.getCause().getCause() instanceof SalesforceMultipleChoicesException);
final SalesforceMultipleChoicesException cause = (SalesforceMultipleChoicesException) e.getCause().getCause();
assertEquals(300, cause.getStatusCode());
final List<String> choices = cause.getChoices();
assertNotNull(choices);
assertFalse(choices.isEmpty());
}
} finally {
// delete the test clone
if (result != null) {
template().requestBody("direct:deleteSObject", result.getId());
}
}
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class SalesforceSession method login.
public synchronized String login(String oldToken) throws SalesforceException {
// this way there's always a single valid session
if ((accessToken == null) || accessToken.equals(oldToken)) {
// try revoking the old access token before creating a new one
accessToken = oldToken;
if (accessToken != null) {
try {
logout();
} catch (SalesforceException e) {
LOG.warn("Error revoking old access token: " + e.getMessage(), e);
}
accessToken = null;
}
// login to Salesforce and get session id
final Request loginPost = getLoginRequest(null);
try {
final ContentResponse loginResponse = loginPost.send();
parseLoginResponse(loginResponse, loginResponse.getContentAsString());
} catch (InterruptedException e) {
throw new SalesforceException("Login error: " + e.getMessage(), e);
} catch (TimeoutException e) {
throw new SalesforceException("Login request timeout: " + e.getMessage(), e);
} catch (ExecutionException e) {
throw new SalesforceException("Unexpected login error: " + e.getCause().getMessage(), e.getCause());
}
}
return accessToken;
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class AbstractClientBase method doHttpRequest.
protected void doHttpRequest(final Request request, final ClientResponseCallback callback) {
// Highly memory inefficient,
// but buffer the request content to allow it to be replayed for authentication retries
final ContentProvider content = request.getContent();
if (content instanceof InputStreamContentProvider) {
final List<ByteBuffer> buffers = new ArrayList<>();
for (ByteBuffer buffer : content) {
buffers.add(buffer);
}
request.content(new ByteBufferContentProvider(buffers.toArray(new ByteBuffer[buffers.size()])));
buffers.clear();
}
// execute the request
request.send(new BufferingResponseListener(httpClient.getMaxContentLength()) {
@Override
public void onComplete(Result result) {
Response response = result.getResponse();
if (result.isFailed()) {
// Failure!!!
// including Salesforce errors reported as exception from SalesforceSecurityHandler
Throwable failure = result.getFailure();
if (failure instanceof SalesforceException) {
callback.onResponse(null, (SalesforceException) failure);
} else {
final String msg = String.format("Unexpected error {%s:%s} executing {%s:%s}", response.getStatus(), response.getReason(), request.getMethod(), request.getURI());
callback.onResponse(null, new SalesforceException(msg, response.getStatus(), failure));
}
} else {
// HTTP error status
final int status = response.getStatus();
SalesforceHttpRequest request = (SalesforceHttpRequest) ((SalesforceHttpRequest) result.getRequest()).getConversation().getAttribute(SalesforceSecurityHandler.AUTHENTICATION_REQUEST_ATTRIBUTE);
if (status == HttpStatus.BAD_REQUEST_400 && request != null) {
// parse login error
ContentResponse contentResponse = new HttpContentResponse(response, getContent(), getMediaType(), getEncoding());
try {
session.parseLoginResponse(contentResponse, getContentAsString());
final String msg = String.format("Unexpected Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
callback.onResponse(null, new SalesforceException(msg, null));
} catch (SalesforceException e) {
final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
callback.onResponse(null, new SalesforceException(msg, response.getStatus(), e));
}
} else if (status < HttpStatus.OK_200 || status >= HttpStatus.MULTIPLE_CHOICES_300) {
// Salesforce HTTP failure!
request = (SalesforceHttpRequest) result.getRequest();
final String msg = String.format("Error {%s:%s} executing {%s:%s}", status, response.getReason(), request.getMethod(), request.getURI());
final SalesforceException cause = createRestException(response, getContentAsInputStream());
// for APIs that return body on status 400, such as Composite API we need content as well
callback.onResponse(getContentAsInputStream(), new SalesforceException(msg, response.getStatus(), cause));
} else {
// Success!!!
callback.onResponse(getContentAsInputStream(), null);
}
}
}
@Override
public InputStream getContentAsInputStream() {
if (getContent().length == 0) {
return null;
}
return super.getContentAsInputStream();
}
});
}
use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.
the class DefaultAnalyticsApiClient method getReportResults.
@Override
public void getReportResults(String reportId, String instanceId, final ReportResultsResponseCallback callback) {
final Request request = getRequest(HttpMethod.GET, reportInstancesUrl(reportId, instanceId));
doHttpRequest(request, new ClientResponseCallback() {
@Override
public void onResponse(InputStream response, SalesforceException ex) {
AsyncReportResults reportResults = null;
try {
reportResults = unmarshalResponse(response, request, AsyncReportResults.class);
} catch (SalesforceException e) {
ex = e;
}
callback.onResponse(reportResults, ex);
}
});
}
Aggregations