mirror of
https://github.com/dkmstr/openuds.git
synced 2024-12-23 17:34:17 +03:00
Fixed intragrations from 2.2
This commit is contained in:
commit
6293c09ca8
@ -26,22 +26,23 @@
|
|||||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
# 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.
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
"""
|
'''
|
||||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||||
"""
|
'''
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import logging
|
from django import http
|
||||||
|
from django.views.generic.base import View
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.utils.decorators import method_decorator
|
||||||
|
from django.utils.translation import ugettext as _, activate
|
||||||
|
from django.conf import settings
|
||||||
|
from uds.REST.handlers import Handler, HandlerError, AccessDenied, NotFound, RequestError, ResponseError, NotSupportedError
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
import six
|
import six
|
||||||
from django import http
|
|
||||||
from django.utils.decorators import method_decorator
|
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
|
||||||
from django.views.generic.base import View
|
|
||||||
|
|
||||||
from uds.REST.handlers import Handler, HandlerError, AccessDenied, NotFound, RequestError, ResponseError, \
|
|
||||||
NotSupportedError
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -51,17 +52,17 @@ AUTH_TOKEN_HEADER = 'X-Auth-Token'
|
|||||||
|
|
||||||
|
|
||||||
class Dispatcher(View):
|
class Dispatcher(View):
|
||||||
"""
|
'''
|
||||||
This class is responsible of dispatching REST requests
|
This class is responsible of dispatching REST requests
|
||||||
"""
|
'''
|
||||||
# This attribute will contain all paths-->handler relations, added at Initialized method
|
# This attribute will contain all paths-->handler relations, added at Initialized method
|
||||||
services = {'': None} # Will include a default /rest handler, but rigth now this will be fine
|
services = {'': None} # Will include a default /rest handler, but rigth now this will be fine
|
||||||
|
|
||||||
@method_decorator(csrf_exempt)
|
@method_decorator(csrf_exempt)
|
||||||
def dispatch(self, request, **kwargs):
|
def dispatch(self, request, **kwargs):
|
||||||
"""
|
'''
|
||||||
Processes the REST request and routes it wherever it needs to be routed
|
Processes the REST request and routes it wherever it needs to be routed
|
||||||
"""
|
'''
|
||||||
logger.debug('Language in dispatcher: {0}'.format(request.LANGUAGE_CODE))
|
logger.debug('Language in dispatcher: {0}'.format(request.LANGUAGE_CODE))
|
||||||
from uds.REST import processors
|
from uds.REST import processors
|
||||||
|
|
||||||
@ -96,7 +97,7 @@ class Dispatcher(View):
|
|||||||
# Here, service points to the path
|
# Here, service points to the path
|
||||||
cls = service['']
|
cls = service['']
|
||||||
if cls is None:
|
if cls is None:
|
||||||
return http.HttpResponseNotFound('method not found')
|
return http.HttpResponseNotFound('method not found', content_type="text/plain")
|
||||||
|
|
||||||
# Guess content type from content type header (post) or ".xxx" to method
|
# Guess content type from content type header (post) or ".xxx" to method
|
||||||
try:
|
try:
|
||||||
@ -117,54 +118,50 @@ class Dispatcher(View):
|
|||||||
except processors.ParametersException as e:
|
except processors.ParametersException as e:
|
||||||
logger.debug('Path: {0}'.format(full_path))
|
logger.debug('Path: {0}'.format(full_path))
|
||||||
logger.debug('Error: {0}'.format(e))
|
logger.debug('Error: {0}'.format(e))
|
||||||
return http.HttpResponseServerError('Invalid parameters invoking {0}: {1}'.format(full_path, e))
|
return http.HttpResponseServerError('Invalid parameters invoking {0}: {1}'.format(full_path, e), content_type="text/plain")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
allowedMethods = []
|
allowedMethods = []
|
||||||
for n in ['get', 'post', 'put', 'delete']:
|
for n in ['get', 'post', 'put', 'delete']:
|
||||||
if hasattr(handler, n):
|
if hasattr(handler, n):
|
||||||
allowedMethods.append(n)
|
allowedMethods.append(n)
|
||||||
return http.HttpResponseNotAllowed(allowedMethods)
|
return http.HttpResponseNotAllowed(allowedMethods, content_type="text/plain")
|
||||||
except AccessDenied:
|
except AccessDenied:
|
||||||
return http.HttpResponseForbidden('access denied')
|
return http.HttpResponseForbidden('access denied', content_type="text/plain")
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('error accessing attribute')
|
logger.exception('error accessing attribute')
|
||||||
logger.debug('Getting attribute {0} for {1}'.format(http_method, full_path))
|
logger.debug('Getting attribute {0} for {1}'.format(http_method, full_path))
|
||||||
return http.HttpResponseServerError('Unexcepected error')
|
return http.HttpResponseServerError('Unexcepected error', content_type="text/plain")
|
||||||
|
|
||||||
# Invokes the handler's operation, add headers to response and returns
|
# Invokes the handler's operation, add headers to response and returns
|
||||||
try:
|
try:
|
||||||
start = time.time()
|
|
||||||
response = operation()
|
response = operation()
|
||||||
logger.debug('Execution time for method: {0}'.format(time.time() - start))
|
|
||||||
|
|
||||||
if not handler.raw: # Raw handlers will return an HttpResponse Object
|
if not handler.raw: # Raw handlers will return an HttpResponse Object
|
||||||
start = time.time()
|
|
||||||
response = processor.getResponse(response)
|
response = processor.getResponse(response)
|
||||||
logger.debug('Execution time for encoding: {0}'.format(time.time() - start))
|
for k, val in handler.headers().items():
|
||||||
for k, val in six.iteritems(handler.headers()):
|
|
||||||
response[k] = val
|
response[k] = val
|
||||||
return response
|
return response
|
||||||
except RequestError as e:
|
except RequestError as e:
|
||||||
return http.HttpResponseBadRequest(six.text_type(e))
|
return http.HttpResponseBadRequest(six.text_type(e), content_type="text/plain")
|
||||||
except ResponseError as e:
|
except ResponseError as e:
|
||||||
return http.HttpResponseServerError(six.text_type(e))
|
return http.HttpResponseServerError(six.text_type(e), content_type="text/plain")
|
||||||
except NotSupportedError as e:
|
except NotSupportedError as e:
|
||||||
return http.HttpResponseBadRequest(six.text_type(e))
|
return http.HttpResponseBadRequest(six.text_type(e), content_type="text/plain")
|
||||||
except AccessDenied as e:
|
except AccessDenied as e:
|
||||||
return http.HttpResponseForbidden(six.text_type(e))
|
return http.HttpResponseForbidden(six.text_type(e), content_type="text/plain")
|
||||||
except NotFound as e:
|
except NotFound as e:
|
||||||
return http.HttpResponseNotFound(six.text_type(e))
|
return http.HttpResponseNotFound(six.text_type(e), content_type="text/plain")
|
||||||
except HandlerError as e:
|
except HandlerError as e:
|
||||||
return http.HttpResponseBadRequest(six.text_type(e))
|
return http.HttpResponseBadRequest(six.text_type(e), content_type="text/plain")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception('Error processing request')
|
logger.exception('Error processing request')
|
||||||
return http.HttpResponseServerError(six.text_type(e))
|
return http.HttpResponseServerError(six.text_type(e), content_type="text/plain")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def registerSubclasses(classes):
|
def registerSubclasses(classes):
|
||||||
"""
|
'''
|
||||||
Try to register Handler subclasses that have not been inherited
|
Try to register Handler subclasses that have not been inherited
|
||||||
"""
|
'''
|
||||||
for cls in classes:
|
for cls in classes:
|
||||||
if len(cls.__subclasses__()) == 0: # Only classes that has not been inherited will be registered as Handlers
|
if len(cls.__subclasses__()) == 0: # Only classes that has not been inherited will be registered as Handlers
|
||||||
logger.debug('Found class {0}'.format(cls))
|
logger.debug('Found class {0}'.format(cls))
|
||||||
@ -189,10 +186,10 @@ class Dispatcher(View):
|
|||||||
# Initializes the dispatchers
|
# Initializes the dispatchers
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def initialize():
|
def initialize():
|
||||||
"""
|
'''
|
||||||
This imports all packages that are descendant of this package, and, after that,
|
This imports all packages that are descendant of this package, and, after that,
|
||||||
it register all subclases of Handler. (In fact, it looks for packages inside "methods" package, child of this)
|
it register all subclases of Handler. (In fact, it looks for packages inside "methods" package, child of this)
|
||||||
"""
|
'''
|
||||||
import os.path
|
import os.path
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import sys
|
import sys
|
||||||
|
Loading…
Reference in New Issue
Block a user