コード例 #1
0
ファイル: test_response_json.py プロジェクト: XmingTec/zato
    def test_response_all_elem_types(self):
        class MyService(Service):
            class SimpleIO:
                output = 'aaa', AsIs('bbb'), Bool('ccc'), 'ddd', Date('eee'), DateTime('fff'), Decimal('ggg'), \
                    Float('jjj'), Int('mmm'), Opaque('ooo'), Text('ppp'), UUID('qqq')

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        aaa = 'aaa-111'
        bbb = 'bbb-222-bbb'
        ccc = True
        ddd = ''
        eee = dt_parse('1999-12-31')
        fff = dt_parse('1988-01-29T11:22:33.0000Z')
        ggg = '123.456'

        jjj = '111.222'
        mmm = '9090'

        ooo = 'ZZZ-ZZZ-ZZZ'
        ppp = 'mytext'
        qqq = uuid_UUID('d011d054-db4b-4320-9e24-7f4c217af673')

        # Note that 'ddd' is optional and we are free to skip it
        data = {
            'aaa': aaa,
            'bbb': bbb,
            'ccc': ccc,
            'ddd': ddd,
            'eee': eee,
            'fff': fff,
            'ggg': ggg,
            'jjj': jjj,
            'mmm': mmm,
            'ooo': ooo,
            'ppp': ppp,
            'qqq': qqq
        }

        result = MyService._sio.get_output(data, DATA_FORMAT.JSON)
        json_data = json_loads(result)

        self.assertEquals(json_data['aaa'], aaa)
        self.assertEquals(json_data['bbb'], bbb)
        self.assertEquals(json_data['ccc'], ccc)
        self.assertEquals(json_data['eee'], '1999-12-31')
        self.assertEquals(json_data['fff'], '1988-01-29T11:22:33+00:00')
        self.assertEquals(json_data['ggg'], ggg)
        self.assertEquals(json_data['jjj'], float(jjj))
        self.assertEquals(json_data['mmm'], int(mmm))
        self.assertEquals(json_data['ooo'], ooo)
        self.assertEquals(json_data['ppp'], ppp)
        self.assertEquals(json_data['qqq'], qqq.hex)
コード例 #2
0
ファイル: test_response_xml.py プロジェクト: myhighland/zato
    def test_response_all_elem_types(self):

        class MyService(Service):
            class SimpleIO:
                xml_pretty_print = False
                xml_declaration = False
                output = 'aaa', AsIs('bbb'), Bool('ccc'), 'ddd', Date('eee'), DateTime('fff'), Decimal('ggg'), \
                    Float('jjj'), Int('mmm'), Opaque('ooo'), Text('ppp'), UUID('qqq')

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        aaa = 'aaa-111'
        bbb = 'bbb-222-bbb'
        ccc = True
        ddd = ''
        eee = dt_parse('1999-12-31')
        fff = dt_parse('1988-01-29T11:22:33.0000Z')
        ggg = '123.456'

        jjj = '111.222'
        mmm = '9090'

        ooo = 'ZZZ-ZZZ-ZZZ'
        ppp = 'mytext'
        qqq = uuid_UUID('d011d054-db4b-4320-9e24-7f4c217af673')

        # Note that 'ddd' is optional and we are free to skip it
        data = {
            'aaa': aaa,
            'bbb': bbb,
            'ccc': ccc,
            'ddd': ddd,
            'eee': eee,
            'fff': fff,
            'ggg': ggg,
            'jjj': jjj,
            'mmm': mmm,
            'ooo': ooo,
            'ppp': ppp,
            'qqq': qqq
        }

        result = MyService._sio.get_output(data, DATA_FORMAT.XML)
        self.assertEquals(result, '<response><aaa>aaa-111</aaa><bbb>bbb-222-bbb</bbb><ccc>True</ccc>' \
            '<ddd></ddd><eee>1999-12-31</eee><fff>1988-01-29T11:22:33+00:00</fff>' \
            '<ggg>123.456</ggg><jjj>111.222</jjj><mmm>9090</mmm><ooo>ZZZ-ZZZ-ZZZ</ooo>' \
            '<ppp>mytext</ppp><qqq>d011d054db4b43209e247f4c217af673</qqq></response>')
コード例 #3
0
ファイル: test_response_csv.py プロジェクト: XmingTec/zato
    def test_response_all_elem_types(self):
        class MyService(Service):
            class SimpleIO:
                output = 'aaa', AsIs('bbb'), Bool('ccc'), 'ddd', Date('eee'), DateTime('fff'), Decimal('ggg'), \
                    Float('jjj'), Int('mmm'), Opaque('ooo'), Text('ppp'), UUID('qqq')

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        aaa = 'aaa-111'
        bbb = 'bbb-222-bbb'
        ccc = 'True'
        ddd = ''
        eee = dt_parse('1999-12-31')
        fff = '1988-01-29T11:22:33.0000Z'
        ggg = '123.456'

        jjj = '111.222'
        mmm = '9090'

        ooo = 'ZZZ-ZZZ-ZZZ'
        ppp = 'mytext'
        qqq = 'd011d054-db4b-4320-9e24-7f4c217af673'

        # Note that 'ddd' is optional and we are free to skip it
        data = {
            'aaa': aaa,
            'bbb': bbb,
            'ccc': ccc,
            'ddd': ddd,
            'eee': eee,
            'fff': fff,
            'ggg': ggg,
            'jjj': jjj,
            'mmm': mmm,
            'ooo': ooo,
            'ppp': ppp,
            'qqq': qqq
        }

        result = MyService._sio.get_output(data, DATA_FORMAT.CSV)
        lines = result.splitlines()

        self.assertEquals(lines[0],
                          'aaa,bbb,ccc,ddd,eee,fff,ggg,jjj,mmm,ooo,ppp,qqq')
        self.assertEquals(lines[1], 'aaa-111,bbb-222-bbb,True,,1999-12-31,1988-01-29T11:22:33+00:00,123.456,111.222,9090,' \
            'ZZZ-ZZZ-ZZZ,mytext,d011d054-db4b-4320-9e24-7f4c217af673')
コード例 #4
0
ファイル: publish.py プロジェクト: Mattlk13/zato
    def _get_message(self,
                     topic,
                     input,
                     now,
                     pub_pattern_matched,
                     endpoint_id,
                     subscriptions_by_topic,
                     has_wsx_no_server,
                     _initialized=_initialized,
                     _zato_none=ZATO_NONE,
                     _skip=PUBSUB.HOOK_ACTION.SKIP,
                     _default_pri=PUBSUB.PRIORITY.DEFAULT,
                     _opaque_only=PUBSUB.DEFAULT.SK_OPAQUE,
                     _float_str=PUBSUB.FLOAT_STRING_CONVERT):

        priority = get_priority(self.cid, input)

        # So as not to send it to SQL if it is a default value anyway = less overhead = better performance
        if priority == _default_pri:
            priority = None

        expiration = get_expiration(self.cid, input)
        expiration_time = now + (expiration / 1000.0)

        pub_msg_id = input.get('msg_id', '').encode('utf8') or new_msg_id()

        # If there is at least one WSX subscriber to this topic which is not connected at the moment,
        # which means it has no delivery server, we uncoditionally turn this message into a GD one ..
        if has_wsx_no_server:
            has_gd = True
            logger_pubsub.info(_log_turning_gd_msg.format('wsx'), pub_msg_id)

        # .. otherwise, use input GD value or the default per topic.
        else:
            has_gd = input.get('has_gd', _zato_none)
            if has_gd != _zato_none:
                if not isinstance(has_gd, bool):
                    raise ValueError(
                        'Input has_gd is not a bool (found:`{}`)'.format(
                            repr(has_gd)))
            else:
                has_gd = topic.has_gd

        pub_correl_id = input.get('correl_id')
        in_reply_to = input.get('in_reply_to')
        ext_client_id = input.get('ext_client_id')
        mime_type = input.get('mime_type')

        ext_pub_time = input.get('ext_pub_time') or None
        if ext_pub_time:
            ext_pub_time = dt_parse(ext_pub_time)
            ext_pub_time = datetime_to_ms(ext_pub_time) / 1000.0

        pub_correl_id = pub_correl_id.encode('utf8') if pub_correl_id else None
        in_reply_to = in_reply_to.encode('utf8') if in_reply_to else None
        ext_client_id = ext_client_id.encode('utf8') if ext_client_id else None
        mime_type = mime_type.encode('utf8') if mime_type else None
        reply_to_sk = input.get('reply_to_sk') or []
        deliver_to_sk = input.get('deliver_to_sk') or []

        user_ctx = input.get('user_ctx')
        zato_ctx = input.get('zato_ctx')

        ps_msg = PubSubMessage()
        ps_msg.topic = topic
        ps_msg.pub_msg_id = pub_msg_id
        ps_msg.pub_correl_id = pub_correl_id
        ps_msg.in_reply_to = in_reply_to

        # Convert to string to prevent pg8000 from rounding up float values
        ps_msg.pub_time = _float_str.format(now)
        ps_msg.ext_pub_time = _float_str.format(
            ext_pub_time) if ext_pub_time else ext_pub_time

        ps_msg.delivery_status = _initialized
        ps_msg.pub_pattern_matched = pub_pattern_matched
        ps_msg.data = input['data']
        ps_msg.mime_type = mime_type
        ps_msg.priority = priority
        ps_msg.expiration = expiration
        ps_msg.expiration_time = expiration_time
        ps_msg.published_by_id = endpoint_id
        ps_msg.topic_id = topic.id
        ps_msg.topic_name = topic.name
        ps_msg.cluster_id = self.server.cluster_id
        ps_msg.has_gd = has_gd
        ps_msg.ext_client_id = ext_client_id
        ps_msg.group_id = input.get('group_id') or None
        ps_msg.position_in_group = input.get('position_in_group') or None
        ps_msg.is_in_sub_queue = bool(subscriptions_by_topic)
        ps_msg.reply_to_sk = reply_to_sk
        ps_msg.deliver_to_sk = deliver_to_sk
        ps_msg.user_ctx = user_ctx
        ps_msg.zato_ctx = zato_ctx

        # Opaque attributes - we only need reply to sub_keys to be placed in there
        # but we do not do it unless we known that any such sub key was actually requested.
        if reply_to_sk or deliver_to_sk:
            set_instance_opaque_attrs(ps_msg, input, only=_opaque_only)

        # If there are any subscriptions for the topic this message was published to, we want to establish
        # based on what subscription pattern each subscriber will receive the message.
        for sub in subscriptions_by_topic:
            ps_msg.sub_pattern_matched[sub.sub_key] = sub.sub_pattern_matched

        if ps_msg.data:
            ps_msg.size = len(
                ps_msg.data.encode('utf8')
            )  # We need to store the size in bytes rather than Unicode codepoints
        else:
            ps_msg.size = 0

        # Invoke hook service here because it may want to update data in which case
        # we need to take it into account below.
        if topic.before_publish_hook_service_invoker:
            response = topic.before_publish_hook_service_invoker(topic, ps_msg)

            # Hook service decided that we should not process this message
            if response['hook_action'] == _skip:
                logger_audit.info(
                    'Skipping message pub_msg_id:`%s`, pub_correl_id:`%s`, ext_client_id:`%s`',
                    ps_msg.pub_msg_id, ps_msg.pub_correl_id,
                    ps_msg.ext_client_id)
                return

        # These are needed only for GD messages that are stored in SQL
        if has_gd:
            data_prefix, data_prefix_short = self._get_data_prefixes(
                ps_msg.data)
            ps_msg.data_prefix = data_prefix
            ps_msg.data_prefix_short = data_prefix_short

        return ps_msg
コード例 #5
0
ファイル: publish.py プロジェクト: xulong2005/zato
    def _get_message(self,
                     topic,
                     input,
                     now,
                     pattern_matched,
                     endpoint_id,
                     _initialized=_initialized,
                     _zato_none=ZATO_NONE):

        priority = get_priority(self.cid, input)
        expiration = get_expiration(self.cid, input)
        expiration_time = now + (expiration * 1000)

        pub_msg_id = input.get('msg_id', '').encode('utf8') or new_msg_id()

        has_gd = input.get('has_gd', _zato_none)
        if has_gd != _zato_none:
            if not isinstance(has_gd, bool):
                raise ValueError(
                    'Input has_gd is not a bool (found:`{}`)'.format(
                        repr(has_gd)))
        else:
            has_gd = topic.has_gd

        pub_correl_id = input.get('correl_id')
        in_reply_to = input.get('in_reply_to')
        ext_client_id = input.get('ext_client_id')

        ext_pub_time = input.get('ext_pub_time')
        if ext_pub_time:
            ext_pub_time = dt_parse(ext_pub_time)
            ext_pub_time = datetime_to_ms(ext_pub_time)

        pub_correl_id = pub_correl_id.encode('utf8') if pub_correl_id else None
        in_reply_to = in_reply_to.encode('utf8') if in_reply_to else None
        ext_client_id = ext_client_id.encode('utf8') if ext_client_id else None

        ps_msg = PubSubMessage()
        ps_msg.topic = topic
        ps_msg.pub_msg_id = pub_msg_id
        ps_msg.pub_correl_id = pub_correl_id
        ps_msg.in_reply_to = in_reply_to
        ps_msg.pub_time = now
        ps_msg.delivery_status = _initialized
        ps_msg.pattern_matched = pattern_matched
        ps_msg.data = input['data'].encode('utf8')
        ps_msg.mime_type = input.get('mime_type', 'text/plain')
        ps_msg.size = len(input['data'])
        ps_msg.priority = priority
        ps_msg.expiration = expiration
        ps_msg.expiration_time = expiration_time
        ps_msg.published_by_id = endpoint_id
        ps_msg.topic_id = topic.id
        ps_msg.cluster_id = self.server.cluster_id
        ps_msg.has_gd = has_gd
        ps_msg.ext_client_id = ext_client_id
        ps_msg.ext_pub_time = ext_pub_time
        ps_msg.group_id = input.get('group_id') or None
        ps_msg.position_in_group = input.get('position_in_group') or None

        # Invoke hook service here because it may want to update data in which case
        # we need to take it into account below.
        if topic.before_publish_hook_service_invoker:
            response = topic.before_publish_hook_service_invoker(topic, ps_msg)

            # Hook service decided that we should not process this message
            if response['skip_msg']:
                logger_audit.info(
                    'Skipping message pub_msg_id:`%s`, pub_correl_id:`%s`, ext_client_id:`%s`',
                    ps_msg.pub_msg_id, ps_msg.pub_correl_id,
                    ps_msg.ext_client_id)
                return
            else:
                ps_msg.size = len(ps_msg.data)

        # These are needed only for GD messages that are stored in SQL
        if has_gd:
            ps_msg.data_prefix = ps_msg.data[:2048].encode('utf8')
            ps_msg.data_prefix_short = ps_msg.data[:64].encode('utf8')

        return ps_msg
コード例 #6
0
    def _get_message(self,
                     topic,
                     input,
                     now,
                     pattern_matched,
                     endpoint_id,
                     has_subs,
                     _initialized=_initialized,
                     _zato_none=ZATO_NONE,
                     _skip=PUBSUB.HOOK_ACTION.SKIP,
                     _default_pri=PUBSUB.PRIORITY.DEFAULT):

        priority = get_priority(self.cid, input)

        # So as not to send it to SQL if it is a default value anyway = less overhead = better performance
        if priority == _default_pri:
            priority = None

        expiration = get_expiration(self.cid, input)
        expiration_time = now + (expiration / 1000.0)

        pub_msg_id = input.get('msg_id', '').encode('utf8') or new_msg_id()

        has_gd = input.get('has_gd', _zato_none)
        if has_gd != _zato_none:
            if not isinstance(has_gd, bool):
                raise ValueError(
                    'Input has_gd is not a bool (found:`{}`)'.format(
                        repr(has_gd)))
        else:
            has_gd = topic.has_gd

        pub_correl_id = input.get('correl_id')
        in_reply_to = input.get('in_reply_to')
        ext_client_id = input.get('ext_client_id')
        mime_type = input.get('mime_type')

        ext_pub_time = input.get('ext_pub_time') or None
        if ext_pub_time:
            ext_pub_time = dt_parse(ext_pub_time)
            ext_pub_time = datetime_to_ms(ext_pub_time)

        pub_correl_id = pub_correl_id.encode('utf8') if pub_correl_id else None
        in_reply_to = in_reply_to.encode('utf8') if in_reply_to else None
        ext_client_id = ext_client_id.encode('utf8') if ext_client_id else None
        mime_type = mime_type.encode('utf8') if mime_type else None

        ps_msg = PubSubMessage()
        ps_msg.topic = topic
        ps_msg.pub_msg_id = pub_msg_id
        ps_msg.pub_correl_id = pub_correl_id
        ps_msg.in_reply_to = in_reply_to
        ps_msg.pub_time = now
        ps_msg.delivery_status = _initialized
        ps_msg.pattern_matched = pattern_matched
        ps_msg.data = input['data']
        ps_msg.mime_type = mime_type
        ps_msg.priority = priority
        ps_msg.expiration = expiration
        ps_msg.expiration_time = expiration_time
        ps_msg.published_by_id = endpoint_id
        ps_msg.topic_id = topic.id
        ps_msg.topic_name = topic.name
        ps_msg.cluster_id = self.server.cluster_id
        ps_msg.has_gd = has_gd
        ps_msg.ext_client_id = ext_client_id
        ps_msg.ext_pub_time = ext_pub_time
        ps_msg.group_id = input.get('group_id') or None
        ps_msg.position_in_group = input.get('position_in_group') or None
        ps_msg.is_in_sub_queue = has_subs

        if ps_msg.data:
            ps_msg.size = len(
                ps_msg.data.encode('utf8')
            )  # We need to store the size in bytes rather than Unicode codepoints
        else:
            ps_msg.size = 0

        # Invoke hook service here because it may want to update data in which case
        # we need to take it into account below.
        if topic.before_publish_hook_service_invoker:
            response = topic.before_publish_hook_service_invoker(topic, ps_msg)

            # Hook service decided that we should not process this message
            if response['hook_action'] == _skip:
                logger_audit.info(
                    'Skipping message pub_msg_id:`%s`, pub_correl_id:`%s`, ext_client_id:`%s`',
                    ps_msg.pub_msg_id, ps_msg.pub_correl_id,
                    ps_msg.ext_client_id)
                return

        # These are needed only for GD messages that are stored in SQL
        if has_gd:
            data_prefix, data_prefix_short = self._get_data_prefixes(
                ps_msg.data)
            ps_msg.data_prefix = data_prefix
            ps_msg.data_prefix_short = data_prefix_short

        return ps_msg