Package ZenHub :: Package services :: Module ZenRender
[hide private]
[frames] | no frames]

Source Code for Module ZenHub.services.ZenRender

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  #! /usr/bin/env python  
 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
34 - def __init__(self):
35 resource.Resource.__init__(self) 36 self.renderers = {}
37 38
39 - def render_GET(self, request):
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
91 - def getChild(self, path, request):
92 "Handle all paths" 93 return self, ()
94 95
96 - def addRenderer(self, renderer):
97 self.renderers[renderer.instance] = renderer
98
99 -class ZenRender(HubService):
100
101 - def __init__(self, *args, **kw):
102 HubService.__init__(self, *args, **kw) 103 global htmlResource 104 if not htmlResource: 105 htmlResource = Render() 106 reactor.listenTCP(8090, server.Site(htmlResource)) 107 htmlResource.addRenderer(self)
108