use of de.ids_mannheim.korap.response.Response in project Krill by KorAP.
the class Resource method collect.
/**
* Collect matches and aggregate the UIDs plus matchcount in the
* database.
*
* @param text_id
*/
@PUT
@Path("/collect/{resultID}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String collect(String json, @PathParam("resultID") String resultID, @Context UriInfo uri) {
Response kresp = _initResponse();
if (kresp.hasErrors())
return kresp.toJsonString();
// Get the database
try {
final MatchCollectorDB mc = new MatchCollectorDB(1000, "Res_" + resultID);
final ComboPooledDataSource pool = Node.getDBPool();
mc.setDBPool("mysql", pool, pool.getConnection());
// TODO: Only search in self documents (REPLICATION FTW!)
final Krill ks = new Krill(json);
// TODO: Reuse response!
final MatchCollector result = Node.getIndex().collect(ks, mc);
result.setNode(Node.getName());
return result.toJsonString();
} catch (SQLException e) {
log.error(e.getLocalizedMessage());
}
;
kresp.addError(604, "Unable to connect to database");
return kresp.toJsonString();
}
use of de.ids_mannheim.korap.response.Response in project Krill by KorAP.
the class TestResponse method testResponse.
@Test
public void testResponse() throws IOException {
Response resp = new Response();
JsonNode respJson = mapper.readTree(resp.toJsonString());
assertEquals("http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", respJson.at("/@context").asText());
assertEquals("", respJson.at("/meta").asText());
resp.setVersion("0.24");
resp.setNode("Tanja");
assertEquals("0.24", resp.getVersion());
assertEquals("Tanja", resp.getNode());
assertFalse(resp.hasWarnings());
assertFalse(resp.hasMessages());
assertFalse(resp.hasErrors());
respJson = mapper.readTree(resp.toJsonString());
assertEquals("0.24", respJson.at("/meta/version").asText());
assertEquals("Tanja", respJson.at("/meta/node").asText());
resp.setName("Index");
respJson = mapper.readTree(resp.toJsonString());
assertEquals("Index-0.24", respJson.at("/meta/version").asText());
assertEquals("Tanja", respJson.at("/meta/node").asText());
resp.setBenchmark("took a while");
resp.setListener("localhost:3000");
respJson = mapper.readTree(resp.toJsonString());
assertEquals("localhost:3000", respJson.at("/meta/listener").asText());
assertEquals("took a while", respJson.at("/meta/benchmark").asText());
}
use of de.ids_mannheim.korap.response.Response in project Krill by KorAP.
the class TestResponse method testResponseDeserialzation.
// TODO: Skip this for the moment and refactor later
@Ignore
public void testResponseDeserialzation() throws IOException {
String jsonResponse = "{\"version\":\"0.38\"}";
Response kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("0.38", kresp.getVersion());
assertNull(kresp.getName());
assertEquals(jsonResponse, kresp.toJsonString());
jsonResponse = "{\"meta\":{\"version\":\"seaweed-0.49\"}}";
kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("0.49", kresp.getVersion());
assertEquals("seaweed", kresp.getName());
assertTrue(kresp.toJsonString().contains("seaweed-0.49"));
jsonResponse = "{\"version\":\"seaweed-\"}";
kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("seaweed-", kresp.getVersion());
assertNull(kresp.getName());
assertTrue(kresp.toJsonString().contains("seaweed-"));
jsonResponse = "{\"timeExceeded\":true}";
kresp = mapper.readValue(jsonResponse, Response.class);
assertTrue(kresp.hasTimeExceeded());
assertTrue(kresp.hasWarnings());
jsonResponse = "{\"benchmark\":\"40.5s\", \"foo\":\"bar\"}";
kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("40.5s", kresp.getBenchmark());
jsonResponse = "{\"listener\":\"10.0.10.14:678\", \"foo\":\"bar\"}";
kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("10.0.10.14:678", kresp.getListener());
jsonResponse = "{\"node\":\"tanja\", \"foo\":\"bar\"}";
kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("tanja", kresp.getNode());
jsonResponse = "{\"node\":\"tanja\", \"version\":\"seaweed-0.49\", " + " \"benchmark\":\"40.5s\", \"listener\":\"10.0.10.14:678\"," + "\"timeExceeded\":true }";
kresp = mapper.readValue(jsonResponse, Response.class);
assertEquals("0.49", kresp.getVersion());
assertEquals("seaweed", kresp.getName());
assertEquals("40.5s", kresp.getBenchmark());
assertEquals("10.0.10.14:678", kresp.getListener());
assertEquals("tanja", kresp.getNode());
assertTrue(kresp.hasTimeExceeded());
assertTrue(kresp.hasWarnings());
jsonResponse = "{\"warnings\":[[123,\"This is a warning\"]," + "[124,\"This is a second warning\"]]," + "\"errors\":[[125,\"This is a single error\"]], " + " \"node\":\"tanja\", \"version\":\"seaweed-0.49\", " + " \"benchmark\":\"40.5s\", \"listener\":\"10.0.10.14:678\"," + "\"timeExceeded\":true }";
kresp = mapper.readValue(jsonResponse, Response.class);
assertTrue(kresp.hasWarnings());
assertTrue(kresp.hasErrors());
assertFalse(kresp.hasMessages());
assertEquals(kresp.getError(0).getMessage(), "This is a single error");
// THIS MAY BREAK!
assertEquals(kresp.getWarning(0).getMessage(), "This is a warning");
assertEquals(kresp.getWarning(1).getMessage(), "This is a second warning");
assertEquals(kresp.getWarning(2).getMessage(), "Response time exceeded");
assertEquals("0.49", kresp.getVersion());
assertEquals("seaweed", kresp.getName());
assertEquals("40.5s", kresp.getBenchmark());
assertEquals("10.0.10.14:678", kresp.getListener());
assertEquals("tanja", kresp.getNode());
assertTrue(kresp.hasTimeExceeded());
}
use of de.ids_mannheim.korap.response.Response in project Krill by KorAP.
the class TestResponse method testResponseJSONadd.
@Test
public void testResponseJSONadd() throws IOException {
Response resp = new Response();
ObjectNode jNode = mapper.createObjectNode();
jNode.put("Hui", "works");
resp.addJsonNode("test", jNode);
JsonNode respJson = mapper.readTree(resp.toJsonString());
assertEquals("http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", respJson.at("/@context").asText());
assertEquals("works", respJson.at("/test/Hui").asText());
}
use of de.ids_mannheim.korap.response.Response in project Krill by KorAP.
the class Resource method commit.
// TODO: Commit changes to the index before the server dies!
/**
* Commit data changes to the index
*/
@POST
@Path("/index")
@Produces(MediaType.APPLICATION_JSON)
public String commit() {
final Response kresp = _initResponse();
if (kresp.hasErrors())
return kresp.toJsonString();
// There are documents to commit
try {
Node.getIndex().commit();
kresp.addMessage(683, "Staged data committed");
} catch (IOException e) {
// Set HTTP to ???
kresp.addError(603, "Unable to commit staged data to index");
return kresp.toJsonString();
}
;
// Set HTTP to ???
return kresp.toJsonString();
}
Aggregations