Search in sources :

Example 6 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TrellisWebDAV method moveResource.

/**
 * Move a resource.
 * @return the async response
 */
@MOVE
@Timed
public CompletionStage<Response> moveResource() {
    final TrellisRequest req = new TrellisRequest(request, uriInfo, headers, security);
    final String baseUrl = getBaseUrl(req);
    final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + req.getPath());
    final IRI destination = getDestination(headers, baseUrl);
    final Session session = HttpSession.from(security);
    return getParent(destination).thenCombine(services.getResourceService().get(destination), this::checkResources).thenCompose(parent -> services.getResourceService().touch(parent.getIdentifier())).thenCompose(future -> services.getResourceService().get(identifier)).thenApply(this::checkResource).thenAccept(res -> recursiveCopy(services, session, res, destination, baseUrl)).thenRun(() -> recursiveDelete(services, session, identifier, baseUrl)).thenCompose(future -> services.getResourceService().delete(Metadata.builder(identifier).interactionModel(LDP.Resource).build())).thenCompose(future -> {
        final Dataset immutable = rdf.createDataset();
        services.getAuditService().creation(identifier, session).stream().map(skolemizeQuads(services.getResourceService(), baseUrl)).forEachOrdered(immutable::add);
        return services.getResourceService().add(identifier, immutable).whenComplete((a, b) -> closeDataset(immutable));
    }).thenRun(() -> services.getNotificationService().emit(new SimpleNotification(externalUrl(identifier, baseUrl), session.getAgent(), List.of(PROV.Activity, AS.Delete), List.of(LDP.Resource), null))).thenApply(future -> status(NO_CONTENT).build()).exceptionally(this::handleException);
}
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) TrellisRequest(org.trellisldp.common.TrellisRequest) Dataset(org.apache.commons.rdf.api.Dataset) WebDAVUtils.closeDataset(org.trellisldp.webdav.impl.WebDAVUtils.closeDataset) SimpleNotification(org.trellisldp.common.SimpleNotification) HttpSession(org.trellisldp.common.HttpSession) Session(org.trellisldp.api.Session) Timed(org.eclipse.microprofile.metrics.annotation.Timed)

Example 7 with Dataset

use of org.apache.commons.rdf.api.Dataset 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 8 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreHealthCheckTest method testHealthy.

@Test
void testHealthy() {
    final Dataset dataset = rdf.createDataset();
    final RDFConnection rdfConnection = connect(wrap(toJena(dataset)));
    final HealthCheck check = new TriplestoreHealthCheck(rdfConnection);
    assertEquals(HealthCheckResponse.Status.UP, check.call().getStatus(), "RDFConnection isn't healthy!");
}
Also used : RDFConnection(org.apache.jena.rdfconnection.RDFConnection) Dataset(org.apache.commons.rdf.api.Dataset) HealthCheck(org.eclipse.microprofile.health.HealthCheck) Test(org.junit.jupiter.api.Test)

Example 9 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreHealthCheckTest method testUnhealthy.

@Test
void testUnhealthy() {
    final Dataset dataset = rdf.createDataset();
    final RDFConnection rdfConnection = connect(wrap(toJena(dataset)));
    rdfConnection.close();
    final HealthCheck check = new TriplestoreHealthCheck(rdfConnection);
    assertEquals(HealthCheckResponse.Status.DOWN, check.call().getStatus(), "Closed RDFConnection doesn't report as unhealthy!");
}
Also used : RDFConnection(org.apache.jena.rdfconnection.RDFConnection) Dataset(org.apache.commons.rdf.api.Dataset) HealthCheck(org.eclipse.microprofile.health.HealthCheck) Test(org.junit.jupiter.api.Test)

Example 10 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceServiceTest method testPutLdpNr.

@Test
void testPutLdpNr() {
    final TriplestoreResourceService svc = new TriplestoreResourceService();
    svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
    svc.idService = idService;
    svc.initialize();
    final IRI binaryIdentifier = rdf.createIRI("foo:binary");
    final Dataset dataset = rdf.createDataset();
    final BinaryMetadata binary = BinaryMetadata.builder(binaryIdentifier).mimeType("text/plain").build();
    dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("title"));
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant later = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.NonRDFSource).container(root).binary(binary).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(resource).thenAccept(checkResource(later, LDP.NonRDFSource, 1L, 1L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertAll("Check binary", checkBinary(res, binaryIdentifier, "text/plain"))).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
    final IRI resource3 = rdf.createIRI(TRELLIS_DATA_PREFIX + "resource/notachild");
    dataset.clear();
    dataset.add(Trellis.PreferUserManaged, resource3, DC.title, rdf.createLiteral("title"));
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant evenLater = meanwhile();
    assertDoesNotThrow(() -> svc.create(builder(resource3).interactionModel(LDP.RDFSource).build(), dataset).toCompletableFuture().join(), "Unsuccessful create operation!");
    allOf(svc.get(resource3).thenAccept(res -> {
        assertAll("Check resource", checkResource(res, resource3, LDP.RDFSource, evenLater));
        assertAll("Check resource stream", checkResourceStream(res, 1L, 0L, 1L, 0L, 0L));
        assertFalse(res.getBinaryMetadata().isPresent(), "Unexpected binary metadata!");
    }).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(evenLater)).toCompletableFuture(), svc.get(root).thenAccept(res -> assertFalse(res.getBinaryMetadata().isPresent(), "unexpected binary metadata!")).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(later, LDP.NonRDFSource, 1L, 1L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(checkPredates(evenLater)).toCompletableFuture()).join();
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) XSD(org.trellisldp.vocabulary.XSD) Optional.of(java.util.Optional.of) RDF.type(org.trellisldp.vocabulary.RDF.type) Resource(org.trellisldp.api.Resource) SKOS(org.trellisldp.vocabulary.SKOS) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DC(org.trellisldp.vocabulary.DC) Literal(org.apache.commons.rdf.api.Literal) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) Awaitility.setDefaultPollInterval(org.awaitility.Awaitility.setDefaultPollInterval) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) RDFConnectionAdapter(org.apache.jena.rdflink.RDFConnectionAdapter) RDFLinkHTTP(org.apache.jena.rdflink.RDFLinkHTTP) RDFS(org.trellisldp.vocabulary.RDFS) Instant.now(java.time.Instant.now) Mock(org.mockito.Mock) Metadata.builder(org.trellisldp.api.Metadata.builder) RDF(org.apache.commons.rdf.api.RDF) AS(org.trellisldp.vocabulary.AS) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) BinaryMetadata(org.trellisldp.api.BinaryMetadata) DefaultIdentifierService(org.trellisldp.api.DefaultIdentifierService) RDFFactory(org.trellisldp.api.RDFFactory) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) BlankNode(org.apache.commons.rdf.api.BlankNode) Predicate.isEqual(java.util.function.Predicate.isEqual) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) IRI(org.apache.commons.rdf.api.IRI) PROV(org.trellisldp.vocabulary.PROV) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) UpdateRequest(org.apache.jena.update.UpdateRequest) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) IRI(org.apache.commons.rdf.api.IRI) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) Dataset(org.apache.commons.rdf.api.Dataset) Instant(java.time.Instant) BinaryMetadata(org.trellisldp.api.BinaryMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

Dataset (org.apache.commons.rdf.api.Dataset)104 IRI (org.apache.commons.rdf.api.IRI)70 Test (org.junit.jupiter.api.Test)59 Resource (org.trellisldp.api.Resource)47 Instant (java.time.Instant)45 RDF (org.apache.commons.rdf.api.RDF)38 LDP (org.trellisldp.vocabulary.LDP)33 Stream (java.util.stream.Stream)30 Map (java.util.Map)28 BinaryMetadata (org.trellisldp.api.BinaryMetadata)28 RDFFactory (org.trellisldp.api.RDFFactory)28 Quad (org.apache.commons.rdf.api.Quad)27 Metadata (org.trellisldp.api.Metadata)27 TRELLIS_DATA_PREFIX (org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX)27 MISSING_RESOURCE (org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE)25 AS (org.trellisldp.vocabulary.AS)25 Trellis (org.trellisldp.vocabulary.Trellis)22 Logger (org.slf4j.Logger)21 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)21 DELETED_RESOURCE (org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE)21