Search in sources :

Example 1 with WonMessageBuilder

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

the class NeedManagementService method deactivateNeed.

public void deactivateNeed(URI needURI, String optionalMessage) {
    if (needURI == null) {
        logger.warn("deactivateNeed called but needUri is null - doing nothing");
        return;
    }
    logger.debug("Deactivating need {}", needURI);
    // check if we have that need (e.g. it's not a need living on another node, or does not exist at all)
    Need need = needRepository.findOneByNeedURI(needURI);
    if (need == null) {
        logger.debug("deactivateNeed called for need {} but that need was not found in the repository - doing nothing");
        return;
    }
    URI wonNodeURI = wonNodeInformationService.getWonNodeUri(needURI);
    if (wonNodeURI == null) {
        logger.debug("deactivateNeed called for need {} but we could not find a WonNodeURI for that need - doing nothing");
        return;
    }
    URI messageURI = wonNodeInformationService.generateEventURI(wonNodeURI);
    WonMessageBuilder builder = WonMessageBuilder.setMessagePropertiesForDeactivateFromSystem(messageURI, needURI, wonNodeURI);
    if (optionalMessage != null && optionalMessage.trim().length() > 0) {
        builder.setTextMessage(optionalMessage);
    }
    sendSystemMessage(builder.build());
}
Also used : Need(won.protocol.model.Need) WonMessageBuilder(won.protocol.message.WonMessageBuilder) URI(java.net.URI)

Example 2 with WonMessageBuilder

use of won.protocol.message.WonMessageBuilder 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 3 with WonMessageBuilder

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

the class NeedManagementService method sendTextMessageToOwner.

public void sendTextMessageToOwner(URI needURI, String message) {
    if (needURI == null) {
        logger.warn("sendTextMessageToOwner called but needUri is null - doing nothing");
        return;
    }
    if (message == null || message.trim().length() == 0) {
        logger.warn("sendTextMessageToOwner called for need {}, but message is null or empty - doing nothing");
        return;
    }
    logger.debug("Sending FromSystem text message to need {}", needURI);
    // check if we have that need (e.g. it's not a need living on another node, or does not exist at all)
    Need need = needRepository.findOneByNeedURI(needURI);
    if (need == null) {
        logger.debug("deactivateNeed called for need {} but that need was not found in the repository - doing nothing");
        return;
    }
    URI wonNodeURI = wonNodeInformationService.getWonNodeUri(needURI);
    if (wonNodeURI == null) {
        logger.debug("deactivateNeed called for need {} but we could not find a WonNodeURI for that need - doing nothing");
        return;
    }
    URI messageURI = wonNodeInformationService.generateEventURI(wonNodeURI);
    WonMessageBuilder builder = WonMessageBuilder.setMessagePropertiesForNeedMessageFromSystem(messageURI, needURI, wonNodeURI);
    builder.setTextMessage(message);
    sendSystemMessage(builder.build());
}
Also used : Need(won.protocol.model.Need) WonMessageBuilder(won.protocol.message.WonMessageBuilder) URI(java.net.URI)

Aggregations

WonMessageBuilder (won.protocol.message.WonMessageBuilder)3 URI (java.net.URI)2 Need (won.protocol.model.Need)2 Dataset (org.apache.jena.query.Dataset)1 Test (org.junit.Test)1 WonMessage (won.protocol.message.WonMessage)1 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)1