Search in sources :

Example 6 with Response

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();
}
Also used : Response(de.ids_mannheim.korap.response.Response) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) MatchCollectorDB(de.ids_mannheim.korap.response.collector.MatchCollectorDB) Krill(de.ids_mannheim.korap.Krill) SQLException(java.sql.SQLException) MatchCollector(de.ids_mannheim.korap.response.MatchCollector) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 7 with Response

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());
}
Also used : Response(de.ids_mannheim.korap.response.Response) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 8 with Response

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());
}
Also used : Response(de.ids_mannheim.korap.response.Response) Ignore(org.junit.Ignore)

Example 9 with Response

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());
}
Also used : Response(de.ids_mannheim.korap.response.Response) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 10 with Response

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();
}
Also used : Response(de.ids_mannheim.korap.response.Response) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

Response (de.ids_mannheim.korap.response.Response)14 Produces (javax.ws.rs.Produces)9 Path (javax.ws.rs.Path)7 Consumes (javax.ws.rs.Consumes)4 GET (javax.ws.rs.GET)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Krill (de.ids_mannheim.korap.Krill)3 KrillIndex (de.ids_mannheim.korap.KrillIndex)3 POST (javax.ws.rs.POST)3 Test (org.junit.Test)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 PUT (javax.ws.rs.PUT)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 KrillCollection (de.ids_mannheim.korap.KrillCollection)1 FieldDocument (de.ids_mannheim.korap.index.FieldDocument)1 MatchCollector (de.ids_mannheim.korap.response.MatchCollector)1 Result (de.ids_mannheim.korap.response.Result)1 MatchCollectorDB (de.ids_mannheim.korap.response.collector.MatchCollectorDB)1 QueryException (de.ids_mannheim.korap.util.QueryException)1