annotate idavoll/gateway.py @ 177:faf1c9bc2612

Add HTTP gateway in a separate plugin. Author: ralphm Fixes #8.
author Ralph Meijer <ralphm@ik.nu>
date Thu, 10 Apr 2008 11:18:29 +0000
parents
children c21b986cff30
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 # Copyright (c) 2003-2008 Ralph Meijer
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2 # See LICENSE for details.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 import cgi
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 from time import gmtime, strftime
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 import simplejson
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
9 from twisted.application import service
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10 from twisted.internet import defer, reactor
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 from twisted.web import client
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 from twisted.web2 import http, http_headers, resource, responsecode
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13 from twisted.web2.stream import readStream
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14 from twisted.words.protocols.jabber.jid import JID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 from twisted.words.protocols.jabber.error import StanzaError
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 from twisted.words.xish import domish
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18 from wokkel.pubsub import Item
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19 from wokkel.pubsub import PubSubClient
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21 from idavoll import error
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 NS_ATOM = 'http://www.w3.org/2005/Atom'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26 class RemoteSubscriptionService(service.Service, PubSubClient):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 def __init__(self, jid):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 self.jid = jid
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31 def startService(self):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 self.callbacks = {}
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34 def trapNotFound(self, failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 failure.trap(StanzaError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36 if not failure.value.condition == 'item-not-found':
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 raise failure
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 raise error.NodeNotFound
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40 def subscribeCallback(self, jid, nodeIdentifier, callback):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 def newCallbackList(result):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43 callbackList = set()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 self.callbacks[jid, nodeIdentifier] = callbackList
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 return callbackList
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 callbackList = self.callbacks[jid, nodeIdentifier]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 except KeyError:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 d = self.subscribe(jid, nodeIdentifier, self.jid)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51 d.addCallback(newCallbackList)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52 else:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
53 d = defer.succeed(callbackList)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55 d.addCallback(lambda callbackList: callbackList.add(callback))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 d.addErrback(self.trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59 def unsubscribeCallback(self, jid, nodeIdentifier, callback):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61 callbackList = self.callbacks[jid, nodeIdentifier]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 callbackList.remove(callback)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 except KeyError:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64 return defer.fail(error.NotSubscribed())
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 if not callbackList:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67 self.unsubscribe(jid, nodeIdentifier, self.jid)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 return defer.succeed(None)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71 def itemsReceived(self, recipient, service, nodeIdentifier, items):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73 Fire up HTTP client to do callback
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 # Collect atom entries
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77 atomEntries = []
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78 for item in items:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 # ignore non-items (i.e. retractions)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 if item.name != 'item':
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81 continue
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 atomEntry = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84 for element in item.elements():
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 # extract the first element that is an atom entry
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86 if element.uri == NS_ATOM and element.name == 'entry':
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 atomEntry = element
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
88 break
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
90 if atomEntry:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91 atomEntries.append(atomEntry)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93 # Don't notify if there are no atom entries
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 if not atomEntries:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
95 return
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
97 if len(atomEntries) == 1:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
98 contentType = 'application/atom+xml;type=entry'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 payload = atomEntries[0]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100 else:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 contentType = 'application/atom+xml;type=feed'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102 nodeURI = 'xmpp:%s?;node=%s' % (service.full(), nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 now = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105 # Collect the received entries in a feed
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
106 payload = domish.Element((NS_ATOM, 'feed'))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107 payload.addElement('title', content='Received item collection')
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108 payload.addElement('id', content=nodeURI)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
109 payload.addElement('updated', content=now)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
110 for atomEntry in atomEntries:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
111 payload.addChild(atomEntry)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
112
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
113 self.callCallbacks(recipient, service, nodeIdentifier, payload,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
114 contentType)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
115
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
116 def deleteReceived(self, recipient, service, nodeIdentifier):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
117 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 Fire up HTTP client to do callback
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
121 self.callCallbacks(recipient, service, nodeIdentifier,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122 eventType='DELETED')
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124 def callCallbacks(self, recipient, service, nodeIdentifier,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
125 payload=None, contentType=None, eventType=None):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
126 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
127 callbacks = self.callbacks[service, nodeIdentifier]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
128 except KeyError:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
129 return
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
130
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131 postdata = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
132 nodeURI = 'xmpp:%s?;node=%s' % (service.full(), nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
133 headers = {'Referer': nodeURI.encode('utf-8'),
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
134 'PubSub-Service': service.full().encode('utf-8')}
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
135
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
136 if payload:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
137 postdata = payload.toXml().encode('utf-8')
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
138 if contentType:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
139 headers['Content-Type'] = "%s;charset=utf-8" % contentType
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
140
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
141 if eventType:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
142 headers['Event'] = eventType
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
143
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
144 def postNotification(callbackURI):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
145 return client.getPage(str(callbackURI),
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
146 method='POST',
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
147 postdata=postdata,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
148 headers=headers)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
149
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
150 for callbackURI in callbacks:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
151 reactor.callLater(0, postNotification, callbackURI)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
152
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
153
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
154 class WebStreamParser(object):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
155 def __init__(self):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
156 self.elementStream = domish.elementStream()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
157 self.elementStream.DocumentStartEvent = self.docStart
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
158 self.elementStream.ElementEvent = self.elem
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
159 self.elementStream.DocumentEndEvent = self.docEnd
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
160 self.done = False
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
161
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
162 def docStart(self, elem):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
163 self.document = elem
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
164
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
165 def elem(self, elem):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
166 self.document.addChild(elem)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
167
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
168 def docEnd(self):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
169 self.done = True
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
170
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
171 def parse(self, stream):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
172 def endOfStream(result):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
173 if not self.done:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
174 raise Exception("No more stuff?")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
175 else:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
176 return self.document
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
178 d = readStream(stream, self.elementStream.parse)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
179 d.addCallback(endOfStream)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
180 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
181
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
182
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
183 class XMPPURIParseError(ValueError):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
184 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
185 Raised when a given XMPP URI couldn't be properly parsed.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
186 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
187
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
188
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
189 def getServiceAndNode(uri):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
190 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
191 Given an XMPP URI, extract the publish subscribe service JID and node ID.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
192 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
193
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
194 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
195 scheme, rest = uri.split(':', 1)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
196 except ValueError:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
197 raise XMPPURIParseError("No URI scheme component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
198
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
199 if scheme != 'xmpp':
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
200 raise XMPPURIParseError("Unknown URI scheme")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
201
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
202 if rest.startswith("//"):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
203 raise XMPPURIParseError("Unexpected URI authority component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
204
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
205 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
206 entity, query = rest.split('?', 1)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
207 except ValueError:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
208 raise XMPPURIParseError("No URI query component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
209
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
210 if not entity:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
211 raise XMPPURIParseError("Empty URI path component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
212
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
213 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
214 jid = JID(entity)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
215 except Exception, e:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
216 raise XMPPURIParseError("Invalid JID: %s" % e.message)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
217
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
218 params = cgi.parse_qs(query)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
219
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
220 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
221 nodeIdentifier = params['node'][0]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
222 except (KeyError, ValueError):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
223 raise XMPPURIParseError("No node in query component of URI")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
224
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
225 return jid, nodeIdentifier
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
226
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
227
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
228 class CreateResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
229 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
230 A resource to create a publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
231 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
232 def __init__(self, backend, serviceJID, owner):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
233 self.backend = backend
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
234 self.serviceJID = serviceJID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
235 self.owner = owner
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
236
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
237 http_GET = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
238
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
239 def http_POST(self, request):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
240 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
241 Respond to a POST request to create a new node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
242 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
243
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
244 def toResponse(nodeIdentifier):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
245 uri = 'xmpp:%s?;node=%s' % (self.serviceJID.full(), nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
246 stream = simplejson.dumps({'uri': uri})
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
247 return http.Response(responsecode.OK, stream=stream)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
248
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
249 d = self.backend.create_node(None, self.owner)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
250 d.addCallback(toResponse)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
251 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
252
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
253
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
254 class DeleteResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
255 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
256 A resource to create a publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
257 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
258 def __init__(self, backend, serviceJID, owner):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
259 self.backend = backend
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
260 self.serviceJID = serviceJID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
261 self.owner = owner
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
262
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
263 http_GET = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
264
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
265 def http_POST(self, request):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
266 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
267 Respond to a POST request to create a new node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
268 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
269
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
270 def respond(result):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
271 return http.Response(responsecode.NO_CONTENT)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
272
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
273 def getNode():
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
274 if request.args.get('uri'):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
275 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0])
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
276 return defer.succeed(nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
277 else:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
278 raise http.HTTPError(http.Response(responsecode.BAD_REQUEST,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
279 "No URI given"))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
280
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
281 def trapNotFound(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
282 failure.trap(error.NodeNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
283 return http.StatusResponse(responsecode.NOT_FOUND,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
284 "Node not found")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
285
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
286 def trapXMPPURIParseError(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
287 failure.trap(XMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
288 return http.StatusResponse(responsecode.BAD_REQUEST,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
289 "Malformed XMPP URI: %s" % failure.value.message)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
290
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
291 d = getNode()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
292 d.addCallback(self.backend.delete_node, self.owner)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
293 d.addCallback(respond)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
294 d.addErrback(trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
295 d.addErrback(trapXMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
296 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
297
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
298
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
299 class PublishResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
300 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
301 A resource to publish to a publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
302 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
303
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
304 def __init__(self, backend, serviceJID, owner):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
305 self.backend = backend
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
306 self.serviceJID = serviceJID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
307 self.owner = owner
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
308
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
309 http_GET = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
310
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
311 def checkMediaType(self, request):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
312 ctype = request.headers.getHeader('content-type')
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
313
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
314 if not ctype:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
315 raise http.HTTPError(
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
316 http.StatusResponse(
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
317 responsecode.BAD_REQUEST,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
318 "No specified Media Type"))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
319
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
320 if (ctype.mediaType != 'application' or
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
321 ctype.mediaSubtype != 'atom+xml' or
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
322 ctype.params.get('type') != 'entry' or
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
323 ctype.params.get('charset', 'utf-8') != 'utf-8'):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
324 raise http.HTTPError(
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
325 http.StatusResponse(
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
326 responsecode.UNSUPPORTED_MEDIA_TYPE,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
327 "Unsupported Media Type: %s" %
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
328 http_headers.generateContentType(ctype)))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
329
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
330 def parseXMLPayload(self, stream):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
331 if not stream:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
332 print "Stream is empty", repr(stream)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
333 elif not stream.length:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
334 print "Stream length is", repr(stream.length)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
335 p = WebStreamParser()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
336 return p.parse(stream)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
337
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
338 def http_POST(self, request):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
339 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
340 Respond to a POST request to create a new item.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
341 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
342
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
343 def toResponse(nodeIdentifier):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
344 uri = 'xmpp:%s?;node=%s' % (self.serviceJID.full(), nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
345 stream = simplejson.dumps({'uri': uri})
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
346 return http.Response(responsecode.OK, stream=stream)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
347
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
348 def gotNode(nodeIdentifier, payload):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
349 item = Item(id='current', payload=payload)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
350 d = self.backend.publish(nodeIdentifier, [item], self.owner)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
351 d.addCallback(lambda _: nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
352 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
353
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
354 def getNode():
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
355 if request.args.get('uri'):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
356 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0])
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
357 return defer.succeed(nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
358 else:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
359 return self.backend.create_node(None, self.owner)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
360
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
361 def doPublish(payload):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
362 d = getNode()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
363 d.addCallback(gotNode, payload)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
364 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
365
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
366 def trapNotFound(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
367 failure.trap(error.NodeNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
368 return http.StatusResponse(responsecode.NOT_FOUND,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
369 "Node not found")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
370
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
371 def trapXMPPURIParseError(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
372 failure.trap(XMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
373 return http.StatusResponse(responsecode.BAD_REQUEST,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
374 "Malformed XMPP URI: %s" % failure.value.message)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
375
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
376 self.checkMediaType(request)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
377 d = self.parseXMLPayload(request.stream)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
378 d.addCallback(doPublish)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
379 d.addCallback(toResponse)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
380 d.addErrback(trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
381 d.addErrback(trapXMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
382 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
383
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
384
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
385 class SubscribeBaseResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
386 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
387 Base resource for remote pubsub node subscription and unsubscription.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
388
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
389 This resource accepts POST request with a JSON document that holds
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
390 a dictionary with the keys C{uri} and C{callback} that respectively map
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
391 to the XMPP URI of the publish-subscribe node and the callback URI.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
392
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
393 This class should be inherited with L{serviceMethod} overridden.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
394
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
395 @cvar serviceMethod: The name of the method to be called with
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
396 the JID of the pubsub service, the node identifier
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
397 and the callback URI as received in the HTTP POST
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
398 request to this resource.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
399 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
400 serviceMethod = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
401
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
402 def __init__(self, service):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
403 self.service = service
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
404 self.params = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
405
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
406 http_GET = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
407
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
408 def http_POST(self, request):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
409 def trapNotFound(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
410 failure.trap(error.NodeNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
411 return http.StatusResponse(responsecode.NOT_FOUND,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
412 "Node not found")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
413
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
414 def respond(result):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
415 return http.Response(responsecode.NO_CONTENT)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
416
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
417 def gotRequest(result):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
418 uri = self.params['uri']
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
419 callback = self.params['callback']
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
420
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
421 jid, nodeIdentifier = getServiceAndNode(uri)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
422 method = getattr(self.service, self.serviceMethod)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
423 d = method(jid, nodeIdentifier, callback)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
424 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
425
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
426 def storeParams(data):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
427 self.params = simplejson.loads(data)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
428
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
429 def trapXMPPURIParseError(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
430 failure.trap(XMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
431 return http.StatusResponse(responsecode.BAD_REQUEST,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
432 "Malformed XMPP URI: %s" % failure.value.message)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
433
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
434 d = readStream(request.stream, storeParams)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
435 d.addCallback(gotRequest)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
436 d.addCallback(respond)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
437 d.addErrback(trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
438 d.addErrback(trapXMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
439 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
440
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
441
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
442 class SubscribeResource(SubscribeBaseResource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
443 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
444 Resource to subscribe to a remote publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
445
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
446 The passed C{uri} is the XMPP URI of the node to subscribe to and the
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
447 C{callback} is the callback URI. Upon receiving notifications from the
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
448 node, a POST request will be perfomed on the callback URI.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
449 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
450 serviceMethod = 'subscribeCallback'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
451
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
452
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
453 class UnsubscribeResource(SubscribeBaseResource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
454 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
455 Resource to unsubscribe from a remote publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
456
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
457 The passed C{uri} is the XMPP URI of the node to unsubscribe from and the
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
458 C{callback} is the callback URI that was registered for it.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
459 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
460 serviceMethod = 'unsubscribeCallback'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
461
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
462
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
463 class ListResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
464 def __init__(self, service):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
465 self.service = service
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
466
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
467 def render(self, request):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
468 def responseFromNodes(nodeIdentifiers):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
469 import pprint
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
470 return http.Response(responsecode.OK, stream=pprint.pformat(nodeIdentifiers))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
471
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
472 d = self.service.get_nodes()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
473 d.addCallback(responseFromNodes)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
474 return d