git.fiddlerwoaroof.com
jsonrpc/server.py
d149e5b8
 #
 #  Copyright (c) 2011 Edward Langley
 #  All rights reserved.
 #
 #  Redistribution and use in source and binary forms, with or without
 #  modification, are permitted provided that the following conditions
 #  are met:
 #
 #  Redistributions of source code must retain the above copyright notice,
 #  this list of conditions and the following disclaimer.
 #
 #  Redistributions in binary form must reproduce the above copyright
 #  notice, this list of conditions and the following disclaimer in the
 #  documentation and/or other materials provided with the distribution.
 #
 #  Neither the name of the project's author nor the names of its
 #  contributors may be used to endorse or promote products derived from
 #  this software without specific prior written permission.
 #
 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 #  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 #  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 #  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 #  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 #  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 #  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
 #
 import jsonrpc.jsonutil
8445cb9a
 from jsonrpc.utilities import public
604c186e
 import jsonrpc.common
d149e5b8
 
 # Twisted imports
 from twisted.web import server
 from twisted.internet import threads
 from twisted.web.resource import Resource
 
 
ed8582fc
 import copy
d149e5b8
 import UserDict, collections
 collections.Mapping.register(UserDict.DictMixin)
 
8445cb9a
 @public
d149e5b8
 class ServerEvents(object):
 	'''Subclass this and pass to :py:meth:`jsonrpc.customize` to customize the jsonrpc server'''
 
8445cb9a
 	def __init__(self, server):
d149e5b8
 		#: A link to the JSON-RPC server instance
8445cb9a
 		self.server = server
d149e5b8
 
ed8582fc
 	def callmethod(self, txrequest, rpcrequest, **extra):
d149e5b8
 		'''Finds the method and calls it with the specified args'''
8445cb9a
 		method = self.findmethod(rpcrequest.method)
604c186e
 		if method is None: raise jsonrpc.common.MethodNotFound
ed8582fc
 		extra.update(rpcrequest.kwargs)
d149e5b8
 
ed8582fc
 		return method(*rpcrequest.args, **extra)
d149e5b8
 
 	def findmethod(self, method):
 		'''Override to allow server to define methods'''
 		return lambda *a, **kw: 'Test Data'
 
ed8582fc
 	def processrequest(self, result, args, **kw):
d149e5b8
 		'''Override to implement custom handling of the method result and request'''
 		return result
 
09f3cd17
 	def log(self, response, txrequest, error=False):
d149e5b8
 		'''Override to implement custom error handling'''
 		pass
 
 	def processcontent(self, content, request):
 		'''Given the freshly decoded content of the request, return what content should be used'''
 		return content
 
 
8445cb9a
 
d149e5b8
 ## Base class providing a JSON-RPC 2.0 implementation with 2 customizable hooks
8445cb9a
 @public
d149e5b8
 class JSON_RPC(Resource):
 	'''This class implements a JSON-RPC 2.0 server as a Twisted Resource'''
 	isLeaf = True
 
 	#: set by :py:meth:`customize` used to change the behavior of the server
 	eventhandler = ServerEvents
 
 	def customize(self, eventhandler):
 		'''customize the behavior of the server'''
 		self.eventhandler = eventhandler(self)
 		return self
 
 
 	def __init__(self, *args, **kwargs):
 		self.customize(self.eventhandler)
 		Resource.__init__(self,*args, **kwargs)
 
 
 	def render(self, request):
 		request.content.seek(0, 0)
 		try:
 			try:
 				content = jsonrpc.jsonutil.decode(request.content.read())
604c186e
 			except ValueError: raise jsonrpc.common.ParseError
3ae840e0
 
d149e5b8
 			content = self.eventhandler.processcontent(content, request)
3ae840e0
 
cc11cbe4
 			content = jsonrpc.common.Request.from_json(content)
8445cb9a
 
 			try:
 				if hasattr(content, 'check'):
 					content.check()
 				else:
 					for item in content: item.check()
 
604c186e
 			except jsonrpc.common.RPCError, e:
8445cb9a
 				self._ebRender(e, request, content.id if hasattr(content, 'id') else None)
 
0bc3c63b
 			else:
 				d = threads.deferToThread(self._action, request, content)
 				d.addCallback(self._cbRender, request)
 				d.addErrback(self._ebRender, request, content.id if hasattr(content, 'id') else None)
d149e5b8
 		except BaseException, e:
 			self._ebRender(e, request, None)
 
 		return server.NOT_DONE_YET
 
ed8582fc
 	def _action(self, request, contents, **kw):
8445cb9a
 		result = []
 
 		islist = (True if isinstance(contents, list) else False)
 		if not islist: contents = [contents]
 
604c186e
 		if contents == []: raise jsonrpc.common.InvalidRequest
8445cb9a
 
 		for rpcrequest in contents:
 			res = None
 
 			try:
ed8582fc
 				add = copy.deepcopy(rpcrequest.extra)
 				add.update(kw)
604c186e
 				res = jsonrpc.common.Response(id=rpcrequest.id, result=self.eventhandler.callmethod(request, rpcrequest, **add))
ed8582fc
 				res = self.eventhandler.processrequest(res, request.args, **kw)
8445cb9a
 			except Exception, e:
 				res = self.render_error(e, rpcrequest.id)
 
 			if res.id is not None:
 				result.append(res)
 
 
 
 		if result != []:
 			if not islist: result = result[0]
 		else: result = None
 
 		return result
 
 
0bc3c63b
 	def _setresponseCode(self, result, request):
 		code = 200
 		if not isinstance(result, list):
 			if result is not None and result.error is not None:
 				code = 500
 		request.setResponseCode(code)
d149e5b8
 
 	def _cbRender(self, result, request):
0bc3c63b
 		self._setresponseCode(result, request)
09f3cd17
 		self.eventhandler.log(result, request, error=False)
d149e5b8
 		if result is not None:
 			request.setHeader("content-type", 'application/json')
 			result = jsonrpc.jsonutil.encode(result).encode('utf-8')
 			request.setHeader("content-length", len(result))
 			request.write(result)
 		request.finish()
 
 	def _ebRender(self, result, request, id, finish=True):
 		err = None
 		if not isinstance(result, BaseException):
 			try: result.raiseException()
 			except BaseException, e:
 				err = e
09f3cd17
 				self.eventhandler.log(err, request, error=True)
d149e5b8
 		else: err = result
cc11cbe4
 
d149e5b8
 		err = self.render_error(err, id)
0bc3c63b
 		self._setresponseCode(err, request)
d149e5b8
 
 		request.setHeader("content-type", 'application/json')
 		result = jsonrpc.jsonutil.encode(err).encode('utf-8')
 		request.setHeader("content-length", len(result))
 		request.write(result)
 		if finish: request.finish()
 
 
 	def render_error(self, e, id):
604c186e
 		if isinstance(e, jsonrpc.common.RPCError):
 			err = jsonrpc.common.Response(id=id, error=e)
d149e5b8
 		else:
604c186e
 			err = jsonrpc.common.Response(id=id, error=dict(code=0, message=str(e), data=e.args))
d149e5b8
 
 		return err