Search in sources :

Example 96 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class SalesforceOAuthConnection method getSOAPEndpoint.

// it's not necessary for bulk, there is another easy way, looking at genBulkEndpoint
private String getSOAPEndpoint(String id, String type, String accessToken, String version) {
    String endpointURL = null;
    BufferedReader reader = null;
    try {
        URLConnection idConn = new URL(id).openConnection();
        idConn.setRequestProperty("Authorization", type + " " + accessToken);
        reader = new BufferedReader(new InputStreamReader(idConn.getInputStream()));
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(reader);
        JsonNode urls = jsonNode.get("urls");
        endpointURL = urls.get("partner").toString().replace("{version}", version);
        endpointURL = StringUtils.strip(endpointURL, "\"");
    } catch (IOException e) {
        throw new ComponentException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
    }
    return endpointURL;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ComponentException(org.talend.components.api.exception.ComponentException) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 97 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class SalesforceBulkExecReader method getCurrent.

@Override
public IndexedRecord getCurrent() {
    BulkResult result = currentBatchResult.get(resultIndex);
    IndexedRecord record = null;
    try {
        record = ((BulkResultAdapterFactory) getFactory()).convertToAvro(result);
    } catch (IOException e) {
        throw new ComponentException(e);
    }
    if ("true".equalsIgnoreCase((String) result.getValue("Success"))) {
        return record;
    } else {
        Map<String, Object> resultMessage = new HashMap<String, Object>();
        String error = (String) result.getValue("Error");
        resultMessage.put("error", error);
        resultMessage.put("talend_record", record);
        throw new DataRejectException(resultMessage);
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) ComponentException(org.talend.components.api.exception.ComponentException) IOException(java.io.IOException)

Example 98 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class SalesforceBulkRuntime method doBulkQuery.

/**
 * Creates and executes job for bulk query. Job must be finished in 2 minutes on Salesforce side.<br/>
 * From Salesforce documentation two scenarios are possible here:
 * <ul>
 * <li>simple bulk query. It should have status - {@link BatchStateEnum#Completed}.</li>
 * <li>primary key chunking bulk query. It should return first batch info with status - {@link BatchStateEnum#NotProcessed}.<br/>
 * Other batch info's should have status - {@link BatchStateEnum#Completed}</li>
 * </ul>
 *
 * @param moduleName - input module name.
 * @param queryStatement - to be executed.
 * @throws AsyncApiException
 * @throws InterruptedException
 * @throws ConnectionException
 */
public void doBulkQuery(String moduleName, String queryStatement) throws AsyncApiException, InterruptedException, ConnectionException {
    job = new JobInfo();
    job.setObject(moduleName);
    job.setOperation(OperationEnum.query);
    if (concurrencyMode != null) {
        job.setConcurrencyMode(concurrencyMode);
    }
    job.setContentType(ContentType.CSV);
    job = createJob(job);
    if (job.getId() == null) {
        // job creation failed
        throw new ComponentException(new DefaultErrorCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "failedBatch"), ExceptionContext.build().put("failedBatch", job));
    }
    ByteArrayInputStream bout = new ByteArrayInputStream(queryStatement.getBytes());
    BatchInfo info = createBatchFromStream(job, bout);
    int secToWait = 1;
    int tryCount = 0;
    while (true) {
        LOGGER.debug("Awaiting " + secToWait + " seconds for results ...\n" + info);
        Thread.sleep(secToWait * 1000);
        info = getBatchInfo(job.getId(), info.getId());
        if (info.getState() == BatchStateEnum.Completed || (BatchStateEnum.NotProcessed == info.getState() && 0 < chunkSize)) {
            break;
        } else if (info.getState() == BatchStateEnum.Failed) {
            throw new ComponentException(new DefaultErrorCode(HttpServletResponse.SC_BAD_REQUEST, "failedBatch"), ExceptionContext.build().put("failedBatch", info));
        }
        tryCount++;
        if (tryCount % 3 == 0 && secToWait < 120) {
            // after 3 attempt to get the result we multiply the time to wait by 2
            // if secToWait < 120 : don't increase exponentially, no need to sleep more than 128 seconds
            secToWait = secToWait * 2;
        }
        // https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/asynch_api_concepts_limits.htm
        if (jobTimeOut > 0) {
            // if 0, timeout is disabled
            long processingTime = System.currentTimeMillis() - job.getCreatedDate().getTimeInMillis();
            if (processingTime > jobTimeOut) {
                throw new ComponentException(new DefaultErrorCode(HttpServletResponse.SC_REQUEST_TIMEOUT, "failedBatch"), ExceptionContext.build().put("failedBatch", info));
            }
        }
    }
    retrieveResultsOfQuery(info);
}
Also used : DefaultErrorCode(org.talend.daikon.exception.error.DefaultErrorCode) JobInfo(com.sforce.async.JobInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) ComponentException(org.talend.components.api.exception.ComponentException) BatchInfo(com.sforce.async.BatchInfo)

Example 99 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class SalesforceDatasetRuntime method getSchema.

@Override
public Schema getSchema() {
    SalesforceDataprepSource sds = new SalesforceDataprepSource();
    SalesforceInputProperties properties = new SalesforceInputProperties("model");
    properties.setDatasetProperties(dataset);
    sds.initialize(container, properties);
    sds.validate(container);
    try {
        // TODO the UI will be a radio, need to adjust here
        return SalesforceSchemaUtils.getSchema(dataset, sds, container);
    } catch (IOException e) {
        throw new ComponentException(e);
    }
}
Also used : ComponentException(org.talend.components.api.exception.ComponentException) SalesforceInputProperties(org.talend.components.salesforce.dataprep.SalesforceInputProperties) IOException(java.io.IOException)

Example 100 with ComponentException

use of org.talend.components.api.exception.ComponentException in project components by Talend.

the class SnowflakeRowStandalone method runAtDriver.

@Override
public void runAtDriver(RuntimeContainer container) {
    Connection connection = null;
    try {
        connection = createConnection(container);
        if (rowProperties.usePreparedStatement()) {
            try (PreparedStatement pstmt = connection.prepareStatement(rowProperties.getQuery())) {
                SnowflakePreparedStatementUtils.fillPreparedStatement(pstmt, rowProperties.preparedStatementTable);
                pstmt.execute();
            }
        } else {
            try (Statement statement = connection.createStatement()) {
                statement.executeQuery(rowProperties.getQuery());
            }
        }
    } catch (SQLException e) {
        throwComponentException(e, "error.queryExecution");
    } catch (IOException ioe) {
        throwComponentException(ioe, "error.acquiringConnection");
    } finally {
        if (connection != null) {
            try {
                closeConnection(container, connection);
            } catch (SQLException e) {
                throw new ComponentException(e);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ComponentException(org.talend.components.api.exception.ComponentException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Aggregations

ComponentException (org.talend.components.api.exception.ComponentException)101 URL (java.net.URL)32 MalformedURLException (java.net.MalformedURLException)30 JarRuntimeInfo (org.talend.components.api.component.runtime.JarRuntimeInfo)27 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 InvalidKeyException (java.security.InvalidKeyException)14 Schema (org.apache.avro.Schema)14 URISyntaxException (java.net.URISyntaxException)12 StorageException (com.microsoft.azure.storage.StorageException)11 NamedThing (org.talend.daikon.NamedThing)11 ValidationResult (org.talend.daikon.properties.ValidationResult)10 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)10 IndexedRecord (org.apache.avro.generic.IndexedRecord)8 Test (org.junit.Test)8 SimpleNamedThing (org.talend.daikon.SimpleNamedThing)8 NetSuiteException (org.talend.components.netsuite.client.NetSuiteException)6 GeneralSecurityException (java.security.GeneralSecurityException)5 JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)5 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)5