annotate sat_pubsub/gateway.py @ 313:e8e1ca94fd8c

removed reference to sat.tmp.delay as it is not used anymore
author Goffi <goffi@goffi.org>
date Sun, 03 Jan 2016 18:33:20 +0100
parents 5d7c3787672e
children 9fbb31ce495b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
1 #!/usr/bin/python
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
2 #-*- coding: utf-8 -*-
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
3
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
4 # Copyright (c) 2003-2011 Ralph Meijer
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
5 # Copyright (c) 2012-2016 Jérôme Poisson
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
6
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
7
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
8 # This program is free software: you can redistribute it and/or modify
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
11 # (at your option) any later version.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
12
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
13 # This program is distributed in the hope that it will be useful,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
16 # GNU Affero General Public License for more details.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
17
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
20 # --
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
21
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
22 # This program is based on Idavoll (http://idavoll.ik.nu/),
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
23 # originaly written by Ralph Meijer (http://ralphm.net/blog/)
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
24 # It is sublicensed under AGPL v3 (or any later version) as allowed by the original
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
25 # license.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
26
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
27 # --
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
28
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
29 # Here is a copy of the original license:
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
30
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
31 # Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
32
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
33 # Permission is hereby granted, free of charge, to any person obtaining
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
34 # a copy of this software and associated documentation files (the
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
35 # "Software"), to deal in the Software without restriction, including
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
36 # without limitation the rights to use, copy, modify, merge, publish,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
37 # distribute, sublicense, and/or sell copies of the Software, and to
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
38 # permit persons to whom the Software is furnished to do so, subject to
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
39 # the following conditions:
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
40
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
41 # The above copyright notice and this permission notice shall be
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
42 # included in all copies or substantial portions of the Software.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
43
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
44 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
45 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
46 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
47 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
48 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
49 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
50 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
51
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
53 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
54 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
55 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
56
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
57 import mimetools
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 from time import gmtime, strftime
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
59 from StringIO import StringIO
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
60 import urllib
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
61 import urlparse
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 import simplejson
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 from twisted.application import service
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 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
67 from twisted.python import log
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
68 from twisted.web import client, http, resource, server
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
69 from twisted.web.error import Error
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 from twisted.words.protocols.jabber.jid import JID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71 from twisted.words.protocols.jabber.error import StanzaError
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 from twisted.words.xish import domish
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
74 from wokkel.generic import parseXml
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 from wokkel.pubsub import Item
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 from wokkel.pubsub import PubSubClient
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77
234
51af2ed8bd50 replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents: 233
diff changeset
78 from sat_pubsub import error
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 NS_ATOM = 'http://www.w3.org/2005/Atom'
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
81 MIME_ATOM_ENTRY = b'application/atom+xml;type=entry'
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
82 MIME_ATOM_FEED = b'application/atom+xml;type=feed'
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
83 MIME_JSON = b'application/json'
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 class XMPPURIParseError(ValueError):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 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
88 """
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
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92 def getServiceAndNode(uri):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 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
95 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
97 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
98 scheme, rest = uri.split(':', 1)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 except ValueError:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100 raise XMPPURIParseError("No URI scheme component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102 if scheme != 'xmpp':
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 raise XMPPURIParseError("Unknown URI scheme")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105 if rest.startswith("//"):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
106 raise XMPPURIParseError("Unexpected URI authority component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
109 entity, query = rest.split('?', 1)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
110 except ValueError:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
111 entity, query = rest, ''
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
112
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
113 if not entity:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
114 raise XMPPURIParseError("Empty URI path component")
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
115
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
116 try:
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
117 service = JID(entity)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 except Exception, e:
210
2a0a6a671776 Fix error reporting for older Python versions.
Ralph Meijer <ralphm@ik.nu>
parents: 209
diff changeset
119 raise XMPPURIParseError("Invalid JID: %s" % e)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
121 params = urlparse.parse_qs(query)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123 try:
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124 nodeIdentifier = params['node'][0]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
125 except (KeyError, ValueError):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
126 nodeIdentifier = ''
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
127
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
128 return service, nodeIdentifier
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
129
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
130
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
132 def getXMPPURI(service, nodeIdentifier):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
133 """
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
134 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
135 """
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
136 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
137
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
138
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
139
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
140 def _parseContentType(header):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
141 """
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
142 Parse a Content-Type header value to a L{mimetools.Message}.
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
143
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
144 L{mimetools.Message} parses a Content-Type header and makes the
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
145 components available with its C{getmaintype}, C{getsubtype}, C{gettype},
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
146 C{getplist} and C{getparam} methods.
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
147 """
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
148 return mimetools.Message(StringIO(b'Content-Type: ' + header))
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
149
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
150
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
151
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
152 def _asyncResponse(render):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
153 """
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
154 """
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
155 def wrapped(self, request):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
156 def eb(failure):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
157 if failure.check(Error):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
158 err = failure.value
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
159 else:
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
160 log.err(failure)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
161 err = Error(500)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
162 request.setResponseCode(err.status, err.message)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
163 return err.response
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
164
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
165 def finish(result):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
166 if result is server.NOT_DONE_YET:
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
167 return
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
168
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
169 if result:
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
170 request.write(result)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
171 request.finish()
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
172
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
173 d = defer.maybeDeferred(render, self, request)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
174 d.addErrback(eb)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
175 d.addCallback(finish)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
176
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
177 return server.NOT_DONE_YET
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
178
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
179 return wrapped
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
180
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
181
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
182
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
183 class CreateResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
184 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
185 A resource to create a publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
186 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
187 def __init__(self, backend, serviceJID, owner):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
188 self.backend = backend
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
189 self.serviceJID = serviceJID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
190 self.owner = owner
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
191
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
192
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
193 http_GET = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
194
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
195
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
196 @_asyncResponse
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
197 def render_POST(self, request):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
198 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
199 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
200 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
201
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
202 def toResponse(nodeIdentifier):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
203 uri = getXMPPURI(self.serviceJID, nodeIdentifier)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
204 body = simplejson.dumps({'uri': uri})
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
205 request.setHeader(b'Content-Type', MIME_JSON)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
206 return body
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
207
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
208 d = self.backend.createNode(None, self.owner)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
209 d.addCallback(toResponse)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
210 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
211
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
212
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
213
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
214 class DeleteResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
215 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
216 A resource to create a publish-subscribe node.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
217 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
218 def __init__(self, backend, serviceJID, owner):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
219 self.backend = backend
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
220 self.serviceJID = serviceJID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
221 self.owner = owner
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
222
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
223
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
224 render_GET = None
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
225
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
226
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
227 @_asyncResponse
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
228 def render_POST(self, request):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
229 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
230 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
231 """
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
232 def toResponse(result):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
233 request.setResponseCode(http.NO_CONTENT)
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
234
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
235 def trapNotFound(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
236 failure.trap(error.NodeNotFound)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
237 raise Error(http.NOT_FOUND, "Node not found")
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
238
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
239 if not request.args.get('uri'):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
240 raise Error(http.BAD_REQUEST, "No URI given")
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
241
274
6641ea7990ee Fix checking for malformed XMPP URI in DeleteResource.
Ralph Meijer <ralphm@ik.nu>
parents: 273
diff changeset
242 try:
6641ea7990ee Fix checking for malformed XMPP URI in DeleteResource.
Ralph Meijer <ralphm@ik.nu>
parents: 273
diff changeset
243 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0])
6641ea7990ee Fix checking for malformed XMPP URI in DeleteResource.
Ralph Meijer <ralphm@ik.nu>
parents: 273
diff changeset
244 except XMPPURIParseError, e:
6641ea7990ee Fix checking for malformed XMPP URI in DeleteResource.
Ralph Meijer <ralphm@ik.nu>
parents: 273
diff changeset
245 raise Error(http.BAD_REQUEST, "Malformed XMPP URI: %s" % e)
6641ea7990ee Fix checking for malformed XMPP URI in DeleteResource.
Ralph Meijer <ralphm@ik.nu>
parents: 273
diff changeset
246
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
247
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
248 data = request.content.read()
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
249 if data:
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
250 params = simplejson.loads(data)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
251 redirectURI = params.get('redirect_uri', None)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
252 else:
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
253 redirectURI = None
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
254
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
255 d = self.backend.deleteNode(nodeIdentifier, self.owner,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
256 redirectURI)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
257 d.addCallback(toResponse)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
258 d.addErrback(trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
259 return d
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
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
262
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
263 class PublishResource(resource.Resource):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
264 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
265 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
266 """
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 __init__(self, backend, serviceJID, owner):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
269 self.backend = backend
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
270 self.serviceJID = serviceJID
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
271 self.owner = owner
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
272
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
273
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
274 render_GET = None
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
275
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
276
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
277 def checkMediaType(self, request):
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
278 ctype = request.getHeader(b'content-type')
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
279
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
280 if not ctype:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
281 request.setResponseCode(http.BAD_REQUEST)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
282
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
283 raise Error(http.BAD_REQUEST, b"No specified Media Type")
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
284
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
285 message = _parseContentType(ctype)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
286 if (message.maintype != b'application' or
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
287 message.subtype != b'atom+xml' or
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
288 message.getparam(b'type') != b'entry' or
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
289 (message.getparam(b'charset') or b'utf-8') != b'utf-8'):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
290 raise Error(http.UNSUPPORTED_MEDIA_TYPE,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
291 b"Unsupported Media Type: %s" % ctype)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
292
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
293
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
294 @_asyncResponse
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
295 def render_POST(self, request):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
296 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
297 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
298 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
299
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
300 def toResponse(nodeIdentifier):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
301 uri = getXMPPURI(self.serviceJID, nodeIdentifier)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
302 body = simplejson.dumps({'uri': uri})
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
303 request.setHeader(b'Content-Type', MIME_JSON)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
304 return body
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
305
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
306 def gotNode(nodeIdentifier, payload):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
307 item = Item(id='current', payload=payload)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
308 d = self.backend.publish(nodeIdentifier, [item], self.owner)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
309 d.addCallback(lambda _: nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
310 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
311
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
312 def getNode():
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
313 if request.args.get('uri'):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
314 jid, nodeIdentifier = getServiceAndNode(request.args['uri'][0])
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
315 return defer.succeed(nodeIdentifier)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
316 else:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
317 return self.backend.createNode(None, self.owner)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
318
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
319 def trapNotFound(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
320 failure.trap(error.NodeNotFound)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
321 raise Error(http.NOT_FOUND, "Node not found")
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
322
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
323 def trapXMPPURIParseError(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
324 failure.trap(XMPPURIParseError)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
325 raise Error(http.BAD_REQUEST,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
326 "Malformed XMPP URI: %s" % failure.value)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
327
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
328 self.checkMediaType(request)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
329 payload = parseXml(request.content.read())
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
330 d = getNode()
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
331 d.addCallback(gotNode, payload)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
332 d.addCallback(toResponse)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
333 d.addErrback(trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
334 d.addErrback(trapXMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
335 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
336
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
337
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
338
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
339 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
340 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
341 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
342
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
343
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
344 @_asyncResponse
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
345 def render_GET(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
346 def responseFromNodes(nodeIdentifiers):
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
347 body = simplejson.dumps(nodeIdentifiers)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
348 request.setHeader(b'Content-Type', MIME_JSON)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
349 return body
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
350
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
351 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
352 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
353 return d
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
354
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
355
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
356
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
357 # 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
358
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
359 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
360 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
361 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
362
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
363 @param items: List of L{domish.Element}s that represent publish-subscribe
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
364 items.
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
365 @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
366 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
367
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
368 atomEntries = []
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
369
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
370 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
371 # 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
372 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
373 continue
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 atomEntry = None
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
376 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
377 # 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
378 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
379 atomEntry = element
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
380 break
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
381
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
382 if atomEntry:
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
383 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
384
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
385 return atomEntries
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
386
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
387
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
388
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
389 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
390 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
391 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
392
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
393 # 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
394 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
395 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
396 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
397 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
398
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
399 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
400 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
401
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
402 return feed
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
403
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
404
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
405
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
406 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
407 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
408 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
409
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
410 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
411 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
412 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
413
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
414 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
415 self.jid = jid
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
416 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
417
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
418
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
419 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
420 failure.trap(StanzaError)
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
421
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
422 if failure.value.condition == 'item-not-found':
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
423 raise error.NodeNotFound()
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
424 else:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
425 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
426
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
427
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
428 def subscribeCallback(self, jid, nodeIdentifier, callback):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
429 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
430 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
431
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
432 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
433 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
434
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
435 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
436 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
437 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
438 callback will be called with that item.
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
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
441 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
442 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
443
69cdd8c6a431 Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents: 186
diff changeset
444 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
445 return
69cdd8c6a431 Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents: 186
diff changeset
446
69cdd8c6a431 Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents: 186
diff changeset
447 self._postTo([callback], jid, nodeIdentifier, atomEntries[0],
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
448 MIME_ATOM_ENTRY)
187
69cdd8c6a431 Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents: 186
diff changeset
449
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
450 def subscribeOrItems(hasCallbacks):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
451 if hasCallbacks:
224
55b45c7dccb4 Upon gateway subscription to the root node, don't retrieve items.
Ralph Meijer <ralphm@ik.nu>
parents: 214
diff changeset
452 if not nodeIdentifier:
55b45c7dccb4 Upon gateway subscription to the root node, don't retrieve items.
Ralph Meijer <ralphm@ik.nu>
parents: 214
diff changeset
453 return None
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
454 d = self.items(jid, nodeIdentifier, 1)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
455 d.addCallback(callbackForLastItem)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
456 else:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
457 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
458
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
459 d.addErrback(self.trapNotFound)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
460 return d
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
461
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
462 d = self.storage.hasCallbacks(jid, nodeIdentifier)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
463 d.addCallback(subscribeOrItems)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
464 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
465 callback))
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
466 return d
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
467
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
468
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
469 def unsubscribeCallback(self, jid, nodeIdentifier, callback):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
470 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
471 Unsubscribe a callback.
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
472
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
473 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
474 gateway will unsubscribe from node.
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
475 """
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
476
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
477 def cb(last):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
478 if last:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
479 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
480
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
481 d = self.storage.removeCallback(jid, nodeIdentifier, callback)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
482 d.addCallback(cb)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
483 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
484
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
485
203
2c46e6664680 Match wokkel API change for PubSubClient.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
486 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
487 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
488 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
489 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
490
203
2c46e6664680 Match wokkel API change for PubSubClient.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
491 atomEntries = extractAtomEntries(event.items)
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
492 service = event.sender
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
493 nodeIdentifier = event.nodeIdentifier
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
494 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
495
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
496 # 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
497 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
498 return
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
499
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
500 if len(atomEntries) == 1:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
501 contentType = MIME_ATOM_ENTRY
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
502 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
503 else:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
504 contentType = MIME_ATOM_FEED
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
505 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
506 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
507
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
508 self.callCallbacks(service, nodeIdentifier, payload, contentType)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
509
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
510 if 'Collection' in headers:
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
511 for collection in headers['Collection']:
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
512 nodeIdentifier = collection or ''
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
513 self.callCallbacks(service, nodeIdentifier, payload,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
514 contentType)
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
515
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
516
203
2c46e6664680 Match wokkel API change for PubSubClient.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
517 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
518 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
519 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
520 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
521
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
522 service = event.sender
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
523 nodeIdentifier = event.nodeIdentifier
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
524 redirectURI = event.redirectURI
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
525 self.callCallbacks(service, nodeIdentifier, eventType='DELETED',
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
526 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
527
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
528
187
69cdd8c6a431 Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents: 186
diff changeset
529 def _postTo(self, callbacks, service, nodeIdentifier,
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
530 payload=None, contentType=None, eventType=None,
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
531 redirectURI=None):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
532
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
533 if not callbacks:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
534 return
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
535
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
536 postdata = None
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
537 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
538 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
539 '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
540
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
541 if payload:
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
542 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
543 if contentType:
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
544 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
545
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
546 if eventType:
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
547 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
548
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
549 if redirectURI:
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
550 headers['Link'] = '<%s>; rel=alternate' % (
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
551 redirectURI.encode('utf-8'),
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
552 )
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
553
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
554 def postNotification(callbackURI):
214
3c45208678fa Make 204 responses not result in failure.
Ralph Meijer <ralphm@ik.nu>
parents: 211
diff changeset
555 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
556 method='POST',
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
557 postdata=postdata,
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
558 headers=headers)
214
3c45208678fa Make 204 responses not result in failure.
Ralph Meijer <ralphm@ik.nu>
parents: 211
diff changeset
559 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
560 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
561
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
562 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
563 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
564
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
565
187
69cdd8c6a431 Make sure second subscribers through HTTP also get a notification of the
Ralph Meijer <ralphm@ik.nu>
parents: 186
diff changeset
566 def callCallbacks(self, service, nodeIdentifier,
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
567 payload=None, contentType=None, eventType=None,
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
568 redirectURI=None):
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
569
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
570 def eb(failure):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
571 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
572
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
573 # 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
574
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
575 d = self.storage.getCallbacks(service, nodeIdentifier)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
576 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
577 contentType, eventType, redirectURI)
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
578 d.addErrback(eb)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 203
diff changeset
579 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
580
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
581
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
582
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
583 class RemoteSubscribeBaseResource(resource.Resource):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
584 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
585 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
586
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
587 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
588 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
589 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
590
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
591 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
592
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
593 @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
594 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
595 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
596 request to this resource.
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
597 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
598 serviceMethod = None
191
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
599 errorMap = {
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
600 error.NodeNotFound:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
601 (http.FORBIDDEN, "Node not found"),
191
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
602 error.NotSubscribed:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
603 (http.FORBIDDEN, "No such subscription found"),
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
604 error.SubscriptionExists:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
605 (http.FORBIDDEN, "Subscription already exists"),
191
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
606 }
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
607
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
608 def __init__(self, service):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
609 self.service = service
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
610 self.params = None
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
611
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
612
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
613 render_GET = None
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
614
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
615
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
616 @_asyncResponse
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
617 def render_POST(self, request):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
618 def trapNotFound(failure):
191
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
619 err = failure.trap(*self.errorMap.keys())
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
620 status, message = self.errorMap[err]
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
621 raise Error(status, message)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
622
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
623 def toResponse(result):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
624 request.setResponseCode(http.NO_CONTENT)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
625 return b''
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
626
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
627 def trapXMPPURIParseError(failure):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
628 failure.trap(XMPPURIParseError)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
629 raise Error(http.BAD_REQUEST,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
630 "Malformed XMPP URI: %s" % failure.value)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
631
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
632 data = request.content.read()
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
633 self.params = simplejson.loads(data)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
634
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
635 uri = self.params['uri']
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
636 callback = self.params['callback']
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
637
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
638 jid, nodeIdentifier = getServiceAndNode(uri)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
639 method = getattr(self.service, self.serviceMethod)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
640 d = method(jid, nodeIdentifier, callback)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
641 d.addCallback(toResponse)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
642 d.addErrback(trapNotFound)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
643 d.addErrback(trapXMPPURIParseError)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
644 return d
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
645
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
646
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
647
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
648 class RemoteSubscribeResource(RemoteSubscribeBaseResource):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
649 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
650 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
651
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
652 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
653 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
654 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
655 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
656 serviceMethod = 'subscribeCallback'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
657
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
658
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
659
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
660 class RemoteUnsubscribeResource(RemoteSubscribeBaseResource):
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
661 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
662 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
663
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
664 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
665 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
666 """
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
667 serviceMethod = 'unsubscribeCallback'
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
668
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
669
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
670
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
671 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
672 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
673 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
674 """
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
675
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
676 def __init__(self, service):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
677 self.service = service
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
678
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
679
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
680 @_asyncResponse
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
681 def render_GET(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
682 try:
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
683 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
684 except ValueError:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
685 raise Error(http.BAD_REQUEST,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
686 "The argument max_items has an invalid value.")
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
687
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
688 try:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
689 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
690 except KeyError:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
691 raise Error(http.BAD_REQUEST,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
692 "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
693
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
694 try:
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 191
diff changeset
695 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
696 except XMPPURIParseError:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
697 raise Error(http.BAD_REQUEST,
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
698 "Malformed XMPP URI: %s" % uri)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
699
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
700 def toResponse(items):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
701 """
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
702 Create a feed out the retrieved items.
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
703 """
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
704 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
705 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
706 "Retrieved item collection")
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
707 body = feed.toXml().encode('utf-8')
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
708 request.setHeader(b'Content-Type', MIME_ATOM_FEED)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
709 return body
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
710
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
711 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
712 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
713 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
714 raise failure
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
715 raise Error(http.NOT_FOUND, "Node not found")
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
716
186
365fd3e4daf8 Add maxItems support.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
717 d = self.service.items(jid, nodeIdentifier, maxItems)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
718 d.addCallback(toResponse)
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
719 d.addErrback(trapNotFound)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
720 return d
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
721
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
722
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
723
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
724 # 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
725
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
726 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
727 """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
728
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
729 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
730 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
731 error.
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
732
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
733 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
734 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
735
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
736 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
737 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
738
275
9c74cd2635f6 Fix getPageWithFactory
Christopher Zorn <tofu@thetofu.com>
parents: 274
diff changeset
739 if factory.scheme == 'https':
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
740 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
741 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
742 contextFactory = ssl.ClientContextFactory()
275
9c74cd2635f6 Fix getPageWithFactory
Christopher Zorn <tofu@thetofu.com>
parents: 274
diff changeset
743 reactor.connectSSL(factory.host, factory.port, factory, contextFactory)
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
744 else:
275
9c74cd2635f6 Fix getPageWithFactory
Christopher Zorn <tofu@thetofu.com>
parents: 274
diff changeset
745 reactor.connectTCP(factory.host, factory.port, factory)
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
746 return factory
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
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
749
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
750 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
751 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
752 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
753 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
754
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
755 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
756 self.callback = callback
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
757
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
758
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
759 http_GET = None
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
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
762 def render_POST(self, request):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
763 if request.requestHeaders.hasHeader(b'Event'):
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
764 payload = None
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
765 else:
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
766 payload = parseXml(request.content.read())
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
767
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
768 self.callback(payload, request.requestHeaders)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
769
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
770 request.setResponseCode(http.NO_CONTENT)
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
771 return b''
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
772
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
773
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
774
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
775
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
776 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
777 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
778 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
779 """
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
780
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
781 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
782
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
783 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
784 self.baseURI = baseURI
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
785 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
786 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
787 root = resource.Resource()
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
788 root.putChild('callback', CallbackResource(
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
789 lambda *args, **kwargs: self.callback(*args, **kwargs)))
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
790 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
791
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
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 startService(self):
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
794 self.port = reactor.listenTCP(self.callbackPort,
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
795 self.site)
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
796
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
797
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
798 def stopService(self):
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
799 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
800
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
801
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
802 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
803 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
804 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
805 uriComponents[1],
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
806 uriComponents[2] + verb,
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
807 '',
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
808 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
809 ''))
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
810 return uri
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
811
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
812
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
813 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
814 pass
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
815
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
816
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
817 def ping(self):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
818 f = getPageWithFactory(self._makeURI(''),
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
819 method='HEAD',
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
820 agent=self.agent)
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
821 return f.deferred
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
822
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
823
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
824 def create(self):
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
825 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
826 method='POST',
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
827 agent=self.agent)
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
828 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
829
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
830
209
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
831 def delete(self, xmppURI, redirectURI=None):
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
832 query = {'uri': xmppURI}
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
833
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
834 if redirectURI:
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
835 params = {'redirect_uri': redirectURI}
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
836 postdata = simplejson.dumps(params)
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
837 headers = {'Content-Type': MIME_JSON}
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
838 else:
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
839 postdata = None
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
840 headers = None
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
841
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
842 f = getPageWithFactory(self._makeURI('delete', query),
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
843 method='POST',
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
844 postdata=postdata,
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
845 headers=headers,
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
846 agent=self.agent)
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
847 return f.deferred
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
848
7f3ffb7a1a9e Add support for node deletion with redirect.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
849
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
850 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
851 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
852
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
853 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
854 method='POST',
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
855 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
856 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
857 agent=self.agent)
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
858 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
859
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
860
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
861 def listNodes(self):
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
862 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
863 method='GET',
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
864 agent=self.agent)
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
865 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
866
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
867
183
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
868 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
869 params = {'uri': xmppURI,
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
870 '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
871 self.callbackPort)}
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
872 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
873 method='POST',
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
874 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
875 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
876 agent=self.agent)
c21b986cff30 Implement HTTP client to gateway and implement functional tests with it.
Ralph Meijer <ralphm@ik.nu>
parents: 177
diff changeset
877 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
878
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
879
191
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
880 def unsubscribe(self, xmppURI):
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
881 params = {'uri': xmppURI,
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
882 '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
883 self.callbackPort)}
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
884 f = getPageWithFactory(self._makeURI('unsubscribe'),
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
885 method='POST',
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
886 postdata=simplejson.dumps(params),
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
887 headers={'Content-Type': MIME_JSON},
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
888 agent=self.agent)
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
889 return f.deferred
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
890
48245777acea Return proper HTTP status codes on failed un-/subscription.
Ralph Meijer <ralphm@ik.nu>
parents: 187
diff changeset
891
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
892 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
893 query = {'uri': xmppURI}
297
4115999d85e9 fixes behavior when maxItems is 0
souliane <souliane@mailoo.org>
parents: 283
diff changeset
894 if maxItems is not None:
186
365fd3e4daf8 Add maxItems support.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
895 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
896 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
897 method='GET',
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
898 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
899 return f.deferred