Search in sources :

Example 1 with WonMessageSenderException

use of won.protocol.message.sender.exception.WonMessageSenderException in project webofneeds by researchstudio-sat.

the class OwnerWonMessageSenderJMSBased method sendMessage.

/**
 * Signs the message, calculates its messageURI based on content and sends it.
 *
 * @param message
 * @return the updated, final message.
 * @throws WonMessageSenderException
 */
public void sendMessage(WonMessage wonMessage) {
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("sending this message: {}", RdfUtils.writeDatasetToString(wonMessage.getCompleteDataset(), Lang.TRIG));
        }
        URI msgUri = wonMessage.getMessageURIRequired();
        if (!WonMessageUtils.isValidMessageUri(msgUri)) {
            throw new WonMessageSenderException("Not a valid message uri: " + msgUri + ". Did you call prepareMessage(message) first?");
        }
        // ToDo (FS): change it to won node URI and create method in the MessageEvent
        // class
        URI wonNodeUri = wonMessage.getSenderNodeURI();
        if (wonNodeUri == null) {
            // obtain the sender won node from the sender atom
            throw new IllegalStateException("a message needs a SenderNodeUri otherwise we can't determine the won node " + "via which to send it");
        }
        // get the camel endpoint for talking to the WoN node
        String ep = ownerProtocolCommunicationServiceImpl.getProtocolCamelConfigurator().getEndpoint(wonNodeUri);
        if (ep == null) {
            // necessary
            if (!ownerProtocolCommunicationServiceImpl.isRegisteredWithWonNode(wonNodeUri)) {
                ownerProtocolCommunicationServiceImpl.register(wonNodeUri, messagingService);
            }
            // try again to get the endpoint
            ep = ownerProtocolCommunicationServiceImpl.getProtocolCamelConfigurator().getEndpoint(wonNodeUri);
            if (ep == null) {
                throw new Exception("could not obtain camel endpoint for WoN node " + wonNodeUri + " even after trying to re-register");
            }
        }
        List<WonNode> wonNodeList = wonNodeRepository.findByWonNodeURI(wonNodeUri);
        String ownerApplicationId = wonNodeList.get(0).getOwnerApplicationID();
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put(WonCamelConstants.OWNER_APPLICATION_ID_HEADER, ownerApplicationId);
        headerMap.put(WonCamelConstants.REMOTE_BROKER_ENDPOINT_HEADER, ep);
        messagingService.sendInOnlyMessage(null, headerMap, WonMessageEncoder.encode(wonMessage, Lang.TRIG), startingEndpoint);
    // camelContext.getShutdownStrategy().setSuppressLoggingOnTimeout(true);
    } catch (Exception e) {
        throw new RuntimeException("could not send message", e);
    }
}
Also used : WonNode(won.protocol.model.WonNode) HashMap(java.util.HashMap) URI(java.net.URI) WonMessageSenderException(won.protocol.message.sender.exception.WonMessageSenderException) WonMessageSenderException(won.protocol.message.sender.exception.WonMessageSenderException)

Aggregations

URI (java.net.URI)1 HashMap (java.util.HashMap)1 WonMessageSenderException (won.protocol.message.sender.exception.WonMessageSenderException)1 WonNode (won.protocol.model.WonNode)1