Skip to content

Commit

Permalink
Merge pull request #671 from nitishgupta/master
Browse files Browse the repository at this point in the history
Added get and post functions for adding Views to JsonStr serialized TA
  • Loading branch information
Daniel Khashabi authored Jul 25, 2018
2 parents 42896ef + d3674a5 commit 7a369ea
Showing 1 changed file with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ public static void startServer(String[] args) {
}
});

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 . . . ");
Expand All @@ -150,6 +171,31 @@ public static void startServer(String[] args) {
}
);

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()) {
Expand Down Expand Up @@ -209,6 +255,44 @@ private static String annotateText(AnnotatorService finalPipeline, String text,
}
}

private static String addAdditionalViewToTA(AnnotatorService finalPipeline, String jsonStrTA, String views,
Logger logger) throws AnnotatorException {
if (views == null || jsonStrTA == null) {
return "The parameters 'jsonstr' and/or 'views' are not specified.";
} else {
logger.info("------------------------------");
logger.info("Views to add: " + views);
String[] viewsInArray = views.split(",");
logger.info("Adding the basic annotations . . . ");

TextAnnotation ta = null;
try {
ta = SerializationHelper.deserializeFromJson(jsonStrTA);
} catch (Exception e) {
logger.error("Error reading TA from JsonStr . . . ");
}

if (ta == null) {
logger.info("Error reading TA from JsonStr. Views cannot be added.");
return jsonStrTA;
}

for (String vuName : viewsInArray) {
logger.info("Adding the view: ->" + vuName.trim() + "<-");
try {
finalPipeline.addView(ta, vuName.trim());
} catch (Exception e) {
e.printStackTrace();
}
printMemoryDetails(logger);
}
logger.info("Done adding the views. Serializing the view now.");
String output = SerializationHelper.serializeToJson(ta);
logger.info("Done. Sending the result back. ");
return output;
}
}

private static void enableCORS(final String origin, final String methods, final String headers) {
before((Filter) (request, response) -> {
response.header("Access-Control-Allow-Origin", origin);
Expand Down

0 comments on commit 7a369ea

Please sign in to comment.