Search in sources :

Example 66 with CamelExchangeException

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

the class JcloudsComputeProducer method runScriptOnNode.

/**
     * Runs a script on the target node.
     */
protected void runScriptOnNode(Exchange exchange) throws CamelException {
    String script = exchange.getIn().getBody(String.class);
    String nodeId = getNodeId(exchange);
    String user = getUser(exchange);
    LoginCredentials credentials = null;
    if (ObjectHelper.isNotEmpty(user)) {
        credentials = LoginCredentials.builder().user(user).build();
    }
    ExecResponse execResponse = null;
    if (credentials == null) {
        execResponse = computeService.runScriptOnNode(nodeId, script);
    } else {
        execResponse = computeService.runScriptOnNode(nodeId, script, RunScriptOptions.Builder.overrideLoginCredentials(credentials).runAsRoot(false));
    }
    if (execResponse == null) {
        throw new CamelExchangeException("Failed to receive response for run script operation on node: " + nodeId + " using script: " + script, exchange);
    }
    exchange.setProperty(JcloudsConstants.RUN_SCRIPT_ERROR, execResponse.getError());
    exchange.setProperty(JcloudsConstants.RUN_SCRIPT_EXIT_CODE, execResponse.getExitStatus());
    exchange.getOut().setBody(execResponse.getOutput());
}
Also used : LoginCredentials(org.jclouds.domain.LoginCredentials) CamelExchangeException(org.apache.camel.CamelExchangeException) ExecResponse(org.jclouds.compute.domain.ExecResponse)

Example 67 with CamelExchangeException

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

the class JcloudsComputeProducer method createNode.

/**
     * Create a node with the specified group.
     */
protected void createNode(Exchange exchange) throws CamelException {
    String group = getGroup(exchange);
    String imageId = getImageId(exchange);
    String locationId = getLocationId(exchange);
    String hardwareId = getHardwareId(exchange);
    if (ObjectHelper.isEmpty(group)) {
        throw new CamelExchangeException("Group must be specific in the URI or as exchange property for the destroy node operation.", exchange);
    }
    TemplateBuilder builder = computeService.templateBuilder();
    builder.any();
    if (ObjectHelper.isNotEmpty(locationId)) {
        builder.locationId(locationId);
    }
    if (ObjectHelper.isNotEmpty(imageId)) {
        builder.imageId(imageId);
    }
    if (ObjectHelper.isNotEmpty(hardwareId)) {
        builder.hardwareId(hardwareId);
    }
    try {
        Set<? extends NodeMetadata> nodeMetadatas = computeService.createNodesInGroup(group, 1, builder.build());
        exchange.getOut().setBody(nodeMetadatas);
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    } catch (RunNodesException e) {
        throw new CamelExchangeException("Error creating jclouds node.", exchange, e);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) RunNodesException(org.jclouds.compute.RunNodesException) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder)

Example 68 with CamelExchangeException

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

the class JettyHttpProducerConnectionFailureTest method testHttpGetWithParamsViaURI.

@Test
public void testHttpGetWithParamsViaURI() throws Exception {
    // these tests does not run well on Windows
    if (isPlatform("windows")) {
        return;
    }
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(0);
    // give Jetty time to startup properly
    Thread.sleep(1000);
    // use another port with no connection
    try {
        template.requestBody("jetty://http://localhost:9999/myservice", null, Object.class);
        fail("Should have thrown an exception");
    } catch (Exception e) {
        CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        assertIsInstanceOf(IOException.class, cause.getCause());
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) IOException(java.io.IOException) IOException(java.io.IOException) CamelExchangeException(org.apache.camel.CamelExchangeException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 69 with CamelExchangeException

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

the class KestrelProducer method process.

public void process(Exchange exchange) throws Exception {
    String msg = exchange.getIn().getBody(String.class);
    String queue = endpoint.getQueue();
    if (msg != null) {
        try {
            log.debug("Sending to: {} message: {}", queue, msg);
            memcachedClient.set(queue, 0, msg);
        } catch (Exception e) {
            throw new CamelExchangeException("Error sending to: " + queue, exchange, e);
        }
    } else {
        log.debug("No message body to send to: " + queue);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 70 with CamelExchangeException

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

the class MinaProducer method doProcess.

protected void doProcess(Exchange exchange) throws Exception {
    if (session == null && !lazySessionCreation) {
        throw new IllegalStateException("Not started yet!");
    }
    if (session == null || !session.isConnected()) {
        openConnection();
    }
    // set the exchange encoding property
    if (getEndpoint().getConfiguration().getCharsetName() != null) {
        exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getEndpoint().getConfiguration().getCharsetName()));
    }
    Object body = MinaPayloadHelper.getIn(getEndpoint(), exchange);
    if (body == null) {
        noReplyLogger.log("No payload to send for exchange: " + exchange);
        // exit early since nothing to write
        return;
    }
    // if textline enabled then covert to a String which must be used for textline
    if (getEndpoint().getConfiguration().isTextline()) {
        body = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, exchange, body);
    }
    // if sync is true then we should also wait for a response (synchronous mode)
    if (sync) {
        // only initialize latch if we should get a response
        latch = new CountDownLatch(1);
        // reset handler if we expect a response
        ResponseHandler handler = (ResponseHandler) session.getHandler();
        handler.reset();
    }
    // log what we are writing
    if (LOG.isDebugEnabled()) {
        Object out = body;
        if (body instanceof byte[]) {
            // byte arrays is not readable so convert to string
            out = exchange.getContext().getTypeConverter().convertTo(String.class, body);
        }
        LOG.debug("Writing body : {}", out);
    }
    // write the body
    MinaHelper.writeBody(session, body, exchange);
    if (sync) {
        // wait for response, consider timeout
        LOG.debug("Waiting for response using timeout {} millis.", timeout);
        boolean done = latch.await(timeout, TimeUnit.MILLISECONDS);
        if (!done) {
            throw new ExchangeTimedOutException(exchange, timeout);
        }
        // did we get a response
        ResponseHandler handler = (ResponseHandler) session.getHandler();
        if (handler.getCause() != null) {
            throw new CamelExchangeException("Error occurred in ResponseHandler", exchange, handler.getCause());
        } else if (!handler.isMessageReceived()) {
            // no message received
            throw new CamelExchangeException("No response received from remote server: " + getEndpoint().getEndpointUri(), exchange);
        } else {
            // set the result on either IN or OUT on the original exchange depending on its pattern
            if (ExchangeHelper.isOutCapable(exchange)) {
                MinaPayloadHelper.setOut(exchange, handler.getMessage());
            } else {
                MinaPayloadHelper.setIn(exchange, handler.getMessage());
            }
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException)

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