def create_onem2m_serializer(content_type): try: factory = _factories[content_type] except KeyError: raise CSEBadRequest("Unsupported content type: %s. Try one of %s" % (content_type, ', '.join(_factories.keys()))) return factory()
def _prepare_discovery(self): self.limit = None self.truncated = False try: self.drt = DiscResTypeE(int(self.request.drt)) except TypeError: self.drt = DiscResTypeE.structured except ValueError: raise CSEBadRequest() if hasattr(self.request.filter_criteria, 'limit'): self.limit = self.request.filter_criteria.limit self.logger.debug("_prepare_resource -> _handle_result: %s" % self.resource) self.discovered = [] self.result = URIList(self.discovered) self._discovery()
def parse_filter_criteria(filter_criteria): if filter_criteria is None: filter_criteria = {} _logger.debug("parsing '%s'", filter_criteria) int_criteria = ('stateTagSmaller', 'stateTagBigger', 'resourceType', 'sizeAbove', 'sizeBelow', 'filterUsage', 'limit') parsed_criteria = {} for k, v in filter_criteria.iteritems(): if k in int_criteria: if isinstance(v, list): parsed_criteria[k] = map(int, v) else: parsed_criteria[k] = int(v) else: parsed_criteria[k] = v try: return FilterCriteria(**parsed_criteria) except ModelTypeError: raise CSEBadRequest()
def decode_resource_values(self, s): def convert_to_long_keys(d): return {get_long_resource_name(k) or get_long_attribute_name(k) or get_long_member_name(k) or k: v for k, v in d.iteritems()} try: if hasattr(s, "read"): data = self.load(s, object_hook=convert_to_long_keys) else: data = self.loads(s, object_hook=convert_to_long_keys) except (ValueError, TypeError) as exc: raise CSEBadRequest("Failed to parse input: %s" % (exc, )) self.logger.debug("Read data: %s", data) try: typename, data = data.items()[0] return get_onem2m_type(get_typename(typename)), data except (AttributeError, IndexError, TypeError): raise CSESyntaxError("Not a valid resource representation")
def map_http_request_to_onem2m_request(self, http_request): """Maps a HTTP request to a OneM2M request. :param http_request: the HTTP Request object to be mapped :returns: OneM2MRequest -- the resulting OneM2M request object :raises: OpenMTCError """ self.logger.debug("Mapping HTTP request '%s' to OneM2M request", http_request) op = _method_map_from_http[http_request.method] to = http_request.path[1:].lstrip('/') if to.startswith('~/'): to = to[1:] elif to.startswith('_/'): to = '/' + to[1:] get_header = http_request.headers.get # The X-M2M-Origin header shall be mapped to the From parameter of # request and response primitives and vice versa, if applicable. fr = get_header("x-m2m-origin") # The X-M2M-RI header shall be mapped to the Request Identifier # parameter of request and response primitives and vice versa. rqi = get_header("x-m2m-ri") # primitive content pc = decode_onem2m_content(http_request.input_stream.read(), http_request.content_type) # resource type # get out of content-type or from resource ty = type(pc) if pc else None if ty is Notification: op = OneM2MOperation.notify # The X-M2M-GID header shall be mapped to the Group Request Identifier # parameter of request primitives and vice versa, if applicable. gid = get_header("x-m2m-gid") # The X-M2M-RTU header shall be mapped to the notificationURI element of # the Response Type parameter of request primitives and vice versa, if # applicable. If there are more than one value in the element, then the # values shall be combined with "&" character. rt = get_header("x-m2m-rtu") # The X-M2M-OT header shall be mapped to the Originating Timestamp # parameter of request and response primitives, and vice versa, if # applicable. ot = get_header("x-m2m-ot") # The X-M2M-RST header shall be mapped to the Result Expiration # Timestamp parameter of request and response primitives, and vice # versa, if applicable. rset = get_header("x-m2m-rst") # The X-M2M-RET header shall be mapped to the Request Expiration # Timestamp parameter of request primitives and vice versa, if # applicable. rqet = get_header("x-m2m-ret") # The X-M2M-OET header shall be mapped to the Operation Execution Time # parameter of request primitives and vice versa, if applicable oet = get_header("x-m2m-oet") # The X-M2M-EC header shall be mapped to the Event Category parameter of # request and response primitives, and vice versa, if applicable. ec = get_header("x-m2m-ec") onem2m_request = OneM2MRequest(op=op, to=to, fr=fr, rqi=rqi, ty=ty, pc=pc, ot=ot, rqet=rqet, rset=rset, oet=oet, rt=rt, ec=ec, gid=gid) not_filter_params = ('rt', 'rp', 'rcn', 'da', 'drt') multiple_params = ('lbl', 'ty', 'cty', 'atr') if http_request.query_string: from openmtc_cse.methoddomain.filtercriteria import filters params = urlparse.parse_qs(http_request.query_string) get_param = params.get f_c = {} for param in params: self.logger.debug("checking '%s'", param) values = get_param(param) if param not in multiple_params and len(values) > 1: raise CSEBadRequest("Multiple field names not permitted " "for parameter %s" % param) param_long_name = get_long_member_name(param) # TODO(rst): handle attributes with get_long_attribute_name if param in not_filter_params: setattr(onem2m_request, param, values[0]) elif param_long_name == 'attributeList': onem2m_request.pc = AttributeList( map(get_long_attribute_name, values[0].split(' '))) elif param_long_name and hasattr(filters, param_long_name): self.logger.debug("got values for '%s' ('%s'): %s", param_long_name, param, values) if param in multiple_params: f_c[param_long_name] = values else: f_c[param_long_name] = values[0] else: raise CSEBadRequest("Unknown parameter: %s" % param) onem2m_request.filter_criteria = parse_filter_criteria(f_c) return onem2m_request