use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class WonMessageIntoCamelProcessor method process.
@Override
public void process(final Exchange exchange) throws Exception {
logger.debug("processing won message");
WonMessage wonMessage = null;
Object body = exchange.getIn().getBody();
if (body != null) {
if (body instanceof WonMessage) {
// is the body an already decoded message?
wonMessage = (WonMessage) body;
} else {
try {
// try to decode the body from TriG
wonMessage = WonMessageDecoder.decode(Lang.TRIG, exchange.getIn().getBody().toString());
} catch (Exception e) {
// stop the exchange in this case - maybe at some point we can return a failure
// response but
// currently, we would have to look into the message for doing that, and looking
// into
// the message is not possible if we cannot decode it.
logger.info("could not decode message as TriG, ignoring it (the offending message is logged at loglevel 'DEBUG')", e);
if (logger.isDebugEnabled()) {
logger.debug("offending message: {}", exchange.getIn().getBody().toString());
}
exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
throw new WonMessageProcessingException("Could not decode message", e);
}
}
}
if (wonMessage == null) {
// at this point, lookinto the standard header
Map headers = exchange.getIn().getHeaders();
// if the wonMessage header is there, don't change it - that way we can re-route
// internal messages
wonMessage = (WonMessage) headers.get(WonCamelConstants.MESSAGE_HEADER);
}
if (wonMessage == null) {
throw new WonMessageProcessingException("No WonMessage found in header '" + WonCamelConstants.MESSAGE_HEADER + "' or in the body");
}
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_TYPE_HEADER, URI.create(wonMessage.getMessageType().getResource().getURI()));
exchange.getIn().setHeader(WonCamelConstants.MESSAGE_HEADER, wonMessage);
exchange.getIn().setBody(null);
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class WonMessageFromHeaderToBodySerializingCamelProcessor method process.
@Override
public void process(final Exchange exchange) throws Exception {
logger.debug("processing won message");
Map headers = exchange.getIn().getHeaders();
// if the wonMessage header is there, don't change it - that way we can re-route
// internal messages
WonMessage wonMessage = (WonMessage) headers.get(WonCamelConstants.MESSAGE_HEADER);
if (wonMessage == null) {
throw new WonMessageProcessingException("No WonMessage found in header '" + WonCamelConstants.MESSAGE_HEADER + "'");
}
StringWriter writer = new StringWriter();
RDFDataMgr.write(writer, wonMessage.getCompleteDataset(), Lang.TRIG);
exchange.getIn().setBody(writer.toString());
logger.debug("wrote serialized wonMessage to message body");
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class KeyForNewAtomAddingProcessor method process.
@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
try {
if (message.getMessageType() == WonMessageType.CREATE_ATOM) {
String atomUri = message.getAtomURIRequired().toString();
// generate and add atom's public key to the atom content
String alias = keyPairAliasDerivationStrategy.getAliasForAtomUri(atomUri);
if (cryptographyService.getPrivateKey(alias) == null) {
cryptographyService.createNewKeyPair(alias, alias);
}
PublicKey pubKey = cryptographyService.getPublicKey(alias);
WonKeysReaderWriter keyWriter = new WonKeysReaderWriter();
Model keyGraph = ModelFactory.createDefaultModel();
keyWriter.writeToModel(keyGraph, keyGraph.createResource(atomUri), pubKey);
message.addOrReplaceContentGraph(WonMessage.KEY_URI_SUFFIX, keyGraph);
}
} catch (Exception e) {
logger.error("Failed to add key", e);
throw new WonMessageProcessingException("Failed to add key for atom in message " + message.getMessageURI().toString(), e);
}
return message;
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class SignatureCheckingWonMessageProcessor method process.
@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
StopWatch sw = new StopWatch();
try {
SignatureVerificationState result;
/*
* If the message is a successResponse to a delete Message then we can't check
* the signature as it is stored in the deleted Atom, so we just accept the
* message as valid and return it.
*/
if (message.getRespondingToMessageType() == WonMessageType.DELETE && message.getMessageType() == WonMessageType.SUCCESS_RESPONSE) {
return message;
}
for (WonMessage toCheck : message.getAllMessages()) {
try {
// obtain public keys
sw.start("get public keys");
Map<String, PublicKey> keys = WonKeysReaderWriter.readKeyFromMessage(toCheck);
WonMessageType type = toCheck.getMessageType();
switch(type) {
case CREATE_ATOM:
if (keys.isEmpty()) {
throw new WonMessageProcessingException("No key found in CREATE message");
}
break;
case REPLACE:
if (keys.isEmpty()) {
keys.putAll(getRequiredPublicKeys(toCheck.getCompleteDataset()));
}
break;
default:
if (!keys.isEmpty()) {
throw new WonMessageProcessingException(String.format("An Atom key may only be embedded in CREATE or REPLACE messages! Found one in %s message %s", type, message.getMessageURIRequired()));
}
keys.putAll(getRequiredPublicKeys(toCheck.getCompleteDataset()));
}
sw.stop();
// verify with those public keys
sw.start("verify");
result = WonMessageSignerVerifier.verify(keys, toCheck);
sw.stop();
if (logger.isDebugEnabled()) {
logger.debug("VERIFIED=" + result.isVerificationPassed() + " with keys: " + keys.values() + " for\n" + RdfUtils.writeDatasetToString(Prefixer.setPrefixes(toCheck.getCompleteDataset()), Lang.TRIG));
}
} catch (LinkedDataFetchingException e) {
/*
* If a delete message could not be validated because the atom was already
* deleted, we assume that this message is just mirrored back to the owner and
* is to be accepteed
*/
if (WonMessageType.DELETE.equals(toCheck.getMessageType())) {
if (e.getCause() instanceof HttpClientErrorException && HttpStatus.GONE.equals(((HttpClientErrorException) e.getCause()).getStatusCode())) {
if (logger.isDebugEnabled()) {
logger.debug("Failure during processing signature check of message" + toCheck.getMessageURI() + " (messageType was DELETE, but atom is already deleted, accept message anyway)");
}
return toCheck;
}
}
// TODO SignatureProcessingException?
throw new WonMessageProcessingException("Could not verify message " + toCheck.getMessageURI(), e);
} catch (Exception e) {
// TODO SignatureProcessingException?
throw new WonMessageProcessingException("Could not verify message " + toCheck.getMessageURI(), e);
}
// throw exception if the verification fails:
if (!result.isVerificationPassed()) {
String errormessage = "Message verification failed. Message:" + toCheck.toStringForDebug(false) + ", Problem:" + result.getMessage();
if (logger.isDebugEnabled()) {
logger.debug(errormessage + ". Offending message:\n" + RdfUtils.toString(Prefixer.setPrefixes(toCheck.getCompleteDataset())));
}
// TODO SignatureProcessingException?
throw new WonMessageProcessingException(new SignatureException(errormessage + ". To log the offending message, set Loglevel to DEBUG for logger '" + this.getClass().getName() + "'"));
}
}
return message;
} finally {
logger.debug(LogMarkers.TIMING, "Signature check for message {} took {} millis, details:\n {}", new Object[] { message.getMessageURIRequired(), sw.getTotalTimeMillis(), sw.prettyPrint() });
}
}
use of won.protocol.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.
the class VerifyAndSignExamples method nodeCreateAtomMsg.
@Test
public /*
* Node receives create atom message, verifies it, if verification succeeds -
* adds envelope that includes reference to verified signatures, and signs it.
*/
void nodeCreateAtomMsg() throws Exception {
// create dataset that contains atom core data graph, envelope and its
// signatures.
// this is what nodes receives when the atom is created
Dataset inputDataset = TestSigningUtils.prepareTestDatasetFromNamedGraphs(RESOURCE_FILE, new String[] { ATOM_CORE_DATA_URI, ATOM_CORE_DATA_SIG_URI, EVENT_ENV1_URI, EVENT_ENV1_SIG_URI });
WonMessage inputMessage = WonMessage.of(inputDataset);
// node verifies the signature:
WonMessage verifiedMessage = null;
try {
verifiedMessage = checkingProcessor.process(inputMessage);
} catch (WonMessageProcessingException e) {
Assert.fail("Signature verification failed");
}
// 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(inputMessage.getCompleteDataset(), Lang.TRIG);
WonMessage outputMessage = WonMessage.of(RdfUtils.readDatasetFromString(datasetString, Lang.TRIG));
try {
checkingProcessor.process(outputMessage);
} catch (WonMessageProcessingException e) {
Assert.fail("Signature verification failed");
}
}
Aggregations