comparison idavoll/generic_backend.py @ 159:6fe78048baf9

Rework error handling, depend on Twisted Words 0.4.0. Twisted Words 0.4.0 introduced support for stanza error handling, much better than the custom error handling in Idavoll. Also, all protocol-level errors were examined and brought up to date with version 1.8 of JEP-0060. As a result of the error examination, the retrieval of default configuration options using <default/> is now supported properly.
author Ralph Meijer <ralphm@ik.nu>
date Wed, 06 Sep 2006 12:38:47 +0000
parents 5191ba7c4df8
children 84cfe9fe38c5
comparison
equal deleted inserted replaced
158:b2149e448465 159:6fe78048baf9
76 implements(backend.IPublishService) 76 implements(backend.IPublishService)
77 77
78 def _check_auth(self, node, requestor): 78 def _check_auth(self, node, requestor):
79 def check(affiliation, node): 79 def check(affiliation, node):
80 if affiliation not in ['owner', 'publisher']: 80 if affiliation not in ['owner', 'publisher']:
81 raise backend.NotAuthorized 81 raise backend.Forbidden
82 return node 82 return node
83 83
84 d = node.get_affiliation(requestor) 84 d = node.get_affiliation(requestor)
85 d.addCallback(check, node) 85 d.addCallback(check, node)
86 return d 86 return d
95 configuration = node.get_configuration() 95 configuration = node.get_configuration()
96 persist_items = configuration["pubsub#persist_items"] 96 persist_items = configuration["pubsub#persist_items"]
97 deliver_payloads = configuration["pubsub#deliver_payloads"] 97 deliver_payloads = configuration["pubsub#deliver_payloads"]
98 98
99 if items and not persist_items and not deliver_payloads: 99 if items and not persist_items and not deliver_payloads:
100 raise backend.NoPayloadAllowed 100 raise backend.ItemForbidden
101 elif not items and (persist_items or deliver_payloads): 101 elif not items and (persist_items or deliver_payloads):
102 raise backend.PayloadExpected 102 raise backend.ItemRequired
103 103
104 if persist_items or deliver_payloads: 104 if persist_items or deliver_payloads:
105 for item in items: 105 for item in items:
106 if not item.getAttribute("id"): 106 if not item.getAttribute("id"):
107 item["id"] = uuid.generate() 107 item["id"] = uuid.generate()
147 implements(backend.ISubscriptionService) 147 implements(backend.ISubscriptionService)
148 148
149 def subscribe(self, node_id, subscriber, requestor): 149 def subscribe(self, node_id, subscriber, requestor):
150 subscriber_entity = subscriber.userhostJID() 150 subscriber_entity = subscriber.userhostJID()
151 if subscriber_entity != requestor: 151 if subscriber_entity != requestor:
152 return defer.fail(backend.NotAuthorized()) 152 return defer.fail(backend.Forbidden())
153 153
154 d = self.parent.storage.get_node(node_id) 154 d = self.parent.storage.get_node(node_id)
155 d.addCallback(_get_affiliation, subscriber_entity) 155 d.addCallback(_get_affiliation, subscriber_entity)
156 d.addCallback(self._do_subscribe, subscriber) 156 d.addCallback(self._do_subscribe, subscriber)
157 return d 157 return d
158 158
159 def _do_subscribe(self, result, subscriber): 159 def _do_subscribe(self, result, subscriber):
160 node, affiliation = result 160 node, affiliation = result
161 161
162 if affiliation == 'outcast': 162 if affiliation == 'outcast':
163 raise backend.NotAuthorized 163 raise backend.Forbidden
164 164
165 d = node.add_subscription(subscriber, 'subscribed') 165 d = node.add_subscription(subscriber, 'subscribed')
166 d.addCallback(lambda _: 'subscribed') 166 d.addCallback(lambda _: 'subscribed')
167 d.addErrback(self._get_subscription, node, subscriber) 167 d.addErrback(self._get_subscription, node, subscriber)
168 d.addCallback(self._return_subscription, node.id) 168 d.addCallback(self._return_subscription, node.id)
175 def _return_subscription(self, result, node_id): 175 def _return_subscription(self, result, node_id):
176 return node_id, result 176 return node_id, result
177 177
178 def unsubscribe(self, node_id, subscriber, requestor): 178 def unsubscribe(self, node_id, subscriber, requestor):
179 if subscriber.userhostJID() != requestor: 179 if subscriber.userhostJID() != requestor:
180 raise backend.NotAuthorized 180 return defer.fail(backend.Forbidden())
181 181
182 d = self.parent.storage.get_node(node_id) 182 d = self.parent.storage.get_node(node_id)
183 d.addCallback(lambda node: node.remove_subscription(subscriber)) 183 d.addCallback(lambda node: node.remove_subscription(subscriber))
184 return d 184 return d
185 185
198 node_id = 'generic/%s' % uuid.generate() 198 node_id = 'generic/%s' % uuid.generate()
199 d = self.parent.storage.create_node(node_id, requestor) 199 d = self.parent.storage.create_node(node_id, requestor)
200 d.addCallback(lambda _: node_id) 200 d.addCallback(lambda _: node_id)
201 return d 201 return d
202 202
203 def get_default_configuration(self):
204 d = defer.succeed(self.parent.default_config)
205 d.addCallback(self._make_config)
206 return d
207
203 def get_node_configuration(self, node_id): 208 def get_node_configuration(self, node_id):
204 if node_id: 209 if not node_id:
205 d = self.parent.storage.get_node(node_id) 210 raise backend.NoRootNode
206 d.addCallback(lambda node: node.get_configuration()) 211
207 else: 212 d = self.parent.storage.get_node(node_id)
208 # XXX: this is disabled in pubsub.py 213 d.addCallback(lambda node: node.get_configuration())
209 d = defer.succeed(self.parent.default_config)
210 214
211 d.addCallback(self._make_config) 215 d.addCallback(self._make_config)
212 return d 216 return d
213 217
214 def _make_config(self, config): 218 def _make_config(self, config):
221 options.append(option) 225 options.append(option)
222 226
223 return options 227 return options
224 228
225 def set_node_configuration(self, node_id, options, requestor): 229 def set_node_configuration(self, node_id, options, requestor):
230 if not node_id:
231 raise backend.NoRootNode
232
226 for key, value in options.iteritems(): 233 for key, value in options.iteritems():
227 if not self.parent.options.has_key(key): 234 if not self.parent.options.has_key(key):
228 raise backend.InvalidConfigurationOption 235 raise backend.InvalidConfigurationOption
229 if self.parent.options[key]["type"] == 'boolean': 236 if self.parent.options[key]["type"] == 'boolean':
230 try: 237 try:
239 246
240 def _do_set_node_configuration(self, result, options): 247 def _do_set_node_configuration(self, result, options):
241 node, affiliation = result 248 node, affiliation = result
242 249
243 if affiliation != 'owner': 250 if affiliation != 'owner':
244 raise backend.NotAuthorized 251 raise backend.Forbidden
245 252
246 return node.set_configuration(options) 253 return node.set_configuration(options)
247 254
248 class AffiliationsService(service.Service): 255 class AffiliationsService(service.Service):
249 256
269 276
270 def _do_get_items(self, result, max_items, item_ids): 277 def _do_get_items(self, result, max_items, item_ids):
271 node, subscribed = result 278 node, subscribed = result
272 279
273 if not subscribed: 280 if not subscribed:
274 raise backend.NotAuthorized 281 raise backend.NotSubscribed
275 282
276 if item_ids: 283 if item_ids:
277 return node.get_items_by_id(item_ids) 284 return node.get_items_by_id(item_ids)
278 else: 285 else:
279 return node.get_items(max_items) 286 return node.get_items(max_items)
291 def _do_retract(self, result, item_ids): 298 def _do_retract(self, result, item_ids):
292 node, affiliation = result 299 node, affiliation = result
293 persist_items = node.get_configuration()["pubsub#persist_items"] 300 persist_items = node.get_configuration()["pubsub#persist_items"]
294 301
295 if affiliation not in ['owner', 'publisher']: 302 if affiliation not in ['owner', 'publisher']:
296 raise backend.NotAuthorized 303 raise backend.Forbidden
297 304
298 if not persist_items: 305 if not persist_items:
299 raise backend.NodeNotPersistent 306 raise backend.NodeNotPersistent
300 307
301 d = node.remove_items(item_ids) 308 d = node.remove_items(item_ids)
315 def _do_purge(self, result): 322 def _do_purge(self, result):
316 node, affiliation = result 323 node, affiliation = result
317 persist_items = node.get_configuration()["pubsub#persist_items"] 324 persist_items = node.get_configuration()["pubsub#persist_items"]
318 325
319 if affiliation != 'owner': 326 if affiliation != 'owner':
320 raise backend.NotAuthorized 327 raise backend.Forbidden
321 328
322 if not persist_items: 329 if not persist_items:
323 raise backend.NodeNotPersistent 330 raise backend.NodeNotPersistent
324 331
325 d = node.purge() 332 d = node.purge()
352 359
353 def _do_pre_delete(self, result): 360 def _do_pre_delete(self, result):
354 node, affiliation = result 361 node, affiliation = result
355 362
356 if affiliation != 'owner': 363 if affiliation != 'owner':
357 raise backend.NotAuthorized 364 raise backend.Forbidden
358 365
359 d = defer.DeferredList([cb(node.id) for cb in self._callback_list], 366 d = defer.DeferredList([cb(node.id) for cb in self._callback_list],
360 consumeErrors=1) 367 consumeErrors=1)
361 d.addCallback(self._do_delete, node.id) 368 d.addCallback(self._do_delete, node.id)
362 369