comparison idavoll/gateway.py @ 209:7f3ffb7a1a9e

Add support for node deletion with redirect.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 30 Jan 2009 14:35:05 +0000
parents 274a45d2a5ab
children 2a0a6a671776
comparison
equal deleted inserted replaced
208:cc4f45ef793e 209:7f3ffb7a1a9e
169 def http_POST(self, request): 169 def http_POST(self, request):
170 """ 170 """
171 Respond to a POST request to create a new node. 171 Respond to a POST request to create a new node.
172 """ 172 """
173 173
174 def respond(result): 174 def gotStream(_):
175 return http.Response(responsecode.NO_CONTENT)
176
177 def getNode():
178 if request.args.get('uri'): 175 if request.args.get('uri'):
179 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0]) 176 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0])
180 return defer.succeed(nodeIdentifier) 177 return defer.succeed(nodeIdentifier)
181 else: 178 else:
182 raise http.HTTPError(http.Response(responsecode.BAD_REQUEST, 179 raise http.HTTPError(http.Response(responsecode.BAD_REQUEST,
183 "No URI given")) 180 "No URI given"))
184 181
182 def doDelete(nodeIdentifier, data):
183 if data:
184 params = simplejson.loads(''.join(data))
185 redirectURI = params.get('redirect_uri')
186 else:
187 redirectURI = None
188
189 return self.backend.deleteNode(nodeIdentifier, self.owner,
190 redirectURI)
191
192 def respond(result):
193 return http.Response(responsecode.NO_CONTENT)
194
195
185 def trapNotFound(failure): 196 def trapNotFound(failure):
186 failure.trap(error.NodeNotFound) 197 failure.trap(error.NodeNotFound)
187 return http.StatusResponse(responsecode.NOT_FOUND, 198 return http.StatusResponse(responsecode.NOT_FOUND,
188 "Node not found") 199 "Node not found")
189 200
190 def trapXMPPURIParseError(failure): 201 def trapXMPPURIParseError(failure):
191 failure.trap(XMPPURIParseError) 202 failure.trap(XMPPURIParseError)
192 return http.StatusResponse(responsecode.BAD_REQUEST, 203 return http.StatusResponse(responsecode.BAD_REQUEST,
193 "Malformed XMPP URI: %s" % failure.value.message) 204 "Malformed XMPP URI: %s" % failure.value.message)
194 205
195 d = getNode() 206 data = []
196 d.addCallback(self.backend.deleteNode, self.owner) 207 d = readStream(request.stream, data.append)
208 d.addCallback(gotStream)
209 d.addCallback(doDelete, data)
197 d.addCallback(respond) 210 d.addCallback(respond)
198 d.addErrback(trapNotFound) 211 d.addErrback(trapNotFound)
199 d.addErrback(trapXMPPURIParseError) 212 d.addErrback(trapXMPPURIParseError)
200 return d 213 return d
201 214
471 Fire up HTTP client to do callback 484 Fire up HTTP client to do callback
472 """ 485 """
473 486
474 service = event.sender 487 service = event.sender
475 nodeIdentifier = event.nodeIdentifier 488 nodeIdentifier = event.nodeIdentifier
476 self.callCallbacks(service, nodeIdentifier, eventType='DELETED') 489 redirectURI = event.redirectURI
490 self.callCallbacks(service, nodeIdentifier, eventType='DELETED',
491 redirectURI=redirectURI)
477 492
478 493
479 def _postTo(self, callbacks, service, nodeIdentifier, 494 def _postTo(self, callbacks, service, nodeIdentifier,
480 payload=None, contentType=None, eventType=None): 495 payload=None, contentType=None, eventType=None,
496 redirectURI=None):
481 497
482 if not callbacks: 498 if not callbacks:
483 return 499 return
484 500
485 postdata = None 501 postdata = None
492 if contentType: 508 if contentType:
493 headers['Content-Type'] = "%s;charset=utf-8" % contentType 509 headers['Content-Type'] = "%s;charset=utf-8" % contentType
494 510
495 if eventType: 511 if eventType:
496 headers['Event'] = eventType 512 headers['Event'] = eventType
513
514 if redirectURI:
515 headers['Link'] = '<%s>; rel=alternate' % (
516 redirectURI.encode('utf-8'),
517 )
497 518
498 def postNotification(callbackURI): 519 def postNotification(callbackURI):
499 d = client.getPage(str(callbackURI), 520 d = client.getPage(str(callbackURI),
500 method='POST', 521 method='POST',
501 postdata=postdata, 522 postdata=postdata,
505 for callbackURI in callbacks: 526 for callbackURI in callbacks:
506 reactor.callLater(0, postNotification, callbackURI) 527 reactor.callLater(0, postNotification, callbackURI)
507 528
508 529
509 def callCallbacks(self, service, nodeIdentifier, 530 def callCallbacks(self, service, nodeIdentifier,
510 payload=None, contentType=None, eventType=None): 531 payload=None, contentType=None, eventType=None,
532 redirectURI=None):
511 533
512 def eb(failure): 534 def eb(failure):
513 failure.trap(error.NoCallbacks) 535 failure.trap(error.NoCallbacks)
514 536
515 # No callbacks were registered for this node. Unsubscribe? 537 # No callbacks were registered for this node. Unsubscribe?
516 538
517 d = self.storage.getCallbacks(service, nodeIdentifier) 539 d = self.storage.getCallbacks(service, nodeIdentifier)
518 d.addCallback(self._postTo, service, nodeIdentifier, payload, 540 d.addCallback(self._postTo, service, nodeIdentifier, payload,
519 contentType, eventType) 541 contentType, eventType, redirectURI)
520 d.addErrback(eb) 542 d.addErrback(eb)
521 d.addErrback(log.err) 543 d.addErrback(log.err)
522 544
523 545
524 546
706 http_GET = None 728 http_GET = None
707 729
708 730
709 def http_POST(self, request): 731 def http_POST(self, request):
710 p = WebStreamParser() 732 p = WebStreamParser()
711 d = p.parse(request.stream) 733 if not request.headers.hasHeader('Event'):
734 d = p.parse(request.stream)
735 else:
736 d = defer.succeed(None)
712 d.addCallback(self.callback, request.headers) 737 d.addCallback(self.callback, request.headers)
713 d.addCallback(lambda _: http.Response(responsecode.NO_CONTENT)) 738 d.addCallback(lambda _: http.Response(responsecode.NO_CONTENT))
714 return d 739 return d
715 740
716 741
765 def create(self): 790 def create(self):
766 f = getPageWithFactory(self._makeURI('create'), 791 f = getPageWithFactory(self._makeURI('create'),
767 method='POST', 792 method='POST',
768 agent=self.agent) 793 agent=self.agent)
769 return f.deferred.addCallback(simplejson.loads) 794 return f.deferred.addCallback(simplejson.loads)
795
796
797 def delete(self, xmppURI, redirectURI=None):
798 query = {'uri': xmppURI}
799
800 if redirectURI:
801 params = {'redirect_uri': redirectURI}
802 postdata = simplejson.dumps(params)
803 headers = {'Content-Type': MIME_JSON}
804 else:
805 postdata = None
806 headers = None
807
808 f = getPageWithFactory(self._makeURI('delete', query),
809 method='POST',
810 postdata=postdata,
811 headers=headers,
812 agent=self.agent)
813 return f.deferred
770 814
771 815
772 def publish(self, entry, xmppURI=None): 816 def publish(self, entry, xmppURI=None):
773 query = xmppURI and {'uri': xmppURI} 817 query = xmppURI and {'uri': xmppURI}
774 818