Java Code Examples for org.apache.camel.Exchange#getContext()

The following examples show how to use org.apache.camel.Exchange#getContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlipRoutePolicy.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Override
public void onExchangeDone(Route route, Exchange exchange) {
    // decide which route to stop and start
    // basically we should flip the two routes
    String stop = route.getId().equals(name1) ? name1 : name2;
    String start = route.getId().equals(name1) ? name2 : name1;

    CamelContext context = exchange.getContext();
    try {
        // force stopping this route while we are routing an Exchange
        // requires two steps:
        // 1) unregister from the inflight registry
        // 2) stop the route
        context.getInflightRepository().remove(exchange);
        context.stopRoute(stop);
        // then we can start the other route
        context.startRoute(start);
    } catch (Exception e) {
        // let the exception handle handle it, which is often just to log it
        getExceptionHandler().handleException("Error flipping routes", e);
    }
}
 
Example 2
Source File: FlipRoutePolicy.java    From camelinaction with Apache License 2.0 6 votes vote down vote up
@Override
public void onExchangeDone(Route route, Exchange exchange) {
    // decide which route to stop and start
    // basically we should flip the two routes
    String stop = route.getId().equals(name1) ? name1 : name2;
    String start = route.getId().equals(name1) ? name2 : name1;

    CamelContext context = exchange.getContext();
    try {
        // force stopping this route while we are routing an Exchange
        // requires two steps:
        // 1) unregister from the inflight registry
        // 2) stop the route
        context.getInflightRepository().remove(exchange);
        context.stopRoute(stop);
        // then we can start the other route
        context.startRoute(start);
    } catch (Exception e) {
        // let the exception handle handle it, which is often just to log it
        getExceptionHandler().handleException("Error flipping routes", e);
    }
}
 
Example 3
Source File: HttpMessageToDefaultMessageProcessor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Exchange exchange) throws Exception {
    final Message message = exchange.getIn();
    if (message instanceof HttpMessage) {
        final Message replacement = new DefaultMessage(exchange.getContext());
        replacement.copyFrom(message);
        ExchangeHelper.replaceMessage(exchange, replacement, false);
    }
}
 
Example 4
Source File: RouteStoppingProcessor.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Exchange exchange) throws Exception {
    final String routeName = exchange.getIn().getBody(String.class);
    final CamelContext context = exchange.getContext();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                context.stopRoute(routeName);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }).start();
}
 
Example 5
Source File: JobReportingProcessor.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * broadcast a message to all orchestra nodes to flush their execution stats
 *
 * @param exchange
 * @param workNote
 */
private void broadcastFlushStats(Exchange exchange, WorkNote workNote) {
    try {
        ProducerTemplate template = new DefaultProducerTemplate(exchange.getContext());
        template.start();
        template.sendBody(this.commandTopicUri, "jobCompleted|" + workNote.getBatchJobId());
        template.stop();
    } catch (Exception e) {
        LOG.error("Error sending `that's all folks` message to the orchestra", e);
    }
}
 
Example 6
Source File: HttpRequestWrapperProcessor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") // https://github.com/spotbugs/spotbugs/issues/259
public void process(Exchange exchange) throws Exception {
    final Message message = exchange.getIn();
    final Object body = message.getBody();

    final ObjectNode rootNode = JsonUtils.copyObjectMapperConfiguration().createObjectNode();

    if (!parameters.isEmpty()) {
        final ObjectNode parametersNode = rootNode.putObject("parameters");

        final HttpServletRequest servletRequest = message.getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class);
        for (String parameter : parameters) {
            final String[] values;
            final String[] headerValues = message.getHeader(parameter, String[].class);
            if (servletRequest != null && headerValues == null) {
                values = servletRequest.getParameterValues(parameter);
            } else {
                values = headerValues;
            }

            putPrameterValueTo(parametersNode, parameter, values);
        }
    }

    if (body instanceof String) {
        final String string = (String) body;
        if (ObjectHelper.isNotEmpty(string)) {
            rootNode.set("body", READER.readValue(string));
        }
    } else if (body instanceof InputStream) {
        try (InputStream stream = (InputStream) body) {
            if (stream.available() > 0) {
                rootNode.set("body", READER.readValue(stream));
            }
        }
    } else if (body != null) {
        rootNode.putPOJO("body", body);
    }

    final String newBody = JsonUtils.toString(rootNode);
    final Message replacement = new DefaultMessage(exchange.getContext());
    replacement.copyFromWithNewBody(message, newBody);

    // we rely on having the Content-Type match the body when we're
    // extracting parameters from the JSON body, otherwise we don't
    // know if the content is JSON or XML (or any future supported
    // content type)
    replacement.setHeader(Exchange.CONTENT_TYPE, "application/json");

    ExchangeHelper.replaceMessage(exchange, replacement, false);
}