瀏覽代碼

Add ETag header.

Stefano Cossu 7 年之前
父節點
當前提交
dc744380d3

+ 2 - 1
lakesuperior/store_strategies/rdf/base_rdf_strategy.py

@@ -57,7 +57,8 @@ class BaseRdfStrategy(metaclass=ABCMeta):
     - Methods starting with `ask_` return a boolean value.
     - Methods starting with `ask_` return a boolean value.
     '''
     '''
 
 
-    UNION_GRAPH_URI = URIRef('urn:x-arq:UnionGraph') # This is Fuseki-specific
+    # N.B. This is Fuseki-specific.
+    UNION_GRAPH_URI = URIRef('urn:x-arq:UnionGraph')
 
 
     _logger = logging.getLogger(__module__)
     _logger = logging.getLogger(__module__)
 
 

+ 12 - 6
lakesuperior/store_strategies/rdf/simple_strategy.py

@@ -9,8 +9,8 @@ from rdflib.term import Literal, URIRef, Variable
 
 
 from lakesuperior.core.namespaces import ns_collection as nsc
 from lakesuperior.core.namespaces import ns_collection as nsc
 from lakesuperior.core.namespaces import ns_mgr as nsm
 from lakesuperior.core.namespaces import ns_mgr as nsm
-from lakesuperior.store_strategies.rdf.base_rdf_strategy import \
-        BaseRdfStrategy
+from lakesuperior.store_strategies.rdf.base_rdf_strategy import BaseRdfStrategy
+from lakesuperior.util.digest import Digest
 
 
 
 
 class SimpleStrategy(BaseRdfStrategy):
 class SimpleStrategy(BaseRdfStrategy):
@@ -31,13 +31,15 @@ class SimpleStrategy(BaseRdfStrategy):
         See base_rdf_strategy.headers.
         See base_rdf_strategy.headers.
         '''
         '''
         headers = {
         headers = {
-            'ETag' : 'W/"{}"'.format(
-                self.rsrc.value(nsc['premis'].hasMessageDigest)),
             'Link' : [],
             'Link' : [],
         }
         }
 
 
-        last_updated_term = self.rsrc.value(nsc['fedora'].lastUpdated) or \
-                self.rsrc.value(nsc['fedora'].lastUpdated)
+        digest = self.rsrc.value(nsc['premis'].hasMessageDigest)
+        if digest:
+            etag = digest.identifier.split(':')[-1]
+            headers['ETag'] = 'W/"{}"'.format(etag),
+
+        last_updated_term = self.rsrc.value(nsc['fedora'].lastUpdated)
         if last_updated_term:
         if last_updated_term:
             headers['Last-Modified'] = arrow.get(last_updated_term)\
             headers['Last-Modified'] = arrow.get(last_updated_term)\
                 .format('ddd, D MMM YYYY HH:mm:ss Z')
                 .format('ddd, D MMM YYYY HH:mm:ss Z')
@@ -120,6 +122,10 @@ class SimpleStrategy(BaseRdfStrategy):
         self.rsrc.set(nsc['fedora'].created, ts)
         self.rsrc.set(nsc['fedora'].created, ts)
         self.rsrc.set(nsc['fedora'].createdBy, Literal('BypassAdmin'))
         self.rsrc.set(nsc['fedora'].createdBy, Literal('BypassAdmin'))
 
 
+        cksum = Digest.rdf_cksum(self.rsrc.graph)
+        self.rsrc.set(nsc['premis'].hasMessageDigest,
+                URIRef('urn:sha1:{}'.format(cksum)))
+
         for s, p, o in g:
         for s, p, o in g:
             self.ds.add((s, p, o))
             self.ds.add((s, p, o))
 
 

+ 9 - 0
lakesuperior/util/digest.py

@@ -1,3 +1,12 @@
+import pickle
+
+from hashlib import sha1
+
+from rdflib.term import Literal, URIRef, Variable
+
+from lakesuperior.core.namespaces import ns_collection as nsc
+
+
 class Digest:
 class Digest:
     '''
     '''
     Various digest functions. May be merged into something more generic later.
     Various digest functions. May be merged into something more generic later.