Search in sources :

Example 1 with HelpScreenException

use of net.sourceforge.argparse4j.internal.HelpScreenException 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 2 with HelpScreenException

use of net.sourceforge.argparse4j.internal.HelpScreenException in project kafka by apache.

the class Commands method parseCommand.

/**
 * Handle the given command.
 *
 * In general this function should not throw exceptions. Instead, it should
 * return ErroneousCommandHandler if the input was invalid.
 *
 * @param arguments     The command line arguments.
 * @return              The command handler.
 */
public Handler parseCommand(List<String> arguments) {
    List<String> trimmedArguments = new ArrayList<>(arguments);
    while (true) {
        if (trimmedArguments.isEmpty()) {
            return new NoOpCommandHandler();
        }
        String last = trimmedArguments.get(trimmedArguments.size() - 1);
        if (!last.isEmpty()) {
            break;
        }
        trimmedArguments.remove(trimmedArguments.size() - 1);
    }
    Namespace namespace;
    try {
        namespace = parser.parseArgs(trimmedArguments.toArray(new String[0]));
    } catch (HelpScreenException e) {
        return new NoOpCommandHandler();
    } catch (ArgumentParserException e) {
        return new ErroneousCommandHandler(e.getMessage());
    }
    String command = namespace.get("command");
    if (!command.equals(trimmedArguments.get(0))) {
        return new ErroneousCommandHandler("invalid choice: '" + trimmedArguments.get(0) + "': did you mean '" + command + "'?");
    }
    Type type = TYPES.get(command);
    if (type == null) {
        return new ErroneousCommandHandler("Unknown command specified: " + command);
    } else {
        return type.createHandler(namespace);
    }
}
Also used : HelpScreenException(net.sourceforge.argparse4j.internal.HelpScreenException) ArrayList(java.util.ArrayList) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Example 3 with HelpScreenException

use of net.sourceforge.argparse4j.internal.HelpScreenException in project cogcomp-nlp by CogComp.

the class MainServer method startServer.

public static void startServer(String[] args) {
    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 :-/ ";
        }
    });
    get("/addviews", "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 jsonStrTA = request.queryParams("jsonstr");
            String views = request.queryParams("views");
            return addAdditionalViewToTA(finalPipeline, jsonStrTA, 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 :-/ ";
        }
    });
    post("/addviews", (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 jsonStrTA = map.get("jsonstr");
            String views = map.get("views");
            return addAdditionalViewToTA(finalPipeline, jsonStrTA, 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);
    get("/version", "application/json", (request, response) -> {
        logger.info("GET request to retrieve version numbers . . . ");
        final Properties properties = new Properties();
        properties.load(pipeline.getClass().getClassLoader().getResourceAsStream("project.properties"));
        System.out.println(properties.getProperty("version"));
        System.out.println(properties.getProperty("artifactId"));
        return properties.getProperty("version");
    });
}
Also used : AnnotatorService(edu.illinois.cs.cogcomp.annotation.AnnotatorService) HelpScreenException(net.sourceforge.argparse4j.internal.HelpScreenException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Properties(java.util.Properties) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Example 4 with HelpScreenException

use of net.sourceforge.argparse4j.internal.HelpScreenException in project cogcomp-nlp by CogComp.

the class ExternalAnnotatorsServer 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)

Aggregations

ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)4 Namespace (net.sourceforge.argparse4j.inf.Namespace)4 HelpScreenException (net.sourceforge.argparse4j.internal.HelpScreenException)4 AnnotatorService (edu.illinois.cs.cogcomp.annotation.AnnotatorService)3 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1