Example #1
0
    def send(self, message, destination=stomp.SUBSCRIPTION_ID_RESPONSE):
        resp = json.loads(message)
        response_id = resp.get("id")

        try:
            destination = self._req_dest[response_id]
            del self._req_dest[response_id]
        except KeyError:
            # we could have no reply-to or we could send events (no message id)
            pass

        try:
            connections = self._sub_map[destination]
        except KeyError:
            self.log.warn("Attempt to reply to unknown destination %s",
                          destination)
            return

        for connection in connections:
            res = stomp.Frame(
                stomp.Command.MESSAGE,
                {
                    stomp.Headers.DESTINATION: destination,
                    stomp.Headers.CONTENT_TYPE: "application/json",
                    stomp.Headers.SUBSCRIPTION: connection.id
                },
                message
            )
            # we need to check whether the channel is not closed
            if not connection.client.is_closed():
                connection.client.send_raw(res)
Example #2
0
    def send(self, message, destination=stomp.SUBSCRIPTION_ID_RESPONSE):
        resp = json.loads(message)
        response_id = resp.get("id")

        try:
            destination = self._req_dest[response_id]
            del self._req_dest[response_id]
        except KeyError:
            # we could have no reply-to or we could send events (no message id)
            pass

        try:
            connections = self._sub_map[destination]
        except KeyError:
            self.log.warn("Attempt to reply to unknown destination %s",
                          destination)
            return

        for connection in connections:
            res = stomp.Frame(
                stomp.Command.MESSAGE, {
                    stomp.Headers.DESTINATION: destination,
                    stomp.Headers.CONTENT_TYPE: "application/json",
                    stomp.Headers.SUBSCRIPTION: connection.id
                }, message)
            # we need to check whether the channel is not closed
            if not connection.client.is_closed():
                connection.client.send_raw(res)
Example #3
0
    def decode(cls, msg):
        try:
            obj = json.loads(msg, 'utf-8')
        except:
            raise JsonRpcParseError()

        return cls.fromRawObject(obj)
Example #4
0
    def decode(cls, msg):
        try:
            obj = json.loads(msg, 'utf-8')
        except:
            raise JsonRpcParseError()

        return cls.fromRawObject(obj)
Example #5
0
    def _handleMessage(self, req):
        transport, message = req
        try:
            mobj = json.loads(message)
            isResponse = self._isResponse(mobj)
        except:
            self.log.exception("Problem parsing message from client")

        if isResponse:
            self._processIncomingResponse(mobj)
        else:
            self._processEvent(mobj)
Example #6
0
 def _handle_internal(self, dispatcher, req_dest, request):
     """
     We need to build response dictionary which maps message id
     with destination. For legacy mode we use known 3.5 destination
     or for standard mode we use 'reply-to' header.
     """
     try:
         self._handle_destination(dispatcher, req_dest, json.loads(request))
     except Exception:
         # let json server process issue
         pass
     dispatcher.connection.handleMessage(request)
Example #7
0
    def _handleMessage(self, req):
        transport, message = req
        try:
            mobj = json.loads(message)
            isResponse = self._isResponse(mobj)
        except:
            self.log.exception("Problem parsing message from client")

        if isResponse:
            self._processIncomingResponse(mobj)
        else:
            self._processEvent(mobj)
Example #8
0
 def _handle_internal(self, dispatcher, req_dest, request):
     """
     We need to build response dictionary which maps message id
     with destination. For legacy mode we use known 3.5 destination
     or for standard mode we use 'reply-to' header.
     """
     try:
         self._handle_destination(dispatcher, req_dest, json.loads(request))
     except Exception:
         # let json server process issue
         pass
     dispatcher.connection.handleMessage(request)
Example #9
0
    def decode(msg):
        obj = json.loads(msg, 'utf-8')

        if "result" not in obj and "error" not in obj:
            raise JsonRpcInvalidRequestError("missing result or error info",
                                             obj)

        result = obj.get('result')
        error = JsonRpcError(**obj.get('error'))

        reqId = obj.get('id')
        if not isinstance(reqId, (str, unicode)):
            raise JsonRpcInvalidRequestError("missing response identifier",
                                             obj)
        return JsonRpcResponse(result, error, reqId)
Example #10
0
    def decode(msg):
        obj = json.loads(msg, 'utf-8')

        if "result" not in obj and "error" not in obj:
            raise JsonRpcInvalidRequestError("missing result or error info",
                                             obj)

        result = obj.get('result')
        error = JsonRpcError(**obj.get('error'))

        reqId = obj.get('id')
        if not isinstance(reqId, (str, unicode)):
            raise JsonRpcInvalidRequestError("missing response identifier",
                                             obj)
        return JsonRpcResponse(result, error, reqId)
Example #11
0
    def _parseMessage(self, client, addr, msg):
        ctx = _JsonRpcServeRequestContext(client, addr)

        try:
            rawRequests = json.loads(msg)
        except:
            ctx.addResponse(JsonRpcResponse(None, JsonRpcParseError(), None))
            ctx.sendReply()
            return

        if isinstance(rawRequests, list):
            # Empty batch request
            if len(rawRequests) == 0:
                ctx.addResponse(
                    JsonRpcResponse(None,
                                    JsonRpcInvalidRequestError(
                                        'request batch is empty',
                                        rawRequests),
                                    None))
                ctx.sendReply()
                return
        else:
            # From this point on we know it's always a list
            rawRequests = [rawRequests]

        # JSON Parsed handling each request
        requests = []
        for rawRequest in rawRequests:
            try:
                req = JsonRpcRequest.fromRawObject(rawRequest)
                requests.append(req)
            except JsonRpcError as err:
                ctx.addResponse(JsonRpcResponse(None, err, None))
            except:
                ctx.addResponse(JsonRpcResponse(None,
                                                JsonRpcInternalError(),
                                                None))

        ctx.setRequests(requests)

        # No request was built successfully or is only notifications
        if ctx.counter == 0:
            ctx.sendReply()

        for request in requests:
            self._runRequest(ctx, request)
Example #12
0
    def _parseMessage(self, client, server_address, msg):
        ctx = _JsonRpcServeRequestContext(client, server_address)

        try:
            rawRequests = json.loads(msg)
        except:
            ctx.addResponse(JsonRpcResponse(None, JsonRpcParseError(), None))
            ctx.sendReply()
            return

        if isinstance(rawRequests, list):
            # Empty batch request
            if len(rawRequests) == 0:
                ctx.addResponse(
                    JsonRpcResponse(
                        None,
                        JsonRpcInvalidRequestError('request batch is empty',
                                                   rawRequests), None))
                ctx.sendReply()
                return
        else:
            # From this point on we know it's always a list
            rawRequests = [rawRequests]

        # JSON Parsed handling each request
        requests = []
        for rawRequest in rawRequests:
            try:
                req = JsonRpcRequest.fromRawObject(rawRequest)
                requests.append(req)
            except JsonRpcError as err:
                ctx.addResponse(JsonRpcResponse(None, err, None))
            except:
                ctx.addResponse(
                    JsonRpcResponse(None, JsonRpcInternalError(), None))

        ctx.setRequests(requests)

        # No request was built successfully or is only notifications
        if ctx.counter == 0:
            ctx.sendReply()

        for request in requests:
            self._runRequest(ctx, request)
Example #13
0
    def serve(self):
        while True:
            data = self._inbox.get()
            if data is None:
                return

            transport, message = data
            client = self._clients[transport]
            try:
                mobj = json.loads(message)
                isResponse = self._isResponse(mobj)
            except:
                self.log.exception("Problem parsing message from client")
                transport.close()
                del self._clients[transport]
                continue

            if isResponse:
                client._processIncomingResponse(mobj)
            else:
                self._processEvent(client, mobj)
Example #14
0
    def send(self, message, destination=stomp.LEGACY_SUBSCRIPTION_ID_RESPONSE):
        resp = json.loads(message)
        response_id = resp.get("id")

        if response_id is None:
            # events do not have ids
            pass
        else:
            try:
                destination = self._req_dest[response_id]
            except KeyError:
                # we could have no reply-to we will use destination
                # provided as method argument
                pass
            try:
                del self._req_dest[response_id]
            except KeyError:
                self.log.warning(
                    "Response to a message with id %s was not found",
                    response_id)

        try:
            connections = self._sub_map[destination]
        except KeyError:
            self.log.warn("Attempt to reply to unknown destination %s",
                          destination)
            return

        for connection in connections:
            res = stomp.Frame(
                stomp.Command.MESSAGE, {
                    stomp.Headers.DESTINATION: destination,
                    stomp.Headers.CONTENT_TYPE: "application/json",
                    stomp.Headers.SUBSCRIPTION: connection.id
                }, message)
            # we need to check whether the channel is not closed
            if not connection.client.is_closed():
                connection.client.send_raw(res)
Example #15
0
 def decode(msg):
     obj = json.loads(msg, 'utf-8')
     return JsonRpcResponse.fromRawObject(obj)
Example #16
0
 def decode(msg):
     obj = json.loads(msg, 'utf-8')
     return JsonRpcResponse.fromRawObject(obj)