Search in sources :

Example 51 with WonMessage

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

the class WonMessageSignerVerifierTest method signAndVerifyUnsignedMessage.

@Test
@Ignore
public void signAndVerifyUnsignedMessage() throws Exception {
    // create signed dataset
    Dataset testDataset = TestSigningUtils.prepareTestDataset(RESOURCE_OWNER_FILE_NOSIG);
    WonMessage testMsg = new WonMessage(testDataset);
    // sign
    testMsg = WonMessageSignerVerifier.sign(needKey, pubKeysMap.get(TestSigningUtils.nodeCertUri), TestSigningUtils.needCertUri, testMsg);
    // pretend msg was serialized and deserialized in between
    // pretend it was serialized and deserialized
    String datasetString = RdfUtils.writeDatasetToString(testMsg.getCompleteDataset(), Lang.TRIG);
    testMsg = new WonMessage(RdfUtils.readDatasetFromString(datasetString, Lang.TRIG));
    // verify
    SignatureVerificationState result = WonMessageSignerVerifier.verify(pubKeysMap, testMsg);
    Assert.assertTrue(result.isVerificationPassed());
    Assert.assertEquals(2, result.getSignatureGraphNames().size());
    Assert.assertEquals(NEED_CORE_DATA_URI, result.getSignedGraphName(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals(EVENT_ENV1_URI, result.getSignedGraphName(EVENT_ENV1_SIG_URI));
    // write for debugging
    TestSigningUtils.writeToTempFile(testMsg.getCompleteDataset());
}
Also used : Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with WonMessage

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

the class WonMessageSignerVerifierTest method testVerify.

@Test
public void testVerify() throws Exception {
    // create signed dataset
    Dataset testDataset = TestSigningUtils.prepareTestDataset(RESOURCE_FILE_SIG);
    WonMessage testMsg = new WonMessage(testDataset);
    // verify
    SignatureVerificationState result = WonMessageSignerVerifier.verify(pubKeysMap, testMsg);
    Assert.assertTrue(result.getMessage(), result.isVerificationPassed());
    Assert.assertEquals(3, result.getSignatureGraphNames().size());
    Assert.assertEquals(NEED_CORE_DATA_URI, result.getSignedGraphName(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals(EVENT_ENV1_URI, result.getSignedGraphName(EVENT_ENV1_SIG_URI));
    Assert.assertEquals(EVENT_ENV2_URI, result.getSignedGraphName(EVENT_ENV2_SIG_URI));
}
Also used : Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) Test(org.junit.Test)

Example 53 with WonMessage

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

the class KeyForNewNeedAddingProcessor method process.

@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
    try {
        if (message.getMessageType() == WonMessageType.CREATE_NEED) {
            String needUri = message.getSenderNeedURI().toString();
            Dataset msgDataset = WonMessageEncoder.encodeAsDataset(message);
            // generate and add need's public key to the need content
            String alias = keyPairAliasDerivationStrategy.getAliasForNeedUri(needUri);
            if (cryptographyService.getPrivateKey(alias) == null) {
                cryptographyService.createNewKeyPair(alias, alias);
            }
            PublicKey pubKey = cryptographyService.getPublicKey(alias);
            WonKeysReaderWriter keyWriter = new WonKeysReaderWriter();
            String contentName = message.getContentGraphURIs().get(0);
            Model contentModel = msgDataset.getNamedModel(contentName);
            keyWriter.writeToModel(contentModel, contentModel.createResource(needUri), pubKey);
            return new WonMessage(msgDataset);
        }
    } catch (Exception e) {
        logger.error("Failed to add key", e);
        throw new WonMessageProcessingException("Failed to add key for need in message " + message.getMessageURI().toString());
    }
    return message;
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) Dataset(org.apache.jena.query.Dataset) PublicKey(java.security.PublicKey) WonMessage(won.protocol.message.WonMessage) Model(org.apache.jena.rdf.model.Model) WonKeysReaderWriter(won.cryptography.rdfsign.WonKeysReaderWriter) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException)

Example 54 with WonMessage

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

the class MessageTypeSlipComputer method evaluate.

@Override
public <T> T evaluate(final Exchange exchange, final Class<T> type) {
    WonMessage message = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    assert message != null : "wonMessage header must not be null";
    String slip = "";
    // exchange.getIn().setHeader();
    URI messageType = (URI) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_TYPE_HEADER);
    assert messageType != null : "messageType header must not be null";
    URI direction = (URI) exchange.getIn().getHeader(WonCamelConstants.DIRECTION_HEADER);
    assert direction != null : "direction header must not be null";
    String method = "process";
    if (WonMessageDirection.FROM_EXTERNAL.isIdentifiedBy(direction)) {
        // we are now handling the response to
        if (WonMessageType.SUCCESS_RESPONSE.isIdentifiedBy(messageType)) {
            method = "onSuccessResponse";
            direction = URI.create(WonMessageDirection.FROM_OWNER.getResource().toString());
            WonMessageType origType = message.getIsResponseToMessageType();
            if (origType == null) {
                throw new MissingMessagePropertyException(URI.create(WONMSG.IS_RESPONSE_TO_MESSAGE_TYPE.getURI().toString()));
            }
            messageType = origType.getURI();
        } else if (WonMessageType.FAILURE_RESPONSE.isIdentifiedBy(messageType)) {
            WonMessageType isResponseToType = message.getIsResponseToMessageType();
            if (WonMessageType.FAILURE_RESPONSE == isResponseToType || WonMessageType.SUCCESS_RESPONSE == isResponseToType) {
                // will specially process this.
                return null;
            }
            method = "onFailureResponse";
            direction = URI.create(WonMessageDirection.FROM_OWNER.getResource().toString());
            WonMessageType origType = message.getIsResponseToMessageType();
            if (origType == null) {
                throw new MissingMessagePropertyException(URI.create(WONMSG.IS_RESPONSE_TO_MESSAGE_TYPE.getURI().toString()));
            }
            messageType = origType.getURI();
        }
    }
    try {
        String bean = computeMessageTypeSlip(messageType, direction);
        if (bean == null) {
            return null;
        }
        slip = "bean:" + bean + "?method=" + method;
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return type.cast(slip);
}
Also used : MissingMessagePropertyException(won.protocol.message.processor.exception.MissingMessagePropertyException) WonMessage(won.protocol.message.WonMessage) WonMessageType(won.protocol.message.WonMessageType) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 55 with WonMessage

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

the class CloseMessageFromSystemProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    logger.debug("CLOSE received from the system side for connection {}", wonMessage.getSenderURI());
    Connection con = connectionRepository.findOneByConnectionURIForUpdate(wonMessage.getSenderURI());
    ConnectionState originalState = con.getState();
    // TODO: we could introduce SYSTEM_CLOSE here
    con = dataService.nextConnectionState(con, ConnectionEventType.OWNER_CLOSE);
    // if we know the remote connection, send a close message to the remote connection
    if (con.getRemoteConnectionURI() != null) {
        URI remoteNodeURI = wonNodeInformationService.getWonNodeUri(con.getRemoteConnectionURI());
        URI remoteMessageUri = wonNodeInformationService.generateEventURI(remoteNodeURI);
        // put the factory into the outbound message factory header. It will be used to generate the outbound message
        // after the wonMessage has been processed and saved, to make sure that the outbound message contains
        // all the data that we also store locally
        OutboundMessageFactory outboundMessageFactory = new OutboundMessageFactory(remoteMessageUri, con);
        message.setHeader(WonCamelConstants.OUTBOUND_MESSAGE_FACTORY_HEADER, outboundMessageFactory);
        // set the sender uri in the envelope TODO: TwoMsgs: do not set sender here
        wonMessage.addMessageProperty(WONMSG.SENDER_PROPERTY, con.getConnectionURI());
        // add the information about the corresponding message to the local one
        wonMessage.addMessageProperty(WONMSG.HAS_CORRESPONDING_REMOTE_MESSAGE, remoteMessageUri);
    // the persister will pick it up later
    }
// because the FromSystem message is now in the message header, it will be
// picked up by the routing system and delivered to the owner.
// the message for the remote connection is in the outbound message header and will be
// sent to the remote connection.
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) ConnectionState(won.protocol.model.ConnectionState) URI(java.net.URI)

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