use of org.trellisldp.api.BinaryMetadata in project trellis by trellis-ldp.
the class DBResourceService method updateResource.
private static int updateResource(final Handle handle, final Metadata metadata, final Dataset dataset, final Instant time, final boolean isDelete) {
handle.execute("DELETE FROM resource WHERE subject = ?", metadata.getIdentifier().getIRIString());
final String query = "INSERT INTO resource (subject, interaction_model, modified, deleted, is_part_of, acl, " + "ldp_member, ldp_membership_resource, ldp_has_member_relation, ldp_is_member_of_relation, " + "ldp_inserted_content_relation, binary_location, binary_format) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// Set ldp:insertedContentRelation only for LDP-IC and LDP-DC resources
final String icr = asList(LDP.DirectContainer, LDP.IndirectContainer).contains(metadata.getInteractionModel()) ? metadata.getInsertedContentRelation().orElse(LDP.MemberSubject).getIRIString() : null;
try (final Update update = handle.createUpdate(query).bind(0, metadata.getIdentifier().getIRIString()).bind(1, metadata.getInteractionModel().getIRIString()).bind(2, time.toEpochMilli()).bind(3, isDelete).bind(4, metadata.getContainer().map(IRI::getIRIString).orElse(null)).bind(5, dataset.contains(of(PreferAccessControl), null, null, null)).bind(6, metadata.getMembershipResource().map(TrellisUtils::normalizeIdentifier).map(IRI::getIRIString).orElse(null)).bind(7, metadata.getMembershipResource().map(IRI::getIRIString).orElse(null)).bind(8, metadata.getMemberRelation().map(IRI::getIRIString).orElse(null)).bind(9, metadata.getMemberOfRelation().map(IRI::getIRIString).orElse(null)).bind(10, icr).bind(11, metadata.getBinary().map(BinaryMetadata::getIdentifier).map(IRI::getIRIString).orElse(null)).bind(12, metadata.getBinary().flatMap(BinaryMetadata::getMimeType).orElse(null))) {
return update.executeAndReturnGeneratedKeys("id").mapTo(Integer.class).one();
}
}
use of org.trellisldp.api.BinaryMetadata 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();
}
use of org.trellisldp.api.BinaryMetadata in project trellis by trellis-ldp.
the class PutHandler method handleResourceUpdate.
private CompletionStage<ResponseBuilder> handleResourceUpdate(final Dataset mutable, final Dataset immutable, final ResponseBuilder builder, final IRI ldpType) {
final Metadata.Builder metadata;
final BinaryMetadata binary;
// Add user-supplied data
if (LDP.NonRDFSource.equals(ldpType) && rdfSyntax == null) {
LOGGER.trace("Successfully checked for bad digest value");
final String mimeType = getRequest().getContentType() != null ? getRequest().getContentType() : APPLICATION_OCTET_STREAM;
final IRI binaryLocation = rdf.createIRI(getServices().getBinaryService().generateIdentifier(internalId));
binary = BinaryMetadata.builder(binaryLocation).mimeType(mimeType).hints(getRequest().getHeaders()).build();
metadata = metadataBuilder(internalId, ldpType, mutable).binary(binary);
builder.link(getIdentifier() + "?ext=description", "describedby");
} else {
final RDFSyntax s = rdfSyntax != null ? rdfSyntax : TURTLE;
final IRI ext = getExtensionGraphName();
final IRI graphName = ext != null ? ext : PreferUserManaged;
readEntityIntoDataset(graphName, s, mutable);
// Check the mutable dataset for any constraints
checkConstraints(mutable, ldpType, s);
metadata = metadataBuilder(internalId, ldpType, mutable);
if (getResource() != null) {
getResource().getBinaryMetadata().ifPresent(metadata::binary);
}
binary = null;
}
if (getResource() != null) {
getResource().getContainer().ifPresent(metadata::container);
metadata.revision(getResource().getRevision());
LOGGER.debug("Resource {} found in persistence", getIdentifier());
try (final Stream<Quad> remaining = getResource().stream(getNonCurrentGraphNames())) {
remaining.forEachOrdered(mutable::add);
}
} else if (!createUncontained) {
getContainer(internalId).ifPresent(metadata::container);
}
getAuditQuadData().forEachOrdered(immutable::add);
LOGGER.trace("Successfully calculated and skolemized immutable data");
ldpResourceTypes(effectiveLdpType(ldpType)).map(IRI::getIRIString).forEach(type -> {
LOGGER.trace("Adding link for type {}", type);
builder.link(type, Link.TYPE);
});
LOGGER.trace("Persisting mutable data for {} with data: {}", internalId, mutable);
final Metadata m = metadata.build();
return createOrReplace(m, mutable, immutable).thenCompose(future -> persistBinaryContent(binary)).thenCompose(future -> handleUpdateNotification(ldpType, m.getRevision().orElse(null))).thenApply(future -> decorateResponse(builder));
}
use of org.trellisldp.api.BinaryMetadata in project trellis-extensions by trellis-ldp.
the class CassandraBuildingService method parse.
default Resource parse(final Row metadata, final Logger log, final IRI id) {
if (metadata == null) {
log.debug("{} was not found.", id);
return MISSING_RESOURCE;
}
log.debug("{} was found, computing metadata.", id);
final IRI ixnModel = metadata.get("interactionModel", IRI.class);
log.debug("Found interactionModel = {} for resource {}", ixnModel, id);
final IRI container = metadata.get("container", IRI.class);
log.debug("Found container = {} for resource {}", container, id);
final IRI binaryId = metadata.get("binaryIdentifier", IRI.class);
log.debug("Found binaryIdentifier = {} for resource {}", binaryId, id);
final String mimeType = metadata.getString("mimetype");
log.debug("Found mimeType = {} for resource {}", mimeType, id);
final Instant modified = metadata.get("modified", Instant.class);
log.debug("Found modified = {} for resource {}", modified, id);
final Dataset dataset = CassandraIOUtils.parse(metadata.getString("quads"));
log.debug("Found dataset = {} for resource {}", dataset, id);
final BinaryMetadata binary = binaryId != null ? BinaryMetadata.builder(binaryId).mimeType(mimeType).build() : null;
final Set<IRI> graphs = dataset.getGraphNames().filter(IRI.class::isInstance).map(IRI.class::cast).filter(isEqual(Trellis.PreferUserManaged).or(isEqual(Trellis.PreferServerManaged)).negate()).collect(toSet());
final Metadata meta = Metadata.builder(id).container(container).interactionModel(ixnModel).metadataGraphNames(graphs).binary(binary).build();
return new CassandraResource(meta, modified, dataset);
}
use of org.trellisldp.api.BinaryMetadata in project trellis-cassandra by ajs6f.
the class CassandraMementoService method put.
@Override
public CompletionStage<Void> put(Resource r) {
IRI id = r.getIdentifier();
IRI ixnModel = r.getInteractionModel();
IRI container = r.getContainer().orElse(null);
Optional<BinaryMetadata> binary = r.getBinaryMetadata();
IRI binaryIdentifier = binary.map(BinaryMetadata::getIdentifier).orElse(null);
String mimeType = binary.flatMap(BinaryMetadata::getMimeType).orElse(null);
Dataset data = r.dataset();
Instant modified = r.getModified();
UUID creation = Uuids.timeBased();
log.debug("Writing Memento for {} at time: {}", id, modified);
return mementoize.execute(ixnModel, mimeType, container, data, modified, binaryIdentifier, creation, id);
}
Aggregations