Ejemplo n.º 1
0
    def map_onem2m_response_to_http_response(self, request, response):
        """Maps a OneM2M response to a HTTP response.

        :param request: the HTTP request
        :param response: the OneM2MResponse object to be mapped
        :returns: Response -- the resulting HTTP Response
        :raises: OpenMTCError
        """
        self.logger.debug("Mapping OneM2M response: %s", response)

        status_code = response.status_code

        # resourceID prefix
        if response.to.startswith('//'):
            resource_id_pre = '/'.join(response.to.split('/')[:4]) + '/'
        elif response.to.startswith('/'):
            resource_id_pre = '/'.join(response.to.split('/')[:2]) + '/'
        else:
            resource_id_pre = ''

        headers = {
            "x-m2m-ri": str(response.rqi),
            "x-m2m-rsc": str(response.rsc)
        }

        try:
            headers['content_location'] = (resource_id_pre +
                                           response.content.resourceID)
        except (AttributeError, TypeError):
            pass

        pretty = self.pretty
        if pretty is None:
            user_agent = request.user_agent
            pretty = user_agent and ("opera" in user_agent
                                     or "mozilla" in user_agent)
        supported = get_onem2m_supported_content_types()
        if request.accept_mimetypes:
            accept = request.accept_mimetypes.best_match(supported)
            if accept is None:
                # TODO(rst): raise 406 or similar
                accept = self.default_content_type
        else:
            accept = self.default_content_type
        try:
            content_type, payload = encode_onem2m_content(response.content,
                                                          accept,
                                                          pretty,
                                                          path=resource_id_pre)
        except CSEContentsUnacceptable as e:
            status_code = e.status_code
            content_type = "text/plain"
            payload = str(e)

        response = Response(payload,
                            status=status_code,
                            headers=headers,
                            content_type=content_type)
        return response
Ejemplo n.º 2
0
    def map_onem2m_request_to_http_request(self, onem2m_request):
        """
        Maps a OneM2M request to a HTTP request
        :param onem2m_request: OneM2M request to be mapped
        :return: request: the resulting HTTP request
        """
        self.logger.debug("Mapping OneM2M request to generic request: %s",
                          onem2m_request)

        params = {
            param: getattr(onem2m_request, param)
            for param in _query_params
            if getattr(onem2m_request, param) is not None
        }

        if onem2m_request.fc is not None:
            filter_criteria = onem2m_request.fc
            params.update({
                (get_short_attribute_name(name)
                 or get_short_member_name(name)): val
                for name, val in filter_criteria.get_values(True).iteritems()
            })

        if onem2m_request.ae_notifying:
            path = ''
        else:
            path = normalize_path(onem2m_request.to)

        if params:
            path += '?' + urllib.urlencode(params, True)

        content_type, data = encode_onem2m_content(onem2m_request.content,
                                                   self.content_type,
                                                   path=path)

        # TODO(rst): check again
        # set resource type
        if onem2m_request.operation == OneM2MOperation.create:
            content_type += '; ty=' + str(
                ResourceTypeE[onem2m_request.resource_type.typename])

        headers = {
            header: getattr(onem2m_request, field)
            for header, field in _header_to_field_map.iteritems()
            if getattr(onem2m_request, field) is not None
        }
        headers['content-type'] = content_type

        self.logger.debug("Added request params: %s", params)

        return {
            'method': _method_map_to_http[onem2m_request.operation],
            'request_uri': self.path + path,
            'body': data,
            'headers': headers,
        }