<s> """<STR_LIT>""" <EOL> from twisted . python import log <EOL> from twisted . web . server import NOT_DONE_YET <EOL> from twisted . web . static import NoRangeStaticProducer <EOL> from vcdm import blob <EOL> from vcdm import c <EOL> from vcdm . server . cdmi . cdmi_content_types import CDMI_OBJECT <EOL> from vcdm . server . cdmi . generic import set_common_headers , parse_path , get_common_body <EOL> from vcdm . server . cdmi . cdmiresource import StorageResource <EOL> from httplib import OK , CREATED , FOUND <EOL> from StringIO import StringIO <EOL> import base64 <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> class Blob ( StorageResource ) : <EOL> isLeaf = True <EOL> allowedMethods = ( '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def render_GET ( self , request , bodyless = False ) : <EOL> """<STR_LIT>""" <EOL> _ , __ , fullpath = parse_path ( request . path ) <EOL> tre_header = request . getHeader ( '<STR_LIT>' ) <EOL> tre_request = tre_header is not None and tre_header . lower ( ) == '<STR_LIT:true>' <EOL> log . msg ( "<STR_LIT>" ) <EOL> status , vals = blob . read ( self . avatar , fullpath , tre_request ) <EOL> request . setResponseCode ( status ) <EOL> request . setHeader ( '<STR_LIT:Content-Type>' , CDMI_OBJECT ) <EOL> if tre_request and status == FOUND : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:/>" . join ( [ c ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> str ( vals [ '<STR_LIT>' ] ) ] ) ) <EOL> request . setLastModified ( float ( vals [ '<STR_LIT>' ] ) ) <EOL> set_common_headers ( request ) <EOL> if status == OK : <EOL> request . setLastModified ( float ( vals [ '<STR_LIT>' ] ) ) <EOL> if not bodyless : <EOL> valueencoding = vals [ '<STR_LIT>' ] <EOL> content = ( vals [ '<STR_LIT:content>' ] . read ( ) if valueencoding == '<STR_LIT:utf-8>' else <EOL> base64 . b64encode ( vals [ '<STR_LIT:content>' ] . read ( ) ) ) <EOL> response_body = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : vals [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : vals [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : valueencoding , <EOL> '<STR_LIT:value>' : content , <EOL> '<STR_LIT>' : vals . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> response_body . update ( get_common_body ( request , str ( vals [ '<STR_LIT>' ] ) , <EOL> fullpath ) ) <EOL> return json . dumps ( response_body ) <EOL> return '<STR_LIT>' <EOL> def render_PUT ( self , request ) : <EOL> """<STR_LIT>""" <EOL> name , container_path , fullpath = parse_path ( request . path ) <EOL> length = int ( request . getHeader ( '<STR_LIT>' ) ) <EOL> request . content . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> body = json . loads ( request . content . read ( length ) ) <EOL> mimetype = body . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> metadata = body . get ( '<STR_LIT>' , { } ) <EOL> desired_backend = ( metadata . get ( '<STR_LIT>' ) or <EOL> request . getHeader ( '<STR_LIT>' ) ) <EOL> valueencoding = body . get ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) <EOL> body_value = body . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> value = ( body_value if valueencoding == '<STR_LIT:utf-8>' else <EOL> base64 . b64decode ( body_value ) ) <EOL> content = ( StringIO ( value ) , len ( value ) ) <EOL> status , uid = blob . write ( self . avatar , name , container_path , fullpath , <EOL> mimetype , metadata , content , valueencoding , <EOL> desired_backend = desired_backend ) <EOL> request . setResponseCode ( status ) <EOL> request . setHeader ( '<STR_LIT:Content-Type>' , CDMI_OBJECT ) <EOL> set_common_headers ( request ) <EOL> if status == OK or status == CREATED : <EOL> response_body = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : mimetype , <EOL> '<STR_LIT>' : metadata , <EOL> } <EOL> response_body . update ( get_common_body ( request , uid , fullpath ) ) <EOL> return json . dumps ( response_body ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def render_DELETE ( self , request ) : <EOL> """<STR_LIT>""" <EOL> _ , __ , fullpath = parse_path ( request . path ) <EOL> status = blob . delete ( self . avatar , fullpath ) <EOL> request . setResponseCode ( status ) <EOL> set_common_headers ( request ) <EOL> return '<STR_LIT>' <EOL> def render_HEAD ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return self . render_GET ( request , bodyless = True ) <EOL> class NonCDMIBlob ( StorageResource ) : <EOL> isLeaf = True <EOL> allowedMethods = ( '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def makeProducer ( self , request , content_object ) : <EOL> request . setResponseCode ( OK ) <EOL> return NoRangeStaticProducer ( request , content_object ) <EOL> def render_GET ( self , request , bodyless = False ) : <EOL> """<STR_LIT>""" <EOL> _ , __ , fullpath = parse_path ( request . path ) <EOL> log . msg ( "<STR_LIT>" % fullpath ) <EOL> tre_header = request . getHeader ( '<STR_LIT>' ) <EOL> tre_request = tre_header is not None and tre_header . lower ( ) == '<STR_LIT:true>' <EOL> status , vals = blob . read ( self . avatar , fullpath , tre_request ) <EOL> request . setResponseCode ( status ) <EOL> set_common_headers ( request , False ) <EOL> if tre_request and status == FOUND : <EOL> request . setHeader ( '<STR_LIT>' , "<STR_LIT:/>" . join ( [ c ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> str ( vals [ '<STR_LIT>' ] ) ] ) ) <EOL> if status is OK : <EOL> mimetype = vals [ '<STR_LIT>' ] <EOL> actual_type = '<STR_LIT>' if mimetype == '<STR_LIT>' else str ( mimetype ) <EOL> request . setHeader ( '<STR_LIT:Content-Type>' , actual_type ) <EOL> request . setLastModified ( float ( vals [ '<STR_LIT>' ] ) ) <EOL> if not bodyless : <EOL> request . setHeader ( '<STR_LIT>' , str ( vals [ '<STR_LIT:size>' ] ) ) <EOL> producer = self . makeProducer ( request , vals [ '<STR_LIT:content>' ] ) <EOL> producer . start ( ) <EOL> return NOT_DONE_YET <EOL> else : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def render_PUT ( self , request ) : <EOL> """<STR_LIT>""" <EOL> name , container_path , fullpath = parse_path ( request . path ) <EOL> length = request . getHeader ( '<STR_LIT>' ) <EOL> if length is None : <EOL> request . setResponseCode ( <NUM_LIT> ) <EOL> return '<STR_LIT>' <EOL> content = ( request . content , int ( length ) ) <EOL> mimetype = request . getHeader ( '<STR_LIT:Content-Type>' ) if request . getHeader ( '<STR_LIT:Content-Type>' ) is not None else '<STR_LIT>' <EOL> desired_backend = request . getHeader ( '<STR_LIT>' ) <EOL> if ( ( len ( mimetype ) == <NUM_LIT:2> and mimetype [ <NUM_LIT:1> ] == '<STR_LIT:utf-8>' ) ) : <EOL> valueencoding = '<STR_LIT:utf-8>' <EOL> else : <EOL> valueencoding = '<STR_LIT>' <EOL> status , _ = blob . write ( self . avatar , name , container_path , fullpath , <EOL> mimetype , { } , content , valueencoding , <EOL> desired_backend ) <EOL> request . setResponseCode ( status ) <EOL> set_common_headers ( request , False ) <EOL> return '<STR_LIT>' <EOL> def render_DELETE ( self , request ) : <EOL> """<STR_LIT>""" <EOL> _ , __ , fullpath = parse_path ( request . path ) <EOL> status = blob . delete ( self . avatar , fullpath ) <EOL> request . setResponseCode ( status ) <EOL> set_common_headers ( request , False ) <EOL> return '<STR_LIT>' <EOL> def render_HEAD ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return self . render_GET ( request , bodyless = True ) </s>
