Exemplo n.º 1
0
 def render_DELETE(self, request):
     """DELETE operations corresponds to the blob deletion operation"""
     _, __, fullpath = parse_path(request.path)
     status = blob.delete(self.avatar, fullpath)
     request.setResponseCode(status)
     set_common_headers(request, False)
     return ''
Exemplo n.º 2
0
    def render_GET(self, request, bodyless=False):
        """GET returns contents of a blob"""
        # process path and extract potential containers/fnm
        _, __, fullpath = parse_path(request.path)
        log.msg("Getting blob (non-cdmi) %s" % fullpath)
        tre_header = request.getHeader('tre-enabled')
        tre_request = tre_header is not None and tre_header.lower() == 'true'
        # perform operation on ADT
        status, vals = blob.read(self.avatar, fullpath, tre_request)
        # construct response
        request.setResponseCode(status)
        set_common_headers(request, False)
        if tre_request and status == FOUND:
            request.setHeader('Location', "/".join([c('general', 'tre_server'),
                                                    str(vals['uid'])]))

        if status is OK:
            # XXX: hack - some-why the response just hangs if to simply path
            # mimetype as a content_object type
            mimetype = vals['mimetype']
            actual_type = 'text/plain' if mimetype == 'text/plain' else str(mimetype)
            request.setHeader('Content-Type', actual_type)
            request.setLastModified(float(vals['mtime']))
            if not bodyless:
                request.setHeader('Content-Length', str(vals['size']))
                producer = self.makeProducer(request, vals['content'])
                producer.start()
                return NOT_DONE_YET
            else:
                return ''
        return ''
Exemplo n.º 3
0
    def render_PUT(self, request):
        name, container_path, fullpath = parse_path(request.path)
        log.msg("Creating container %s" % fullpath)
        req_length = request.getHeader("Content-Length")
        if req_length is None:
            request.setResponseCode(411)
            return ""

        req_length = int(req_length)
        request.content.seek(0, 0)
        # process json encoded request body
        body = request.content.read(req_length) if req_length > 0 else "{}"  # a workaround for a buggy curl behavior
        body = json.loads(body)
        metadata = {}
        if "metadata" in body:
            metadata = body["metadata"]
        status, vals = container.create_or_update(self.avatar, name, container_path, fullpath, metadata)
        children = vals["children"].values() if "children" in vals else {}
        request.setResponseCode(status)
        request.setHeader("Content-Type", CDMI_CONTAINER)
        set_common_headers(request)

        # and a body
        response_body = {
            "completionStatus": "Complete",
            "metadata": metadata,
            "children": children,
            "childrenrange": "" if len(children) == 0 else "0-%s" % len(children),
        }
        response_body.update(get_common_body(request, vals["uid"], fullpath))

        return json.dumps(response_body)
Exemplo n.º 4
0
    def render_GET(self, request):
        """GET operation corresponds to reading container's data"""
        # parse the request
        _, __, fullpath = parse_path(request.path)

        # contact the backend
        status, vals = container.read(self.avatar, fullpath)

        # create a header
        request.setResponseCode(status)
        request.setHeader("Content-Type", CDMI_CONTAINER)
        set_common_headers(request)

        # and a body
        if status == OK:
            request.setLastModified(float(vals["mtime"]))
            children = vals["children"].values()
            response_body = {
                "completionStatus": "Complete",
                "metadata": vals["metadata"],
                "children": children,
                "childrenrange": "" if len(children) == 0 else "0-%s" % len(children),
                "capabilitiesURI": "/cdmi_capabilities/container",
            }
            response_body.update(get_common_body(request, vals["uid"], fullpath))
            return json.dumps(response_body)
        else:
            return ""
Exemplo n.º 5
0
    def render_GET(self, request):
        """GET operation corresponds to reading a container's data."""
        # parse the request
        _, __, fullpath = parse_path(request.path)

        # contact the backend
        status, vals = container.read(self.avatar, fullpath)
        # create a header
        request.setResponseCode(status)
        set_common_headers(request, False)
        if status == OK:
            request.setHeader("Content-Type", "application/json")
            request.setLastModified(float(vals["mtime"]))
            children = None if not "children" in vals else vals["children"].values()
            response_body = {"metadata": vals["metadata"], "children": children}
            return json.dumps(response_body)
        else:
            return ""
Exemplo n.º 6
0
    def render_GET(self, request, bodyless=False):
        """GET operation corresponds to reading of the blob object"""
        # process path and extract potential containers/fnm
        _, __, fullpath = parse_path(request.path)

        tre_header = request.getHeader('tre-enabled')
        tre_request = tre_header is not None and tre_header.lower() == 'true'
        log.msg("Request for TRE-enabled download received.")
        # perform operation on ADT
        status, vals = blob.read(self.avatar, fullpath, tre_request)
        # construct response
        request.setResponseCode(status)

        request.setHeader('Content-Type', CDMI_OBJECT)
        if tre_request and status == FOUND:
            request.setHeader('Location', "/".join([c('general', 'tre_server'),
                                                    str(vals['uid'])]))
            request.setLastModified(float(vals['mtime']))

        set_common_headers(request)
        if status == OK:
            request.setLastModified(float(vals['mtime']))
            if not bodyless:
                valueencoding = vals['valuetransferencoding']
                # for content we want to read in the full object into memory
                content = (vals['content'].read() if valueencoding == 'utf-8' else
                          base64.b64encode(vals['content'].read()))

                # construct body
                response_body = {
                                 'completionStatus': 'Complete',
                                 'mimetype': vals['mimetype'],
                                 'metadata': vals['metadata'],
                                 'valuetransferencoding': valueencoding,
                                 'value': content,
                                 'actual_uri': vals.get('actual_uri'),
                                 'capabilitiesURI': '/cdmi_capabilities/dataobject'
                                 }
                response_body.update(get_common_body(request, str(vals['uid']),
                                                     fullpath))
                return json.dumps(response_body)
        return ''
Exemplo n.º 7
0
 def render_PUT(self, request):
     """PUT corresponds to a create/update operation on a blob"""
     # process path and extract potential containers/fnm
     name, container_path, fullpath = parse_path(request.path)
     length = request.getHeader('Content-Length')
     if length is None:
         request.setResponseCode(411)
         return ''
     content = (request.content, int(length))
     # default values of mimetype and metadata
     mimetype = request.getHeader('Content-Type') if request.getHeader('Content-Type') is not None else 'text/plain'
     desired_backend = request.getHeader('desired_backend')
     if ((len(mimetype) == 2 and mimetype[1] == 'utf-8')):
         valueencoding = 'utf-8'
     else:
         valueencoding = 'base64'
     status, _ = blob.write(self.avatar, name, container_path, fullpath,
                            mimetype, {}, content, valueencoding,
                            desired_backend)
     request.setResponseCode(status)
     set_common_headers(request, False)
     return ''
Exemplo n.º 8
0
    def render_PUT(self, request):
        """PUT corresponds to a create/update operation on a blob"""
        # process path and extract potential containers/fnm
        name, container_path, fullpath = parse_path(request.path)

        length = int(request.getHeader('Content-Length'))
        request.content.seek(0, 0)
        # process json encoded request body
        body = json.loads(request.content.read(length))
        # default values of mimetype and metadata
        mimetype = body.get('mimetype', 'text/plain')
        metadata = body.get('metadata', {})
        desired_backend = (metadata.get('desired_backend') or
                           request.getHeader('desired_backend'))
        valueencoding = body.get('valuetransferencoding', 'utf-8')
        body_value = body.get('value', '')
        value = (body_value if valueencoding == 'utf-8' else
                                  base64.b64decode(body_value))
        content = (StringIO(value), len(value))
        status, uid = blob.write(self.avatar, name, container_path, fullpath,
                                 mimetype, metadata, content, valueencoding,
                                 desired_backend=desired_backend)
        request.setResponseCode(status)
        request.setHeader('Content-Type', CDMI_OBJECT)
        set_common_headers(request)
        if status == OK or status == CREATED:
            response_body = {
                             'completionStatus': 'Complete',
                             'mimetype': mimetype,
                             'metadata': metadata,
                             }
            # add common elements
            response_body.update(get_common_body(request, uid, fullpath))
            return json.dumps(response_body)
        else:
            # error state
            return ''
Exemplo n.º 9
0
 def render_DELETE(self, request):
     _, __, fullpath = parse_path(request.path)
     status = container.delete(self.avatar, fullpath)
     request.setResponseCode(status)
     set_common_headers(request)
     return ""
Exemplo n.º 10
0
 def render_PUT(self, request):
     name, container_path, fullpath = parse_path(request.path)
     status, _ = container.create_or_update(self.avatar, name, container_path, fullpath, {})
     request.setResponseCode(status)
     set_common_headers(request, False)
     return ""