use of javax.ws.rs.core.Response.Status.NO_CONTENT in project trellis by trellis-ldp.
the class TrellisWebDAV method copyResource.
/**
* Copy a resource.
* @return the async response
*/
@COPY
@Timed
public CompletionStage<Response> copyResource() {
final TrellisRequest req = new TrellisRequest(request, uriInfo, headers, security);
final Session session = HttpSession.from(security);
final IRI destination = getDestination(headers, getBaseUrl(req));
final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + req.getPath());
// Default is recursive copy as per RFC-4918
final Depth.DEPTH depth = getDepth(headers.getHeaderString("Depth"));
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).thenCompose(res -> copyTo(res, session, depth, destination, getBaseUrl(req))).thenApply(future -> status(NO_CONTENT).build()).exceptionally(this::handleException);
}
use of javax.ws.rs.core.Response.Status.NO_CONTENT 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);
}
use of javax.ws.rs.core.Response.Status.NO_CONTENT 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 javax.ws.rs.core.Response.Status.NO_CONTENT in project trellis by trellis-ldp.
the class GetHandler method getLdpRs.
private ResponseBuilder getLdpRs(final ResponseBuilder builder, final RDFSyntax syntax, final IRI profile) {
final Prefer prefer = getRequest().getPrefer();
// Check for a cache hit
if (!RDFSyntax.RDFA.equals(syntax)) {
final EntityTag etag = generateEtag(getResource().getRevision(), weakEtags);
checkCache(getResource().getModified(), etag);
builder.tag(etag);
}
addAllowHeaders(builder);
if (prefer != null) {
builder.header(PREFERENCE_APPLIED, PREFER_RETURN + "=" + prefer.getPreference().orElse(PREFER_REPRESENTATION));
if (prefer.getPreference().filter(PREFER_MINIMAL::equals).isPresent()) {
return builder.status(NO_CONTENT);
}
}
// Short circuit HEAD requests
if (HEAD.equals(getRequest().getMethod())) {
return builder;
}
// Stream the rdf content
return builder.entity((StreamingOutput) out -> {
try (final Stream<Quad> stream = getResource().stream(getPreferredGraphs(prefer))) {
getServices().getIOService().write(stream.map(Quad::asTriple).map(unskolemizeTriples(getServices().getResourceService(), getBaseUrl())), out, syntax, getIdentifier(), getJsonLdProfile(profile, syntax));
}
});
}
Aggregations