1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from Products.ZenHub.HubService import HubService
16 from twisted.web import resource, server
17 from twisted.internet import reactor
18
19 import xmlrpclib
20
21 import mimetypes
22
23 htmlResource = None
24
25 import logging
26 log = logging.getLogger("zenrender")
27
28 __doc__ = "Provide a simple web server to forward render requests"
29
30 -class Render(resource.Resource):
31
32 isLeaf = True
33
35 resource.Resource.__init__(self)
36 self.renderers = {}
37
38
40 "Deal with http requests"
41 args = request.args.copy()
42 for k, v in args.items():
43 if len(v) == 1:
44 args[k] = v[0]
45
46 listener = request.postpath[-2]
47 command = request.postpath[-1]
48 args.setdefault('ftype', 'PNG')
49 ftype = args['ftype']
50 del args['ftype']
51 mimetype = mimetypes.guess_type('.%s'%ftype)[0]
52 if not mimetype: mimetype = 'image/%s'%ftype
53 request.setHeader('Content-type', mimetype)
54 def write(result):
55 if result:
56 request.write(result)
57 request.finish()
58 def error(reason):
59 log.error("Unable to fetch graph: %s", reason)
60 request.finish()
61 renderer = self.renderers.get(listener, False)
62 if not renderer or not renderer.listeners:
63 raise Exception("Renderer %s unavailable" % listener)
64 d = renderer.listeners[0].callRemote(command, **args)
65 d.addCallbacks(write, error)
66 return server.NOT_DONE_YET
67
68 - def render_POST(self, request):
69 "Deal with XML-RPC requests"
70 for instance, renderer in self.renderers.items():
71 for listener in renderer.listeners:
72 try:
73 args, command = xmlrpclib.loads(request.content.read())
74 request.setHeader('Content-type', 'text/xml')
75 d = listener.callRemote(str(command), *args)
76 def write(result):
77 response = xmlrpclib.dumps((result,),
78 methodresponse=True)
79 request.write(response)
80 request.finish()
81 def error(reason):
82 log.error("Unable to fetch graph: %s", reason)
83 request.finish()
84 d.addCallbacks(write, error)
85 return server.NOT_DONE_YET
86 except Exception, ex:
87 log.exception(ex)
88 log.warning("Skipping renderer %s" % instance)
89 raise Exception("No renderer registered")
90
92 "Handle all paths"
93 return self, ()
94
95
97 self.renderers[renderer.instance] = renderer
98
108