Search in sources :

Example 26 with Namespace

use of net.sourceforge.argparse4j.inf.Namespace in project cogcomp-nlp by CogComp.

the class MainServer method startServer.

public static void startServer(String[] args, Logger logger) {
    Namespace parseResults;
    try {
        parseResults = argumentParser.parseArgs(args);
    } catch (HelpScreenException ex) {
        return;
    } catch (ArgumentParserException ex) {
        logger.error("Exception while parsing arguments", ex);
        return;
    }
    port(parseResults.getInt("port"));
    // create a hashmap to keep track of client ip addresses and their
    int rate = parseResults.getInt("rate");
    if (rate > 0) {
        clients = new HashMap<String, Integer>();
    }
    AnnotatorService finalPipeline = pipeline;
    get("/annotate", "application/json", (request, response) -> {
        logger.info("GET request . . . ");
        boolean canServe = true;
        if (rate > 0) {
            resetServer();
            String ip = request.ip();
            int callsSofar = (Integer) clients.getOrDefault(ip, 0);
            if (callsSofar > rate)
                canServe = false;
            clients.put(ip, callsSofar + 1);
        }
        if (canServe) {
            logger.info("request.body(): " + request.body());
            String text = request.queryParams("text");
            String views = request.queryParams("views");
            return annotateText(finalPipeline, text, views, logger);
        } else {
            response.status(429);
            return "You have reached your maximum daily query limit :-/ ";
        }
    });
    post("/annotate", (request, response) -> {
        logger.info("POST request . . . ");
        boolean canServe = true;
        if (rate > 0) {
            resetServer();
            String ip = request.ip();
            int callsSofar = (Integer) clients.getOrDefault(ip, 0);
            if (callsSofar > rate)
                canServe = false;
            clients.put(ip, callsSofar + 1);
        }
        if (canServe) {
            logger.info("request.body(): " + request.body());
            Map<String, String> map = splitQuery(request.body());
            System.out.println("POST body parameters parsed: " + map);
            String text = map.get("text");
            String views = map.get("views");
            return annotateText(finalPipeline, text, views, logger);
        } else {
            response.status(429);
            return "You have reached your maximum daily query limit :-/ ";
        }
    });
    // api to get name of the available views
    String viewsString = "";
    for (String view : pipeline.getAvailableViews()) {
        viewsString += ", " + view;
    }
    String finalViewsString = viewsString;
    enableCORS("*", "*", "*");
    get("/viewNames", (req, res) -> finalViewsString);
    post("/viewNames", (req, res) -> finalViewsString);
}
Also used : AnnotatorService(edu.illinois.cs.cogcomp.annotation.AnnotatorService) HelpScreenException(net.sourceforge.argparse4j.internal.HelpScreenException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Example 27 with Namespace

use of net.sourceforge.argparse4j.inf.Namespace in project dropwizard by dropwizard.

the class DbTagCommandTest method testRun.

@Test
public void testRun() throws Exception {
    // Migrate some DDL changes
    final TestMigrationConfiguration conf = createConfiguration(getDatabaseUrl());
    final DbMigrateCommand<TestMigrationConfiguration> dbMigrateCommand = new DbMigrateCommand<>(new TestMigrationDatabaseConfiguration(), TestMigrationConfiguration.class, migrationsFileName);
    dbMigrateCommand.run(null, new Namespace(ImmutableMap.of()), conf);
    // Tag them
    dbTagCommand.run(null, new Namespace(ImmutableMap.of("tag-name", ImmutableList.of("v1"))), conf);
    // Verify that the tag exists
    try (CloseableLiquibase liquibase = dbTagCommand.openLiquibase(conf.getDataSource(), new Namespace(ImmutableMap.of()))) {
        assertThat(liquibase.tagExists("v1")).isTrue();
    }
}
Also used : Namespace(net.sourceforge.argparse4j.inf.Namespace) Test(org.junit.Test)

Example 28 with Namespace

use of net.sourceforge.argparse4j.inf.Namespace in project dropwizard by dropwizard.

the class DbCalculateChecksumCommandTest method testRun.

@Test
public void testRun() throws Exception {
    final AtomicBoolean checkSumVerified = new AtomicBoolean();
    migrateCommand.setCheckSumConsumer(checkSum -> {
        assertThat(checkSum).isEqualTo(CheckSum.parse("7:3a61a7a72c9ce082b7059215975e6e09"));
        checkSumVerified.set(true);
    });
    migrateCommand.run(null, new Namespace(ImmutableMap.of("id", ImmutableList.of("2"), "author", ImmutableList.of("db_dev"))), createConfiguration(getDatabaseUrl()));
    assertThat(checkSumVerified.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Namespace(net.sourceforge.argparse4j.inf.Namespace) Test(org.junit.Test)

Example 29 with Namespace

use of net.sourceforge.argparse4j.inf.Namespace in project dropwizard by dropwizard.

the class DbCommandTest method testRunSubCommand.

@Test
public void testRunSubCommand() throws Exception {
    final String databaseUrl = getDatabaseUrl();
    final TestMigrationConfiguration conf = createConfiguration(databaseUrl);
    dbCommand.run(null, new Namespace(ImmutableMap.of("subcommand", "migrate")), conf);
    try (Handle handle = new DBI(databaseUrl, "sa", "").open()) {
        assertThat(handle.createQuery("select count(*) from persons").mapTo(Integer.class).first()).isEqualTo(1);
    }
}
Also used : DBI(org.skife.jdbi.v2.DBI) Namespace(net.sourceforge.argparse4j.inf.Namespace) Handle(org.skife.jdbi.v2.Handle) Test(org.junit.Test)

Example 30 with Namespace

use of net.sourceforge.argparse4j.inf.Namespace in project dropwizard by dropwizard.

the class DbLocksCommandTest method testFailsWhenNoListOrRelease.

@Test
public void testFailsWhenNoListOrRelease() throws Exception {
    final Liquibase liquibase = Mockito.mock(Liquibase.class);
    assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> locksCommand.run(new Namespace(ImmutableMap.of("list", false, "release", false)), liquibase)).withMessage("Must specify either --list or --force-release");
}
Also used : Liquibase(liquibase.Liquibase) Namespace(net.sourceforge.argparse4j.inf.Namespace) Test(org.junit.Test)

Aggregations

Namespace (net.sourceforge.argparse4j.inf.Namespace)46 Test (org.junit.Test)36 PrintStream (java.io.PrintStream)9 Handle (org.skife.jdbi.v2.Handle)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Liquibase (liquibase.Liquibase)6 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)6 DBI (org.skife.jdbi.v2.DBI)6 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)5 IOException (java.io.IOException)4 Properties (java.util.Properties)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 File (java.io.File)3 Configuration (io.dropwizard.Configuration)2 Bootstrap (io.dropwizard.setup.Bootstrap)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Element (org.w3c.dom.Element)2