Search in sources :

Example 6 with DefaultExchange

use of org.apache.camel.support.DefaultExchange in project webofneeds by researchstudio-sat.

the class WonMessageSlipComputerTests method testCloseMessageFromNode.

@Test
public void testCloseMessageFromNode() throws Exception {
    Exchange exchange = new DefaultExchange(new DefaultCamelContext());
    exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, dummyMessage);
    exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(WONMSG.CloseMessageString));
    exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_EXTERNAL.getResource().getURI().toString()));
    exchange.getIn().setHeader(WonCamelConstants.SOCKET_TYPE_URI_HEADER, WXCHAT.ChatSocket.asURI());
    String slip = fixedMessageProcessorSlip.evaluate(exchange, String.class);
    Assert.assertEquals("bean:closeMessageFromNodeProcessor?method=process", slip);
}
Also used : DefaultExchange(org.apache.camel.support.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.support.DefaultExchange) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 7 with DefaultExchange

use of org.apache.camel.support.DefaultExchange in project webofneeds by researchstudio-sat.

the class WonMessageSlipComputerTests method testDeactivateAtomMessageFromOwner.

@Test
public void testDeactivateAtomMessageFromOwner() throws Exception {
    Exchange exchange = new DefaultExchange(new DefaultCamelContext());
    exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, dummyMessage);
    exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(WONMSG.DeactivateMessageString));
    exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_OWNER.getResource().getURI().toString()));
    exchange.getIn().setHeader(WonCamelConstants.SOCKET_TYPE_URI_HEADER, WXCHAT.ChatSocket.asURI());
    String slip = fixedMessageProcessorSlip.evaluate(exchange, String.class);
    Assert.assertEquals("bean:deactivateAtomMessageProcessor?method=process", slip);
}
Also used : DefaultExchange(org.apache.camel.support.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.support.DefaultExchange) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 8 with DefaultExchange

use of org.apache.camel.support.DefaultExchange in project ddf by codice.

the class HttpProxyCamelHttpTransportServlet method doService.

@Override
protected void doService(HttpServletRequest oldRequest, HttpServletResponse response) throws ServletException, IOException {
    // Wrap request and clean the query String
    HttpProxyWrappedCleanRequest request = new HttpProxyWrappedCleanRequest(oldRequest);
    log.trace("Service: {}", LogSanitizer.sanitize(request));
    // Is there a consumer registered for the request.
    HttpConsumer consumer = resolve(request);
    if (consumer == null) {
        String path = request.getPathInfo();
        log.trace("Service Request Path = {}", LogSanitizer.sanitize(path));
        String endpointName = getEndpointNameFromPath(path);
        log.trace("Endpoint Name = {}", LogSanitizer.sanitize(endpointName));
        Route route = camelContext.getRoute(endpointName);
        try {
            if (route != null) {
                connect((HttpConsumer) route.getConsumer());
            }
        } catch (Exception e) {
            log.debug("Exception while creating consumer", e);
        }
        consumer = resolve(request);
    }
    try {
        if (consumer == null) {
            log.debug("No consumer to service request {}", LogSanitizer.sanitize(request));
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        // are we suspended?
        if (consumer.getEndpoint().isSuspended()) {
            log.debug("Consumer suspended, cannot service request {}", LogSanitizer.sanitize(request));
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        if (consumer.getEndpoint().getHttpMethodRestrict() != null && !consumer.getEndpoint().getHttpMethodRestrict().equals(request.getMethod())) {
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            return;
        }
        if ("TRACE".equals(request.getMethod()) && !consumer.getEndpoint().isTraceEnabled()) {
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            return;
        }
    } catch (IOException e) {
        LOGGER.warn("Could not send error due to: {}", e.getMessage());
        LOGGER.debug("Could not send error due to: ", e);
    }
    if (consumer == null) {
        return;
    }
    // create exchange and set data on it
    Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
    if (consumer.getEndpoint().isBridgeEndpoint()) {
        exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
    }
    if (consumer.getEndpoint().isDisableStreamCache()) {
        exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
    }
    // we override the classloader before building the HttpMessage just in case the binding
    // does some class resolution
    ClassLoader oldTccl = overrideTccl(exchange);
    HttpHelper.setCharsetFromContentType(request.getContentType(), exchange);
    exchange.setIn(new HttpMessage(exchange, consumer.getEndpoint(), request, response));
    // set context path as header
    String contextPath = consumer.getEndpoint().getPath();
    exchange.getIn().setHeader("CamelServletContextPath", contextPath);
    String httpPath = (String) exchange.getIn().getHeader(Exchange.HTTP_PATH);
    // here we just remove the CamelServletContextPath part from the HTTP_PATH
    if (contextPath != null && httpPath.startsWith(contextPath)) {
        exchange.getIn().setHeader(Exchange.HTTP_PATH, httpPath.substring(contextPath.length()));
    }
    // we want to handle the UoW
    try {
        consumer.createUoW(exchange);
    } catch (Exception e) {
        log.debug(PROCESSING_ERROR_MSG, e);
        throw new ServletException(e);
    }
    try {
        if (log.isTraceEnabled()) {
            log.trace("Processing request for exchangeId: {}", exchange.getExchangeId());
        }
        // process the exchange
        consumer.getProcessor().process(exchange);
    } catch (Exception e) {
        exchange.setException(e);
    }
    try {
        // now lets output to the response
        if (log.isTraceEnabled()) {
            log.trace("Writing response for exchangeId: {}", exchange.getExchangeId());
        }
        Integer bs = consumer.getEndpoint().getResponseBufferSize();
        if (bs != null) {
            log.trace("Using response buffer size: {}", bs);
            response.setBufferSize(bs);
        }
        consumer.getBinding().writeResponse(exchange, response);
    } catch (IOException e) {
        log.debug(PROCESSING_ERROR_MSG, e);
        throw e;
    } catch (Exception e) {
        log.debug(PROCESSING_ERROR_MSG, e);
        throw new ServletException(e);
    } finally {
        consumer.doneUoW(exchange);
        restoreTccl(exchange, oldTccl);
    }
}
Also used : DefaultExchange(org.apache.camel.support.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.support.DefaultExchange) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpMessage(org.apache.camel.http.common.HttpMessage) Route(org.apache.camel.Route) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 9 with DefaultExchange

use of org.apache.camel.support.DefaultExchange in project webofneeds by researchstudio-sat.

the class MessagingServiceImpl method sendInOutMessageGeneric.

/**
 * This method shall be used for Request-Reply messaging.
 *
 * @param properties
 * @param headers
 * @param body
 * @param endpoint
 * @return
 */
public ListenableFuture<String> sendInOutMessageGeneric(Map properties, Map headers, Object body, String endpoint) {
    Exchange exchange = new DefaultExchange(getCamelContext());
    // TODO: the method name shall be set in the header of the message.
    Endpoint ep = getCamelContext().getEndpoint(endpoint);
    if (properties != null) {
        if (properties.containsKey("methodName")) {
            exchange.setProperty("methodName", properties.get("methodName"));
        }
    }
    if (headers != null) {
        exchange.getIn().setHeaders(headers);
    }
    // exchange.getIn().getHeaders().put("CamelJmsRequestTimeout",DEFAULT_JMS_EXPIRATION_TIME);
    // exchange.setProperty("JMSExpiration",DEFAULT_JMS_EXPIRATION_TIME);
    exchange.getIn().setBody(body);
    // exchange.getOut().setBody(body);
    exchange.setPattern(ExchangePattern.InOut);
    final SettableFuture<String> result = SettableFuture.create();
    logger.debug("sending inout message");
    producerTemplate.asyncCallback(ep, exchange, new Synchronization() {

        @Override
        public void onComplete(Exchange exchange) {
            String resultObject = (String) exchange.getOut().getBody();
            result.set(resultObject);
        }

        @Override
        public void onFailure(Exchange exchange) {
            if (exchange.getException() != null) {
                logger.warn("caught exception while sending jms message", exchange.getException());
            }
            result.cancel(true);
        }
    });
    return result;
}
Also used : DefaultExchange(org.apache.camel.support.DefaultExchange) DefaultExchange(org.apache.camel.support.DefaultExchange) Synchronization(org.apache.camel.spi.Synchronization)

Example 10 with DefaultExchange

use of org.apache.camel.support.DefaultExchange in project webofneeds by researchstudio-sat.

the class MessagingServiceImpl method sendInOnlyMessage.

public void sendInOnlyMessage(Map properties, Map headers, Object body, String endpoint) {
    Exchange exchange = new DefaultExchange(getCamelContext());
    exchange.setPattern(ExchangePattern.InOnly);
    Endpoint ep = getCamelContext().getEndpoint(endpoint);
    if (properties != null) {
        if (properties.containsKey("methodName")) {
            exchange.setProperty("methodName", properties.get("methodName"));
        }
        if (properties.containsKey("protocol")) {
            exchange.setProperty("protocol", properties.get("protocol"));
        }
    }
    if (headers != null) {
        exchange.getIn().setHeaders(headers);
    }
    exchange.getIn().setBody(body);
    producerTemplate.send(ep, exchange);
    if (exchange.getException() != null) {
        logger.warn("caught exception while sending jms message", exchange.getException());
    }
}
Also used : DefaultExchange(org.apache.camel.support.DefaultExchange) DefaultExchange(org.apache.camel.support.DefaultExchange)

Aggregations

DefaultExchange (org.apache.camel.support.DefaultExchange)16 Exchange (org.apache.camel.Exchange)13 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)10 Test (org.junit.Test)10 URI (java.net.URI)2 WonMessage (won.protocol.message.WonMessage)2 IOException (java.io.IOException)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ServletException (javax.servlet.ServletException)1 CamelContext (org.apache.camel.CamelContext)1 Route (org.apache.camel.Route)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 HttpConsumer (org.apache.camel.http.common.HttpConsumer)1 HttpMessage (org.apache.camel.http.common.HttpMessage)1 Synchronization (org.apache.camel.spi.Synchronization)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 AbstractCamelProcessor (won.node.camel.processor.AbstractCamelProcessor)1