comparison idavoll/pubsub.py @ 81:995ba223a43b

Implemented ComponentServiceToItemRetrievalService. Redone error() of Service a bit, now prints a brief traceback of unexpected exceptions and doens't reraise the exception.
author Ralph Meijer <ralphm@ik.nu>
date Tue, 09 Nov 2004 14:53:50 +0000
parents 5d7a924ebddb
children f3f31aa491df
comparison
equal deleted inserted replaced
80:ec354aab3949 81:995ba223a43b
24 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options' 24 PUBSUB_OPTIONS_GET = PUBSUB_GET + '/options'
25 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options' 25 PUBSUB_OPTIONS_SET = PUBSUB_SET + '/options'
26 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure' 26 PUBSUB_CONFIGURE_GET = PUBSUB_GET + '/configure'
27 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure' 27 PUBSUB_CONFIGURE_SET = PUBSUB_SET + '/configure'
28 PUBSUB_AFFILIATIONS = PUBSUB_GET + '/affiliations' 28 PUBSUB_AFFILIATIONS = PUBSUB_GET + '/affiliations'
29 PUBSUB_ITEMS = PUBSUB_GET + '/items'
29 30
30 class Error(Exception): 31 class Error(Exception):
31 pubsub_error = None 32 pubsub_error = None
32 stanza_error = None 33 stanza_error = None
33 msg = '' 34 msg = ''
75 self.backend = backend 76 self.backend = backend
76 77
77 def error(self, failure, iq): 78 def error(self, failure, iq):
78 try: 79 try:
79 e = failure.trap(Error, *error_map.keys()) 80 e = failure.trap(Error, *error_map.keys())
80 81 except:
82 failure.printBriefTraceback()
83 xmpp_error.error_from_iq(iq, 'internal-server-error')
84 return iq
85 else:
81 if e == Error: 86 if e == Error:
82 stanza_error = failure.value.stanza_error 87 stanza_error = failure.value.stanza_error
83 pubsub_error = failure.value.pubsub_error 88 pubsub_error = failure.value.pubsub_error
84 msg = '' 89 msg = ''
85 else: 90 else:
88 93
89 xmpp_error.error_from_iq(iq, stanza_error, msg) 94 xmpp_error.error_from_iq(iq, stanza_error, msg)
90 if pubsub_error: 95 if pubsub_error:
91 iq.error.addElement((NS_PUBSUB_ERRORS, pubsub_error)) 96 iq.error.addElement((NS_PUBSUB_ERRORS, pubsub_error))
92 return iq 97 return iq
93 except:
94 xmpp_error.error_from_iq(iq, 'internal-server-error')
95 self.send(iq)
96 raise
97 98
98 def success(self, result, iq): 99 def success(self, result, iq):
99 iq.swapAttributeValues("to", "from") 100 iq.swapAttributeValues("to", "from")
100 iq["type"] = 'result' 101 iq["type"] = 'result'
101 iq.children = result or [] 102 iq.children = result or []
346 entity['affiliation'] = r['affiliation'] or 'none' 347 entity['affiliation'] = r['affiliation'] or 'none'
347 entity['subscription'] = r['subscription'] or 'none' 348 entity['subscription'] = r['subscription'] or 'none'
348 return [reply] 349 return [reply]
349 350
350 components.registerAdapter(ComponentServiceFromAffiliationsService, backend.IAffiliationsService, component.IService) 351 components.registerAdapter(ComponentServiceFromAffiliationsService, backend.IAffiliationsService, component.IService)
352
353 class ComponentServiceFromItemRetrievalService(Service):
354
355 def componentConnected(self, xmlstream):
356 xmlstream.addObserver(PUBSUB_ITEMS, self.onItems)
357
358 def onItems(self, iq):
359 self.handler_wrapper(self._onItems, iq)
360
361 def _onItems(self, iq):
362 try:
363 node_id = iq.pubsub.items["node"]
364 except KeyError:
365 raise BadRequest
366
367 max_items = iq.pubsub.items.getAttribute('max_items')
368
369 if max_items:
370 try:
371 max_items = int(max_items)
372 except ValueError:
373 raise BadRequest
374
375 item_ids = []
376 for child in iq.pubsub.items.children:
377 if child.name == 'item' and child.uri == NS_PUBSUB:
378 try:
379 item_ids.append(child["id"])
380 except KeyError:
381 raise BadRequest
382
383 d = self.backend.get_items(node_id, jid.JID(iq["from"]), max_items,
384 item_ids)
385 d.addCallback(self._return_items_response, node_id)
386 return d
387
388 def _return_items_response(self, result, node_id):
389 reply = domish.Element((NS_PUBSUB, 'pubsub'))
390 items = reply.addElement('items')
391 items["node"] = node_id
392 try:
393 for r in result:
394 items.addRawXml(r)
395 except Exception, e:
396 print e
397
398 return [reply]
399
400 components.registerAdapter(ComponentServiceFromItemRetrievalService, backend.IItemRetrievalService, component.IService)