|
@@ -17,14 +17,10 @@ from rdflib import Graph, plugin, parser#, serializer
|
|
from werkzeug.http import parse_date
|
|
from werkzeug.http import parse_date
|
|
|
|
|
|
from lakesuperior import env
|
|
from lakesuperior import env
|
|
|
|
+from lakesuperior import exceptions as exc
|
|
from lakesuperior.api import resource as rsrc_api
|
|
from lakesuperior.api import resource as rsrc_api
|
|
from lakesuperior.dictionaries.namespaces import ns_collection as nsc
|
|
from lakesuperior.dictionaries.namespaces import ns_collection as nsc
|
|
from lakesuperior.dictionaries.namespaces import ns_mgr as nsm
|
|
from lakesuperior.dictionaries.namespaces import ns_mgr as nsm
|
|
-from lakesuperior.exceptions import (
|
|
|
|
- ChecksumValidationError, ResourceNotExistsError, TombstoneError,
|
|
|
|
- ServerManagedTermError, InvalidResourceError, SingleSubjectError,
|
|
|
|
- ResourceExistsError, IncompatibleLdpTypeError,
|
|
|
|
- IndigestibleError)
|
|
|
|
from lakesuperior.globals import RES_CREATED
|
|
from lakesuperior.globals import RES_CREATED
|
|
from lakesuperior.model.ldp.ldp_factory import LdpFactory
|
|
from lakesuperior.model.ldp.ldp_factory import LdpFactory
|
|
from lakesuperior.model.ldp.ldp_nr import LdpNr
|
|
from lakesuperior.model.ldp.ldp_nr import LdpNr
|
|
@@ -152,7 +148,7 @@ def get_resource(uid, out_fmt=None):
|
|
try:
|
|
try:
|
|
if not rsrc_api.exists(uid):
|
|
if not rsrc_api.exists(uid):
|
|
return '', 404
|
|
return '', 404
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
|
|
|
|
# Then process the condition headers.
|
|
# Then process the condition headers.
|
|
@@ -229,11 +225,11 @@ def get_version_info(uid):
|
|
rdf_mimetype = _best_rdf_mimetype() or DEFAULT_RDF_MIMETYPE
|
|
rdf_mimetype = _best_rdf_mimetype() or DEFAULT_RDF_MIMETYPE
|
|
try:
|
|
try:
|
|
imr = rsrc_api.get_version_info(uid)
|
|
imr = rsrc_api.get_version_info(uid)
|
|
- except ResourceNotExistsError as e:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError as e:
|
|
return str(e), 404
|
|
return str(e), 404
|
|
- except InvalidResourceError as e:
|
|
|
|
|
|
+ except exc.InvalidResourceError as e:
|
|
return str(e), 409
|
|
return str(e), 409
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
else:
|
|
else:
|
|
with store.txn_ctx():
|
|
with store.txn_ctx():
|
|
@@ -251,11 +247,11 @@ def get_version(uid, ver_uid):
|
|
rdf_mimetype = _best_rdf_mimetype() or DEFAULT_RDF_MIMETYPE
|
|
rdf_mimetype = _best_rdf_mimetype() or DEFAULT_RDF_MIMETYPE
|
|
try:
|
|
try:
|
|
imr = rsrc_api.get_version(uid, ver_uid)
|
|
imr = rsrc_api.get_version(uid, ver_uid)
|
|
- except ResourceNotExistsError as e:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError as e:
|
|
return str(e), 404
|
|
return str(e), 404
|
|
- except InvalidResourceError as e:
|
|
|
|
|
|
+ except exc.InvalidResourceError as e:
|
|
return str(e), 409
|
|
return str(e), 409
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
else:
|
|
else:
|
|
with store.txn_ctx():
|
|
with store.txn_ctx():
|
|
@@ -277,17 +273,19 @@ def post_resource(parent_uid):
|
|
try:
|
|
try:
|
|
kwargs = _create_args_from_req(slug)
|
|
kwargs = _create_args_from_req(slug)
|
|
rsrc = rsrc_api.create(parent_uid, slug, **kwargs)
|
|
rsrc = rsrc_api.create(parent_uid, slug, **kwargs)
|
|
- except IndigestibleError:
|
|
|
|
|
|
+ except exc.RdfParsingError as e:
|
|
|
|
+ return str(e), 400
|
|
|
|
+ except exc.IndigestibleError:
|
|
return (
|
|
return (
|
|
f'Unable to parse digest header: {request.headers["digest"]}'
|
|
f'Unable to parse digest header: {request.headers["digest"]}'
|
|
), 400
|
|
), 400
|
|
- except ResourceNotExistsError as e:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError as e:
|
|
return str(e), 404
|
|
return str(e), 404
|
|
- except (InvalidResourceError, ChecksumValidationError) as e:
|
|
|
|
|
|
+ except (exc.InvalidResourceError, exc.ChecksumValidationError) as e:
|
|
return str(e), 409
|
|
return str(e), 409
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
- except ServerManagedTermError as e:
|
|
|
|
|
|
+ except exc.ServerManagedTermError as e:
|
|
rsp_headers['Link'] = (
|
|
rsp_headers['Link'] = (
|
|
f'<{uri}>; rel="{nsc["ldp"].constrainedBy}"; '
|
|
f'<{uri}>; rel="{nsc["ldp"].constrainedBy}"; '
|
|
f'{g.webroot}/info/ldp_constraints"'
|
|
f'{g.webroot}/info/ldp_constraints"'
|
|
@@ -326,19 +324,21 @@ def put_resource(uid):
|
|
try:
|
|
try:
|
|
kwargs = _create_args_from_req(uid)
|
|
kwargs = _create_args_from_req(uid)
|
|
evt, rsrc = rsrc_api.create_or_replace(uid, **kwargs)
|
|
evt, rsrc = rsrc_api.create_or_replace(uid, **kwargs)
|
|
- except IndigestibleError:
|
|
|
|
|
|
+ except exc.RdfParsingError as e:
|
|
|
|
+ return str(e), 400
|
|
|
|
+ except exc.IndigestibleError:
|
|
return (
|
|
return (
|
|
- f'Unable to parse digest header: {request.headers["digest"]}'
|
|
|
|
- ), 400
|
|
|
|
|
|
+ f'Unable to parse digest header: {request.headers["digest"]}',
|
|
|
|
+ 400)
|
|
except (
|
|
except (
|
|
- InvalidResourceError, ChecksumValidationError,
|
|
|
|
- ResourceExistsError) as e:
|
|
|
|
|
|
+ exc.InvalidResourceError, exc.ChecksumValidationError,
|
|
|
|
+ exc.ResourceExistsError) as e:
|
|
return str(e), 409
|
|
return str(e), 409
|
|
- except (ServerManagedTermError, SingleSubjectError) as e:
|
|
|
|
|
|
+ except (exc.ServerManagedTermError, exc.SingleSubjectError) as e:
|
|
return str(e), 412
|
|
return str(e), 412
|
|
- except IncompatibleLdpTypeError as e:
|
|
|
|
|
|
+ except exc.IncompatibleLdpTypeError as e:
|
|
return str(e), 415
|
|
return str(e), 415
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
|
|
|
|
with store.txn_ctx():
|
|
with store.txn_ctx():
|
|
@@ -371,7 +371,7 @@ def patch_resource(uid, is_metadata=False):
|
|
try:
|
|
try:
|
|
if not rsrc_api.exists(uid):
|
|
if not rsrc_api.exists(uid):
|
|
return '', 404
|
|
return '', 404
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
|
|
|
|
# Then process the condition headers.
|
|
# Then process the condition headers.
|
|
@@ -390,9 +390,9 @@ def patch_resource(uid, is_metadata=False):
|
|
local_update_str = g.tbox.localize_ext_str(update_str, nsc['fcres'][uid])
|
|
local_update_str = g.tbox.localize_ext_str(update_str, nsc['fcres'][uid])
|
|
try:
|
|
try:
|
|
rsrc = rsrc_api.update(uid, local_update_str, is_metadata, handling)
|
|
rsrc = rsrc_api.update(uid, local_update_str, is_metadata, handling)
|
|
- except (ServerManagedTermError, SingleSubjectError) as e:
|
|
|
|
|
|
+ except (exc.ServerManagedTermError, exc.SingleSubjectError) as e:
|
|
return str(e), 412
|
|
return str(e), 412
|
|
- except InvalidResourceError as e:
|
|
|
|
|
|
+ except exc.InvalidResourceError as e:
|
|
return str(e), 415
|
|
return str(e), 415
|
|
else:
|
|
else:
|
|
with store.txn_ctx():
|
|
with store.txn_ctx():
|
|
@@ -423,7 +423,7 @@ def delete_resource(uid):
|
|
try:
|
|
try:
|
|
if not rsrc_api.exists(uid):
|
|
if not rsrc_api.exists(uid):
|
|
return '', 404
|
|
return '', 404
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
|
|
|
|
# Then process the condition headers.
|
|
# Then process the condition headers.
|
|
@@ -455,7 +455,7 @@ def tombstone(uid):
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
rsrc_api.get(uid)
|
|
rsrc_api.get(uid)
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
if request.method == 'DELETE':
|
|
if request.method == 'DELETE':
|
|
if e.uid == uid:
|
|
if e.uid == uid:
|
|
rsrc_api.delete(uid, False)
|
|
rsrc_api.delete(uid, False)
|
|
@@ -471,7 +471,7 @@ def tombstone(uid):
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
else:
|
|
else:
|
|
return 'Method Not Allowed.', 405
|
|
return 'Method Not Allowed.', 405
|
|
- except ResourceNotExistsError as e:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError as e:
|
|
return str(e), 404
|
|
return str(e), 404
|
|
else:
|
|
else:
|
|
return '', 404
|
|
return '', 404
|
|
@@ -488,11 +488,11 @@ def post_version(uid):
|
|
|
|
|
|
try:
|
|
try:
|
|
ver_uid = rsrc_api.create_version(uid, ver_uid)
|
|
ver_uid = rsrc_api.create_version(uid, ver_uid)
|
|
- except ResourceNotExistsError as e:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError as e:
|
|
return str(e), 404
|
|
return str(e), 404
|
|
- except InvalidResourceError as e:
|
|
|
|
|
|
+ except exc.InvalidResourceError as e:
|
|
return str(e), 409
|
|
return str(e), 409
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
else:
|
|
else:
|
|
return '', 201, {'Location': g.tbox.uid_to_uri(ver_uid)}
|
|
return '', 201, {'Location': g.tbox.uid_to_uri(ver_uid)}
|
|
@@ -510,11 +510,11 @@ def patch_version(uid, ver_uid):
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
rsrc_api.revert_to_version(uid, ver_uid)
|
|
rsrc_api.revert_to_version(uid, ver_uid)
|
|
- except ResourceNotExistsError as e:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError as e:
|
|
return str(e), 404
|
|
return str(e), 404
|
|
- except InvalidResourceError as e:
|
|
|
|
|
|
+ except exc.InvalidResourceError as e:
|
|
return str(e), 409
|
|
return str(e), 409
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
else:
|
|
else:
|
|
return '', 204
|
|
return '', 204
|
|
@@ -611,7 +611,7 @@ def _create_args_from_req(uid):
|
|
request.headers['digest'].split('=')
|
|
request.headers['digest'].split('=')
|
|
)
|
|
)
|
|
except ValueError:
|
|
except ValueError:
|
|
- raise IndigestibleError(uid)
|
|
|
|
|
|
+ raise exc.IndigestibleError(uid)
|
|
|
|
|
|
return kwargs
|
|
return kwargs
|
|
|
|
|
|
@@ -798,7 +798,7 @@ def _condition_hdr_match(uid, headers, safe=True):
|
|
with store.txn_ctx():
|
|
with store.txn_ctx():
|
|
try:
|
|
try:
|
|
rsrc_meta = rsrc_api.get_metadata(uid)
|
|
rsrc_meta = rsrc_api.get_metadata(uid)
|
|
- except ResourceNotExistsError:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError:
|
|
rsrc_meta = Graph(uri=nsc['fcres'][uid])
|
|
rsrc_meta = Graph(uri=nsc['fcres'][uid])
|
|
|
|
|
|
digest_prop = rsrc_meta.value(nsc['premis'].hasMessageDigest)
|
|
digest_prop = rsrc_meta.value(nsc['premis'].hasMessageDigest)
|
|
@@ -820,7 +820,7 @@ def _condition_hdr_match(uid, headers, safe=True):
|
|
|
|
|
|
try:
|
|
try:
|
|
rsrc_meta = rsrc_api.get_metadata(uid)
|
|
rsrc_meta = rsrc_api.get_metadata(uid)
|
|
- except ResourceNotExistsError:
|
|
|
|
|
|
+ except exc.ResourceNotExistsError:
|
|
return {
|
|
return {
|
|
'if-modified-since': False,
|
|
'if-modified-since': False,
|
|
'if-unmodified-since': False
|
|
'if-unmodified-since': False
|
|
@@ -861,7 +861,7 @@ def _process_cond_headers(uid, headers, safe=True):
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
cond_match = _condition_hdr_match(uid, headers, safe)
|
|
cond_match = _condition_hdr_match(uid, headers, safe)
|
|
- except TombstoneError as e:
|
|
|
|
|
|
+ except exc.TombstoneError as e:
|
|
return _tombstone_response(e, uid)
|
|
return _tombstone_response(e, uid)
|
|
|
|
|
|
if cond_match:
|
|
if cond_match:
|