use of org.trellisldp.api.TrellisRuntimeException in project trellis by trellis-ldp.
the class DBResourceService method storeResource.
private void storeResource(final Metadata metadata, final Dataset dataset, final Instant time, final OperationType opType) {
try {
jdbi.useTransaction(handle -> {
final int resourceId = updateResource(handle, metadata, dataset, time, opType == OperationType.DELETE);
updateDescription(handle, resourceId, dataset, batchSize);
updateAcl(handle, resourceId, dataset, batchSize);
updateExtra(handle, resourceId, metadata.getIdentifier(), dataset);
extensions.forEach((ext, graph) -> dataset.getGraph(graph).filter(g -> !"acl".equals(ext)).ifPresent(g -> updateExtension(handle, resourceId, ext, g)));
if (opType == OperationType.DELETE) {
// Verify that the container really is empty
final String query = "SELECT EXISTS(SELECT 1 FROM resource WHERE is_part_of = ?)";
if (Boolean.TRUE.equals(handle.select(query, metadata.getIdentifier().getIRIString()).map((rs, ctx) -> rs.getBoolean(1)).one())) {
throw new StorageConflictException("Cannot delete non-empty containers");
}
}
});
} catch (final TrellisRuntimeException ex) {
throw ex;
} catch (final Exception ex) {
throw new TrellisRuntimeException("Could not update data for " + metadata.getIdentifier(), ex);
}
}
use of org.trellisldp.api.TrellisRuntimeException in project trellis by trellis-ldp.
the class TriplestoreResourceService method delete.
@Override
public CompletionStage<Void> delete(final Metadata metadata) {
LOGGER.debug("Deleting: {}", metadata.getIdentifier());
return runAsync(() -> {
try (final Dataset dataset = rdf.createDataset()) {
final Instant eventTime = now();
dataset.add(PreferServerManaged, metadata.getIdentifier(), DC.type, DeletedResource);
dataset.add(PreferServerManaged, metadata.getIdentifier(), type, LDP.Resource);
storeResource(metadata.getIdentifier(), dataset, eventTime, OperationType.DELETE);
} catch (final Exception ex) {
throw new TrellisRuntimeException("Error deleting resource: " + metadata.getIdentifier(), ex);
}
});
}
use of org.trellisldp.api.TrellisRuntimeException in project trellis by trellis-ldp.
the class PatchHandler method assembleResponse.
private CompletionStage<ResponseBuilder> assembleResponse(final Dataset mutable, final Dataset immutable, final ResponseBuilder builder) {
final IRI ext = getExtensionGraphName();
final IRI graphName = ext != null ? ext : PreferUserManaged;
// Put triples in buffer, short-circuit on exception
final List<Triple> triples;
try {
triples = updateGraph(syntax, graphName);
} catch (final TrellisRuntimeException ex) {
throw new BadRequestException("Invalid RDF: " + ex.getMessage(), ex);
}
triples.stream().map(skolemizeTriples(getServices().getResourceService(), getBaseUrl())).map(triple -> rdf.createQuad(graphName, triple.getSubject(), triple.getPredicate(), triple.getObject())).forEachOrdered(mutable::add);
// Check any constraints on the resulting dataset
final List<ConstraintViolation> violations = new ArrayList<>();
getServices().getConstraintServices().forEach(svc -> handleConstraintViolation(svc, getInternalId(), mutable, graphName, getLdpType()).forEach(violations::add));
// Short-ciruit if there is a constraint violation
if (!violations.isEmpty()) {
final ResponseBuilder err = status(CONFLICT);
violations.forEach(v -> err.link(v.getConstraint().getIRIString(), LDP.constrainedBy.getIRIString()));
throw new ClientErrorException(err.entity((StreamingOutput) out -> getServices().getIOService().write(violations.stream().flatMap(v2 -> v2.getTriples().stream()), out, RDFSyntax.TURTLE, getIdentifier())).type(RDFSyntax.TURTLE.mediaType()).build());
}
// When updating one particular graph, be sure to add the other category to the dataset
if (getResource() != null) {
try (final Stream<Quad> remaining = getResource().stream(getNonCurrentGraphNames())) {
remaining.forEachOrdered(mutable::add);
}
}
// Collect the audit data
getAuditQuadData().forEachOrdered(immutable::add);
final Metadata metadata;
if (getResource() == null) {
metadata = metadataBuilder(getInternalId(), LDP.RDFSource, mutable).container(TrellisUtils.getContainer(getInternalId()).orElse(null)).build();
} else {
final Metadata.Builder mbuilder = metadataBuilder(getResource().getIdentifier(), getResource().getInteractionModel(), mutable);
getResource().getContainer().ifPresent(mbuilder::container);
getResource().getBinaryMetadata().ifPresent(mbuilder::binary);
mbuilder.revision(getResource().getRevision());
metadata = mbuilder.build();
}
return createOrReplace(metadata, mutable, immutable).thenCompose(future -> emitNotification(metadata.getIdentifier(), getResource() == null ? AS.Create : AS.Update, getLdpType(), metadata.getRevision().orElse(null))).thenApply(future -> {
final RDFSyntax outputSyntax = getSyntax(getServices().getIOService(), getRequest().getAcceptableMediaTypes(), null);
if (preference != null) {
final IRI profile = getResponseProfile(outputSyntax);
return builder.header(PREFERENCE_APPLIED, "return=representation").type(outputSyntax.mediaType()).entity((StreamingOutput) out -> getServices().getIOService().write(triples.stream().map(unskolemizeTriples(getServices().getResourceService(), getBaseUrl())), out, outputSyntax, getIdentifier(), profile));
}
return builder.status(getResource() == null ? CREATED : NO_CONTENT);
});
}
use of org.trellisldp.api.TrellisRuntimeException in project trellis by trellis-ldp.
the class AbstractTrellisHttpResourceTest method testPostException.
@Test
void testPostException() {
when(mockResource.getInteractionModel()).thenReturn(LDP.Container);
when(mockResourceService.get(identifier)).thenAnswer(inv -> supplyAsync(() -> {
throw new TrellisRuntimeException(EXPECTED_EXCEPTION);
}));
try (final Response res = target(RESOURCE_PATH).request().post(entity("", TEXT_TURTLE_TYPE))) {
assertEquals(SC_INTERNAL_SERVER_ERROR, res.getStatus(), ERR_RESPONSE_CODE);
}
}
use of org.trellisldp.api.TrellisRuntimeException in project trellis by trellis-ldp.
the class TrellisHttpResourceTest method testInitializeExistingLdpResourceWithFailure.
@Test
void testInitializeExistingLdpResourceWithFailure() throws Exception {
final ResourceService mockService = mock(ResourceService.class);
when(mockBundler.getResourceService()).thenReturn(mockService);
when(mockService.get(root)).thenAnswer(inv -> runAsync(() -> {
throw new TrellisRuntimeException("Expected exception");
}));
final TrellisHttpResource matcher = new TrellisHttpResource();
matcher.services = mockBundler;
assertDoesNotThrow(() -> matcher.initialize());
assertAll("Verify interactions with init-errored resource service", verifyInteractions(mockService));
}
Aggregations