Search in sources :

Example 46 with WonMessage

use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.

the class ShouldCallFacetImplForMessagePredicate method matches.

@Override
public boolean matches(final Exchange exchange) {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    WonMessageType messageType = wonMessage.getMessageType();
    switch(messageType) {
        case DEACTIVATE:
            return false;
        case ACTIVATE:
            return false;
        case CREATE_NEED:
            return false;
        case SUCCESS_RESPONSE:
            return false;
        case FAILURE_RESPONSE:
            return false;
    }
    return true;
}
Also used : WonMessage(won.protocol.message.WonMessage) WonMessageType(won.protocol.message.WonMessageType)

Example 47 with WonMessage

use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.

the class SendMessageFromNodeGroupFacetImpl method process.

@Override
public void process(final Exchange exchange) throws Exception {
    final WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    // whatever happens, this message is not sent to the owner:
    exchange.getIn().setHeader(WonCamelConstants.SUPPRESS_MESSAGE_TO_OWNER, Boolean.TRUE);
    if (messageEventRepository.existEarlierMessageWithSameInnermostMessageURIAndReceiverNeedURI(wonMessage.getMessageURI())) {
        if (logger.isDebugEnabled()) {
            URI innermostMessageURI = wonMessage.getInnermostMessageURI();
            URI groupUri = wonMessage.getReceiverNeedURI();
            logger.debug("suppressing message {} " + "as its innermost message is {} which has already " + "been processed by group {}", new Object[] { wonMessage.getMessageURI(), innermostMessageURI, groupUri });
        }
        return;
    }
    final Connection conOfIncomingMessage = connectionRepository.findByConnectionURI(wonMessage.getReceiverURI()).get(0);
    final List<Connection> consInGroup = connectionRepository.findByNeedURIAndStateAndTypeURI(conOfIncomingMessage.getNeedURI(), ConnectionState.CONNECTED, FacetType.GroupFacet.getURI());
    if (consInGroup == null || consInGroup.size() < 2)
        return;
    if (logger.isDebugEnabled()) {
        logger.debug("processing message {} received from need {} in group {} - preparing to send it to {} group members (text message: '{}'}", new Object[] { wonMessage.getMessageURI(), wonMessage.getSenderNeedURI(), wonMessage.getReceiverNeedURI(), consInGroup.size() - 1, WonRdfUtils.MessageUtils.getTextMessage(wonMessage) });
    }
    for (final Connection conToSendTo : consInGroup) {
        try {
            if (!conToSendTo.equals(conOfIncomingMessage)) {
                if (messageEventRepository.isReceivedSameInnermostMessageFromSender(wonMessage.getMessageURI(), conToSendTo.getRemoteNeedURI())) {
                    if (logger.isDebugEnabled()) {
                        URI innermostMessageURI = wonMessage.getInnermostMessageURI();
                        URI groupUri = wonMessage.getReceiverNeedURI();
                        logger.debug("suppressing forward of message {} to {} in group {}" + "as its innermost message is {} which has already " + "been received from that need", new Object[] { wonMessage.getMessageURI(), conToSendTo.getRemoteNeedURI(), groupUri, innermostMessageURI });
                    }
                    continue;
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("forwarding message {} received from need {} in group {} to group member {}", new Object[] { wonMessage.getMessageURI(), wonMessage.getSenderNeedURI(), wonMessage.getReceiverNeedURI(), conToSendTo.getRemoteNeedURI() });
                }
                URI forwardedMessageURI = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
                URI remoteWonNodeUri = WonLinkedDataUtils.getWonNodeURIForNeedOrConnectionURI(conOfIncomingMessage.getRemoteConnectionURI(), linkedDataSource);
                WonMessage newWonMessage = WonMessageBuilder.forwardReceivedNodeToNodeMessageAsNodeToNodeMessage(forwardedMessageURI, wonMessage, conToSendTo.getConnectionURI(), conToSendTo.getNeedURI(), wonMessage.getReceiverNodeURI(), conToSendTo.getRemoteConnectionURI(), conToSendTo.getRemoteNeedURI(), remoteWonNodeUri);
                sendSystemMessage(newWonMessage);
            }
        } catch (Exception e) {
            logger.warn("caught Exception:", e);
        }
    }
}
Also used : WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 48 with WonMessage

use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.

the class WonMessageSignerVerifier method sign.

public static WonMessage sign(PrivateKey privateKey, PublicKey publicKey, String privateKeyUri, WonMessage message) throws Exception {
    Dataset msgDataset = message.getCompleteDataset();
    SigningStage sigStage = new SigningStage(message);
    addUnreferencedSigReferences(msgDataset, sigStage);
    WonSigner signer = new WonSigner(msgDataset);
    signContents(msgDataset, sigStage, signer, privateKey, privateKeyUri, publicKey);
    signEnvelopes(msgDataset, sigStage, signer, privateKey, privateKeyUri, publicKey);
    return new WonMessage(msgDataset);
}
Also used : SigningStage(won.cryptography.rdfsign.SigningStage) Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) WonSigner(won.cryptography.rdfsign.WonSigner)

Example 49 with WonMessage

use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.

the class VerifyAndSignExamples method ownerCreateNeedMsg.

@Test
public /**
 * Owner Server receives create need message from Owner Client, adds public key (this
 * step is omitted in the below example), and signs the message.
 */
void ownerCreateNeedMsg() throws Exception {
    // create dataset that contains need core data graph
    Dataset inputDataset = TestSigningUtils.prepareTestDatasetFromNamedGraphs(RESOURCE_FILE, new String[] { NEED_CORE_DATA_URI });
    // owner adds need's public key - in this demo this step is omitted and we assume
    // the key is already added - to avoid new key generation each time the demo is run.
    // KeyForNewNeedAddingProcessor processor = new KeyForNewNeedAddingProcessor();
    // WonMessage inputMessage = needKeyGeneratorAndAdder.process(inputMessage);
    // owner adds envelope
    WonMessage wonMessage = new WonMessageBuilder(URI.create(EVENT_ENV1_URI)).setSenderNeedURI(URI.create(NEED_URI)).addContent(inputDataset.getNamedModel(NEED_CORE_DATA_URI)).setWonMessageDirection(WonMessageDirection.FROM_OWNER).build();
    Dataset outputDataset = wonMessage.getCompleteDataset();
    Assert.assertEquals(2, RdfUtils.getModelNames(outputDataset).size());
    // write for debugging
    TestSigningUtils.writeToTempFile(outputDataset);
    // owner signs, - on behalf of need
    WonMessage signedMessage = ownerAddingProcessor.processOnBehalfOfNeed(wonMessage);
    outputDataset = signedMessage.getCompleteDataset();
    // write for debugging
    // TestSigningUtils.writeToTempFile(outputDataset);
    Assert.assertEquals(4, RdfUtils.getModelNames(outputDataset).size());
    // pretend it was serialized and deserialized
    String datasetString = RdfUtils.writeDatasetToString(outputDataset, Lang.JSONLD);
    outputDataset = RdfUtils.readDatasetFromString(datasetString, Lang.JSONLD);
    WonMessage outputMessage = new WonMessage(outputDataset);
    // the receiver of this message should be able to verify it
    try {
        checkingProcessor.process(outputMessage);
    // if we got to here without exceptions - we were able to verify the signature
    } catch (WonMessageProcessingException e) {
        Assert.fail("Signature verification failed");
    }
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) WonMessageBuilder(won.protocol.message.WonMessageBuilder) Test(org.junit.Test)

Example 50 with WonMessage

use of won.protocol.message.WonMessage in project webofneeds by researchstudio-sat.

the class VerifyAndSignExamples method nodeCreateNeedMsg.

@Test
public /**
 * Node receives create need message, verifies it, if verification succeeds -
 * adds envelope that includes reference to verified signatures, and signs it.
 */
void nodeCreateNeedMsg() throws Exception {
    // create dataset that contains need core data graph, envelope and its signatures.
    // this is what nodes receives when the need is created
    Dataset inputDataset = TestSigningUtils.prepareTestDatasetFromNamedGraphs(RESOURCE_FILE, new String[] { NEED_CORE_DATA_URI, NEED_CORE_DATA_SIG_URI, EVENT_ENV1_URI, EVENT_ENV1_SIG_URI });
    WonMessage inputMessage = new WonMessage(inputDataset);
    // node verifies the signature:
    WonMessage verifiedMessage = null;
    try {
        verifiedMessage = checkingProcessor.process(inputMessage);
    } catch (WonMessageProcessingException e) {
        Assert.fail("Signature verification failed");
    }
    // node then process the message in some way, and adds its own envelope,
    // the envelope should contain the reference to the verified signatures
    WonMessage nodeWonMessage = WonMessageBuilder.wrap(verifiedMessage).setWonMessageDirection(WonMessageDirection.FROM_SYSTEM).build();
    Dataset outputDataset = nodeWonMessage.getCompleteDataset();
    Assert.assertEquals(5, RdfUtils.getModelNames(outputDataset).size());
    // write for debugging
    // TestSigningUtils.writeToTempFile(outputDataset);
    // node should then sign its envelope
    WonMessage signedMessage = nodeAddingProcessor.process(nodeWonMessage);
    Assert.assertEquals(6, RdfUtils.getModelNames(outputDataset).size());
    // write for debugging
    // TestSigningUtils.writeToTempFile(outputDataset);
    // everyone should be able to verify this message, inculding when it was read from RDF:
    String datasetString = RdfUtils.writeDatasetToString(signedMessage.getCompleteDataset(), Lang.TRIG);
    WonMessage outputMessage = new WonMessage(RdfUtils.readDatasetFromString(datasetString, Lang.TRIG));
    try {
        checkingProcessor.process(outputMessage);
    } catch (WonMessageProcessingException e) {
        Assert.fail("Signature verification failed");
    }
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) Test(org.junit.Test)

Aggregations

WonMessage (won.protocol.message.WonMessage)89 URI (java.net.URI)47 Connection (won.protocol.model.Connection)19 Test (org.junit.Test)18 Message (org.apache.camel.Message)17 Dataset (org.apache.jena.query.Dataset)17 Exchange (org.apache.camel.Exchange)13 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)13 DefaultExchange (org.apache.camel.impl.DefaultExchange)13 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)11 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)10 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)9 Event (won.bot.framework.eventbot.event.Event)8 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)8 EventListener (won.bot.framework.eventbot.listener.EventListener)7 Need (won.protocol.model.Need)7 WonNodeInformationService (won.protocol.service.WonNodeInformationService)7 Model (org.apache.jena.rdf.model.Model)5 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)5 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)4