'Respond to an incoming registration request
@param r: the S3Request
@param attr: controller parameters for the request'

def __register(self, r, **attr):

    from s3validators import JSONERRORS
    source = r.read_body()
    if (not source):
        r.error(400, 'Missing    parameters')
    try:
        parameters = json.load(source[0])
    except JSONERRORS:
        r.error(400, ('Invalid    parameters:    %s' % sys.exc_info()[1]))
    log = self.log
    result = log.SUCCESS
    repository_id = None
    ruid = parameters.get('uuid')
    if ruid:
        db = current.db
        rtable = current.s3db.sync_repository
        row = db((rtable.uuid == ruid)).select(rtable.id, limitby=(0, 1)).first()
        permitted = current.auth.s3_has_permission
        if row:
            repository_id = row.id
            if (not permitted('update', rtable, record_id=repository_id)):
                r.unauthorised()
            data = {'deleted': False}
        else:
            if (not permitted('create', rtable)):
                r.unauthorised()
            data = {'uuid': ruid}
        apitype = parameters.get('apitype')
        if apitype:
            data['apitype'] = apitype
        name = parameters.get('name')
        if name:
            data['name'] = name
        if row:
            success = row.update_record(**data)
            if success:
                message = 'Registration    update    successful'
            else:
                result = log.ERROR
                message = 'Registration    update    failed'
        else:
            repository_id = rtable.insert(**data)
            if repository_id:
                message = 'Registration    successful'
            else:
                result = log.ERROR
                message = 'Registration    failed'
    else:
        result = log.ERROR
        message = 'No    repository    identifier    specified'
    if (result == log.SUCCESS):
        output = current.xml.json_message(message=message, sender=('%s' % self.config.uuid))
    else:
        output = current.xml.json_message(False, 400, message=message, sender=('%s' % self.config.uuid))
    current.response.headers['Content-Type'] = 'application/json'
    log.write(repository_id=repository_id, resource_name=log.NONE, transmission=log.IN, mode=log.REGISTER, result=result, message=message)
    return output
