use of org.apache.commons.rdf.api.Quad in project trellis by trellis-ldp.
the class FileUtilsTest method testSerializeQuad.
@Test
void testSerializeQuad() {
final Quad quad = rdf.createQuad(Trellis.PreferServerManaged, rdf.createIRI("trellis:data/resource"), DC.subject, rdf.createIRI("http://example.org"));
assertEquals("<trellis:data/resource> <http://purl.org/dc/terms/subject> <http://example.org> " + "<http://www.trellisldp.org/ns/trellis#PreferServerManaged> .", FileUtils.serializeQuad(quad), "Quad isn't serialized properly!");
}
use of org.apache.commons.rdf.api.Quad in project trellis by trellis-ldp.
the class TrellisWebDAV method propertiesToMultiStatus.
private Function<Resource, DavMultiStatus> propertiesToMultiStatus(final Document doc, final String baseUrl, final DavPropFind propfind) {
final Set<IRI> properties = getProperties(propfind);
final boolean allprops = propfind.getAllProp() != null;
final boolean propname = propfind.getPropName() != null;
return res -> {
final DavProp prop = new DavProp();
prop.setNodes(getPropertyElements(doc, res, properties, allprops, propname));
final DavPropStat propstat = new DavPropStat();
propstat.setProp(prop);
propstat.setStatus(SUCCESS);
final DavResponse response = new DavResponse();
response.setHref(externalUrl(res.getIdentifier(), baseUrl));
response.setDescription("PROPFIND request for " + externalUrl(res.getIdentifier(), baseUrl));
response.setPropStats(List.of(propstat));
final List<DavResponse> responses = new ArrayList<>();
responses.add(response);
try (final Stream<Quad> children = res.stream(LDP.PreferContainment)) {
children.map(Quad::getObject).filter(IRI.class::isInstance).map(IRI.class::cast).parallel().map(id -> services.getResourceService().get(id).thenApply(r -> {
final DavProp childProp = new DavProp();
childProp.setNodes(getPropertyElements(doc, r, properties, allprops, propname));
final DavPropStat childPropstat = new DavPropStat();
childPropstat.setProp(childProp);
childPropstat.setStatus(SUCCESS);
final DavResponse childResponse = new DavResponse();
childResponse.setHref(externalUrl(r.getIdentifier(), baseUrl));
childResponse.setDescription("PROPFIND request for " + externalUrl(r.getIdentifier(), baseUrl));
childResponse.setPropStats(List.of(childPropstat));
return childResponse;
})).map(CompletionStage::toCompletableFuture).map(CompletableFuture::join).forEach(responses::add);
}
final DavMultiStatus multistatus = new DavMultiStatus();
multistatus.setResponses(responses);
multistatus.setDescription("Response to property find request");
return multistatus;
};
}
use of org.apache.commons.rdf.api.Quad in project trellis by trellis-ldp.
the class WebDAVUtils method recursiveDelete.
/**
* Recursively delete resources under the given identifier.
* @param services the trellis services
* @param session the session
* @param identifier the identifier
* @param baseUrl the baseURL
*/
public static void recursiveDelete(final ServiceBundler services, final Session session, final IRI identifier, final String baseUrl) {
final List<IRI> resources = services.getResourceService().get(identifier).thenApply(res -> res.stream(LDP.PreferContainment).map(Quad::getObject).filter(IRI.class::isInstance).map(IRI.class::cast).collect(toList())).toCompletableFuture().join();
resources.forEach(id -> recursiveDelete(services, session, id, baseUrl));
resources.stream().parallel().map(id -> {
final Dataset immutable = rdf.createDataset();
services.getAuditService().creation(id, session).stream().map(skolemizeQuads(services.getResourceService(), baseUrl)).forEachOrdered(immutable::add);
return services.getResourceService().delete(Metadata.builder(id).interactionModel(LDP.Resource).container(identifier).build()).thenCompose(future -> services.getResourceService().add(id, immutable)).whenComplete((a, b) -> closeDataset(immutable)).thenRun(() -> services.getNotificationService().emit(new SimpleNotification(externalUrl(id, baseUrl), session.getAgent(), List.of(PROV.Activity, AS.Delete), List.of(LDP.Resource), null)));
}).map(CompletionStage::toCompletableFuture).forEach(CompletableFuture::join);
}
use of org.apache.commons.rdf.api.Quad in project trellis by trellis-ldp.
the class TriplestoreResource method fetchIndirectMemberQuads.
/**
* This code is equivalent to the SPARQL query below.
*
* <p><pre><code>
* SELECT ?subject ?predicate ?object
* WHERE {
* GRAPH trellis:PreferServerManaged {
* ?s ldp:member IDENTIFIER .
* ?s ldp:membershipResource ?subject .
* ?s rdf:type ldp:IndirectContainer .
* ?s ldp:membershipRelation ?predicate .
* ?s ldp:insertedContentRelation ?o .
* ?res dc:isPartOf ?s .
* }
* GRAPH ?res { ?res ?o ?object }
* }
* </code></pre>
*/
private Stream<Quad> fetchIndirectMemberQuads() {
final Var s = Var.alloc("s");
final Var o = Var.alloc("o");
final Var res = Var.alloc("res");
final Query q = new Query();
q.setQuerySelectType();
q.addResultVar(SUBJECT);
q.addResultVar(PREDICATE);
q.addResultVar(OBJECT);
final ElementPathBlock epb1 = new ElementPathBlock();
epb1.addTriple(create(s, toJena(LDP.member), toJena(identifier)));
epb1.addTriple(create(s, toJena(LDP.membershipResource), SUBJECT));
epb1.addTriple(create(s, type.asNode(), toJena(LDP.IndirectContainer)));
epb1.addTriple(create(s, toJena(LDP.hasMemberRelation), PREDICATE));
epb1.addTriple(create(s, toJena(LDP.insertedContentRelation), o));
epb1.addTriple(create(res, toJena(DC.isPartOf), s));
final ElementPathBlock epb2 = new ElementPathBlock();
epb2.addTriple(create(res, o, OBJECT));
final ElementGroup elg = new ElementGroup();
elg.addElement(new ElementNamedGraph(toJena(Trellis.PreferServerManaged), epb1));
elg.addElement(new ElementNamedGraph(res, epb2));
q.setQueryPattern(elg);
final Stream.Builder<Quad> builder = builder();
rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(LDP.PreferMembership, getSubject(qs), getPredicate(qs), getObject(qs))));
return builder.build();
}
use of org.apache.commons.rdf.api.Quad in project trellis by trellis-ldp.
the class TriplestoreResource method fetchDirectMemberQuads.
/**
* This code is equivalent to the SPARQL query below.
*
* <p><pre><code>
* SELECT ?subject ?predicate ?object ?type
* WHERE {
* GRAPH trellis:PreferServerManaged {
* ?s ldp:member IDENTIFIER
* ?s ldp:membershipResource ?subject
* ?s ldp:hasMemberRelation ?predicate
* ?s ldp:insertedContentRelation ldp:MemberSubject
* ?object dc:isPartOf ?s
* ?object rdf:type ?type .
* }
* }
* </code></pre>
*/
private Stream<Quad> fetchDirectMemberQuads() {
final Query q = new Query();
q.setQuerySelectType();
q.addResultVar(SUBJECT);
q.addResultVar(PREDICATE);
q.addResultVar(OBJECT);
q.addResultVar(TYPE);
final Var s = Var.alloc("s");
final ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(create(s, toJena(LDP.member), toJena(identifier)));
epb.addTriple(create(s, toJena(LDP.membershipResource), SUBJECT));
epb.addTriple(create(s, toJena(LDP.hasMemberRelation), PREDICATE));
epb.addTriple(create(s, toJena(LDP.insertedContentRelation), toJena(LDP.MemberSubject)));
epb.addTriple(create(OBJECT, toJena(DC.isPartOf), s));
epb.addTriple(create(OBJECT, type.asNode(), TYPE));
final ElementNamedGraph ng = new ElementNamedGraph(toJena(Trellis.PreferServerManaged), epb);
final ElementGroup elg = new ElementGroup();
elg.addElement(ng);
q.setQueryPattern(elg);
final Stream.Builder<Quad> builder = builder();
rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(LDP.PreferMembership, getSubject(qs), getPredicate(qs), adjustIdentifier((IRI) getObject(qs), getType(qs)))));
return builder.build();
}
Aggregations