Search in sources :

Example 1 with WonMessageProcessingException

use of won.protocol.message.processor.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");
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) StringWriter(java.io.StringWriter) WonMessage(won.protocol.message.WonMessage) Map(java.util.Map)

Example 2 with WonMessageProcessingException

use of won.protocol.message.processor.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");
    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) {
        try {
            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) {
        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);
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) WonMessage(won.protocol.message.WonMessage) Map(java.util.Map) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException)

Example 3 with WonMessageProcessingException

use of won.protocol.message.processor.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class SignatureAddingWonMessageProcessor method processOnBehalfOfNeed.

public WonMessage processOnBehalfOfNeed(final WonMessage message) throws WonMessageProcessingException {
    // use senderNeed key for signing
    String alias = keyPairAliasDerivationStrategy.getAliasForNeedUri(message.getSenderNeedURI().toString());
    PrivateKey privateKey = cryptographyService.getPrivateKey(alias);
    PublicKey publicKey = cryptographyService.getPublicKey(alias);
    try {
        return processWithKey(message, message.getSenderNeedURI().toString(), privateKey, publicKey);
    } catch (Exception e) {
        logger.error("Failed to sign", e);
        throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString());
    }
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException)

Example 4 with WonMessageProcessingException

use of won.protocol.message.processor.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class SignatureAddingWonMessageProcessor method process.

@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
    // use default key for signing
    PrivateKey privateKey = cryptographyService.getDefaultPrivateKey();
    String webId = cryptographyService.getDefaultPrivateKeyAlias();
    PublicKey publicKey = cryptographyService.getPublicKey(webId);
    try {
        return processWithKey(message, webId, privateKey, publicKey);
    } catch (Exception e) {
        logger.error("Failed to sign", e);
        throw new WonMessageProcessingException("Failed to sign message " + message.getMessageURI().toString());
    }
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException)

Example 5 with WonMessageProcessingException

use of won.protocol.message.processor.exception.WonMessageProcessingException in project webofneeds by researchstudio-sat.

the class SignatureCheckingWonMessageProcessor method process.

@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
    SignatureVerificationState result = null;
    try {
        // obtain public keys
        Map<String, PublicKey> keys = getRequiredPublicKeys(message.getCompleteDataset());
        // verify with those public keys
        result = WonMessageSignerVerifier.verify(keys, message);
        logger.debug("VERIFIED=" + result.isVerificationPassed() + " with keys: " + keys.values() + " for\n" + RdfUtils.writeDatasetToString(message.getCompleteDataset(), Lang.TRIG));
    } catch (Exception e) {
        // TODO SignatureProcessingException?
        throw new WonMessageProcessingException("Could not verify message " + message.getMessageURI(), e);
    }
    // throw exception if the verification fails:
    if (!result.isVerificationPassed()) {
        // TODO SignatureProcessingException?
        throw new WonMessageProcessingException(new SignatureException("Could not verify message " + message.getMessageURI() + ": " + result.getMessage()));
    }
    return message;
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) PublicKey(java.security.PublicKey) SignatureVerificationState(won.cryptography.rdfsign.SignatureVerificationState) SignatureException(java.security.SignatureException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)16 WonMessage (won.protocol.message.WonMessage)11 URI (java.net.URI)6 PublicKey (java.security.PublicKey)4 Map (java.util.Map)4 Dataset (org.apache.jena.query.Dataset)3 Connection (won.protocol.model.Connection)3 Need (won.protocol.model.Need)3 Annotation (java.lang.annotation.Annotation)2 PrivateKey (java.security.PrivateKey)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Test (org.junit.Test)2 StringWriter (java.io.StringWriter)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SignatureException (java.security.SignatureException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 HashSet (java.util.HashSet)1 List (java.util.List)1