Example #1
0
def main():
    opts, args = parse_opts()
    logging.basicConfig(filename=opts.log_file, level=logging.DEBUG,
                        format='[%(asctime)s|%(levelname)s|%(name)s|%(threadName)s|%(message)s]')

    solr = SolrConnection(opts.solr_uri)
    protocol = LineProtocol()
    for request in protocol.input():
        try:
            query = build_query(request)
            if query is None:
                protocol.output(query_failed(), True)
                continue
            log.debug("Running query: " + str(query))
            results = solr.search(**query)
            if results is None:
                protocol.output({'code' : 400})
                continue
            resp = json.loads(results)
            ret = {
                'code' : 200,
                'json' : resp['response']
            }
            protocol.output(ret, True)
        except Exception:
            log.exception("Uncaught exception")
    return 0
Example #2
0
def eval_loop(updater):
    protocol = LineProtocol()
    for notify in protocol.input():
        log.debug("Received update notification: " + str(notify))

        try:
            db = notify['db']
            taipu = notify['type']
        except KeyError:
            log.exception("Expected keys 'db' and 'type' not found")
            continue

        try:
            if taipu == 'updated':
                updater.update_index(db)
            elif taipu == 'deleted':
                updater.delete_database(db)
            else:
                log.error("Unknown update notification: %s" % taipu)
        except Exception:
            log.exception("Uncaught exception")
            continue
Example #3
0
def process_requests():
    # TODO: Change the admin user and password in the next line
    manager = AccountManager("admin", "password")

    protocol = LineProtocol()
    for req in protocol.input():
        if (req["headers"].has_key("Content-Type")
                and req["headers"]["Content-Type"].lower()
                == "application/x-www-form-urlencoded"):
            log.debug("Data received as application/x-www-form-urlencoded")
            formdata = urlparse.parse_qs(req["body"])
        else:
            log.debug("Data received as query string")
            formdata = req["query"]

        if not (formdata.has_key("username")) or formdata["username"] is None:
            log.info('Attempt to create account without an account name')
            protocol.outputJSON(code=400,
                                data={
                                    "error": "Bad Request",
                                    "reason": "Username must be supplied"
                                })
            continue
        elif not (formdata.has_key("passwd")) or formdata["passwd"] is None:
            log.info('Attempt to create account without a password')
            protocol.outputJSON(code=400,
                                data={
                                    "error": "Bad Request",
                                    "reason": "Password must be supplied"
                                })
            continue

        if (req["headers"].has_key("Content-Type")
                and req["headers"]["Content-Type"].lower()
                == "application/x-www-form-urlencoded"):
            username = formdata["username"][0]
            passwd = formdata["passwd"][0]
        else:
            username = formdata["username"]
            passwd = formdata["passwd"]

        try:
            # TODO: Change the name of the user database template in the next line
            manager.create_account(username, passwd, "template_db")
            protocol.outputJSON(code=201, data={"result": "ok"})

        except RegistrationError, e:
            log.info("Validation error creating user %s. Reason %s", username,
                     e)
            protocol.outputJSON(code=400,
                                data={
                                    "error": "Registration Failure",
                                    "reason": e.value
                                })
            continue
        except couchdb.http.Unauthorized:
            log.error(
                "Could not complete account creation for %s. Admin account not authorized successfully"
                % username)
            protocol.outputJSON(code=500,
                                data={
                                    "error": "Server Error",
                                    "reason": "Server misconfigured"
                                })
Example #4
0
def process_requests():
    # TODO: Change the admin user and password in the next line
    manager = AccountManager("admin", "password")

    protocol = LineProtocol()
    for req in protocol.input():
        if (req["headers"].has_key("Content-Type")
            and req["headers"]["Content-Type"].lower() == "application/x-www-form-urlencoded"):
            log.debug("Data received as application/x-www-form-urlencoded")
            formdata = urlparse.parse_qs(req["body"])
        else:
            log.debug("Data received as query string")
            formdata = req["query"]

        if not (formdata.has_key("username")) or formdata["username"] is None:
            log.info('Attempt to create account without an account name')
            protocol.outputJSON(code=400, data={"error":"Bad Request", "reason":"Username must be supplied"})
            continue
        elif not (formdata.has_key("passwd")) or formdata["passwd"] is None:
            log.info('Attempt to create account without a password')
            protocol.outputJSON(code=400, data={"error":"Bad Request", "reason":"Password must be supplied"})
            continue

        if (req["headers"].has_key("Content-Type")
            and req["headers"]["Content-Type"].lower() == "application/x-www-form-urlencoded"):
            username = formdata["username"][0]
            passwd = formdata["passwd"][0]
        else:
            username = formdata["username"]
            passwd = formdata["passwd"]

        try:
            # TODO: Change the name of the user database template in the next line
            manager.create_account(username, passwd, "template_db")
            protocol.outputJSON(code=201, data={"result" : "ok"})

        except RegistrationError, e:
            log.info("Validation error creating user %s. Reason %s", username, e)
            protocol.outputJSON(code=400, data={"error":"Registration Failure", "reason" : e.value})
            continue
        except couchdb.http.Unauthorized:
            log.error("Could not complete account creation for %s. Admin account not authorized successfully" % username)
            protocol.outputJSON(code=500, data={"error":"Server Error", "reason": "Server misconfigured" })