Search in sources :

Example 1 with DefaultExchange

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

the class ToOwnerSender method process.

@Override
public void process(Exchange exchange) throws Exception {
    WonMessage msg = getMessageToSendRequired(exchange);
    Objects.requireNonNull(msg);
    URI recipientAtom = getRecipientAtomURIRequired(exchange);
    Optional<Atom> atom = atomService.getAtom(recipientAtom);
    List<OwnerApplication> ownerApps = getOwnerApplications(msg, atom, getOwnerApplicationId(exchange));
    // String methodName =headers.get("methodName").toString();
    logger.debug("number of registered owner applications: {}", ownerApps == null ? 0 : ownerApps.size());
    List<String> queueNames = ownerApps.stream().flatMap(app -> {
        return app.getQueueNames().stream().map(queue -> ownerManagementService.sanitizeQueueNameForOwnerApplication(app, queue)).filter(queue -> ownerManagementService.existsCamelEndpointForOwnerApplicationQueue(queue));
    }).collect(Collectors.toList());
    if (queueNames.isEmpty()) {
        if (logger.isDebugEnabled()) {
            logger.warn("Cannot send message to {} via ownerApplication with Id(s){}: no queues registered", msg.getRecipientAtomURI(), Arrays.toString(ownerApps.stream().map(OwnerApplication::getOwnerApplicationId).collect(Collectors.toList()).toArray()));
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("sending message to owner(s) {}: {}", Arrays.toString(queueNames.toArray()), msg.toStringForDebug(true));
        }
        Exchange exchangeToOwners = new DefaultExchange(camelContext);
        putMessageIntoBody(exchangeToOwners, msg);
        exchangeToOwners.getIn().setHeader(WonCamelConstants.OWNER_APPLICATION_IDS_HEADER, queueNames);
        exchangeToOwners.getIn().setHeader("JMSExpiration", System.currentTimeMillis() + 20000);
        messagingService.send(exchangeToOwners, "direct:sendToOwnerApplications");
    }
    removeMessageToSend(exchange);
}
Also used : CamelContext(org.apache.camel.CamelContext) OwnerApplicationRepository(won.protocol.repository.OwnerApplicationRepository) java.util(java.util) Logger(org.slf4j.Logger) DefaultExchange(org.apache.camel.support.DefaultExchange) AbstractCamelProcessor(won.node.camel.processor.AbstractCamelProcessor) WonCamelHelper(won.node.camel.service.WonCamelHelper) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveMQOwnerManagementService(won.node.service.persistence.ActiveMQOwnerManagementService) Exchange(org.apache.camel.Exchange) Collectors(java.util.stream.Collectors) WonMessage(won.protocol.message.WonMessage) MessagingService(won.protocol.jms.MessagingService) URI(java.net.URI) WonCamelConstants(won.protocol.message.processor.camel.WonCamelConstants) Atom(won.protocol.model.Atom) OwnerApplication(won.protocol.model.OwnerApplication) DefaultExchange(org.apache.camel.support.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.support.DefaultExchange) OwnerApplication(won.protocol.model.OwnerApplication) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 2 with DefaultExchange

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

the class ToNodeSender method process.

public void process(Exchange exchange) throws Exception {
    logger.debug("processing message for sending to remote node");
    WonMessage msg = getMessageToSendRequired(exchange);
    // senderNode may be null, in this case we have to go via activemq
    Optional<URI> senderNode = messageRoutingInfoService.senderNode(msg);
    Optional<URI> recipientNode = messageRoutingInfoService.recipientNode(msg);
    if (!(senderNode.isPresent() && recipientNode.isPresent())) {
        logger.warn("Cannot send message {} to remote node: could not determine sender/recipient node", msg.getMessageURI());
    }
    if (senderNode.get().equals(recipientNode.get())) {
        // sending locally, directly put message into the incoming atom protocol
        Exchange newExchangeFromExternal = new DefaultExchange(camelContext);
        putMessage(newExchangeFromExternal, msg);
        putDirection(newExchangeFromExternal, WonMessageDirection.FROM_EXTERNAL);
        putMessageType(newExchangeFromExternal, msg.getMessageType());
        messagingService.send(newExchangeFromExternal, "seda:msgFromExternal");
        removeMessageToSend(exchange);
        return;
    }
    // add a camel endpoint for the remote won node
    atomProtocolCommunicationService.configureCamelEndpoint(recipientNode.get());
    // send the message to that endpoint
    String ep = atomProtocolCommunicationService.getProtocolCamelConfigurator().getEndpoint(recipientNode.get());
    // messageService.sendInOnlyMessage(null, null, wonMessage,
    // wonMessage.getRecipientNodeURI().toString());
    String msgBody = WonMessageEncoder.encode(msg, Lang.TRIG);
    if (logger.isDebugEnabled()) {
        logger.debug("sending message to node {}: {}", recipientNode, msg.toStringForDebug(true));
    }
    messagingService.sendInOnlyMessage(null, null, msgBody, ep);
    removeMessageToSend(exchange);
}
Also used : DefaultExchange(org.apache.camel.support.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.support.DefaultExchange) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI)

Example 3 with DefaultExchange

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

the class WonMessageSlipComputerTests method testSendMessageFromNodeGroupSocket.

@Test
public void testSendMessageFromNodeGroupSocket() 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.ConnectionMessageString));
    exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WonMessageDirection.FROM_EXTERNAL.getResource().getURI().toString()));
    exchange.getIn().setHeader(WonCamelConstants.SOCKET_TYPE_URI_HEADER, WXGROUP.GroupSocket.asURI());
    String slip = fixedMessageProcessorSlip.evaluate(exchange, String.class);
    Assert.assertEquals("bean:sendMessageFromNodeProcessor?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 4 with DefaultExchange

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

the class WonMessageSlipComputerTests method testActivateAtomMessage.

@Test
public void testActivateAtomMessage() 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.ActivateMessageString));
    exchange.getIn().setHeader(WonCamelConstants.DIRECTION_HEADER, URI.create(WONMSG.FromOwner.getURI().toString()));
    exchange.getIn().setHeader(WonCamelConstants.SOCKET_TYPE_URI_HEADER, WXCHAT.ChatSocket.asURI());
    String slip = fixedMessageProcessorSlip.evaluate(exchange, String.class);
    Assert.assertEquals("bean:activateAtomMessageProcessor?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 5 with DefaultExchange

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

the class WonMessageSlipComputerTests method testCloseMessageFromOwner.

@Test
public void testCloseMessageFromOwner() 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_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:closeMessageFromOwnerProcessor?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)

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