Package pyamf :: Package remoting :: Module amf0
[hide private]
[frames] | no frames]

Source Code for Module pyamf.remoting.amf0

  1  # Copyright (c) 2007-2008 The PyAMF Project. 
  2  # See LICENSE for details. 
  3   
  4  """ 
  5  AMF0 Remoting support. 
  6   
  7  @author: U{Nick Joyce<mailto:nick@boxdesign.co.uk>} 
  8   
  9  @since: 0.1.0 
 10  """ 
 11   
 12  import traceback, sys 
 13   
 14  from pyamf import remoting 
 15  from pyamf.remoting import gateway 
 16   
17 -class RequestProcessor(object):
18 - def __init__(self, gateway):
19 self.gateway = gateway
20
21 - def authenticateRequest(self, request, service_request, *args, **kwargs):
22 """ 23 Authenticates the request against the service. 24 25 @param request: The AMF request 26 @type request: L{Request<pyamf.remoting.Request>} 27 """ 28 username = password = None 29 30 if 'Credentials' in request.headers: 31 cred = request.headers['Credentials'] 32 33 username = cred['userid'] 34 password = cred['password'] 35 36 return self.gateway.authenticateRequest(service_request, username, password, *args, **kwargs)
37
38 - def buildErrorResponse(self, request, error=None):
39 """ 40 Builds an error response. 41 42 @param request: The AMF request 43 @type request: L{Request<pyamf.remoting.Request>} 44 @return: The AMF response 45 @rtype: L{Response<pyamf.remoting.Response>} 46 """ 47 if error is not None: 48 cls, e, tb = error 49 else: 50 cls, e, tb = sys.exc_info() 51 52 return remoting.Response(build_fault(cls, e, tb), status=remoting.STATUS_ERROR)
53
54 - def _getBody(self, request, response, service_request, **kwargs):
55 if 'DescribeService' in request.headers: 56 return service_request.service.description 57 58 return self.gateway.callServiceRequest(service_request, *request.body, **kwargs)
59
60 - def __call__(self, request, *args, **kwargs):
61 """ 62 Processes an AMF0 request. 63 64 @param request: The request to be processed. 65 @type request: L{Request<pyamf.remoting.Request>} 66 67 @return: The response to the request. 68 @rtype: L{Response<pyamf.remoting.Response>} 69 """ 70 response = remoting.Response(None) 71 72 try: 73 service_request = self.gateway.getServiceRequest(request, request.target) 74 except gateway.UnknownServiceError, e: 75 return self.buildErrorResponse(request) 76 77 # we have a valid service, now attempt authentication 78 try: 79 authd = self.authenticateRequest(request, service_request, *args, **kwargs) 80 except (SystemExit, KeyboardInterrupt): 81 raise 82 except: 83 return self.buildErrorResponse(request) 84 85 if not authd: 86 # authentication failed 87 response.status = remoting.STATUS_ERROR 88 response.body = remoting.ErrorFault(code='AuthenticationError', 89 description='Authentication failed') 90 91 return response 92 93 # authentication succeeded, now fire the preprocessor (if there is one) 94 try: 95 self.gateway.preprocessRequest(service_request, *args, **kwargs) 96 except (SystemExit, KeyboardInterrupt): 97 raise 98 except: 99 return self.buildErrorResponse(request) 100 101 try: 102 response.body = self._getBody(request, response, service_request, *args, **kwargs) 103 104 return response 105 except (SystemExit, KeyboardInterrupt): 106 raise 107 except: 108 return self.buildErrorResponse(request)
109
110 -def build_fault(cls, e, tb):
111 """ 112 Builds a L{remoting.ErrorFault} object based on the last exception raised. 113 """ 114 if hasattr(cls, '_amf_code'): 115 code = cls._amf_code 116 else: 117 code = cls.__name__ 118 119 return remoting.ErrorFault(code=code, description=str(e), 120 details=str(traceback.format_exception(cls, e, tb)).replace("\\n", ''))
121