def content_instance_builder(self, res): contentInstance = ContentInstance( resourceName=res.resourceName, resourceType=res.resourceType, resourceID=res.resourceID, parentID=res.parentID, lastModifiedTime=res.lastModifiedTime, creationTime=res.creationTime, content=res.content, contentSize=res.contentSize) #self.print_resource(res) return contentInstance
def push_content(self, container, content, fmt=None, text=None): """ Creates a ContentInstance resource in the given container, wrapping the content. Defaults to serialising the content as JSON and base64 encodes it. NOTE: Will attempt to create the container, if not found. :param container: Container object or container path string :param content: the content data :param fmt: :param text: """ path = getattr(container, "path", container) if isinstance(content, (str, unicode)): fmt = 'text/plain' if fmt is None else fmt text = True if text is None else text elif isinstance(content, (dict, list)): fmt = 'application/json' if fmt is None else fmt text = False if text is None else text else: raise CSENotImplemented("Only dict, list and str are supported!") if fmt == 'application/json' or re.search(self.fmt_json_regex, fmt): if text: # TODO(rst): check if it should be with masked quotation marks # con = json_dumps(content) # cnf = fmt + ':' + str(EncodingTypeE.plain.value) raise CSENotImplemented("Only json as b64 is supported!") else: con = b64encode(json_dumps(content)) cnf = fmt + ':' + str(EncodingTypeE.base64String.value) elif fmt == 'text/plain': if text: con = content cnf = fmt + ':' + str(EncodingTypeE.plain.value) else: con = b64encode(content) cnf = fmt + ':' + str(EncodingTypeE.base64String.value) else: # TODO(rst): add handling of other formats or raise not implemented raise CSENotImplemented("Only json and text are supported!") cin = ContentInstance(content=con, contentInfo=cnf) try: return self.mapper.create(path, cin) except CSENotFound: raise CSENotFound()
def decode_request(self, resource, attr_to_write, value_to_write): if isinstance(resource, CSEBase): cse = CSEBase() setattr(cse, attr_to_write, value_to_write) return cse elif isinstance(resource, AE): ae = AE() setattr(ae, attr_to_write, value_to_write) return ae elif isinstance(resource, Container): cnt = Container() setattr(cnt, attr_to_write, value_to_write) return cnt elif isinstance(resource, ContentInstance): cin = ContentInstance() setattr(cin, attr_to_write, value_to_write) return cin return resource