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")
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()
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")) )
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()
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() )
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)
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)
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" } ) )
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)
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)
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" }))
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)
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)
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")) )
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")))
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")) )
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())
def setUp(self): self.doc = xml_document(b"<foo>bar</foo>")
def test_valid(self): return self.validator(xml_document(DUMMY_VOEVENT))
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)
def test_invalid(self): d = self.validator(xml_document(BAD_EVENT_TEXT)) return self.assertFailure(d, Exception)
def setUp(self): self.event = xml_document(DUMMY_VOEVENT) self.event_writer = EventWriter()
def test_valid(self): # Should not raise self.validator(xml_document(DUMMY_VOEVENT))
def test_invalid(self): self.assertRaises( Exception, self.validator, xml_document( BAD_EVENT_TEXT.replace(DUMMY_EVENT_IVOID, b"bad_ivoid")))
def setUp(self): self.doc = xml_document(etree.fromstring(EXAMPLE_XML))
def test_invalid(self): self.assertRaises(Exception, self.validator, xml_document(BAD_EVENT_TEXT.replace(DUMMY_EVENT_IVOID, b"bad_ivoid")) )
def setUp(self): self.doc = xml_document(EXAMPLE_XML)