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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations