コード例 #1
0
    def test_encoding_detection(self):
        # Default case: UTF-8
        doc = xml_document(b"<foo>bar</foo>")
        self.assertEqual(doc.encoding, "UTF-8")

        # Something more exotic!
        doc = xml_document(b"<?xml version=\'1.0\' encoding=\'BIG5\'?><foo>bar</foo>")
        self.assertEqual(doc.encoding, "BIG5")
コード例 #2
0
ファイル: test_xml.py プロジェクト: sipior/Comet
    def test_encoding_detection(self):
        # Default case: UTF-8
        doc = xml_document(b"<foo>bar</foo>")
        self.assertEqual(doc.encoding, "UTF-8")

        # Something more exotic!
        doc = xml_document(b"<?xml version=\'1.0\' encoding=\'BIG5\'?><foo>bar</foo>")
        self.assertEqual(doc.encoding, "BIG5")
コード例 #3
0
    def stringReceived(self, data):
        """
        Called when we receive a string.

        The sender should only ever receive an ack or a nak, after which it
        should disconnect.
        """
        log.debug("Got response from %s" % str(self.transport.getPeer()))
        try:
            incoming = xml_document(data)

            if incoming.element.get('role') == "ack":
                log.info("Acknowledgement received from %s" %
                         str(self.transport.getPeer()))
                self.factory.ack = True
            elif incoming.element.get('role') == "nak":
                log.warn("Nak received: %s refused to accept VOEvent (%s)" %
                         (str(self.transport.getPeer()),
                          incoming.element.findtext(
                              "Meta/Result", default="no reason given")))
            else:
                log.warn(
                    "Incomprehensible data received from %s (role=%s)" %
                    (self.transport.getPeer(), incoming.element.get("role")))

        except ParseError:
            log.warn("Unparsable message received from %s" %
                     str(self.transport.getPeer()))

        finally:
            # After receiving a message, we shut down the connection.
            self.transport.loseConnection()
コード例 #4
0
ファイル: subscriber.py プロジェクト: jdswinbank/Comet
    def stringReceived(self, data):
        """
        Called when a complete new message is received.
        """
        try:
            incoming = xml_document(data)
        except ParseError:
            log.warn("Unparsable message received")
            return

        # Reset the timeout counter and wait another 120 seconds before
        # disconnecting due to inactivity.
        self.resetTimeout()

        # The root element of both VOEvent and Transport packets has a
        # "role" element which we use to identify the type of message we
        # have received.
        if incoming.element.get("role") == "iamalive":
            log.debug("IAmAlive received from %s" % str(self.transport.getPeer()))
            self.send_xml(iamaliveresponse(self.factory.local_ivo, incoming.element.find("Origin").text))
        elif incoming.element.get("role") == "authenticate":
            log.debug("Authenticate received from %s" % str(self.transport.getPeer()))
            self.send_xml(
                authenticateresponse(self.factory.local_ivo, incoming.element.find("Origin").text, self.filters)
            )
        elif incoming.element.get("role") in VOEVENT_ROLES:
            log.info("VOEvent %s received from %s" % (incoming.element.attrib["ivorn"], str(self.transport.getPeer())))
            # We don't send a NAK even if the event is invalid since we don't
            # want to be removed from upstream's distribution list.
            self.process_event(incoming, can_nak=False)
        else:
            log.warn(
                "Incomprehensible data received from %s (role=%s)"
                % (self.transport.getPeer(), incoming.element.get("role"))
            )
コード例 #5
0
ファイル: sender.py プロジェクト: timstaley/Comet
    def stringReceived(self, data):
        """
        Called when we receive a string.

        The sender should only ever receive an ack or a nak, after which it
        should disconnect.
        """
        log.debug("Got response from %s" % str(self.transport.getPeer()))
        try:
            incoming = xml_document(data)

            if incoming.get('role') == "ack":
                log.info("Acknowledgement received from %s" % str(self.transport.getPeer()))
                self.factory.ack = True
            elif incoming.get('role') == "nak":
                log.warn("Nak received: %s refused to accept VOEvent (%s)" %
                    (
                        str(self.transport.getPeer()),
                        incoming.findtext("Meta/Result", default="no reason given")
                    )
                )
            else:
                log.warn(
                    "Incomprehensible data received from %s (role=%s)" %
                    (self.transport.getPeer(), incoming.get("role"))
                )

        except ParseError:
            log.warn("Unparsable message received from %s" % str(self.transport.getPeer()))

        finally:
            # After receiving a message, we shut down the connection.
            self.transport.loseConnection()
コード例 #6
0
 def stringReceived(self, data):
     """
     Called when a complete new message is received.
     """
     try:
         incoming = xml_document(data)
     except ParseError:
         d = log.warn("Unparsable message received from %s" % str(self.transport.getPeer()))
     else:
         # The root element of both VOEvent and Transport packets has a
         # "role" element which we use to identify the type of message we
         # have received.
         if incoming.element.get('role') in VOEVENT_ROLES:
             log.info(
                 "VOEvent %s received from %s" % (
                     incoming.element.attrib['ivorn'],
                     str(self.transport.getPeer())
                 )
             )
             d = self.process_event(incoming)
         else:
             d = log.warn(
                 "Incomprehensible data received from %s (role=%s)" %
                 (self.transport.getPeer(), incoming.element.get("role"))
             )
     finally:
         return d.addCallback(
             lambda x: self.transport.loseConnection()
         )
コード例 #7
0
def nak(local_ivo, remote_ivo, result=None):
    root_element = origin_response_message(local_ivo, remote_ivo).element
    root_element.set("role", "nak")
    if result:
        meta = ElementTree.SubElement(root_element, "Meta")
        result_element = ElementTree.SubElement(meta, "Result")
        result_element.text = result
    return xml_document(root_element)
コード例 #8
0
def authenticate(local_ivo):
    root_element = transport_message().element
    root_element.set("role", "authenticate")
    origin = ElementTree.SubElement(root_element, "Origin")
    origin.text = local_ivo
    timestamp = ElementTree.SubElement(root_element, "TimeStamp")
    timestamp.text = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
    return xml_document(root_element)
コード例 #9
0
ファイル: messages.py プロジェクト: timstaley/Comet
def nak(local_ivo, remote_ivo, result=None):
    root_element = origin_response_message(local_ivo, remote_ivo).element
    root_element.set("role", "nak")
    if result:
        meta = ElementTree.SubElement(root_element, "Meta")
        result_element = ElementTree.SubElement(meta, "Result")
        result_element.text = result
    return xml_document(root_element)
コード例 #10
0
ファイル: messages.py プロジェクト: timstaley/Comet
def authenticate(local_ivo):
    root_element = transport_message().element
    root_element.set("role", "authenticate")
    origin = ElementTree.SubElement(root_element, "Origin")
    origin.text = local_ivo
    timestamp = ElementTree.SubElement(root_element, "TimeStamp")
    timestamp.text = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
    return xml_document(root_element)
コード例 #11
0
ファイル: messages.py プロジェクト: timstaley/Comet
def transport_message():
    return xml_document(
        ElementTree.Element("{http://www.telescope-networks.org/xml/Transport/v1.1}Transport",
            attrib={
                "version": "1.0",
                "{http://www.w3.org/2001/XMLSchema-instance}schemaLocation": "http://telescope-networks.org/schema/Transport/v1.1 http://www.telescope-networks.org/schema/Transport-v1.1.xsd"
            }
        )
    )
コード例 #12
0
ファイル: messages.py プロジェクト: timstaley/Comet
def origin_response_message(local_ivo, remote_ivo):
    root_element = transport_message().element
    origin = ElementTree.SubElement(root_element, "Origin")
    origin.text = remote_ivo
    response = ElementTree.SubElement(root_element, "Response")
    response.text = local_ivo
    timestamp = ElementTree.SubElement(root_element, "TimeStamp")
    timestamp.text = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
    return xml_document(root_element)
コード例 #13
0
ファイル: messages.py プロジェクト: timstaley/Comet
def authenticateresponse(local_ivo, remote_ivo, filters):
    root_element = origin_response_message(local_ivo, remote_ivo).element
    root_element.set("role", "authenticate")
    meta = ElementTree.SubElement(root_element, "Meta")
    for my_filter in filters:
        ElementTree.SubElement(
            meta, "Param", attrib={"name": "xpath-filter", "value": my_filter}
        )
    return xml_document(root_element)
コード例 #14
0
def transport_message():
    return xml_document(
        ElementTree.Element(
            "{http://www.telescope-networks.org/xml/Transport/v1.1}Transport",
            attrib={
                "version":
                "1.0",
                "{http://www.w3.org/2001/XMLSchema-instance}schemaLocation":
                "http://telescope-networks.org/schema/Transport/v1.1 http://www.telescope-networks.org/schema/Transport-v1.1.xsd"
            }))
コード例 #15
0
def origin_response_message(local_ivo, remote_ivo):
    root_element = transport_message().element
    origin = ElementTree.SubElement(root_element, "Origin")
    origin.text = remote_ivo
    if local_ivo:
        response = ElementTree.SubElement(root_element, "Response")
        response.text = local_ivo
    timestamp = ElementTree.SubElement(root_element, "TimeStamp")
    timestamp.text = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
    return xml_document(root_element)
コード例 #16
0
def authenticateresponse(local_ivo, remote_ivo, filters):
    root_element = origin_response_message(local_ivo, remote_ivo).element
    root_element.set("role", "authenticate")
    meta = ElementTree.SubElement(root_element, "Meta")
    for my_filter in filters:
        ElementTree.SubElement(meta,
                               "Param",
                               attrib={
                                   "name": "xpath-filter",
                                   "value": my_filter
                               })
    return xml_document(root_element)
コード例 #17
0
    def stringReceived(self, data):
        """
        Called when a complete new message is received.
        """
        try:
            incoming = xml_document(data)
        except ParseError:
            log.warn("Unparsable message received")
            return

        # Reset the timeout counter and wait another 120 seconds before
        # disconnecting due to inactivity.
        self.resetTimeout()

        # The root element of both VOEvent and Transport packets has a
        # "role" element which we use to identify the type of message we
        # have received.
        if incoming.element.get('role') == "iamalive":
            log.debug("IAmAlive received from %s" % str(self.transport.getPeer()))
            self.send_xml(
                iamaliveresponse(self.factory.local_ivo,
                incoming.element.find('Origin').text)
            )
        elif incoming.element.get('role') == "authenticate":
            log.debug("Authenticate received from %s" % str(self.transport.getPeer()))
            self.send_xml(
                authenticateresponse(
                    self.factory.local_ivo,
                    incoming.element.find('Origin').text,
                    self.filters
                )
            )
        elif incoming.element.get('role') in VOEVENT_ROLES:
            log.info(
                "VOEvent %s received from %s" % (
                    incoming.element.attrib['ivorn'],
                    str(self.transport.getPeer())
                )
            )
            # We don't send a NAK even if the event is invalid since we don't
            # want to be removed from upstream's distribution list.
            self.process_event(incoming, can_nak=False)
        else:
            log.warn(
                "Incomprehensible data received from %s (role=%s)" %
                (self.transport.getPeer(), incoming.element.get("role"))
            )
コード例 #18
0
    def stringReceived(self, data):
        try:
            incoming = xml_document(data)
        except ParseError:
            log.warn("Unparsable message received")
            return

        if incoming.element.get('role') == "iamalive":
            log.debug("IAmAlive received from %s" %
                      str(self.transport.getPeer()))
            self.alive_count -= 1
        elif incoming.element.get('role') == "ack":
            log.debug("Ack received from %s" % str(self.transport.getPeer()))
            self.outstanding_ack -= 1
        elif incoming.element.get('role') == "nak":
            log.info("Nak received from %s; terminating" %
                     str(self.transport.getPeer()))
            self.transport.loseConnection()
        elif incoming.element.get('role') == "authenticate":
            log.debug("Authentication received from %s" %
                      str(self.transport.getPeer()))
            self.filters = []
            # Accept both "new-style" (<Param type="xpath-filter" />) and
            # old-style (<filter type="xpath" />) filters.
            for xpath in chain([
                    elem.get('value') for elem in incoming.element.findall(
                        "Meta/Param[@name=\"xpath-filter\"]")
            ], [
                    elem.text for elem in incoming.element.findall(
                        "Meta/filter[@type=\"xpath\"]")
            ]):
                log.info("Installing filter %s for %s" %
                         (xpath, str(self.transport.getPeer())))
                try:
                    self.filters.append(ElementTree.XPath(xpath))
                except ElementTree.XPathSyntaxError:
                    log.info("Filter %s is not valid XPath" % (xpath, ))
        else:
            log.warn("Incomprehensible data received from %s (role=%s)" %
                     (self.transport.getPeer(), incoming.element.get("role")))
コード例 #19
0
ファイル: broadcaster.py プロジェクト: timstaley/Comet
    def stringReceived(self, data):
        try:
            incoming = xml_document(data)
        except ParseError:
            log.warn("Unparsable message received")
            return

        if incoming.get('role') == "iamalive":
            log.debug("IAmAlive received from %s" % str(self.transport.getPeer()))
            self.alive_count -= 1
        elif incoming.get('role') == "ack":
            log.debug("Ack received from %s" % str(self.transport.getPeer()))
            self.outstanding_ack -= 1
        elif incoming.get('role') == "nak":
            log.info("Nak received from %s; terminating" % str(self.transport.getPeer()))
            self.transport.loseConnection()
        elif incoming.get('role') == "authenticate":
            log.debug("Authentication received from %s" % str(self.transport.getPeer()))
            self.filters = []
            # Accept both "new-style" (<Param type="xpath-filter" />) and
            # old-style (<filter type="xpath" />) filters.
            for xpath in chain(
                [elem.get('value') for elem in incoming.findall("Meta/Param[@name=\"xpath-filter\"]")],
                [elem.text for elem in incoming.findall("Meta/filter[@type=\"xpath\"]")]
            ):
                log.info(
                    "Installing filter %s for %s" %
                    (xpath, str(self.transport.getPeer()))
                )
                try:
                    self.filters.append(ElementTree.XPath(xpath))
                except ElementTree.XPathSyntaxError:
                    log.info("Filter %s is not valid XPath" % (xpath,))
        else:
            log.warn(
                "Incomprehensible data received from %s (role=%s)" %
                (self.transport.getPeer(), incoming.get("role"))
            )
コード例 #20
0
 def stringReceived(self, data):
     """
     Called when a complete new message is received.
     """
     try:
         incoming = xml_document(data)
     except ParseError:
         d = log.warn("Unparsable message received from %s" %
                      str(self.transport.getPeer()))
     else:
         # The root element of both VOEvent and Transport packets has a
         # "role" element which we use to identify the type of message we
         # have received.
         if incoming.element.get('role') in VOEVENT_ROLES:
             log.info("VOEvent %s received from %s" %
                      (incoming.element.attrib['ivorn'],
                       str(self.transport.getPeer())))
             d = self.process_event(incoming)
         else:
             d = log.warn(
                 "Incomprehensible data received from %s (role=%s)" %
                 (self.transport.getPeer(), incoming.element.get("role")))
     finally:
         return d.addCallback(lambda x: self.transport.loseConnection())
コード例 #21
0
ファイル: test_xml.py プロジェクト: sipior/Comet
 def setUp(self):
     self.doc = xml_document(b"<foo>bar</foo>")
コード例 #22
0
ファイル: test_schema.py プロジェクト: timstaley/Comet
 def test_valid(self):
     return self.validator(xml_document(DUMMY_VOEVENT))
コード例 #23
0
ファイル: messages.py プロジェクト: timstaley/Comet
def ack(local_ivo, remote_ivo):
    root_element = origin_response_message(local_ivo, remote_ivo).element
    root_element.set("role", "ack")
    return xml_document(root_element)
コード例 #24
0
ファイル: test_schema.py プロジェクト: timstaley/Comet
 def test_invalid(self):
     d = self.validator(xml_document(BAD_EVENT_TEXT))
     return self.assertFailure(d, Exception)
コード例 #25
0
ファイル: test_eventwriter.py プロジェクト: timstaley/Comet
 def setUp(self):
     self.event = xml_document(DUMMY_VOEVENT)
     self.event_writer = EventWriter()
コード例 #26
0
 def test_valid(self):
     # Should not raise
     self.validator(xml_document(DUMMY_VOEVENT))
コード例 #27
0
 def test_invalid(self):
     self.assertRaises(
         Exception, self.validator,
         xml_document(
             BAD_EVENT_TEXT.replace(DUMMY_EVENT_IVOID, b"bad_ivoid")))
コード例 #28
0
def ack(local_ivo, remote_ivo):
    root_element = origin_response_message(local_ivo, remote_ivo).element
    root_element.set("role", "ack")
    return xml_document(root_element)
コード例 #29
0
 def test_valid(self):
     return self.validator(xml_document(DUMMY_VOEVENT))
コード例 #30
0
 def test_invalid(self):
     d = self.validator(xml_document(BAD_EVENT_TEXT))
     return self.assertFailure(d, Exception)
コード例 #31
0
 def setUp(self):
     self.event = xml_document(DUMMY_VOEVENT)
     self.event_writer = EventWriter()
コード例 #32
0
ファイル: test_xml.py プロジェクト: sipior/Comet
 def setUp(self):
     self.doc = xml_document(etree.fromstring(EXAMPLE_XML))
コード例 #33
0
 def setUp(self):
     self.doc = xml_document(etree.fromstring(EXAMPLE_XML))
コード例 #34
0
ファイル: test_ivoid.py プロジェクト: jdswinbank/Comet
 def test_invalid(self):
     self.assertRaises(Exception, self.validator,
         xml_document(BAD_EVENT_TEXT.replace(DUMMY_EVENT_IVOID, b"bad_ivoid"))
     )
コード例 #35
0
ファイル: test_ivoid.py プロジェクト: jdswinbank/Comet
 def test_valid(self):
     # Should not raise
     self.validator(xml_document(DUMMY_VOEVENT))
コード例 #36
0
 def setUp(self):
     self.doc = xml_document(b"<foo>bar</foo>")
コード例 #37
0
ファイル: test_xml.py プロジェクト: sipior/Comet
 def setUp(self):
     self.doc = xml_document(EXAMPLE_XML)
コード例 #38
0
 def setUp(self):
     self.doc = xml_document(EXAMPLE_XML)