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());
}
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");
}
}
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());
}
Aggregations