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