Mercurial > libervia-pubsub
annotate sat_pubsub/gateway.py @ 232:923281d4c5bc
renamed idavoll directory to sat_pubsub
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 17 May 2012 12:48:14 +0200 |
parents | idavoll/gateway.py@55b45c7dccb4 |
children | 564ae55219e1 |
rev | line source |
---|---|
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
1 # -*- test-case-name: idavoll.test.test_gateway -*- |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
2 # |
211 | 3 # Copyright (c) 2003-2009 Ralph Meijer |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
4 # See LICENSE for details. |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
5 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
6 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
7 Web resources and client for interacting with pubsub services. |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
8 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
9 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
10 import cgi |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
11 from time import gmtime, strftime |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
12 import urllib |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
13 import urlparse |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
14 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
15 import simplejson |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
16 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
17 from twisted.application import service |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
18 from twisted.internet import defer, reactor |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
19 from twisted.python import log |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
20 from twisted.web import client |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
21 from twisted.web2 import http, http_headers, resource, responsecode |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
22 from twisted.web2 import channel, server |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
23 from twisted.web2.stream import readStream |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
24 from twisted.words.protocols.jabber.jid import JID |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber.error import StanzaError |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
26 from twisted.words.xish import domish |
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 from wokkel.pubsub import Item |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
29 from wokkel.pubsub import PubSubClient |
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 from idavoll import error |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
32 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
33 NS_ATOM = 'http://www.w3.org/2005/Atom' |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
34 MIME_ATOM_ENTRY = 'application/atom+xml;type=entry' |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
35 MIME_JSON = 'application/json' |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
36 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
37 class XMPPURIParseError(ValueError): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
38 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
39 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
|
40 """ |
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 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
43 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
44 def getServiceAndNode(uri): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
45 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
46 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
|
47 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
48 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
49 try: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
50 scheme, rest = uri.split(':', 1) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
51 except ValueError: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
52 raise XMPPURIParseError("No URI scheme component") |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
53 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
54 if scheme != 'xmpp': |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
55 raise XMPPURIParseError("Unknown URI scheme") |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
56 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
57 if rest.startswith("//"): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
58 raise XMPPURIParseError("Unexpected URI authority component") |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
59 |
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 entity, query = rest.split('?', 1) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
62 except ValueError: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
63 raise XMPPURIParseError("No URI query component") |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
64 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
65 if not entity: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
66 raise XMPPURIParseError("Empty URI path component") |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
67 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
68 try: |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
69 service = JID(entity) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
70 except Exception, e: |
210
2a0a6a671776
Fix error reporting for older Python versions.
Ralph Meijer <ralphm@ik.nu>
parents:
209
diff
changeset
|
71 raise XMPPURIParseError("Invalid JID: %s" % e) |
177
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 params = cgi.parse_qs(query) |
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 try: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
76 nodeIdentifier = params['node'][0] |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
77 except (KeyError, ValueError): |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
78 nodeIdentifier = '' |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
79 |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
80 return service, nodeIdentifier |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
81 |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
82 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
83 |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
84 def getXMPPURI(service, nodeIdentifier): |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
85 """ |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
86 Construct an XMPP URI from a service JID and node identifier. |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
87 """ |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
88 return "xmpp:%s?;node=%s" % (service.full(), nodeIdentifier or '') |
177
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 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
91 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
92 class WebStreamParser(object): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
93 def __init__(self): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
94 self.elementStream = domish.elementStream() |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
95 self.elementStream.DocumentStartEvent = self.docStart |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
96 self.elementStream.ElementEvent = self.elem |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
97 self.elementStream.DocumentEndEvent = self.docEnd |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
98 self.done = False |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
99 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
100 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
101 def docStart(self, elem): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
102 self.document = elem |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
103 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
104 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
105 def elem(self, elem): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
106 self.document.addChild(elem) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
107 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
108 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
109 def docEnd(self): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
110 self.done = True |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
111 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
112 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
113 def parse(self, stream): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
114 def endOfStream(result): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
115 if not self.done: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
116 raise Exception("No more stuff?") |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
117 else: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
118 return self.document |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
119 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
120 d = readStream(stream, self.elementStream.parse) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
121 d.addCallback(endOfStream) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
122 return d |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
123 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
124 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
125 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
126 class CreateResource(resource.Resource): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
127 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
128 A resource to create a publish-subscribe node. |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
129 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
130 def __init__(self, backend, serviceJID, owner): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
131 self.backend = backend |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
132 self.serviceJID = serviceJID |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
133 self.owner = owner |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
134 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
135 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
136 http_GET = None |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
137 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
138 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
139 def http_POST(self, request): |
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 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
|
142 """ |
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 toResponse(nodeIdentifier): |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
145 uri = getXMPPURI(self.serviceJID, nodeIdentifier) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
146 stream = simplejson.dumps({'uri': uri}) |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
147 contentType = http_headers.MimeType.fromString(MIME_JSON) |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
148 return http.Response(responsecode.OK, stream=stream, |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
149 headers={'Content-Type': contentType}) |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
150 d = self.backend.createNode(None, self.owner) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
151 d.addCallback(toResponse) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
152 return d |
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 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
155 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
156 class DeleteResource(resource.Resource): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
157 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
158 A resource to create a publish-subscribe node. |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
159 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
160 def __init__(self, backend, serviceJID, owner): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
161 self.backend = backend |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
162 self.serviceJID = serviceJID |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
163 self.owner = owner |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
164 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
165 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
166 http_GET = None |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
167 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
168 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
169 def http_POST(self, request): |
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 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
|
172 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
173 |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
174 def gotStream(_): |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
175 if request.args.get('uri'): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
176 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0]) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
177 return defer.succeed(nodeIdentifier) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
178 else: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
179 raise http.HTTPError(http.Response(responsecode.BAD_REQUEST, |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
180 "No URI given")) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
181 |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
182 def doDelete(nodeIdentifier, data): |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
183 if data: |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
184 params = simplejson.loads(''.join(data)) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
185 redirectURI = params.get('redirect_uri') |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
186 else: |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
187 redirectURI = None |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
188 |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
189 return self.backend.deleteNode(nodeIdentifier, self.owner, |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
190 redirectURI) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
191 |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
192 def respond(result): |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
193 return http.Response(responsecode.NO_CONTENT) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
194 |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
195 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
196 def trapNotFound(failure): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
197 failure.trap(error.NodeNotFound) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
198 return http.StatusResponse(responsecode.NOT_FOUND, |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
199 "Node not found") |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
200 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
201 def trapXMPPURIParseError(failure): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
202 failure.trap(XMPPURIParseError) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
203 return http.StatusResponse(responsecode.BAD_REQUEST, |
210
2a0a6a671776
Fix error reporting for older Python versions.
Ralph Meijer <ralphm@ik.nu>
parents:
209
diff
changeset
|
204 "Malformed XMPP URI: %s" % failure.value) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
205 |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
206 data = [] |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
207 d = readStream(request.stream, data.append) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
208 d.addCallback(gotStream) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
209 d.addCallback(doDelete, data) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
210 d.addCallback(respond) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
211 d.addErrback(trapNotFound) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
212 d.addErrback(trapXMPPURIParseError) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
213 return d |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
214 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
215 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
216 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
217 class PublishResource(resource.Resource): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
218 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
219 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
|
220 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
221 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
222 def __init__(self, backend, serviceJID, owner): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
223 self.backend = backend |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
224 self.serviceJID = serviceJID |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
225 self.owner = owner |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
226 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
227 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
228 http_GET = None |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
229 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
230 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
231 def checkMediaType(self, request): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
232 ctype = request.headers.getHeader('content-type') |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
233 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
234 if not ctype: |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
235 raise http.HTTPError( |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
236 http.StatusResponse( |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
237 responsecode.BAD_REQUEST, |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
238 "No specified Media Type")) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
239 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
240 if (ctype.mediaType != 'application' or |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
241 ctype.mediaSubtype != 'atom+xml' or |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
242 ctype.params.get('type') != 'entry' or |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
243 ctype.params.get('charset', 'utf-8') != 'utf-8'): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
244 raise http.HTTPError( |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
245 http.StatusResponse( |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
246 responsecode.UNSUPPORTED_MEDIA_TYPE, |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
247 "Unsupported Media Type: %s" % |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
248 http_headers.generateContentType(ctype))) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
249 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
250 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
251 def parseXMLPayload(self, stream): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
252 p = WebStreamParser() |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
253 return p.parse(stream) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
254 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
255 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
256 def http_POST(self, request): |
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 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
|
259 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
260 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
261 def toResponse(nodeIdentifier): |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
262 uri = getXMPPURI(self.serviceJID, nodeIdentifier) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
263 stream = simplejson.dumps({'uri': uri}) |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
264 contentType = http_headers.MimeType.fromString(MIME_JSON) |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
265 return http.Response(responsecode.OK, stream=stream, |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
266 headers={'Content-Type': contentType}) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
267 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
268 def gotNode(nodeIdentifier, payload): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
269 item = Item(id='current', payload=payload) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
270 d = self.backend.publish(nodeIdentifier, [item], self.owner) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
271 d.addCallback(lambda _: nodeIdentifier) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
272 return d |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
273 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
274 def getNode(): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
275 if request.args.get('uri'): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
276 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0]) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
277 return defer.succeed(nodeIdentifier) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
278 else: |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
279 return self.backend.createNode(None, self.owner) |
177
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 doPublish(payload): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
282 d = getNode() |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
283 d.addCallback(gotNode, payload) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
284 return d |
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 trapNotFound(failure): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
287 failure.trap(error.NodeNotFound) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
288 return http.StatusResponse(responsecode.NOT_FOUND, |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
289 "Node not found") |
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 def trapXMPPURIParseError(failure): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
292 failure.trap(XMPPURIParseError) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
293 return http.StatusResponse(responsecode.BAD_REQUEST, |
210
2a0a6a671776
Fix error reporting for older Python versions.
Ralph Meijer <ralphm@ik.nu>
parents:
209
diff
changeset
|
294 "Malformed XMPP URI: %s" % failure.value) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
295 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
296 self.checkMediaType(request) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
297 d = self.parseXMLPayload(request.stream) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
298 d.addCallback(doPublish) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
299 d.addCallback(toResponse) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
300 d.addErrback(trapNotFound) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
301 d.addErrback(trapXMPPURIParseError) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
302 return d |
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 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
305 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
306 class ListResource(resource.Resource): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
307 def __init__(self, service): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
308 self.service = service |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
309 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
310 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
311 def render(self, request): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
312 def responseFromNodes(nodeIdentifiers): |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
313 stream = simplejson.dumps(nodeIdentifiers) |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
314 contentType = http_headers.MimeType.fromString(MIME_JSON) |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
315 return http.Response(responsecode.OK, stream=stream, |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
316 headers={'Content-Type': contentType}) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
317 |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
318 d = self.service.getNodes() |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
319 d.addCallback(responseFromNodes) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
320 return d |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
321 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
322 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
323 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
324 # Service for subscribing to remote XMPP Pubsub nodes and web resources |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
325 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
326 def extractAtomEntries(items): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
327 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
328 Extract atom entries from a list of publish-subscribe items. |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
329 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
330 @param items: List of L{domish.Element}s that represent publish-subscribe |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
331 items. |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
332 @type items: C{list} |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
333 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
334 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
335 atomEntries = [] |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
336 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
337 for item in items: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
338 # ignore non-items (i.e. retractions) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
339 if item.name != 'item': |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
340 continue |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
341 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
342 atomEntry = None |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
343 for element in item.elements(): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
344 # extract the first element that is an atom entry |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
345 if element.uri == NS_ATOM and element.name == 'entry': |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
346 atomEntry = element |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
347 break |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
348 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
349 if atomEntry: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
350 atomEntries.append(atomEntry) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
351 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
352 return atomEntries |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
353 |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
354 |
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
355 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
356 def constructFeed(service, nodeIdentifier, entries, title): |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
357 nodeURI = getXMPPURI(service, nodeIdentifier) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
358 now = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
359 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
360 # Collect the received entries in a feed |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
361 feed = domish.Element((NS_ATOM, 'feed')) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
362 feed.addElement('title', content=title) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
363 feed.addElement('id', content=nodeURI) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
364 feed.addElement('updated', content=now) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
365 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
366 for entry in entries: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
367 feed.addChild(entry) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
368 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
369 return feed |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
370 |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
371 |
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
372 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
373 class RemoteSubscriptionService(service.Service, PubSubClient): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
374 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
375 Service for subscribing to remote XMPP Publish-Subscribe nodes. |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
376 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
377 Subscriptions are created with a callback HTTP URI that is POSTed |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
378 to with the received items in notifications. |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
379 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
380 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
381 def __init__(self, jid, storage): |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
382 self.jid = jid |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
383 self.storage = storage |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
384 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
385 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
386 def trapNotFound(self, failure): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
387 failure.trap(StanzaError) |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
388 |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
389 if failure.value.condition == 'item-not-found': |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
390 raise error.NodeNotFound() |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
391 else: |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
392 return failure |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
393 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
394 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
395 def subscribeCallback(self, jid, nodeIdentifier, callback): |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
396 """ |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
397 Subscribe a callback URI. |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
398 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
399 This registers a callback URI to be called when a notification is |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
400 received for the given node. |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
401 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
402 If this is the first callback registered for this node, the gateway |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
403 will subscribe to the node. Otherwise, the most recently published item |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
404 for this node is retrieved and, if present, the newly registered |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
405 callback will be called with that item. |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
406 """ |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
407 |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
408 def callbackForLastItem(items): |
187
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
409 atomEntries = extractAtomEntries(items) |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
410 |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
411 if not atomEntries: |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
412 return |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
413 |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
414 self._postTo([callback], jid, nodeIdentifier, atomEntries[0], |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
415 'application/atom+xml;type=entry') |
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
416 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
417 def subscribeOrItems(hasCallbacks): |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
418 if hasCallbacks: |
224
55b45c7dccb4
Upon gateway subscription to the root node, don't retrieve items.
Ralph Meijer <ralphm@ik.nu>
parents:
214
diff
changeset
|
419 if not nodeIdentifier: |
55b45c7dccb4
Upon gateway subscription to the root node, don't retrieve items.
Ralph Meijer <ralphm@ik.nu>
parents:
214
diff
changeset
|
420 return None |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
421 d = self.items(jid, nodeIdentifier, 1) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
422 d.addCallback(callbackForLastItem) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
423 else: |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
424 d = self.subscribe(jid, nodeIdentifier, self.jid) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
425 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
426 d.addErrback(self.trapNotFound) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
427 return d |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
428 |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
429 d = self.storage.hasCallbacks(jid, nodeIdentifier) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
430 d.addCallback(subscribeOrItems) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
431 d.addCallback(lambda _: self.storage.addCallback(jid, nodeIdentifier, |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
432 callback)) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
433 return d |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
434 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
435 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
436 def unsubscribeCallback(self, jid, nodeIdentifier, callback): |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
437 """ |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
438 Unsubscribe a callback. |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
439 |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
440 If this was the last registered callback for this node, the |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
441 gateway will unsubscribe from node. |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
442 """ |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
443 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
444 def cb(last): |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
445 if last: |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
446 return self.unsubscribe(jid, nodeIdentifier, self.jid) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
447 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
448 d = self.storage.removeCallback(jid, nodeIdentifier, callback) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
449 d.addCallback(cb) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
450 return d |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
451 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
452 |
203
2c46e6664680
Match wokkel API change for PubSubClient.
Ralph Meijer <ralphm@ik.nu>
parents:
198
diff
changeset
|
453 def itemsReceived(self, event): |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
454 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
455 Fire up HTTP client to do callback |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
456 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
457 |
203
2c46e6664680
Match wokkel API change for PubSubClient.
Ralph Meijer <ralphm@ik.nu>
parents:
198
diff
changeset
|
458 atomEntries = extractAtomEntries(event.items) |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
459 service = event.sender |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
460 nodeIdentifier = event.nodeIdentifier |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
461 headers = event.headers |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
462 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
463 # Don't notify if there are no atom entries |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
464 if not atomEntries: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
465 return |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
466 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
467 if len(atomEntries) == 1: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
468 contentType = 'application/atom+xml;type=entry' |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
469 payload = atomEntries[0] |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
470 else: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
471 contentType = 'application/atom+xml;type=feed' |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
472 payload = constructFeed(service, nodeIdentifier, atomEntries, |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
473 title='Received item collection') |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
474 |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
475 self.callCallbacks(service, nodeIdentifier, payload, contentType) |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
476 |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
477 if 'Collection' in headers: |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
478 for collection in headers['Collection']: |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
479 nodeIdentifier = collection or '' |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
480 self.callCallbacks(service, nodeIdentifier, payload, |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
481 contentType) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
482 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
483 |
203
2c46e6664680
Match wokkel API change for PubSubClient.
Ralph Meijer <ralphm@ik.nu>
parents:
198
diff
changeset
|
484 def deleteReceived(self, event): |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
485 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
486 Fire up HTTP client to do callback |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
487 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
488 |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
489 service = event.sender |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
490 nodeIdentifier = event.nodeIdentifier |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
491 redirectURI = event.redirectURI |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
492 self.callCallbacks(service, nodeIdentifier, eventType='DELETED', |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
493 redirectURI=redirectURI) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
494 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
495 |
187
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
496 def _postTo(self, callbacks, service, nodeIdentifier, |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
497 payload=None, contentType=None, eventType=None, |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
498 redirectURI=None): |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
499 |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
500 if not callbacks: |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
501 return |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
502 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
503 postdata = None |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
504 nodeURI = getXMPPURI(service, nodeIdentifier) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
505 headers = {'Referer': nodeURI.encode('utf-8'), |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
506 'PubSub-Service': service.full().encode('utf-8')} |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
507 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
508 if payload: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
509 postdata = payload.toXml().encode('utf-8') |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
510 if contentType: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
511 headers['Content-Type'] = "%s;charset=utf-8" % contentType |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
512 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
513 if eventType: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
514 headers['Event'] = eventType |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
515 |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
516 if redirectURI: |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
517 headers['Link'] = '<%s>; rel=alternate' % ( |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
518 redirectURI.encode('utf-8'), |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
519 ) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
520 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
521 def postNotification(callbackURI): |
214
3c45208678fa
Make 204 responses not result in failure.
Ralph Meijer <ralphm@ik.nu>
parents:
211
diff
changeset
|
522 f = getPageWithFactory(str(callbackURI), |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
523 method='POST', |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
524 postdata=postdata, |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
525 headers=headers) |
214
3c45208678fa
Make 204 responses not result in failure.
Ralph Meijer <ralphm@ik.nu>
parents:
211
diff
changeset
|
526 d = f.deferred |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
527 d.addErrback(log.err) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
528 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
529 for callbackURI in callbacks: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
530 reactor.callLater(0, postNotification, callbackURI) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
531 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
532 |
187
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
533 def callCallbacks(self, service, nodeIdentifier, |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
534 payload=None, contentType=None, eventType=None, |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
535 redirectURI=None): |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
536 |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
537 def eb(failure): |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
538 failure.trap(error.NoCallbacks) |
187
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
539 |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
540 # No callbacks were registered for this node. Unsubscribe? |
187
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
541 |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
542 d = self.storage.getCallbacks(service, nodeIdentifier) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
543 d.addCallback(self._postTo, service, nodeIdentifier, payload, |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
544 contentType, eventType, redirectURI) |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
545 d.addErrback(eb) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
203
diff
changeset
|
546 d.addErrback(log.err) |
187
69cdd8c6a431
Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents:
186
diff
changeset
|
547 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
548 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
549 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
550 class RemoteSubscribeBaseResource(resource.Resource): |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
551 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
552 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
|
553 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
554 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
|
555 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
|
556 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
|
557 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
558 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
|
559 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
560 @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
|
561 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
|
562 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
|
563 request to this resource. |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
564 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
565 serviceMethod = None |
191
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
566 errorMap = { |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
567 error.NodeNotFound: |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
568 (responsecode.FORBIDDEN, "Node not found"), |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
569 error.NotSubscribed: |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
570 (responsecode.FORBIDDEN, "No such subscription found"), |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
571 error.SubscriptionExists: |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
572 (responsecode.FORBIDDEN, "Subscription already exists"), |
191
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
573 } |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
574 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
575 def __init__(self, service): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
576 self.service = service |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
577 self.params = None |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
578 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
579 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
580 http_GET = None |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
581 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
582 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
583 def http_POST(self, request): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
584 def trapNotFound(failure): |
191
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
585 err = failure.trap(*self.errorMap.keys()) |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
586 code, msg = self.errorMap[err] |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
587 return http.StatusResponse(code, msg) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
588 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
589 def respond(result): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
590 return http.Response(responsecode.NO_CONTENT) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
591 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
592 def gotRequest(result): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
593 uri = self.params['uri'] |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
594 callback = self.params['callback'] |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
595 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
596 jid, nodeIdentifier = getServiceAndNode(uri) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
597 method = getattr(self.service, self.serviceMethod) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
598 d = method(jid, nodeIdentifier, callback) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
599 return d |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
600 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
601 def storeParams(data): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
602 self.params = simplejson.loads(data) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
603 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
604 def trapXMPPURIParseError(failure): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
605 failure.trap(XMPPURIParseError) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
606 return http.StatusResponse(responsecode.BAD_REQUEST, |
210
2a0a6a671776
Fix error reporting for older Python versions.
Ralph Meijer <ralphm@ik.nu>
parents:
209
diff
changeset
|
607 "Malformed XMPP URI: %s" % failure.value) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
608 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
609 d = readStream(request.stream, storeParams) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
610 d.addCallback(gotRequest) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
611 d.addCallback(respond) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
612 d.addErrback(trapNotFound) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
613 d.addErrback(trapXMPPURIParseError) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
614 return d |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
615 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
616 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
617 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
618 class RemoteSubscribeResource(RemoteSubscribeBaseResource): |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
619 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
620 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
|
621 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
622 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
|
623 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
|
624 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
|
625 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
626 serviceMethod = 'subscribeCallback' |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
627 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
628 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
629 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
630 class RemoteUnsubscribeResource(RemoteSubscribeBaseResource): |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
631 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
632 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
|
633 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
634 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
|
635 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
|
636 """ |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
637 serviceMethod = 'unsubscribeCallback' |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
638 |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
639 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
640 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
641 class RemoteItemsResource(resource.Resource): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
642 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
643 Resource for retrieving items from a remote pubsub node. |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
644 """ |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
645 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
646 def __init__(self, service): |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
647 self.service = service |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
648 |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
649 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
650 def render(self, request): |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
651 try: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
652 maxItems = int(request.args.get('max_items', [0])[0]) or None |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
653 except ValueError: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
654 return http.StatusResponse(responsecode.BAD_REQUEST, |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
655 "The argument max_items has an invalid value.") |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
656 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
657 try: |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
658 uri = request.args['uri'][0] |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
659 except KeyError: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
660 return http.StatusResponse(responsecode.BAD_REQUEST, |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
661 "No URI for the remote node provided.") |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
662 |
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
663 try: |
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
664 jid, nodeIdentifier = getServiceAndNode(uri) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
665 except XMPPURIParseError: |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
666 return http.StatusResponse(responsecode.BAD_REQUEST, |
198
e404775b12df
Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents:
191
diff
changeset
|
667 "Malformed XMPP URI: %s" % uri) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
668 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
669 def respond(items): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
670 """Create a feed out the retrieved items.""" |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
671 contentType = http_headers.MimeType('application', |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
672 'atom+xml', |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
673 {'type': 'feed'}) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
674 atomEntries = extractAtomEntries(items) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
675 feed = constructFeed(jid, nodeIdentifier, atomEntries, |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
676 "Retrieved item collection") |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
677 payload = feed.toXml().encode('utf-8') |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
678 return http.Response(responsecode.OK, stream=payload, |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
679 headers={'Content-Type': contentType}) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
680 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
681 def trapNotFound(failure): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
682 failure.trap(StanzaError) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
683 if not failure.value.condition == 'item-not-found': |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
684 raise failure |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
685 return http.StatusResponse(responsecode.NOT_FOUND, |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
686 "Node not found") |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
687 |
186 | 688 d = self.service.items(jid, nodeIdentifier, maxItems) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
689 d.addCallback(respond) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
690 d.addErrback(trapNotFound) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff
changeset
|
691 return d |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
692 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
693 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
694 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
695 # Client side code to interact with a service as provided above |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
696 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
697 def getPageWithFactory(url, contextFactory=None, *args, **kwargs): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
698 """Download a web page. |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
699 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
700 Download a page. Return the factory that holds a deferred, which will |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
701 callback with a page (as a string) or errback with a description of the |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
702 error. |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
703 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
704 See HTTPClientFactory to see what extra args can be passed. |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
705 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
706 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
707 scheme, host, port, path = client._parse(url) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
708 factory = client.HTTPClientFactory(url, *args, **kwargs) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
709 factory.protocol.handleStatus_204 = lambda self: self.handleStatus_200() |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
710 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
711 if scheme == 'https': |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
712 from twisted.internet import ssl |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
713 if contextFactory is None: |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
714 contextFactory = ssl.ClientContextFactory() |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
715 reactor.connectSSL(host, port, factory, contextFactory) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
716 else: |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
717 reactor.connectTCP(host, port, factory) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
718 return factory |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
719 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
720 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
721 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
722 class CallbackResource(resource.Resource): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
723 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
724 Web resource for retrieving gateway notifications. |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
725 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
726 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
727 def __init__(self, callback): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
728 self.callback = callback |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
729 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
730 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
731 http_GET = None |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
732 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
733 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
734 def http_POST(self, request): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
735 p = WebStreamParser() |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
736 if not request.headers.hasHeader('Event'): |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
737 d = p.parse(request.stream) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
738 else: |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
739 d = defer.succeed(None) |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
740 d.addCallback(self.callback, request.headers) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
741 d.addCallback(lambda _: http.Response(responsecode.NO_CONTENT)) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
742 return d |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
743 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
744 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
745 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
746 class GatewayClient(service.Service): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
747 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
748 Service that provides client access to the HTTP Gateway into Idavoll. |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
749 """ |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
750 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
751 agent = "Idavoll HTTP Gateway Client" |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
752 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
753 def __init__(self, baseURI, callbackHost=None, callbackPort=None): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
754 self.baseURI = baseURI |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
755 self.callbackHost = callbackHost or 'localhost' |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
756 self.callbackPort = callbackPort or 8087 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
757 root = resource.Resource() |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
758 root.child_callback = CallbackResource(lambda *args, **kwargs: self.callback(*args, **kwargs)) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
759 self.site = server.Site(root) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
760 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
761 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
762 def startService(self): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
763 self.port = reactor.listenTCP(self.callbackPort, |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
764 channel.HTTPFactory(self.site)) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
765 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
766 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
767 def stopService(self): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
768 return self.port.stopListening() |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
769 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
770 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
771 def _makeURI(self, verb, query=None): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
772 uriComponents = urlparse.urlparse(self.baseURI) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
773 uri = urlparse.urlunparse((uriComponents[0], |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
774 uriComponents[1], |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
775 uriComponents[2] + verb, |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
776 '', |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
777 query and urllib.urlencode(query) or '', |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
778 '')) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
779 return uri |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
780 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
781 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
782 def callback(self, data, headers): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
783 pass |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
784 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
785 |
206
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
786 def ping(self): |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
787 f = getPageWithFactory(self._makeURI(''), |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
788 method='HEAD', |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
789 agent=self.agent) |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
790 return f.deferred |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
791 |
274a45d2a5ab
Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents:
204
diff
changeset
|
792 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
793 def create(self): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
794 f = getPageWithFactory(self._makeURI('create'), |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
795 method='POST', |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
796 agent=self.agent) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
797 return f.deferred.addCallback(simplejson.loads) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
798 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
799 |
209
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
800 def delete(self, xmppURI, redirectURI=None): |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
801 query = {'uri': xmppURI} |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
802 |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
803 if redirectURI: |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
804 params = {'redirect_uri': redirectURI} |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
805 postdata = simplejson.dumps(params) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
806 headers = {'Content-Type': MIME_JSON} |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
807 else: |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
808 postdata = None |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
809 headers = None |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
810 |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
811 f = getPageWithFactory(self._makeURI('delete', query), |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
812 method='POST', |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
813 postdata=postdata, |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
814 headers=headers, |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
815 agent=self.agent) |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
816 return f.deferred |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
817 |
7f3ffb7a1a9e
Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents:
206
diff
changeset
|
818 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
819 def publish(self, entry, xmppURI=None): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
820 query = xmppURI and {'uri': xmppURI} |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
821 |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
822 f = getPageWithFactory(self._makeURI('publish', query), |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
823 method='POST', |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
824 postdata=entry.toXml().encode('utf-8'), |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
825 headers={'Content-Type': MIME_ATOM_ENTRY}, |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
826 agent=self.agent) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
827 return f.deferred.addCallback(simplejson.loads) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
828 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
829 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
830 def listNodes(self): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
831 f = getPageWithFactory(self._makeURI('list'), |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
832 method='GET', |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
833 agent=self.agent) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
834 return f.deferred.addCallback(simplejson.loads) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
835 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
836 |
183
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
837 def subscribe(self, xmppURI): |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
838 params = {'uri': xmppURI, |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
839 'callback': 'http://%s:%s/callback' % (self.callbackHost, |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
840 self.callbackPort)} |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
841 f = getPageWithFactory(self._makeURI('subscribe'), |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
842 method='POST', |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
843 postdata=simplejson.dumps(params), |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
844 headers={'Content-Type': MIME_JSON}, |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
845 agent=self.agent) |
c21b986cff30
Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents:
177
diff
changeset
|
846 return f.deferred |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
847 |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
848 |
191
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
849 def unsubscribe(self, xmppURI): |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
850 params = {'uri': xmppURI, |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
851 'callback': 'http://%s:%s/callback' % (self.callbackHost, |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
852 self.callbackPort)} |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
853 f = getPageWithFactory(self._makeURI('unsubscribe'), |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
854 method='POST', |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
855 postdata=simplejson.dumps(params), |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
856 headers={'Content-Type': MIME_JSON}, |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
857 agent=self.agent) |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
858 return f.deferred |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
859 |
48245777acea
Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents:
187
diff
changeset
|
860 |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
861 def items(self, xmppURI, maxItems=None): |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
862 query = {'uri': xmppURI} |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
863 if maxItems: |
186 | 864 query['max_items'] = int(maxItems) |
185
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
865 f = getPageWithFactory(self._makeURI('items', query), |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
866 method='GET', |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
867 agent=self.agent) |
9038908dc2f5
Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents:
183
diff
changeset
|
868 return f.deferred |