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