Search in sources :

Example 1 with WonKeysReaderWriter

use of won.cryptography.rdfsign.WonKeysReaderWriter in project webofneeds by researchstudio-sat.

the class TestingDataSource method getDataForResource.

@Override
public Dataset getDataForResource(final URI resourceURI) {
    Dataset dataset = DatasetFactory.createGeneral();
    DefaultPrefixUtils.setDefaultPrefixes(dataset.getDefaultModel());
    WonKeysReaderWriter keyWriter = new WonKeysReaderWriter();
    Model model = dataset.getDefaultModel();
    Resource subj = model.createResource(resourceURI.toString());
    try {
        keyWriter.writeToModel(model, subj, pubKeysMap.get(resourceURI.toString()));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return dataset;
}
Also used : Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) WonKeysReaderWriter(won.cryptography.rdfsign.WonKeysReaderWriter)

Example 2 with WonKeysReaderWriter

use of won.cryptography.rdfsign.WonKeysReaderWriter 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;
}
Also used : WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) PublicKey(java.security.PublicKey) Model(org.apache.jena.rdf.model.Model) WonKeysReaderWriter(won.cryptography.rdfsign.WonKeysReaderWriter) WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException)

Example 3 with WonKeysReaderWriter

use of won.cryptography.rdfsign.WonKeysReaderWriter in project webofneeds by researchstudio-sat.

the class TestingDataSource method getDataForPublicResource.

@Override
public Dataset getDataForPublicResource(final URI resourceURI) {
    Dataset dataset = DatasetFactory.createGeneral();
    DefaultPrefixUtils.setDefaultPrefixes(dataset.getDefaultModel());
    WonKeysReaderWriter keyWriter = new WonKeysReaderWriter();
    Model model = dataset.getDefaultModel();
    Resource subj = model.createResource(resourceURI.toString());
    try {
        keyWriter.writeToModel(model, subj, pubKeysMap.get(resourceURI.toString()));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return dataset;
}
Also used : Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) WonKeysReaderWriter(won.cryptography.rdfsign.WonKeysReaderWriter)

Example 4 with WonKeysReaderWriter

use of won.cryptography.rdfsign.WonKeysReaderWriter 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;
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) Dataset(org.apache.jena.query.Dataset) PublicKey(java.security.PublicKey) WonMessage(won.protocol.message.WonMessage) Model(org.apache.jena.rdf.model.Model) WonKeysReaderWriter(won.cryptography.rdfsign.WonKeysReaderWriter) WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException)

Aggregations

Model (org.apache.jena.rdf.model.Model)4 WonKeysReaderWriter (won.cryptography.rdfsign.WonKeysReaderWriter)4 Dataset (org.apache.jena.query.Dataset)3 PublicKey (java.security.PublicKey)2 Resource (org.apache.jena.rdf.model.Resource)2 WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)1 WonMessage (won.protocol.message.WonMessage)1 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)1