annotate sat_pubsub/tap_http.py @ 273:6ba0d6def7f5

Use twisted.web instead of web2, initial tests.
author Ralph Meijer <ralphm@ik.nu>
date Sun, 20 Jan 2013 13:38:41 +0100
parents d55620ceafed
children 002c59dbc23f
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
4 """
235
64f780413b82 fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents: 234
diff changeset
5 Copyright (c) 2003-2011 Ralph Meijer
255
d55620ceafed copyright update
Goffi <goffi@goffi.org>
parents: 235
diff changeset
6 Copyright (c) 2012, 2013 Jérôme Poisson
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
7
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
8
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
9 This program is free software: you can redistribute it and/or modify
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
10 it under the terms of the GNU Affero General Public License as published by
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
11 the Free Software Foundation, either version 3 of the License, or
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
12 (at your option) any later version.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
13
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
14 This program is distributed in the hope that it will be useful,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
17 GNU Affero General Public License for more details.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
18
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
19 You should have received a copy of the GNU Affero General Public License
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
21 --
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
22
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
23 This program is based on Idavoll (http://idavoll.ik.nu/),
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
24 originaly written by Ralph Meijer (http://ralphm.net/blog/)
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
25 It is sublicensed under AGPL v3 (or any later version) as allowed by the original
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
26 license.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
27
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
28 --
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
29
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
30 Here is a copy of the original license:
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
31
235
64f780413b82 fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents: 234
diff changeset
32 Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
33
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
34 Permission is hereby granted, free of charge, to any person obtaining
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
35 a copy of this software and associated documentation files (the
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
36 "Software"), to deal in the Software without restriction, including
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
37 without limitation the rights to use, copy, modify, merge, publish,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
38 distribute, sublicense, and/or sell copies of the Software, and to
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
39 permit persons to whom the Software is furnished to do so, subject to
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
40 the following conditions:
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
41
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
42 The above copyright notice and this permission notice shall be
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
43 included in all copies or substantial portions of the Software.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
44
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
45 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
46 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
47 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
48 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
49 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
50 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
51 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
52
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
53 """
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
55 from twisted.application import internet, strports
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 from twisted.conch import manhole, manhole_ssh
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 from twisted.cred import portal, checkers
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
58 from twisted.web import resource, server
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59
234
51af2ed8bd50 replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents: 233
diff changeset
60 from sat_pubsub import gateway, tap
51af2ed8bd50 replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents: 233
diff changeset
61 from sat_pubsub.gateway import RemoteSubscriptionService
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 class Options(tap.Options):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64 optParameters = [
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 ('webport', None, '8086', 'Web port'),
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 ]
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
69
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 def getManholeFactory(namespace, **passwords):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71 def getManHole(_):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 return manhole.Manhole(namespace)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74 realm = manhole_ssh.TerminalRealm()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 realm.chainedProtocolFactory.protocolFactory = getManHole
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 p = portal.Portal(realm)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77 p.registerChecker(
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78 checkers.InMemoryUsernamePasswordDatabaseDontUse(**passwords))
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 f = manhole_ssh.ConchFactory(p)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 return f
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
83
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84 def makeService(config):
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 s = tap.makeService(config)
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 bs = s.getServiceNamed('backend')
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
88 cs = s.getServiceNamed('component')
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 # Set up XMPP service for subscribing to remote nodes
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
92 if config['backend'] == 'pgsql':
234
51af2ed8bd50 replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents: 233
diff changeset
93 from sat_pubsub.pgsql_storage import GatewayStorage
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
94 gst = GatewayStorage(bs.storage.dbpool)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
95 elif config['backend'] == 'memory':
234
51af2ed8bd50 replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents: 233
diff changeset
96 from sat_pubsub.memory_storage import GatewayStorage
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
97 gst = GatewayStorage()
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
98
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 185
diff changeset
99 ss = RemoteSubscriptionService(config['jid'], gst)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100 ss.setHandlerParent(cs)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 ss.startService()
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
103 # Set up web service
177
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 root = resource.Resource()
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
106
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
107 # Set up resources that exposes the backend
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
108 root.putChild('create', gateway.CreateResource(bs, config['jid'],
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
109 config['jid']))
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
110 root.putChild('delete', gateway.DeleteResource(bs, config['jid'],
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
111 config['jid']))
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
112 root.putChild('publish', gateway.PublishResource(bs, config['jid'],
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
113 config['jid']))
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
114 root.putChild('list', gateway.ListResource(bs))
185
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
115
9038908dc2f5 Add gateway support for retrieving items from a node. Reorder gateway module.
Ralph Meijer <ralphm@ik.nu>
parents: 183
diff changeset
116 # Set up resources for accessing remote pubsub nodes.
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
117 root.putChild('subscribe', gateway.RemoteSubscribeResource(ss))
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
118 root.putChild('unsubscribe', gateway.RemoteUnsubscribeResource(ss))
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
119 root.putChild('items', gateway.RemoteItemsResource(ss))
213
a321f9300054 Actually log requests if verbose.
Ralph Meijer <ralphm@ik.nu>
parents: 206
diff changeset
120
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
121 site = server.Site(root)
273
6ba0d6def7f5 Use twisted.web instead of web2, initial tests.
Ralph Meijer <ralphm@ik.nu>
parents: 255
diff changeset
122 w = internet.TCPServer(int(config['webport']), site)
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123 w.setServiceParent(s)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
125 # Set up a manhole
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
126
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
127 namespace = {'service': s,
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
128 'component': cs,
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
129 'backend': bs,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
130 'root': root}
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
131
177
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
132 f = getManholeFactory(namespace, admin='admin')
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
133 manholeService = strports.service('2222', f)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
134 manholeService.setServiceParent(s)
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
135
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
136 return s
faf1c9bc2612 Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
137