Search in sources :

Example 81 with Statement

use of org.apache.jena.rdf.model.Statement in project jena by apache.

the class UpdateTest method getAll.

static List<String> getAll(Resource r, Property p) {
    List<String> l = new ArrayList<>();
    ClosableIterator<Statement> cIter = r.listProperties(p);
    for (; cIter.hasNext(); ) {
        Statement stmt = cIter.next();
        String df = stmt.getObject().asResource().getURI();
        l.add(df);
    }
    cIter.close();
    return l;
}
Also used : Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList)

Example 82 with Statement

use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.

the class WonLinkedDataUtils method getWonNodeURIForNeedOrConnection.

public static URI getWonNodeURIForNeedOrConnection(final URI resURI, final Model resourceModel) {
    assert resourceModel != null : "model must not be null";
    // we didnt't get the queue name. Check if the model contains a triple <baseuri> won:hasWonNode
    // <wonNode> and get the information from there.
    logger.debug("getting WON node URI from model");
    Resource baseResource = resourceModel.getResource(resURI.toString());
    logger.debug("resourceModel: {}", RdfUtils.toString(resourceModel));
    StmtIterator wonNodeStatementIterator = baseResource.listProperties(WON.HAS_WON_NODE);
    if (!wonNodeStatementIterator.hasNext()) {
        // no won:hasWonNode triple found. we can't do anything.
        logger.debug("base resource {} has no won:hasWonNode property", baseResource);
        return null;
    }
    Statement stmt = wonNodeStatementIterator.nextStatement();
    RDFNode wonNodeNode = stmt.getObject();
    if (!wonNodeNode.isResource()) {
        logger.debug("won:hasWonNode property of base resource {} is not a resource", baseResource);
        return null;
    }
    URI wonNodeUri = URI.create(wonNodeNode.asResource().getURI().toString());
    logger.debug("obtained WON node URI: {}", wonNodeUri);
    if (wonNodeStatementIterator.hasNext()) {
        logger.warn("multiple WON node URIs found for resource {}, using first one: {} ", baseResource, wonNodeUri);
    }
    return wonNodeUri;
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) URI(java.net.URI) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 83 with Statement

use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.

the class ModelConverter method modelToGraphCollection.

private static GraphCollection modelToGraphCollection(String name, Model model, Map<String, String> pm) {
    // Convert each subj pred obj in Jena Statement into String and add to
    // SigningFramework's NamedGraph.
    // The simpler approach with just using Jena's writer and Signingframework's
    // reader to transform data between data structures won't work since
    // Signingframework has problems with recognizing the [] structure
    GraphCollection graphc = new GraphCollection();
    NamedGraph namedGraph = new NamedGraph(enclose(name, "<", ">"), 0, null);
    StmtIterator iterator = model.listStatements();
    while (iterator.hasNext()) {
        Statement stmt = iterator.nextStatement();
        String subjString = rdfNodeAsString(stmt.getSubject());
        String objString = rdfNodeAsString(stmt.getObject());
        String predString = enclose(stmt.getPredicate().asResource().getURI(), "<", ">");
        Triple gcTriple = new Triple(subjString, predString, objString);
        namedGraph.addTriple(gcTriple);
    }
    graphc.addGraph(namedGraph);
    for (String prefix : pm.keySet()) {
        graphc.addPrefix(new Prefix(prefix + ":", "<" + pm.get(prefix) + ">"));
    }
    return graphc;
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) Triple(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Triple) StmtIterator(org.apache.jena.rdf.model.StmtIterator) NamedGraph(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph) Statement(org.apache.jena.rdf.model.Statement) Prefix(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix)

Example 84 with Statement

use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.

the class WonMessageValidatorTest method testAllGraphsSigned.

@Test
public void testAllGraphsSigned() throws IOException {
    // this check actually is redundant with other checks,
    // therefore the error message can be from any of those...
    // create a dataset where there is a non-signature graph that is not signed
    Dataset invalidDataset = WonRdfUtils.MessageUtils.copyByDatasetSerialization(new WonMessage(createMessageDataset)).getCompleteDataset();
    Model env2sigModel = invalidDataset.getNamedModel(CREATE_ENV2_SIG_NAME);
    StmtIterator iter = env2sigModel.listStatements(ResourceFactory.createResource(CREATE_ENV2_SIG_NAME), WONMSG.HAS_SIGNED_GRAPH_PROPERTY, RdfUtils.EMPTY_RDF_NODE);
    Statement stmtOld = iter.removeNext();
    Statement stmtNew = env2sigModel.createStatement(ResourceFactory.createResource(CREATE_ENV2_SIG_NAME), WONMSG.HAS_SIGNED_GRAPH_PROPERTY, ResourceFactory.createResource("test:resource:uri"));
    env2sigModel.add(stmtNew);
    WonMessageValidator validator = new WonMessageValidator();
    StringBuilder message = new StringBuilder();
    // validate this invalid dataset
    boolean valid = validator.validate(invalidDataset, message);
    Assert.assertTrue("validation is expected to fail", !valid);
    // actually
    Assert.assertTrue(message.toString().contains("signed_or_signature") || message.toString().contains("signer") || message.toString().contains("graph_uris"));
    // reset for further testing:
    env2sigModel.add(stmtOld);
    env2sigModel.remove(stmtNew);
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) WonMessageValidator(won.protocol.validation.WonMessageValidator) Test(org.junit.Test)

Example 85 with Statement

use of org.apache.jena.rdf.model.Statement in project webofneeds by researchstudio-sat.

the class WonMessageValidatorTest method testSignatureChainParallelToEnvelopeChain.

@Test
@Ignore
public void testSignatureChainParallelToEnvelopeChain() throws IOException {
    // create a dataset where the first envelope references the signature of the second envelope instead of content's
    // and the 2nd references the signature of the content
    Dataset invalidDataset = WonRdfUtils.MessageUtils.copyByDatasetSerialization(new WonMessage(createMessageDataset)).getCompleteDataset();
    Model env1Model = invalidDataset.getNamedModel(CREATE_ENV1_NAME);
    StmtIterator iter = env1Model.listStatements(null, WONMSG.HAS_SIGNATURE_GRAPH_PROPERTY, RdfUtils.EMPTY_RDF_NODE);
    Statement stmtOld = iter.removeNext();
    iter = env1Model.listStatements(null, WONMSG.HAS_SIGNED_GRAPH_PROPERTY, RdfUtils.EMPTY_RDF_NODE);
    Statement stmtOld2 = iter.removeNext();
    Statement stmtNew = stmtOld.changeObject(ResourceFactory.createResource(CREATE_ENV1_SIG_NAME));
    env1Model.add(stmtNew);
    Statement stmtNew2 = stmtOld2.changeObject(ResourceFactory.createResource(CREATE_ENV1_NAME));
    env1Model.add(stmtNew2);
    Model env2Model = invalidDataset.getNamedModel(CREATE_ENV2_NAME);
    iter = env2Model.listStatements(null, WONMSG.HAS_SIGNATURE_GRAPH_PROPERTY, RdfUtils.EMPTY_RDF_NODE);
    Statement stmtOld3 = iter.removeNext();
    iter = env2Model.listStatements(null, WONMSG.HAS_SIGNED_GRAPH_PROPERTY, RdfUtils.EMPTY_RDF_NODE);
    Statement stmtOld4 = iter.removeNext();
    Statement stmtNew3 = stmtOld3.changeObject(ResourceFactory.createResource(CREATE_CONTENT_NAME_SIG));
    env2Model.add(stmtNew3);
    Statement stmtNew4 = stmtOld4.changeObject(ResourceFactory.createResource(CREATE_CONTENT_NAME));
    env2Model.add(stmtNew4);
    // String test = RdfUtils.writeDatasetToString(invalidDataset, Lang.TRIG);
    // System.out.println("OUT:\n" + test);
    WonMessageValidator validator = new WonMessageValidator();
    StringBuilder message = new StringBuilder();
    // validate this invalid dataset
    boolean valid = validator.validate(invalidDataset, message);
    Assert.assertTrue("validation is expected to fail", !valid);
    // actually
    Assert.assertTrue(message.toString().contains("signature_chain"));
    // reset for further testing:
    env1Model.add(stmtOld);
    env1Model.remove(stmtNew);
    env1Model.add(stmtOld2);
    env1Model.remove(stmtNew2);
    env2Model.add(stmtOld3);
    env2Model.remove(stmtNew3);
    env2Model.add(stmtOld4);
    env2Model.remove(stmtNew4);
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) WonMessageValidator(won.protocol.validation.WonMessageValidator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Statement (org.apache.jena.rdf.model.Statement)141 Model (org.apache.jena.rdf.model.Model)56 Resource (org.apache.jena.rdf.model.Resource)46 StmtIterator (org.apache.jena.rdf.model.StmtIterator)46 Test (org.junit.Test)39 RDFNode (org.apache.jena.rdf.model.RDFNode)29 Dataset (org.apache.jena.query.Dataset)28 ArrayList (java.util.ArrayList)25 Property (org.apache.jena.rdf.model.Property)18 ReifiedStatement (org.apache.jena.rdf.model.ReifiedStatement)17 Triple (org.apache.jena.graph.Triple)16 WonMessageValidator (won.protocol.validation.WonMessageValidator)14 TextIndexException (org.apache.jena.query.text.TextIndexException)12 Ignore (org.junit.Ignore)10 ReadDeniedException (org.apache.jena.shared.ReadDeniedException)8 Iterator (java.util.Iterator)7 SecuredReifiedStatement (org.apache.jena.permissions.model.SecuredReifiedStatement)7 SecuredStatement (org.apache.jena.permissions.model.SecuredStatement)7 Literal (org.apache.jena.rdf.model.Literal)7 SecuredPrefixMappingTest (org.apache.jena.permissions.graph.SecuredPrefixMappingTest)6