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