示例#1
0
 def post(self, queue_name, messages, client_uuid, project=None):
     """Send messages to the subscribers."""
     if self.subscription_controller:
         if not isinstance(self.subscription_controller,
                           pooling.SubscriptionController):
             marker = None
             while True:
                 subscribers = self.subscription_controller.list(
                     queue_name, project, marker=marker)
                 for sub in next(subscribers):
                     LOG.debug("Notifying subscriber %r" % (sub, ))
                     s_type = urllib_parse.urlparse(
                         sub['subscriber']).scheme
                     # If the subscriber doesn't contain 'confirmed', it
                     # means that this kind of subscriber was created before
                     # the confirm feature be introduced into Zaqar. We
                     # should allow them be subscribed.
                     if (self.require_confirmation
                             and not sub.get('confirmed', True)):
                         LOG.info(
                             _LI('The subscriber %s is not '
                                 'confirmed.'), sub['subscriber'])
                         continue
                     for msg in messages:
                         msg['Message_Type'] = MessageType.Notification.name
                     self._execute(s_type, sub, messages)
                 marker = next(subscribers)
                 if not marker:
                     break
     else:
         LOG.error(_LE('Failed to get subscription controller.'))
示例#2
0
 def post(self, queue_name, messages, client_uuid, project=None):
     """Send messages to the subscribers."""
     if self.subscription_controller:
         if not isinstance(self.subscription_controller,
                           pooling.SubscriptionController):
             marker = None
             while True:
                 subscribers = self.subscription_controller.list(
                     queue_name, project, marker=marker)
                 for sub in next(subscribers):
                     LOG.debug("Notifying subscriber %r" % (sub,))
                     s_type = urllib_parse.urlparse(
                         sub['subscriber']).scheme
                     # If the subscriber doesn't contain 'confirmed', it
                     # means that this kind of subscriber was created before
                     # the confirm feature be introduced into Zaqar. We
                     # should allow them be subscribed.
                     if (self.require_confirmation and
                             not sub.get('confirmed', True)):
                         LOG.info(_LI('The subscriber %s is not '
                                      'confirmed.'), sub['subscriber'])
                         continue
                     for msg in messages:
                         msg['Message_Type'] = MessageType.Notification.name
                     self._execute(s_type, sub, messages)
                 marker = next(subscribers)
                 if not marker:
                     break
     else:
         LOG.error(_LE('Failed to get subscription controller.'))
示例#3
0
 def _send_response(self, resp, in_binary):
     if in_binary:
         pack_name = 'bin'
         self.sendMessage(msgpack.packb(resp.get_response()), True)
     else:
         pack_name = 'txt'
         self.sendMessage(json.dumps(resp.get_response()), False)
     if LOG.isEnabledFor(logging.INFO):
         api = resp._request._api
         status = resp._headers['status']
         action = resp._request._action
         # Dump to JSON to print body without unicode prefixes on Python 2
         body = json.dumps(resp._request._body)
         var_dict = {'api': api, 'pack_name': pack_name, 'status':
                     status, 'action': action, 'body': body}
         LOG.info(_LI('Response: API %(api)s %(pack_name)s, %(status)s. '
                      'Request: action "%(action)s", body %(body)s.'),
                  var_dict)
示例#4
0
 def _send_response(self, resp, in_binary):
     if in_binary:
         pack_name = 'bin'
         self.sendMessage(msgpack.packb(resp.get_response()), True)
     else:
         pack_name = 'txt'
         self.sendMessage(json.dumps(resp.get_response()), False)
     if LOG.isEnabledFor(logging.INFO):
         api = resp._request._api
         status = resp._headers['status']
         action = resp._request._action
         # Dump to JSON to print body without unicode prefixes on Python 2
         body = json.dumps(resp._request._body)
         var_dict = {'api': api, 'pack_name': pack_name, 'status':
                     status, 'action': action, 'body': body}
         LOG.info(_LI('Response: API %(api)s %(pack_name)s, %(status)s. '
                      'Request: action "%(action)s", body %(body)s.'),
                  var_dict)
示例#5
0
    def send_confirm_notification(self, queue, subscription, conf,
                                  project=None, expires=None,
                                  api_version=None):
        key = conf.signed_url.secret_key
        if not key:
            LOG.error(_LE("Can't send confirm notification due to the value of"
                          " secret_key option is None"))
            return
        url = "/%s/queues/%s/subscriptions/%s/confirm" % (api_version, queue,
                                                          subscription['id'])
        pre_url = urls.create_signed_url(key, [url], project=project,
                                         expires=expires, methods=['PUT'])
        message_type = MessageType.SubscriptionConfirmation.name

        messages = {}
        endpoint_dict = auth.get_public_endpoint()
        if endpoint_dict:
            wsgi_endpoint = endpoint_dict.get('zaqar', None)
            if wsgi_endpoint:
                wsgi_subscribe_url = urllib_parse.urljoin(
                    wsgi_endpoint, url)
                messages['WSGISubscribeURL'] = wsgi_subscribe_url
            websocket_endpoint = endpoint_dict.get('zaqar-websocket', None)
            if websocket_endpoint:
                websocket_subscribe_url = urllib_parse.urljoin(
                    websocket_endpoint, url)
                messages['WebSocketSubscribeURL'] = websocket_subscribe_url
        messages.update({'Message_Type': message_type,
                         'Message': 'You have chosen to subscribe to the '
                                    'queue: %s' % queue,
                         'URL-Signature': pre_url['signature'],
                         'URL-Methods': pre_url['methods'][0],
                         'URL-Paths': pre_url['paths'][0],
                         'X-Project-ID': pre_url['project'],
                         'URL-Expires': pre_url['expires'],
                         'SubscribeBody': {'confirmed': True},
                         'UnsubscribeBody': {'confirmed': False}})
        s_type = urllib_parse.urlparse(subscription['subscriber']).scheme
        LOG.info(_LI('Begin to send %(type)s confirm notification. The request'
                     'body is %(messages)s'),
                 {'type': s_type, 'messages': messages})

        self._execute(s_type, subscription, [messages], conf)
示例#6
0
 def onOpen(self):
     LOG.info(_LI("WebSocket connection open."))
示例#7
0
 def onConnect(self, request):
     LOG.info(_LI("Client connecting: %s"), request.peer)
示例#8
0
 def onClose(self, wasClean, code, reason):
     self._handler.clean_subscriptions(self._subscriptions)
     self.factory.unregister(self.proto_id)
     LOG.info(_LI("WebSocket connection closed: %s"), reason)
示例#9
0
    def send_confirm_notification(self,
                                  queue,
                                  subscription,
                                  conf,
                                  project=None,
                                  expires=None,
                                  api_version=None):
        key = conf.signed_url.secret_key
        if not key:
            LOG.error(
                _LE("Can't send confirm notification due to the value of"
                    " secret_key option is None"))
            return
        url = "/%s/queues/%s/subscriptions/%s/confirm" % (api_version, queue,
                                                          subscription['id'])
        pre_url = urls.create_signed_url(key, [url],
                                         project=project,
                                         expires=expires,
                                         methods=['PUT'])
        message_type = MessageType.SubscriptionConfirmation.name

        messages = {}
        endpoint_dict = auth.get_public_endpoint()
        if endpoint_dict:
            wsgi_endpoint = endpoint_dict.get('zaqar', None)
            if wsgi_endpoint:
                wsgi_subscribe_url = urllib_parse.urljoin(wsgi_endpoint, url)
                messages['WSGISubscribeURL'] = wsgi_subscribe_url
            websocket_endpoint = endpoint_dict.get('zaqar-websocket', None)
            if websocket_endpoint:
                websocket_subscribe_url = urllib_parse.urljoin(
                    websocket_endpoint, url)
                messages['WebSocketSubscribeURL'] = websocket_subscribe_url
        messages.update({
            'Message_Type':
            message_type,
            'Message':
            'You have chosen to subscribe to the '
            'queue: %s' % queue,
            'URL-Signature':
            pre_url['signature'],
            'URL-Methods':
            pre_url['methods'][0],
            'URL-Paths':
            pre_url['paths'][0],
            'X-Project-ID':
            pre_url['project'],
            'URL-Expires':
            pre_url['expires'],
            'SubscribeBody': {
                'confirmed': True
            },
            'UnsubscribeBody': {
                'confirmed': False
            }
        })
        s_type = urllib_parse.urlparse(subscription['subscriber']).scheme
        LOG.info(
            _LI('Begin to send %(type)s confirm notification. The request'
                'body is %(messages)s'), {
                    'type': s_type,
                    'messages': messages
                })

        self._execute(s_type, subscription, [messages], conf)
示例#10
0
 def onOpen(self):
     LOG.info(_LI("WebSocket connection open."))
示例#11
0
 def onConnect(self, request):
     LOG.info(_LI("Client connecting: %s"), request.peer)
示例#12
0
 def onClose(self, wasClean, code, reason):
     self._handler.clean_subscriptions(self._subscriptions)
     self.factory.unregister(self.proto_id)
     LOG.info(_LI("WebSocket connection closed: %s"), reason)
示例#13
0
 def onClose(self, wasClean, code, reason):
     LOG.info(_LI("WebSocket connection closed: %s"), reason)
示例#14
0
    def send_confirm_notification(self,
                                  queue,
                                  subscription,
                                  conf,
                                  project=None,
                                  expires=None,
                                  api_version=None,
                                  is_unsubscribed=False):
        # NOTE(flwang): If the confirmation feature isn't enabled, just do
        # nothing. Here we're getting the require_confirmation from conf
        # object instead of using self.require_confirmation, because the
        # variable from self object really depends on the kwargs when
        # initializing the NotifierDriver object. See bug 1655812 for more
        # information.
        if not conf.notification.require_confirmation:
            return

        key = conf.signed_url.secret_key
        if not key:
            LOG.error(
                _LE("Can't send confirm notification due to the value of"
                    " secret_key option is None"))
            return
        url = "/%s/queues/%s/subscriptions/%s/confirm" % (api_version, queue,
                                                          subscription['id'])
        pre_url = urls.create_signed_url(key, [url],
                                         project=project,
                                         expires=expires,
                                         methods=['PUT'])
        message = None
        if is_unsubscribed:
            message_type = MessageType.UnsubscribeConfirmation.name
            message = ('You have unsubscribed successfully to the queue: %s, '
                       'you can resubscribe it by using confirmed=True.' %
                       queue)
        else:
            message_type = MessageType.SubscriptionConfirmation.name
            message = 'You have chosen to subscribe to the queue: %s' % queue

        messages = {}
        endpoint_dict = auth.get_public_endpoint()
        if endpoint_dict:
            wsgi_endpoint = endpoint_dict.get('zaqar', None)
            if wsgi_endpoint:
                wsgi_subscribe_url = urllib_parse.urljoin(wsgi_endpoint, url)
                messages['WSGISubscribeURL'] = wsgi_subscribe_url
            websocket_endpoint = endpoint_dict.get('zaqar-websocket', None)
            if websocket_endpoint:
                websocket_subscribe_url = urllib_parse.urljoin(
                    websocket_endpoint, url)
                messages['WebSocketSubscribeURL'] = websocket_subscribe_url
        messages.update({
            'Message_Type': message_type,
            'Message': message,
            'URL-Signature': pre_url['signature'],
            'URL-Methods': pre_url['methods'][0],
            'URL-Paths': pre_url['paths'][0],
            'X-Project-ID': pre_url['project'],
            'URL-Expires': pre_url['expires'],
            'SubscribeBody': {
                'confirmed': True
            },
            'UnsubscribeBody': {
                'confirmed': False
            }
        })
        s_type = urllib_parse.urlparse(subscription['subscriber']).scheme
        LOG.info(
            _LI('Begin to send %(type)s confirm/unsubscribe notification.'
                ' The request body is %(messages)s'), {
                    'type': s_type,
                    'messages': messages
                })

        self._execute(s_type, subscription, [messages], conf)