src/main/java/org/trellisldp/http/impl/PatchHandler.java
/**
     * Update a resource with Sparql-Update and build an HTTP response
     * @param res the resource
     * @return the Response builder
     */
Update a resource with Sparql-Update and build an HTTP response
@param res the resource
@return the Response builder
Update a resource with Sparql - Update and build an HTTP response @param res the resource @return the Response builder
public ResponseBuilder updateResource(final Resource res) {
        final String baseUrl = getBaseUrl();
        final String identifier = baseUrl + req.getPartition() + req.getPath() +
            (ACL.equals(req.getExt()) ? "?ext=acl" : "");
        if (isNull(sparqlUpdate)) {
            throw new WebApplicationException("Missing Sparql-Update body", BAD_REQUEST);
        }
        final Session session = ofNullable(req.getSession()).orElseGet(HttpSession::new);
        checkDeleted(res, identifier);
        final EntityTag etag = new EntityTag(md5Hex(res.getModified() + identifier));
        checkCache(req.getRequest(), res.getModified(), etag);
        LOGGER.debug("Updating {} via PATCH", identifier);
        final IRI graphName = ACL.equals(req.getExt()) ? PreferAccessControl : PreferUserManaged;
        final IRI otherGraph = ACL.equals(req.getExt()) ? PreferUserManaged : PreferAccessControl;
        final List<Triple> triples = updateGraph(res, graphName);
        try (final TrellisDataset dataset = TrellisDataset.createDataset()) {
            triples.stream().map(skolemizeTriples(resourceService, baseUrl))
                .map(t -> rdf.createQuad(graphName, t.getSubject(), t.getPredicate(), t.getObject()))
                .forEachOrdered(dataset::add);
            audit.ifPresent(svc -> svc.update(res.getIdentifier(), session).stream()
                    .map(skolemizeQuads(resourceService, baseUrl)).forEachOrdered(dataset::add));
            dataset.add(rdf.createQuad(PreferServerManaged, res.getIdentifier(), RDF.type, res.getInteractionModel()));
            final List<ConstraintViolation> violations = constraintServices.stream()
                .flatMap(svc -> dataset.getGraph(graphName).map(Stream::of).orElseGet(Stream::empty)
                    .flatMap(g -> svc.constrainedBy(res.getInteractionModel(), baseUrl, g)))
                .collect(toList());
            if (!violations.isEmpty()) {
                final ResponseBuilder err = status(CONFLICT);
                violations.forEach(v -> err.link(v.getConstraint().getIRIString(), LDP.constrainedBy.getIRIString()));
                throw new WebApplicationException(err.build());
            }
            try (final Stream<? extends Triple> remaining = res.stream(otherGraph)) {
                remaining.map(t -> rdf.createQuad(otherGraph, t.getSubject(), t.getPredicate(), t.getObject()))
                    .forEachOrdered(dataset::add);
            }
            if (resourceService.put(res.getIdentifier(), dataset.asDataset())) {
                final ResponseBuilder builder = ok();
                ldpResourceTypes(res.getInteractionModel()).map(IRI::getIRIString)
                    .forEach(type -> builder.link(type, "type"));
                return ofNullable(req.getPrefer()).flatMap(Prefer::getPreference).filter(PREFER_REPRESENTATION::equals)
                    .map(prefer -> {
                        final RDFSyntax syntax = getSyntax(req.getHeaders().getAcceptableMediaTypes(), empty())
                            .orElseThrow(NotAcceptableException::new);
                        final IRI profile = ofNullable(getProfile(req.getHeaders().getAcceptableMediaTypes(), syntax))
                            .orElseGet(() -> getDefaultProfile(syntax, identifier));
                        final StreamingOutput stream = new StreamingOutput() {
                            @Override
                            public void write(final OutputStream out) throws IOException {
                                ioService.write(triples.stream().map(unskolemizeTriples(resourceService, baseUrl)),
                                        out, syntax, profile);
                            }
                        };
                        return builder.header(PREFERENCE_APPLIED, "return=representation").type(syntax.mediaType)
                               .entity(stream);
                    }).orElseGet(() -> builder.status(NO_CONTENT));
            }
        }
        LOGGER.error("Unable to persist data to location at {}", res.getIdentifier());
        return serverError().type(TEXT_PLAIN)
            .entity("Unable to persist data. Please consult the logs for more information");
    }
public ResponseBuilder updateResource ( final Resource res ) { final String baseUrl = getBaseUrl ( ) ; final String identifier = baseUrl + req . getPartition ( ) + req . getPath ( ) + ( ACL . equals ( req . getExt ( ) ) ? "?ext=acl" : "" ) ; if ( isNull ( sparqlUpdate ) ) { throw new WebApplicationException ( "Missing Sparql-Update body" , BAD_REQUEST ) ; } final Session session = ofNullable ( req . getSession ( ) ) . orElseGet ( HttpSession :: new ) ; checkDeleted ( res , identifier ) ; final EntityTag etag = new EntityTag ( md5Hex ( res . getModified ( ) + identifier ) ) ; checkCache ( req . getRequest ( ) , res . getModified ( ) , etag ) ; LOGGER . debug ( "Updating {} via PATCH" , identifier ) ; final IRI graphName = ACL . equals ( req . getExt ( ) ) ? PreferAccessControl : PreferUserManaged ; final IRI otherGraph = ACL . equals ( req . getExt ( ) ) ? PreferUserManaged : PreferAccessControl ; final List < Triple > triples = updateGraph ( res , graphName ) ; try ( final TrellisDataset dataset = TrellisDataset . createDataset ( ) ) { triples . stream ( ) . map ( skolemizeTriples ( resourceService , baseUrl ) ) . map ( t -> rdf . createQuad ( graphName , t . getSubject ( ) , t . getPredicate ( ) , t . getObject ( ) ) ) . forEachOrdered ( dataset :: add ) ; audit . ifPresent ( svc -> svc . update ( res . getIdentifier ( ) , session ) . stream ( ) . map ( skolemizeQuads ( resourceService , baseUrl ) ) . forEachOrdered ( dataset :: add ) ) ; dataset . add ( rdf . createQuad ( PreferServerManaged , res . getIdentifier ( ) , RDF . type , res . getInteractionModel ( ) ) ) ; final List < ConstraintViolation > violations = constraintServices . stream ( ) . flatMap ( svc -> dataset . getGraph ( graphName ) . map ( Stream :: of ) . orElseGet ( Stream :: empty ) . flatMap ( g -> svc . constrainedBy ( res . getInteractionModel ( ) , baseUrl , g ) ) ) . collect ( toList ( ) ) ; if ( ! violations . isEmpty ( ) ) { final ResponseBuilder err = status ( CONFLICT ) ; violations . forEach ( v -> err . link ( v . getConstraint ( ) . getIRIString ( ) , LDP . constrainedBy . getIRIString ( ) ) ) ; throw new WebApplicationException ( err . build ( ) ) ; } try ( final Stream < ? extends Triple > remaining = res . stream ( otherGraph ) ) { remaining . map ( t -> rdf . createQuad ( otherGraph , t . getSubject ( ) , t . getPredicate ( ) , t . getObject ( ) ) ) . forEachOrdered ( dataset :: add ) ; } if ( resourceService . put ( res . getIdentifier ( ) , dataset . asDataset ( ) ) ) { final ResponseBuilder builder = ok ( ) ; ldpResourceTypes ( res . getInteractionModel ( ) ) . map ( IRI :: getIRIString ) . forEach ( type -> builder . link ( type , "type" ) ) ; return ofNullable ( req . getPrefer ( ) ) . flatMap ( Prefer :: getPreference ) . filter ( PREFER_REPRESENTATION :: equals ) . map ( prefer -> { final RDFSyntax syntax = getSyntax ( req . getHeaders ( ) . getAcceptableMediaTypes ( ) , empty ( ) ) . orElseThrow ( NotAcceptableException :: new ) ; final IRI profile = ofNullable ( getProfile ( req . getHeaders ( ) . getAcceptableMediaTypes ( ) , syntax ) ) . orElseGet ( ( ) -> getDefaultProfile ( syntax , identifier ) ) ; final StreamingOutput stream = new StreamingOutput ( ) { @ Override public void write ( final OutputStream out ) throws IOException { ioService . write ( triples . stream ( ) . map ( unskolemizeTriples ( resourceService , baseUrl ) ) , out , syntax , profile ) ; } } ; return builder . header ( PREFERENCE_APPLIED , "return=representation" ) . type ( syntax . mediaType ) . entity ( stream ) ; } ) . orElseGet ( ( ) -> builder . status ( NO_CONTENT ) ) ; } } LOGGER . error ( "Unable to persist data to location at {}" , res . getIdentifier ( ) ) ; return serverError ( ) . type ( TEXT_PLAIN ) . entity ( "Unable to persist data. Please consult the logs for more information" ) ; }
Update a resource with Sparql-Update and build an HTTP response
 @param res the resource
 @return the Response builder
Update a resource with Sparql - Update and build an HTTP response @param res the resource @return the Response builder
// Check if this is already deleted // Check the cache // Put triples in buffer // Add audit-related triples // Add existing LDP type // Check any constraints // When updating User or ACL triples, be sure to add the other category to the dataset // Save new dataset