Search in sources :

Example 1 with IOException

use of java.io.IOException in project camel by apache.

the class CassandraAggregationRepository method add.

/**
     * Insert or update exchange in aggregation table.
     */
@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
    final Object[] idValues = getPKValues(key);
    LOGGER.debug("Inserting key {} exchange {}", idValues, exchange);
    try {
        ByteBuffer marshalledExchange = exchangeCodec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
        Object[] cqlParams = concat(idValues, new Object[] { exchange.getExchangeId(), marshalledExchange });
        getSession().execute(insertStatement.bind(cqlParams));
        return exchange;
    } catch (IOException iOException) {
        throw new CassandraAggregationException("Failed to write exchange", exchange, iOException);
    }
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with IOException

use of java.io.IOException in project camel by apache.

the class CassandraAggregationRepository method get.

/**
     * Get exchange from aggregation table by aggregation key.
     */
@Override
public Exchange get(CamelContext camelContext, String key) {
    Object[] pkValues = getPKValues(key);
    LOGGER.debug("Selecting key {} ", pkValues);
    Row row = getSession().execute(selectStatement.bind(pkValues)).one();
    Exchange exchange = null;
    if (row != null) {
        try {
            exchange = exchangeCodec.unmarshallExchange(camelContext, row.getBytes(exchangeColumn));
        } catch (IOException iOException) {
            throw new CassandraAggregationException("Failed to read exchange", exchange, iOException);
        } catch (ClassNotFoundException classNotFoundException) {
            throw new CassandraAggregationException("Failed to read exchange", exchange, classNotFoundException);
        }
    }
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange) Row(com.datastax.driver.core.Row) IOException(java.io.IOException)

Example 3 with IOException

use of java.io.IOException in project camel by apache.

the class CMSenderOneMessageImpl method doHttpPost.

private void doHttpPost(final String urlString, final String requestString) {
    final HttpClient client = HttpClientBuilder.create().build();
    final HttpPost post = new HttpPost(urlString);
    post.setEntity(new StringEntity(requestString, Charset.forName("UTF-8")));
    try {
        final HttpResponse response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        LOG.debug("Response Code : {}", statusCode);
        if (statusCode == 400) {
            throw new CMDirectException("CM Component and CM API show some kind of inconsistency. " + "CM is complaining about not using a post method for the request. And this component only uses POST requests. What happens?");
        }
        if (statusCode != 200) {
            throw new CMDirectException("CM Component and CM API show some kind of inconsistency. The component expects the status code to be 200 or 400. New api released? ");
        }
        // So we have 200 status code...
        // The response type is 'text/plain' and contains the actual
        // result of the request processing.
        // We obtaing the result text
        final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        final StringBuffer result = new StringBuffer();
        String line = null;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        // ... and process it
        line = result.toString();
        if (!line.isEmpty()) {
            // Line is not empty = error
            LOG.debug("Result of the request processing: FAILED\n{}", line);
            if (line.contains(CMConstants.ERROR_UNKNOWN)) {
                throw new UnknownErrorException();
            } else if (line.contains(CMConstants.ERROR_NO_ACCOUNT)) {
                throw new NoAccountFoundForProductTokenException();
            } else if (line.contains(CMConstants.ERROR_INSUFICIENT_BALANCE)) {
                throw new InsufficientBalanceException();
            } else if (line.contains(CMConstants.ERROR_UNROUTABLE_MESSAGE)) {
                throw new UnroutableMessageException();
            } else if (line.contains(CMConstants.ERROR_INVALID_PRODUCT_TOKEN)) {
                throw new InvalidProductTokenException();
            } else {
                // responsibility
                throw new CMResponseException("CHECK ME. I am not expecting this. ");
            }
        }
        // Ok. Line is EMPTY - successfully submitted
        LOG.debug("Result of the request processing: Successfully submited");
    } catch (final IOException io) {
        throw new CMDirectException(io);
    } catch (Throwable t) {
        if (!(t instanceof CMDirectException)) {
            // Chain it
            t = new CMDirectException(t);
        }
        throw (CMDirectException) t;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CMDirectException(org.apache.camel.component.cm.exceptions.CMDirectException) UnknownErrorException(org.apache.camel.component.cm.exceptions.cmresponse.UnknownErrorException) InputStreamReader(java.io.InputStreamReader) CMResponseException(org.apache.camel.component.cm.exceptions.cmresponse.CMResponseException) InsufficientBalanceException(org.apache.camel.component.cm.exceptions.cmresponse.InsufficientBalanceException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) UnroutableMessageException(org.apache.camel.component.cm.exceptions.cmresponse.UnroutableMessageException) InvalidProductTokenException(org.apache.camel.component.cm.exceptions.cmresponse.InvalidProductTokenException) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader) NoAccountFoundForProductTokenException(org.apache.camel.component.cm.exceptions.cmresponse.NoAccountFoundForProductTokenException)

Example 4 with IOException

use of java.io.IOException in project camel by apache.

the class OsgiClassResolver method loadResourceAsStream.

@Override
public InputStream loadResourceAsStream(String uri) {
    ObjectHelper.notEmpty(uri, "uri");
    String resolvedName = resolveUriPath(uri);
    URL url = loadResourceAsURL(resolvedName);
    InputStream answer = null;
    if (url != null) {
        try {
            answer = url.openStream();
        } catch (IOException ex) {
            throw new RuntimeException("Cannot load resource: " + uri, ex);
        }
    }
    // fallback to default as spring-dm may have issues loading resources
    if (answer == null) {
        answer = super.loadResourceAsStream(uri);
    }
    return answer;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with IOException

use of java.io.IOException in project camel by apache.

the class OsgiClassResolver method loadAllResourcesAsURL.

@Override
public Enumeration<URL> loadAllResourcesAsURL(String uri) {
    ObjectHelper.notEmpty(uri, "uri");
    Vector<URL> answer = new Vector<URL>();
    try {
        String resolvedName = resolveUriPath(uri);
        Enumeration<URL> e = bundleContext.getBundle().getResources(resolvedName);
        while (e != null && e.hasMoreElements()) {
            answer.add(e.nextElement());
        }
        String path = FileUtil.onlyPath(uri);
        String name = FileUtil.stripPath(uri);
        if (path != null && name != null) {
            for (Bundle bundle : bundleContext.getBundles()) {
                LOG.trace("Finding all entries in path: {} with pattern: {}", path, name);
                e = bundle.findEntries(path, name, false);
                while (e != null && e.hasMoreElements()) {
                    answer.add(e.nextElement());
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Cannot load resource: " + uri, e);
    }
    return answer.elements();
}
Also used : Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) Vector(java.util.Vector) URL(java.net.URL)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916