Search in sources :

Example 1 with Trellis

use of org.trellisldp.vocabulary.Trellis 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 2 with Trellis

use of org.trellisldp.vocabulary.Trellis in project trellis by trellis-ldp.

the class WebAcService method initialize.

/**
 * Initializes the root ACL, if there is no root ACL.
 */
@PostConstruct
public void initialize() {
    defaultRootAuthorizations = unmodifiableList(getDefaultRootAuthorizations(defaultAuthResourceLocation));
    if (initializeRoot) {
        try (final Dataset dataset = generateDefaultRootAuthorizationsDataset(defaultAuthResourceLocation)) {
            this.resourceService.get(root).thenCompose(res -> initialize(res, dataset)).exceptionally(err -> {
                LOGGER.warn("Unable to auto-initialize Trellis: {}. See DEBUG log for more info", err.getMessage());
                LOGGER.debug("Error auto-initializing Trellis", err);
                return null;
            }).toCompletableFuture().join();
        } catch (final Exception ex) {
            throw new TrellisRuntimeException("Error initializing Trellis ACL", ex);
        }
    }
}
Also used : TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Collections.unmodifiableList(java.util.Collections.unmodifiableList) Resource(org.trellisldp.api.Resource) Model(org.apache.jena.rdf.model.Model) Graph(org.apache.commons.rdf.api.Graph) CacheService(org.trellisldp.api.CacheService) Collectors.toSet(java.util.stream.Collectors.toSet) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) TYPE(java.lang.annotation.ElementType.TYPE) FIELD(java.lang.annotation.ElementType.FIELD) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Set(java.util.Set) TrellisUtils.toGraph(org.trellisldp.api.TrellisUtils.toGraph) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) FOAF(org.trellisldp.vocabulary.FOAF) JenaCommonsRDF.fromJena(org.apache.jena.commonsrdf.JenaCommonsRDF.fromJena) RiotException(org.apache.jena.riot.RiotException) TURTLE(org.apache.jena.riot.Lang.TURTLE) Session(org.trellisldp.api.Session) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PARAMETER(java.lang.annotation.ElementType.PARAMETER) CompletableFuture(java.util.concurrent.CompletableFuture) TrellisUtils.getContainer(org.trellisldp.api.TrellisUtils.getContainer) VCARD(org.trellisldp.vocabulary.VCARD) Function(java.util.function.Function) RDF(org.apache.commons.rdf.api.RDF) Triple(org.apache.commons.rdf.api.Triple) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) String.join(java.lang.String.join) ResourceService(org.trellisldp.api.ResourceService) Objects.requireNonNull(java.util.Objects.requireNonNull) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) RDFFactory(org.trellisldp.api.RDFFactory) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) RDFParser(org.apache.jena.riot.RDFParser) IOException(java.io.IOException) TrellisUtils(org.trellisldp.api.TrellisUtils) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) METHOD(java.lang.annotation.ElementType.METHOD) Collectors.toList(java.util.stream.Collectors.toList) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) Collections(java.util.Collections) InputStream(java.io.InputStream) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Dataset(org.apache.commons.rdf.api.Dataset) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) RiotException(org.apache.jena.riot.RiotException) IOException(java.io.IOException) PostConstruct(javax.annotation.PostConstruct)

Example 3 with Trellis

use of org.trellisldp.vocabulary.Trellis in project trellis-extensions by trellis-ldp.

the class S3MementoService method put.

@Override
public CompletionStage<Void> put(final Resource resource) {
    return runAsync(() -> {
        try {
            final File file = createTempFile("trellis-memento-", ".nq");
            file.deleteOnExit();
            final Map<String, String> metadata = new HashMap<>();
            metadata.put(S3Resource.INTERACTION_MODEL, resource.getInteractionModel().getIRIString());
            metadata.put(S3Resource.MODIFIED, resource.getModified().toString());
            resource.getContainer().map(IRI::getIRIString).ifPresent(c -> metadata.put(S3Resource.CONTAINER, c));
            resource.getBinaryMetadata().ifPresent(b -> {
                metadata.put(S3Resource.BINARY_LOCATION, b.getIdentifier().getIRIString());
                b.getMimeType().ifPresent(m -> metadata.put(S3Resource.BINARY_TYPE, m));
            });
            resource.getMembershipResource().map(IRI::getIRIString).ifPresent(m -> metadata.put(S3Resource.MEMBERSHIP_RESOURCE, m));
            resource.getMemberRelation().map(IRI::getIRIString).ifPresent(m -> metadata.put(S3Resource.MEMBER_RELATION, m));
            resource.getMemberOfRelation().map(IRI::getIRIString).ifPresent(m -> metadata.put(S3Resource.MEMBER_OF_RELATION, m));
            resource.getInsertedContentRelation().map(IRI::getIRIString).ifPresent(m -> metadata.put(S3Resource.INSERTED_CONTENT_RELATION, m));
            try (final Dataset dataset = rdf.createDataset();
                final OutputStream output = Files.newOutputStream(file.toPath());
                final Stream<? extends Quad> quads = resource.stream()) {
                quads.forEachOrdered(dataset::add);
                metadata.put(S3Resource.METADATA_GRAPHS, dataset.getGraphNames().filter(IRI.class::isInstance).map(IRI.class::cast).filter(graph -> !IGNORE.contains(graph)).map(IRI::getIRIString).collect(joining(",")));
                RDFDataMgr.write(output, toJena(dataset), NQUADS);
            }
            final ObjectMetadata md = new ObjectMetadata();
            md.setContentType("application/n-quads");
            md.setUserMetadata(metadata);
            final PutObjectRequest req = new PutObjectRequest(bucketName, getKey(resource.getIdentifier(), resource.getModified().truncatedTo(SECONDS)), file);
            client.putObject(req.withMetadata(md));
            Files.delete(file.toPath());
        } catch (final Exception ex) {
            throw new TrellisRuntimeException("Error deleting locally buffered file", ex);
        }
    });
}
Also used : TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) SortedSet(java.util.SortedSet) File.createTempFile(java.io.File.createTempFile) Resource(org.trellisldp.api.Resource) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) Collections.unmodifiableSortedSet(java.util.Collections.unmodifiableSortedSet) AmazonS3ClientBuilder.defaultClient(com.amazonaws.services.s3.AmazonS3ClientBuilder.defaultClient) NQUADS(org.apache.jena.riot.Lang.NQUADS) Set(java.util.Set) ConfigProvider.getConfig(org.eclipse.microprofile.config.ConfigProvider.getConfig) Config(org.eclipse.microprofile.config.Config) Instant(java.time.Instant) Collectors.joining(java.util.stream.Collectors.joining) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) MementoService(org.trellisldp.api.MementoService) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) Alternative(javax.enterprise.inject.Alternative) HashMap(java.util.HashMap) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) TreeSet(java.util.TreeSet) RDF(org.apache.commons.rdf.api.RDF) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Objects.requireNonNull(java.util.Objects.requireNonNull) CompletableFuture.supplyAsync(java.util.concurrent.CompletableFuture.supplyAsync) AmazonS3(com.amazonaws.services.s3.AmazonS3) LDP(org.trellisldp.vocabulary.LDP) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) OutputStream(java.io.OutputStream) SECONDS(java.time.temporal.ChronoUnit.SECONDS) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Stream.of(java.util.stream.Stream.of) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) File(java.io.File) IRI(org.apache.commons.rdf.api.IRI) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) Quad(org.apache.commons.rdf.api.Quad) RDFDataMgr(org.apache.jena.riot.RDFDataMgr) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) IRI(org.apache.commons.rdf.api.IRI) HashMap(java.util.HashMap) Dataset(org.apache.commons.rdf.api.Dataset) OutputStream(java.io.OutputStream) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) File.createTempFile(java.io.File.createTempFile) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 4 with Trellis

use of org.trellisldp.vocabulary.Trellis in project trellis by trellis-ldp.

the class WebDAVUtils method copy.

/**
 * Copy a resource to another location.
 * @param services the trellis services
 * @param session the session
 * @param resource the resource
 * @param destination the destination identifier
 * @param baseUrl the baseURL
 * @return the next stage of completion
 */
public static CompletionStage<Void> copy(final ServiceBundler services, final Session session, final Resource resource, final IRI destination, final String baseUrl) {
    final Metadata.Builder builder = Metadata.builder(destination).interactionModel(resource.getInteractionModel());
    resource.getContainer().ifPresent(builder::container);
    resource.getBinaryMetadata().ifPresent(builder::binary);
    resource.getInsertedContentRelation().ifPresent(builder::insertedContentRelation);
    resource.getMemberOfRelation().ifPresent(builder::memberOfRelation);
    resource.getMemberRelation().ifPresent(builder::memberRelation);
    resource.getMembershipResource().ifPresent(builder::membershipResource);
    builder.metadataGraphNames(resource.getMetadataGraphNames());
    try (final Stream<Quad> stream = resource.stream(Trellis.PreferUserManaged)) {
        LOGGER.debug("Copying {} to {}", resource.getIdentifier(), destination);
        final Dataset mutable = stream.collect(toDataset());
        return services.getResourceService().create(builder.build(), mutable).whenComplete((a, b) -> closeDataset(mutable)).thenCompose(future -> {
            final Dataset immutable = rdf.createDataset();
            services.getAuditService().creation(resource.getIdentifier(), session).stream().map(skolemizeQuads(services.getResourceService(), baseUrl)).forEachOrdered(immutable::add);
            return services.getResourceService().add(resource.getIdentifier(), immutable).whenComplete((a, b) -> closeDataset(immutable));
        }).thenCompose(future -> services.getMementoService().put(services.getResourceService(), resource.getIdentifier())).thenRun(() -> services.getNotificationService().emit(new SimpleNotification(externalUrl(destination, baseUrl), session.getAgent(), List.of(PROV.Activity, AS.Create), List.of(resource.getInteractionModel()), null)));
    }
}
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) Quad(org.apache.commons.rdf.api.Quad) TrellisUtils.toDataset(org.trellisldp.api.TrellisUtils.toDataset) Dataset(org.apache.commons.rdf.api.Dataset) SimpleNotification(org.trellisldp.common.SimpleNotification) Metadata(org.trellisldp.api.Metadata)

Aggregations

CompletionStage (java.util.concurrent.CompletionStage)4 Stream (java.util.stream.Stream)4 Dataset (org.apache.commons.rdf.api.Dataset)4 IRI (org.apache.commons.rdf.api.IRI)4 Quad (org.apache.commons.rdf.api.Quad)4 RDF (org.apache.commons.rdf.api.RDF)4 Logger (org.slf4j.Logger)4 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)4 RDFFactory (org.trellisldp.api.RDFFactory)4 Resource (org.trellisldp.api.Resource)4 TrellisRuntimeException (org.trellisldp.api.TrellisRuntimeException)4 TRELLIS_DATA_PREFIX (org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX)4 Trellis (org.trellisldp.vocabulary.Trellis)4 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Function (java.util.function.Function)3 Collectors.joining (java.util.stream.Collectors.joining)3 Collectors.toList (java.util.stream.Collectors.toList)3 Metadata (org.trellisldp.api.Metadata)3 ResourceService (org.trellisldp.api.ResourceService)3