def is_accept_hdr_rdf_parsable(): ''' Check if any of the 'Accept' header values provided is a RDF parsable format. ''' for mimetype in request.accept_mimetypes.values(): if LdpFactory.is_rdf_parsable(mimetype): return True return False
def put_resource(uid): """ https://www.w3.org/TR/ldp/#ldpr-HTTP_PUT Add or replace a new resource at a specified URI. """ # Parse headers. logger.debug('Request headers: {}'.format(request.headers)) rsp_headers = {'Content-Type': 'text/plain; charset=utf-8'} handling, disposition = set_post_put_params() stream, mimetype = _bistream_from_req() if LdpFactory.is_rdf_parsable(mimetype): # If the content is RDF, localize in-repo URIs. global_rdf = stream.read() local_rdf = g.tbox.localize_payload(global_rdf) graph = Graph().parse(data=local_rdf, format=mimetype, publicID=nsc['fcres'][uid]) stream = mimetype = None else: graph = None try: evt = rsrc_api.create_or_replace(uid, stream=stream, mimetype=mimetype, graph=graph, handling=handling, disposition=disposition) except (InvalidResourceError, ResourceExistsError) as e: return str(e), 409 except (ServerManagedTermError, SingleSubjectError) as e: return str(e), 412 except IncompatibleLdpTypeError as e: return str(e), 415 except TombstoneError as e: return _tombstone_response(e, uid) uri = g.tbox.uid_to_uri(uid) if evt == RES_CREATED: rsp_code = 201 rsp_headers['Location'] = rsp_body = uri if mimetype and not graph: rsp_headers['Link'] = ( '<{0}/fcr:metadata>; rel="describedby"'.format(uri)) else: rsp_code = 204 rsp_body = '' return rsp_body, rsp_code, rsp_headers
def post_resource(parent_uid): """ https://www.w3.org/TR/ldp/#ldpr-HTTP_POST Add a new resource in a new URI. """ out_headers = std_headers try: slug = request.headers['Slug'] logger.debug('Slug: {}'.format(slug)) except KeyError: slug = None handling, disposition = set_post_put_params() stream, mimetype = _bistream_from_req() if LdpFactory.is_rdf_parsable(mimetype): # If the content is RDF, localize in-repo URIs. global_rdf = stream.read() local_rdf = g.tbox.localize_payload(global_rdf) stream = BytesIO(local_rdf) is_rdf = True else: is_rdf = False try: uid = rsrc_api.create(parent_uid, slug, stream=stream, mimetype=mimetype, handling=handling, disposition=disposition) except ResourceNotExistsError as e: return str(e), 404 except InvalidResourceError as e: return str(e), 409 except TombstoneError as e: return _tombstone_response(e, uid) except ServerManagedTermError as e: return str(e), 412 uri = g.tbox.uid_to_uri(uid) hdr = {'Location': uri} if mimetype and not is_rdf: hdr['Link'] = '<{0}/fcr:metadata>; rel="describedby"; anchor="<{0}>"'\ .format(uri) out_headers.update(hdr) return uri, 201, out_headers