Search in sources :

Example 21 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class BulkApiProcessor method processGetAllBatches.

private void processGetAllBatches(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    JobInfo jobBody;
    String jobId;
    jobBody = exchange.getIn().getBody(JobInfo.class);
    if (jobBody != null) {
        jobId = jobBody.getId();
    } else {
        jobId = getParameter(JOB_ID, exchange, USE_BODY, NOT_OPTIONAL);
    }
    bulkClient.getAllBatches(jobId, new BulkApiClient.BatchInfoListResponseCallback() {

        @Override
        public void onResponse(List<BatchInfo> batchInfoList, SalesforceException ex) {
            processResponse(exchange, batchInfoList, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) JobInfo(org.apache.camel.component.salesforce.api.dto.bulk.JobInfo) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Example 22 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class SalesforceSession method logout.

public synchronized void logout() throws SalesforceException {
    if (accessToken == null) {
        return;
    }
    try {
        String logoutUrl = (instanceUrl == null ? config.getLoginUrl() : instanceUrl) + OAUTH2_REVOKE_PATH + accessToken;
        final Request logoutGet = httpClient.newRequest(logoutUrl).timeout(timeout, TimeUnit.MILLISECONDS);
        final ContentResponse logoutResponse = logoutGet.send();
        final int statusCode = logoutResponse.getStatus();
        final String reason = logoutResponse.getReason();
        if (statusCode == HttpStatus.OK_200) {
            LOG.info("Logout successful");
        } else {
            throw new SalesforceException(String.format("Logout error, code: [%s] reason: [%s]", statusCode, reason), statusCode);
        }
    } catch (InterruptedException e) {
        String msg = "Logout error: " + e.getMessage();
        throw new SalesforceException(msg, e);
    } catch (ExecutionException e) {
        final Throwable ex = e.getCause();
        throw new SalesforceException("Unexpected logout exception: " + ex.getMessage(), ex);
    } catch (TimeoutException e) {
        throw new SalesforceException("Logout request TIMEOUT!", null);
    } finally {
        // reset session
        accessToken = null;
        instanceUrl = null;
        // notify all session listeners about logout
        for (SalesforceSessionListener listener : listeners) {
            try {
                listener.onLogout();
            } catch (Throwable t) {
                LOG.warn("Unexpected error from listener {}: {}", listener, t.getMessage());
            }
        }
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 23 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class SalesforceSession method parseLoginResponse.

/**
     * Parses login response, allows SalesforceSecurityHandler to parse a login request for a failed authentication
     * conversation.
     */
public synchronized void parseLoginResponse(ContentResponse loginResponse, String responseContent) throws SalesforceException {
    final int responseStatus = loginResponse.getStatus();
    try {
        switch(responseStatus) {
            case HttpStatus.OK_200:
                // parse the response to get token
                LoginToken token = objectMapper.readValue(responseContent, LoginToken.class);
                // don't log token or instance URL for security reasons
                LOG.info("Login successful");
                accessToken = token.getAccessToken();
                instanceUrl = token.getInstanceUrl();
                // notify all session listeners
                for (SalesforceSessionListener listener : listeners) {
                    try {
                        listener.onLogin(accessToken, instanceUrl);
                    } catch (Throwable t) {
                        LOG.warn("Unexpected error from listener {}: {}", listener, t.getMessage());
                    }
                }
                break;
            case HttpStatus.BAD_REQUEST_400:
                // parse the response to get error
                final LoginError error = objectMapper.readValue(responseContent, LoginError.class);
                final String msg = String.format("Login error code:[%s] description:[%s]", error.getError(), error.getErrorDescription());
                final List<RestError> errors = new ArrayList<RestError>();
                errors.add(new RestError(msg, error.getErrorDescription()));
                throw new SalesforceException(errors, HttpStatus.BAD_REQUEST_400);
            default:
                throw new SalesforceException(String.format("Login error status:[%s] reason:[%s]", responseStatus, loginResponse.getReason()), responseStatus);
        }
    } catch (IOException e) {
        String msg = "Login error: response parse exception " + e.getMessage();
        throw new SalesforceException(msg, e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) RestError(org.apache.camel.component.salesforce.api.dto.RestError) LoginError(org.apache.camel.component.salesforce.internal.dto.LoginError) ArrayList(java.util.ArrayList) LoginToken(org.apache.camel.component.salesforce.internal.dto.LoginToken) IOException(java.io.IOException)

Example 24 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class DefaultAnalyticsApiClient method executeSyncReport.

@Override
public void executeSyncReport(String reportId, Boolean includeDetails, ReportMetadata reportMetadata, final ReportResultsResponseCallback callback) {
    final boolean useGet = reportMetadata == null;
    final Request request = getRequest(useGet ? HttpMethod.GET : HttpMethod.POST, reportsUrl(reportId, includeDetails));
    // set POST data
    if (!useGet) {
        try {
            // wrap reportMetadata in a map
            final HashMap<String, Object> input = new HashMap<String, Object>();
            input.put("reportMetadata", reportMetadata);
            marshalRequest(input, request);
        } catch (SalesforceException e) {
            callback.onResponse(null, e);
            return;
        }
    }
    doHttpRequest(request, new ClientResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException ex) {
            SyncReportResults reportResults = null;
            try {
                reportResults = unmarshalResponse(response, request, SyncReportResults.class);
            } catch (SalesforceException e) {
                ex = e;
            }
            callback.onResponse(reportResults, ex);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) SyncReportResults(org.apache.camel.component.salesforce.api.dto.analytics.reports.SyncReportResults)

Example 25 with SalesforceException

use of org.apache.camel.component.salesforce.api.SalesforceException in project camel by apache.

the class DefaultAnalyticsApiClient method getReportDescription.

@Override
public void getReportDescription(String reportId, final ReportDescriptionResponseCallback callback) {
    final Request request = getRequest(HttpMethod.GET, reportsDescribeUrl(reportId));
    doHttpRequest(request, new ClientResponseCallback() {

        @Override
        public void onResponse(InputStream response, SalesforceException ex) {
            ReportDescription reportDescription = null;
            try {
                reportDescription = unmarshalResponse(response, request, ReportDescription.class);
            } catch (SalesforceException e) {
                ex = e;
            }
            callback.onResponse(reportDescription, ex);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) InputStream(java.io.InputStream) Request(org.eclipse.jetty.client.api.Request) ReportDescription(org.apache.camel.component.salesforce.api.dto.analytics.reports.ReportDescription)

Aggregations

SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)77 InputStream (java.io.InputStream)35 Request (org.eclipse.jetty.client.api.Request)25 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)12 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)12 DefaultRestClient (org.apache.camel.component.salesforce.internal.client.DefaultRestClient)12 IOException (java.io.IOException)11 BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)11 RestClient (org.apache.camel.component.salesforce.internal.client.RestClient)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 JobInfo (org.apache.camel.component.salesforce.api.dto.bulk.JobInfo)10 AbstractSObjectBase (org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase)9 CamelException (org.apache.camel.CamelException)7 HashMap (java.util.HashMap)6 Message (org.apache.camel.Message)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 List (java.util.List)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3