src/main/java/org/trellisldp/http/impl/PostHandler.java
/**
     * Create a new resource
     * @return the response builder
     */
Create a new resource
@return the response builder
Create a new resource @return the response builder
public ResponseBuilder createResource() {
        final String baseUrl = getBaseUrl();
        final String identifier = baseUrl + req.getPartition() + req.getPath() + id;
        final String contentType = req.getContentType();
        final Session session = ofNullable(req.getSession()).orElseGet(HttpSession::new);
        LOGGER.info("Creating resource as {}", identifier);
        final Optional<RDFSyntax> rdfSyntax = ofNullable(contentType).flatMap(RDFSyntax::byMediaType)
            .filter(SUPPORTED_RDF_TYPES::contains);
        final IRI defaultType = nonNull(contentType) && !rdfSyntax.isPresent() ? LDP.NonRDFSource : LDP.RDFSource;
        final IRI internalId = rdf.createIRI(TRELLIS_PREFIX + req.getPartition() + req.getPath() + id);
        final IRI ldpType = ofNullable(req.getLink())
            .filter(l -> "type".equals(l.getRel())).map(Link::getUri).map(URI::toString)
            .filter(l -> l.startsWith(LDP.URI)).map(rdf::createIRI)
            .filter(l -> !LDP.Resource.equals(l)).orElse(defaultType);
        if (ldpType.equals(LDP.NonRDFSource) && rdfSyntax.isPresent()) {
            return status(BAD_REQUEST).type(TEXT_PLAIN).entity("Cannot save a NonRDFSource with RDF syntax");
        }
        try (final TrellisDataset dataset = TrellisDataset.createDataset()) {
            audit.ifPresent(svc ->
                    svc.creation(internalId, session).stream().map(skolemizeQuads(resourceService, baseUrl))
                .forEachOrdered(dataset::add));
            dataset.add(rdf.createQuad(PreferServerManaged, internalId, RDF.type, ldpType));
            if (ldpType.equals(LDP.NonRDFSource)) {
                final Digest digest = req.getDigest();
                if (nonNull(digest) && !getDigestForEntity(digest).equals(digest.getDigest())) {
                    return status(BAD_REQUEST);
                }
                final Map<String, String> metadata = singletonMap(CONTENT_TYPE, ofNullable(contentType)
                        .orElse(APPLICATION_OCTET_STREAM));
                final IRI binaryLocation = rdf.createIRI(binaryService.getIdentifierSupplier(req.getPartition())
                        .get());
                dataset.add(rdf.createQuad(PreferServerManaged, internalId, DC.hasPart, binaryLocation));
                dataset.add(rdf.createQuad(PreferServerManaged, binaryLocation, DC.modified,
                            rdf.createLiteral(now().toString(), XSD.dateTime)));
                dataset.add(rdf.createQuad(PreferServerManaged, binaryLocation, DC.format,
                            rdf.createLiteral(ofNullable(contentType).orElse(APPLICATION_OCTET_STREAM))));
                dataset.add(rdf.createQuad(PreferServerManaged, binaryLocation, DC.extent,
                            rdf.createLiteral(Long.toString(entity.length()), XSD.long_)));
                persistContent(binaryLocation, metadata);
            } else {
                readEntityIntoDataset(identifier, baseUrl, PreferUserManaged, rdfSyntax.orElse(TURTLE), dataset);
                checkConstraint(dataset, PreferUserManaged, ldpType, TRELLIS_PREFIX + req.getPartition(),
                        rdfSyntax.orElse(TURTLE));
            }
            if (resourceService.put(internalId, dataset.asDataset())) {
                final ResponseBuilder builder = status(CREATED).location(create(identifier));
                ldpResourceTypes(ldpType).map(IRI::getIRIString).forEach(type -> builder.link(type, "type"));
                return builder;
            }
        }
        LOGGER.error("Unable to persist data to location at {}", internalId.getIRIString());
        return serverError().type(TEXT_PLAIN)
            .entity("Unable to persist data. Please consult the logs for more information");
    }
public ResponseBuilder createResource ( ) { final String baseUrl = getBaseUrl ( ) ; final String identifier = baseUrl + req . getPartition ( ) + req . getPath ( ) + id ; final String contentType = req . getContentType ( ) ; final Session session = ofNullable ( req . getSession ( ) ) . orElseGet ( HttpSession :: new ) ; LOGGER . info ( "Creating resource as {}" , identifier ) ; final Optional < RDFSyntax > rdfSyntax = ofNullable ( contentType ) . flatMap ( RDFSyntax :: byMediaType ) . filter ( SUPPORTED_RDF_TYPES :: contains ) ; final IRI defaultType = nonNull ( contentType ) && ! rdfSyntax . isPresent ( ) ? LDP . NonRDFSource : LDP . RDFSource ; final IRI internalId = rdf . createIRI ( TRELLIS_PREFIX + req . getPartition ( ) + req . getPath ( ) + id ) ; final IRI ldpType = ofNullable ( req . getLink ( ) ) . filter ( l -> "type" . equals ( l . getRel ( ) ) ) . map ( Link :: getUri ) . map ( URI :: toString ) . filter ( l -> l . startsWith ( LDP . URI ) ) . map ( rdf :: createIRI ) . filter ( l -> ! LDP . Resource . equals ( l ) ) . orElse ( defaultType ) ; if ( ldpType . equals ( LDP . NonRDFSource ) && rdfSyntax . isPresent ( ) ) { return status ( BAD_REQUEST ) . type ( TEXT_PLAIN ) . entity ( "Cannot save a NonRDFSource with RDF syntax" ) ; } try ( final TrellisDataset dataset = TrellisDataset . createDataset ( ) ) { audit . ifPresent ( svc -> svc . creation ( internalId , session ) . stream ( ) . map ( skolemizeQuads ( resourceService , baseUrl ) ) . forEachOrdered ( dataset :: add ) ) ; dataset . add ( rdf . createQuad ( PreferServerManaged , internalId , RDF . type , ldpType ) ) ; if ( ldpType . equals ( LDP . NonRDFSource ) ) { final Digest digest = req . getDigest ( ) ; if ( nonNull ( digest ) && ! getDigestForEntity ( digest ) . equals ( digest . getDigest ( ) ) ) { return status ( BAD_REQUEST ) ; } final Map < String , String > metadata = singletonMap ( CONTENT_TYPE , ofNullable ( contentType ) . orElse ( APPLICATION_OCTET_STREAM ) ) ; final IRI binaryLocation = rdf . createIRI ( binaryService . getIdentifierSupplier ( req . getPartition ( ) ) . get ( ) ) ; dataset . add ( rdf . createQuad ( PreferServerManaged , internalId , DC . hasPart , binaryLocation ) ) ; dataset . add ( rdf . createQuad ( PreferServerManaged , binaryLocation , DC . modified , rdf . createLiteral ( now ( ) . toString ( ) , XSD . dateTime ) ) ) ; dataset . add ( rdf . createQuad ( PreferServerManaged , binaryLocation , DC . format , rdf . createLiteral ( ofNullable ( contentType ) . orElse ( APPLICATION_OCTET_STREAM ) ) ) ) ; dataset . add ( rdf . createQuad ( PreferServerManaged , binaryLocation , DC . extent , rdf . createLiteral ( Long . toString ( entity . length ( ) ) , XSD . long_ ) ) ) ; persistContent ( binaryLocation , metadata ) ; } else { readEntityIntoDataset ( identifier , baseUrl , PreferUserManaged , rdfSyntax . orElse ( TURTLE ) , dataset ) ; checkConstraint ( dataset , PreferUserManaged , ldpType , TRELLIS_PREFIX + req . getPartition ( ) , rdfSyntax . orElse ( TURTLE ) ) ; } if ( resourceService . put ( internalId , dataset . asDataset ( ) ) ) { final ResponseBuilder builder = status ( CREATED ) . location ( create ( identifier ) ) ; ldpResourceTypes ( ldpType ) . map ( IRI :: getIRIString ) . forEach ( type -> builder . link ( type , "type" ) ) ; return builder ; } } LOGGER . error ( "Unable to persist data to location at {}" , internalId . getIRIString ( ) ) ; return serverError ( ) . type ( TEXT_PLAIN ) . entity ( "Unable to persist data. Please consult the logs for more information" ) ; }
Create a new resource
 @return the response builder
Create a new resource @return the response builder
// Add LDP type (ldp:Resource results in the defaultType) // Add Audit quads // Add user-supplied data // Check the expected digest value // Persist the content // Check for any constraints // Add LDP types