Search in sources :

Example 11 with WonMessageProcessingException

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

the class VerifyAndSignExamples method ownerCreateAtomMsg.

@Test
public /*
     * Owner Server receives create atom message from Owner Client, adds public key
     * (this step is omitted in the below example), and signs the message.
     */
void ownerCreateAtomMsg() throws Exception {
    // create dataset that contains atom core data graph
    Dataset inputDataset = TestSigningUtils.prepareTestDatasetFromNamedGraphs(RESOURCE_FILE, new String[] { ATOM_CORE_DATA_URI });
    // owner adds atom'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.
    // KeyForNewAtomAddingProcessor processor = new KeyForNewAtomAddingProcessor();
    // WonMessage inputMessage = atomKeyGeneratorAndAdder.process(inputMessage);
    // owner adds envelope
    WonMessage wonMessage = WonMessageBuilder.createAtom().atom(URI.create(ATOM_URI)).content().model(inputDataset.getNamedModel(ATOM_CORE_DATA_URI)).direction().fromOwner().build();
    Dataset outputDataset = wonMessage.getCompleteDataset();
    Assert.assertEquals(2, RdfUtils.getModelNames(outputDataset).size());
    // write for debugging
    TestSigningUtils.writeToTempFile(outputDataset);
    // owner signs, - on behalf of atom
    WonMessage signedMessage = ownerAddingProcessor.signWithAtomKey(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 = WonMessage.of(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.exception.WonMessageProcessingException) Dataset(org.apache.jena.query.Dataset) WonMessage(won.protocol.message.WonMessage) Test(org.junit.Test)

Example 12 with WonMessageProcessingException

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

the class DeactivateAtomMessageFromSystemReactionProcessor method process.

public void process(final Exchange exchange) throws Exception {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI recipientAtomURI = wonMessage.getRecipientAtomURI();
    logger.debug("DEACTIVATING atom. atomURI:{}", recipientAtomURI);
    if (recipientAtomURI == null)
        throw new WonMessageProcessingException("recipientAtomURI is not set");
    Atom atom = atomService.getAtomRequired(recipientAtomURI);
    matcherProtocolMatcherClient.atomDeactivated(atom.getAtomURI(), wonMessage);
    // close all connections
    Collection<Connection> conns = connectionRepository.findByAtomURIAndNotStateForUpdate(atom.getAtomURI(), ConnectionState.CLOSED);
    for (Connection con : conns) {
        entityManager.refresh(con);
        closeConnection(atom, con);
    }
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 13 with WonMessageProcessingException

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

the class DeleteAtomMessageFromOwnerReactionProcessor method process.

public void process(final Exchange exchange) throws Exception {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI recipientAtomURI = wonMessage.getRecipientAtomURI();
    logger.debug("DELETING atom. atomURI:{}", recipientAtomURI);
    if (recipientAtomURI == null) {
        throw new WonMessageProcessingException("recipientAtomURI is not set");
    }
    Atom atom = atomService.getAtomRequired(recipientAtomURI);
    matcherProtocolMatcherClient.atomDeleted(atom.getAtomURI(), wonMessage);
    // Check if atom already in State DELETED
    if (atom.getState() == AtomState.DELETED) {
        // Get all connections of this atom
        Collection<Connection> conns = connectionRepository.findByAtomURIAndNotState(atom.getAtomURI(), ConnectionState.DELETED);
        for (Connection con : conns) {
            entityManager.refresh(con);
            // Delete all connection data
            messageEventRepository.deleteByParentURI(con.getConnectionURI());
            connectionRepository.delete(con);
        }
    } else {
        // Get only not closed connections of this atom to close them
        Collection<Connection> conns = connectionRepository.findByAtomURIAndNotState(atom.getAtomURI(), ConnectionState.CLOSED);
        // Close open connections
        for (Connection con : conns) {
            entityManager.refresh(con);
            closeConnection(atom, con);
        }
    }
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 14 with WonMessageProcessingException

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

the class MessageTypeSlipComputer method computeMessageTypeSlip.

private String computeMessageTypeSlip(URI messageType, URI direction) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Iterator iter = fixedMessageProcessorsMap.entrySet().iterator();
    StringBuilder slipBuilder = new StringBuilder();
    while (iter.hasNext()) {
        Map.Entry pair = (Map.Entry) iter.next();
        Processor wonMessageProcessor = (Processor) pair.getValue();
        Annotation annotation = AopUtils.getTargetClass(wonMessageProcessor).getAnnotation(annotationClazz);
        if (matches(annotation, messageType, direction, null)) {
            slipBuilder.append("bean:").append(pair.getKey().toString()).append("?method=process,");
        }
    }
    String slip = slipBuilder.toString();
    if (!slip.isEmpty()) {
        // cut off the trailing comma
        return slip.substring(0, slip.length() - 1);
    }
    if (allowNoMatchingProcessor) {
        return null;
    }
    logger.debug("unexpected combination of messageType {} and direction {} encountered " + "- this causes an exception,which triggers a FailureResponse", messageType, direction);
    throw new WonMessageProcessingException(String.format("unexpected combination of messageType %s " + "and direction %s encountered", messageType, direction));
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) Processor(org.apache.camel.Processor) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map) Annotation(java.lang.annotation.Annotation)

Example 15 with WonMessageProcessingException

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

the class SocketTypeSlipComputer method computeSocketSlip.

private String computeSocketSlip(URI messageType, URI socketType, URI direction) {
    if (socketType != null) {
        Optional<String> processorName = socketMessageProcessorsMap.entrySet().stream().filter(entry -> {
            Object socket = entry.getValue();
            Annotation annotation = AopUtils.getTargetClass(socket).getAnnotation(SocketMessageProcessor.class);
            return matches(annotation, messageType, direction, socketType);
        }).findFirst().map(Map.Entry::getKey);
        if (processorName.isPresent()) {
            return processorName.get();
        }
    }
    Optional<String> processorName = socketMessageProcessorsMap.entrySet().stream().filter(entry -> {
        Object socket = entry.getValue();
        Annotation annotation = AopUtils.getTargetClass(socket).getAnnotation(DefaultSocketMessageProcessor.class);
        return matches(annotation, messageType, direction, null);
    }).findFirst().map(Map.Entry::getKey);
    if (processorName.isPresent()) {
        return processorName.get();
    }
    throw new WonMessageProcessingException(String.format("unexpected combination of messageType %s, " + "socketType %s and direction %s encountered", messageType, socketType, direction));
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) Annotation(java.lang.annotation.Annotation)

Aggregations

WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)18 WonMessage (won.protocol.message.WonMessage)13 URI (java.net.URI)8 PublicKey (java.security.PublicKey)5 Atom (won.protocol.model.Atom)4 Connection (won.protocol.model.Connection)4 PrivateKey (java.security.PrivateKey)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Dataset (org.apache.jena.query.Dataset)3 Annotation (java.lang.annotation.Annotation)2 Test (org.junit.Test)2 StopWatch (org.springframework.util.StopWatch)2 WonMessageType (won.protocol.message.WonMessageType)2 StringWriter (java.io.StringWriter)1 MethodHandles (java.lang.invoke.MethodHandles)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SignatureException (java.security.SignatureException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1