Ejemplo n.º 1
0
    def map_http_response_to_onem2m_response(self, onem2m_request, response):
        """
        Maps HTTP response to OneM2M response
        :param onem2m_request: the OneM2M request that created the response
        :param response: the HTTP response
        :return: resulting OneM2MResponse or OneM2MErrorResponse
        """
        if not isinstance(response, HTTPResponse):
            self.logger.error("Not a valid response: %s", response)
            # return OneM2MErrorResponse(STATUS_INTERNAL_SERVER_ERROR)
        self.logger.debug("Mapping HTTP response for OneM2M response: %s", response)
        rsc = response.get("x-m2m-rsc", 5000)
        if int(rsc) >= ERROR_MIN:
            return OneM2MErrorResponse(
                get_error_class(rsc).response_status_code, onem2m_request)

        return OneM2MResponse(
            get_response_status(rsc),
            request=onem2m_request,
            rsc=rsc,
            pc=decode_onem2m_content(response.read(), response.get("content-type"))
        )
Ejemplo n.º 2
0
    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