Search in sources :

Example 1 with Quad

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!");
}
Also used : Quad(org.apache.commons.rdf.api.Quad) Test(org.junit.jupiter.api.Test)

Example 2 with Quad

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;
    };
}
Also used : Produces(javax.ws.rs.Produces) WebDAVUtils.recursiveCopy(org.trellisldp.webdav.impl.WebDAVUtils.recursiveCopy) UriBuilder.fromUri(javax.ws.rs.core.UriBuilder.fromUri) SecurityContext(javax.ws.rs.core.SecurityContext) Resource(org.trellisldp.api.Resource) Document(org.w3c.dom.Document) Map(java.util.Map) Response.status(javax.ws.rs.core.Response.status) ACCESS_EXTERNAL_SCHEMA(javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA) DavPropertyUpdate(org.trellisldp.webdav.xml.DavPropertyUpdate) INFINITY(org.trellisldp.webdav.Depth.DEPTH.INFINITY) Set(java.util.Set) Literal(org.apache.commons.rdf.api.Literal) WebDAVUtils.depth1Copy(org.trellisldp.webdav.impl.WebDAVUtils.depth1Copy) SplitIRI.namespaceXML(org.apache.jena.util.SplitIRI.namespaceXML) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) WebDAVUtils.skolemizeQuads(org.trellisldp.webdav.impl.WebDAVUtils.skolemizeQuads) TrellisExtensions(org.trellisldp.common.TrellisExtensions) TrellisUtils.getContainer(org.trellisldp.api.TrellisUtils.getContainer) ArrayList(java.util.ArrayList) Metadata(org.trellisldp.api.Metadata) LdpResource(org.trellisldp.common.LdpResource) GONE(javax.ws.rs.core.Response.Status.GONE) DavPropFind(org.trellisldp.webdav.xml.DavPropFind) FEATURE_SECURE_PROCESSING(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING) LDP(org.trellisldp.vocabulary.LDP) Dataset(org.apache.commons.rdf.api.Dataset) DavProp(org.trellisldp.webdav.xml.DavProp) BinaryMetadata(org.trellisldp.api.BinaryMetadata) SECONDS(java.time.temporal.ChronoUnit.SECONDS) RDFFactory(org.trellisldp.api.RDFFactory) TrellisRequest(org.trellisldp.common.TrellisRequest) ZonedDateTime.ofInstant(java.time.ZonedDateTime.ofInstant) WebDAVUtils.externalUrl(org.trellisldp.webdav.impl.WebDAVUtils.externalUrl) NO_CONTENT(javax.ws.rs.core.Response.Status.NO_CONTENT) PROV(org.trellisldp.vocabulary.PROV) DavRemove(org.trellisldp.webdav.xml.DavRemove) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) RFC_1123_DATE_TIME(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Path(javax.ws.rs.Path) ACCESS_EXTERNAL_DTD(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD) ClientErrorException(javax.ws.rs.ClientErrorException) APPLICATION_XML(javax.ws.rs.core.MediaType.APPLICATION_XML) Consumes(javax.ws.rs.Consumes) Timed(org.eclipse.microprofile.metrics.annotation.Timed) BadRequestException(javax.ws.rs.BadRequestException) Collectors.toSet(java.util.stream.Collectors.toSet) Context(javax.ws.rs.core.Context) WebDAVUtils.closeDataset(org.trellisldp.webdav.impl.WebDAVUtils.closeDataset) HttpConstants(org.trellisldp.common.HttpConstants) PermitAll(javax.annotation.security.PermitAll) WebDAVUtils.copy(org.trellisldp.webdav.impl.WebDAVUtils.copy) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) SimpleNotification(org.trellisldp.common.SimpleNotification) DavResponse(org.trellisldp.webdav.xml.DavResponse) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) DavMultiStatus(org.trellisldp.webdav.xml.DavMultiStatus) UTC(java.time.ZoneOffset.UTC) DAV_NAMESPACE(org.trellisldp.webdav.xml.DavUtils.DAV_NAMESPACE) PostConstruct(javax.annotation.PostConstruct) HttpSession(org.trellisldp.common.HttpSession) Optional(java.util.Optional) SpecialResources(org.trellisldp.api.Resource.SpecialResources) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Request(javax.ws.rs.core.Request) ServiceBundler(org.trellisldp.common.ServiceBundler) Session(org.trellisldp.api.Session) CompletableFuture(java.util.concurrent.CompletableFuture) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) Function(java.util.function.Function) RDF(org.apache.commons.rdf.api.RDF) HashSet(java.util.HashSet) Inject(javax.inject.Inject) AS(org.trellisldp.vocabulary.AS) ZERO(org.trellisldp.webdav.Depth.DEPTH.ZERO) Node(org.w3c.dom.Node) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) DavPropStat(org.trellisldp.webdav.xml.DavPropStat) Collectors.toList(java.util.stream.Collectors.toList) IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) Element(org.w3c.dom.Element) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DavSet(org.trellisldp.webdav.xml.DavSet) WebDAVUtils.recursiveDelete(org.trellisldp.webdav.impl.WebDAVUtils.recursiveDelete) SplitIRI.localnameXML(org.apache.jena.util.SplitIRI.localnameXML) ONE(org.trellisldp.webdav.Depth.DEPTH.ONE) IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) DavMultiStatus(org.trellisldp.webdav.xml.DavMultiStatus) DavProp(org.trellisldp.webdav.xml.DavProp) DavPropStat(org.trellisldp.webdav.xml.DavPropStat) CompletableFuture(java.util.concurrent.CompletableFuture) DavResponse(org.trellisldp.webdav.xml.DavResponse) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream)

Example 3 with Quad

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);
}
Also used : TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) ServiceBundler(org.trellisldp.common.ServiceBundler) Session(org.trellisldp.api.Session) CompletableFuture(java.util.concurrent.CompletableFuture) TrellisUtils.toDataset(org.trellisldp.api.TrellisUtils.toDataset) Function(java.util.function.Function) RDF(org.apache.commons.rdf.api.RDF) Resource(org.trellisldp.api.Resource) AS(org.trellisldp.vocabulary.AS) Metadata(org.trellisldp.api.Metadata) ResourceService(org.trellisldp.api.ResourceService) LDP(org.trellisldp.vocabulary.LDP) Dataset(org.apache.commons.rdf.api.Dataset) StringUtils.replaceOnce(org.apache.commons.lang3.StringUtils.replaceOnce) Trellis(org.trellisldp.vocabulary.Trellis) RDFFactory(org.trellisldp.api.RDFFactory) Logger(org.slf4j.Logger) PathSegment(javax.ws.rs.core.PathSegment) SimpleNotification(org.trellisldp.common.SimpleNotification) Collectors.joining(java.util.stream.Collectors.joining) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BlankNodeOrIRI(org.apache.commons.rdf.api.BlankNodeOrIRI) IRI(org.apache.commons.rdf.api.IRI) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Quad(org.apache.commons.rdf.api.Quad) PROV(org.trellisldp.vocabulary.PROV) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) BlankNodeOrIRI(org.apache.commons.rdf.api.BlankNodeOrIRI) IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) CompletableFuture(java.util.concurrent.CompletableFuture) TrellisUtils.toDataset(org.trellisldp.api.TrellisUtils.toDataset) Dataset(org.apache.commons.rdf.api.Dataset) SimpleNotification(org.trellisldp.common.SimpleNotification)

Example 4 with Quad

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();
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Quad(org.apache.commons.rdf.api.Quad) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Stream(java.util.stream.Stream) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Example 5 with Quad

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();
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Quad(org.apache.commons.rdf.api.Quad) IRI(org.apache.commons.rdf.api.IRI) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Stream(java.util.stream.Stream) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Aggregations

Quad (org.apache.commons.rdf.api.Quad)221 Test (org.junit.Test)142 IRI (org.apache.commons.rdf.api.IRI)139 ArrayList (java.util.ArrayList)108 RefNodeFactory.toIRI (bio.guoda.preston.RefNodeFactory.toIRI)90 BlobStoreReadOnly (bio.guoda.preston.store.BlobStoreReadOnly)61 IOException (java.io.IOException)47 BlankNodeOrIRI (org.apache.commons.rdf.api.BlankNodeOrIRI)32 InputStream (java.io.InputStream)29 List (java.util.List)29 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)25 RefNodeFactory.toStatement (bio.guoda.preston.RefNodeFactory.toStatement)24 Stream (java.util.stream.Stream)24 Dataset (org.apache.commons.rdf.api.Dataset)24 RDFTerm (org.apache.commons.rdf.api.RDFTerm)23 HashType (bio.guoda.preston.HashType)22 TestUtilForProcessor (bio.guoda.preston.store.TestUtilForProcessor)21 Is.is (org.hamcrest.core.Is.is)21 TestUtil (bio.guoda.preston.store.TestUtil)19 Resource (org.trellisldp.api.Resource)19