Search in sources :

Example 61 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class RouteboxDirectProducer method process.

public boolean process(Exchange exchange, final AsyncCallback callback) {
    boolean flag = true;
    if ((((RouteboxDirectEndpoint) getRouteboxEndpoint()).getConsumer() == null) && ((getRouteboxEndpoint()).getConfig().isSendToConsumer())) {
        exchange.setException(new CamelExchangeException("No consumers available on endpoint: " + getRouteboxEndpoint(), exchange));
        callback.done(true);
        flag = true;
    } else {
        try {
            LOG.debug("Dispatching to Inner Route {}", exchange);
            RouteboxDispatcher dispatcher = new RouteboxDispatcher(producer);
            exchange = dispatcher.dispatchAsync(getRouteboxEndpoint(), exchange);
            if (getRouteboxEndpoint().getConfig().isSendToConsumer()) {
                AsyncProcessor processor = ((RouteboxDirectEndpoint) getRouteboxEndpoint()).getConsumer().getAsyncProcessor();
                flag = processor.process(exchange, callback);
            }
        } catch (Exception e) {
            getExceptionHandler().handleException("Error processing exchange", exchange, e);
        }
    }
    return flag;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) AsyncProcessor(org.apache.camel.AsyncProcessor) RouteboxDispatcher(org.apache.camel.component.routebox.strategy.RouteboxDispatcher) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 62 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class RouteboxDispatcher method selectDispatchUri.

protected URI selectDispatchUri(RouteboxEndpoint endpoint, Exchange exchange) throws Exception {
    URI dispatchUri;
    List<URI> consumerUris = getInnerContextConsumerList(endpoint.getConfig().getInnerContext());
    if (consumerUris.isEmpty()) {
        throw new CamelExchangeException("No routes found to dispatch in Routebox at " + endpoint, exchange);
    } else if (consumerUris.size() == 1) {
        dispatchUri = consumerUris.get(0);
    } else {
        if (!endpoint.getConfig().getDispatchMap().isEmpty()) {
            // apply URI string found in dispatch Map
            String key = exchange.getIn().getHeader("ROUTE_DISPATCH_KEY", String.class);
            if (endpoint.getConfig().getDispatchMap().containsKey(key)) {
                dispatchUri = new URI(endpoint.getConfig().getDispatchMap().get(key));
            } else {
                throw new CamelExchangeException("No matching entry found in Dispatch Map for ROUTE_DISPATCH_KEY: " + key, exchange);
            }
        } else {
            // apply dispatch strategy
            dispatchUri = endpoint.getConfig().getDispatchStrategy().selectDestinationUri(consumerUris, exchange);
            if (dispatchUri == null) {
                throw new CamelExchangeException("No matching inner routes found for Operation", exchange);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Dispatch URI set to: " + dispatchUri.toASCIIString());
    }
    return dispatchUri;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) URI(java.net.URI)

Example 63 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class HttpProducer method createRequestEntity.

/**
     * Creates a holder object for the data to send to the remote server.
     *
     * @param exchange the exchange with the IN message with data to send
     * @return the data holder
     * @throws CamelExchangeException is thrown if error creating RequestEntity
     */
protected HttpEntity createRequestEntity(Exchange exchange) throws CamelExchangeException {
    Message in = exchange.getIn();
    if (in.getBody() == null) {
        return null;
    }
    HttpEntity answer = in.getBody(HttpEntity.class);
    if (answer == null) {
        try {
            Object data = in.getBody();
            if (data != null) {
                String contentTypeString = ExchangeHelper.getContentType(exchange);
                ContentType contentType = null;
                //it removes "boundary" from Content-Type; I have to use contentType.create method.
                if (contentTypeString != null) {
                    // using ContentType.parser for charset
                    if (contentTypeString.indexOf("charset") > 0) {
                        contentType = ContentType.parse(contentTypeString);
                    } else {
                        contentType = ContentType.create(contentTypeString);
                    }
                }
                if (contentTypeString != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentTypeString)) {
                    if (!getEndpoint().getComponent().isAllowJavaSerializedObject()) {
                        throw new CamelExchangeException("Content-type " + org.apache.camel.http.common.HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
                    }
                    // serialized java object
                    Serializable obj = in.getMandatoryBody(Serializable.class);
                    // write object to output stream
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    HttpHelper.writeObjectToStream(bos, obj);
                    ByteArrayEntity entity = new ByteArrayEntity(bos.toByteArray());
                    entity.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                    IOHelper.close(bos);
                    answer = entity;
                } else if (data instanceof File || data instanceof GenericFile) {
                    // file based (could potentially also be a FTP file etc)
                    File file = in.getBody(File.class);
                    if (file != null) {
                        if (contentType != null) {
                            answer = new FileEntity(file, contentType);
                        } else {
                            answer = new FileEntity(file);
                        }
                    }
                } else if (data instanceof String) {
                    // be a bit careful with String as any type can most likely be converted to String
                    // so we only do an instanceof check and accept String if the body is really a String
                    // do not fallback to use the default charset as it can influence the request
                    // (for example application/x-www-form-urlencoded forms being sent)
                    String charset = IOHelper.getCharsetName(exchange, false);
                    if (charset == null && contentType != null) {
                        // okay try to get the charset from the content-type
                        Charset cs = contentType.getCharset();
                        if (cs != null) {
                            charset = cs.name();
                        }
                    }
                    StringEntity entity = new StringEntity((String) data, charset);
                    if (contentType != null) {
                        entity.setContentType(contentType.toString());
                    }
                    answer = entity;
                }
                // fallback as input stream
                if (answer == null) {
                    // force the body as an input stream since this is the fallback
                    InputStream is = in.getMandatoryBody(InputStream.class);
                    String length = in.getHeader(Exchange.CONTENT_LENGTH, String.class);
                    InputStreamEntity entity = null;
                    if (ObjectHelper.isEmpty(length)) {
                        entity = new InputStreamEntity(is, -1);
                    } else {
                        entity = new InputStreamEntity(is, Long.parseLong(length));
                    }
                    if (contentType != null) {
                        entity.setContentType(contentType.toString());
                    }
                    answer = entity;
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new CamelExchangeException("Error creating RequestEntity from message body", exchange, e);
        } catch (IOException e) {
            throw new CamelExchangeException("Error serializing message body", exchange, e);
        }
    }
    return answer;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) Serializable(java.io.Serializable) FileEntity(org.apache.http.entity.FileEntity) Message(org.apache.camel.Message) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile)

Example 64 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class JsonPathEngine method doRead.

private Object doRead(JsonPath path, Exchange exchange) throws IOException, CamelExchangeException {
    Object json = exchange.getIn().getBody();
    if (json instanceof InputStream) {
        return readWithInputStream(path, exchange);
    } else if (json instanceof GenericFile) {
        LOG.trace("JSonPath: {} is read as generic file from message body: {}", path, json);
        GenericFile<?> genericFile = (GenericFile<?>) json;
        if (genericFile.getCharset() != null) {
            // special treatment for generic file with charset
            InputStream inputStream = new FileInputStream((File) genericFile.getFile());
            return path.read(inputStream, genericFile.getCharset(), configuration);
        }
    }
    if (json instanceof String) {
        LOG.trace("JSonPath: {} is read as String from message body: {}", path, json);
        String str = (String) json;
        return path.read(str, configuration);
    } else if (json instanceof Map) {
        LOG.trace("JSonPath: {} is read as Map from message body: {}", path, json);
        Map map = (Map) json;
        return path.read(map, configuration);
    } else if (json instanceof List) {
        LOG.trace("JSonPath: {} is read as List from message body: {}", path, json);
        List list = (List) json;
        return path.read(list, configuration);
    } else {
        // can we find an adapter which can read the message body
        Object answer = readWithAdapter(path, exchange);
        if (answer == null) {
            // fallback and attempt input stream for any other types
            answer = readWithInputStream(path, exchange);
        }
        if (answer != null) {
            return answer;
        }
    }
    // is json path configured to suppress exceptions
    if (configuration.getOptions().contains(SUPPRESS_EXCEPTIONS)) {
        if (configuration.getOptions().contains(ALWAYS_RETURN_LIST)) {
            return Collections.emptyList();
        } else {
            return null;
        }
    }
    // okay it was not then lets throw a failure
    throw new CamelExchangeException("Cannot read message body as supported JSon value", exchange);
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) List(java.util.List) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) Map(java.util.Map) GenericFile(org.apache.camel.component.file.GenericFile) FileInputStream(java.io.FileInputStream)

Example 65 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class Mina2Helper method writeBody.

/**
     * Asynchronously writes the given body to MINA session. Will wait at most for
     * 10 seconds until the body has been written.
     *
     * @param session  the MINA session
     * @param body     the body to write (send)
     * @param exchange the exchange
     * @throws CamelExchangeException is thrown if the body could not be written for some reasons
     *                                (eg remote connection is closed etc.)
     */
public static void writeBody(IoSession session, Object body, Exchange exchange) throws CamelExchangeException {
    // the write operation is asynchronous. Use WriteFuture to wait until the session has been written
    WriteFuture future = session.write(body);
    // must use a timeout (we use 10s) as in some very high performance scenarios a write can cause 
    // thread hanging forever
    LOG.trace("Waiting for write to complete for body: {} using session: {}", body, session);
    if (!future.awaitUninterruptibly(10000L)) {
        String message = "Cannot write body: " + body.getClass().getCanonicalName() + " using session: " + session;
        if (future.getException() != null) {
            throw new CamelExchangeException(message, exchange, future.getException());
        } else {
            throw new CamelExchangeException(message, exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) WriteFuture(org.apache.mina.core.future.WriteFuture)

Aggregations

CamelExchangeException (org.apache.camel.CamelExchangeException)82 IApplication (com.openshift.client.IApplication)23 Exchange (org.apache.camel.Exchange)17 IOException (java.io.IOException)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 InputStream (java.io.InputStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AsyncCallback (org.apache.camel.AsyncCallback)5 CamelExecutionException (org.apache.camel.CamelExecutionException)5 Message (org.apache.camel.Message)5 File (java.io.File)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Processor (org.apache.camel.Processor)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Serializable (java.io.Serializable)3 URI (java.net.URI)3 List (java.util.List)3