Beispiel #1
0
def dispatch(http_args, client_ip, client_hostname):
    action = http_args['action']

    log_info('dispatcher handling action: %s' % (action, ))

    # Verify that we don't have a protocol version mismatch
    PROTOCOL_VERSION = 1
    try:
        protocol_version = int(http_args['protocol'])
        if protocol_version != PROTOCOL_VERSION:
            raise ProtocolVersionMismatchError(protocol_version,
                                               PROTOCOL_VERSION)
    except TypeError:
        #raise ProtocolVersionMismatchError('None', PROTOCOL_VERSION)
        pass
    except ValueError:
        #raise ProtocolVersionMismatchError(http_args['protocol'],
        #                                   PROTOCOL_VERSION)
        pass

    # Was an action supplied?
    if action is None:
        raise NoActionError

    # If we got a directory (collection), check it for security
    if http_args['collection'] is not None:
        if not _directory_is_safe(http_args['collection']):
            raise DirectorySecurityError(http_args['collection'])

    # Make sure that we are authenticated if we are to do certain actions
    if action in REQUIRES_AUTHENTICATION:
        try:
            user = get_session()['user']
        except KeyError:
            user = None
        if user is None:
            log_info('Authorization failure for "%s" with hostname "%s"'
                     % (client_ip, client_hostname))
            raise NotAuthorisedError(action)

    # Fetch the action function for this action (if any)
    try:
        action_function = DISPATCHER[action]
    except KeyError:
        log_info('Invalid action "%s"' % action)
        raise InvalidActionError(action)

    # Determine what arguments the action function expects
    args, varargs, keywords, defaults = getargspec(action_function)
    # We will not allow this for now, there is most likely no need for it
    assert varargs is None, 'no varargs for action functions'
    assert keywords is None, 'no keywords for action functions'

    # XXX: Quick hack
    if defaults is None:
        defaults = []

    # These arguments already has default values
    default_val_by_arg = {}
    for arg, default_val in zip(args[-len(defaults):], defaults):
        default_val_by_arg[arg] = default_val

    action_args = []
    for arg_name in args:
        arg_val = http_args[arg_name]

        # The client failed to provide this argument
        if arg_val is None:
            try:
                arg_val = default_val_by_arg[arg_name]
            except KeyError:
                raise InvalidActionArgsError(action, arg_name)

        action_args.append(arg_val)

    log_info('dispatcher will call %s(%s)' %
             (action, ', '.join((repr(a) for a in action_args)), ))

    # Log annotation actions separately (if so configured)
    if action in LOGGED_ANNOTATOR_ACTION:
        log_annotation(http_args['collection'],
                       http_args['document'],
                       'START', action, action_args)

    # TODO: log_annotation for exceptions?

    json_dic = action_function(*action_args)

    # Log annotation actions separately (if so configured)
    if action in LOGGED_ANNOTATOR_ACTION:
        log_annotation(http_args['collection'],
                       http_args['document'],
                       'FINISH', action, action_args)

    # Assign which action that was performed to the json_dic
    json_dic['action'] = action
    # Return the protocol version for symmetry
    json_dic['protocol'] = PROTOCOL_VERSION
    return json_dic
Beispiel #2
0
def dispatch(params, client_ip, client_hostname):
    action = params.getvalue('action')
    log_info(dir(params))
    log_info(str(params))

    log_info('dispatcher handling action: %s' % (action, ));
    
    # Was an action supplied?
    if action is None:
        raise NoActionError

    # If we got a directory (collection), check it for security
    if params.getvalue('collection') is not None:
        if not _directory_is_safe(params.getvalue('collection')):
            raise DirectorySecurityError(params.getvalue('collection'))

    # Make sure that we are authenticated if we are to do certain actions
    if action in REQUIRES_AUTHENTICATION:
        try:
            user = get_session()['user']
        except KeyError:
            user = None
        if user is None:
            log_info('Authorization failure for "%s" with hostname "%s"'
                     % (client_ip, client_hostname))
            raise NotAuthorisedError(action)

    # Fetch the action function for this action (if any)
    try:
        action_fuction = DISPATCHER[action]
    except KeyError:
        log_info('Invalid action "%s"' % action)
        raise InvalidActionError(action)

    # Determine what arguments the action function expects
    args, varargs, keywords, defaults = getargspec(action_fuction)
    # We will not allow this for now, there is most likely no need for it
    assert varargs is None, 'no varargs for action functions'
    assert keywords is None, 'no keywords for action functions'

    # XXX: Quick hack
    if defaults is None:
        defaults = []

    # These arguments already has default values
    default_val_by_arg = {}
    for arg, default_val in izip(args[-len(defaults):], defaults):
        default_val_by_arg[arg] = default_val

    action_args = []
    for arg_name in args:
        arg_val = params.getvalue(arg_name)

        # The client failed to provide this argument
        if arg_val is None:
            try:
                arg_val = default_val_by_arg[arg_name]
            except KeyError:
                raise InvalidActionArgsError(action, arg_name)

        action_args.append(arg_val)

    log_info('dispatcher will call %s(%s)' % (action,
        ', '.join((repr(a) for a in action_args)), ))

    json_dic = action_fuction(*action_args)

    # Assign which action that was performed to the json_dic
    json_dic['action'] = action
    return json_dic