Added resources to server
diff --git a/src/main/java/de/ids_mannheim/korap/KorapIndex.java b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
index 7a75071..54932da 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapIndex.java
@@ -606,6 +606,7 @@
* are of interest.
*/
StringBuilder regex = new StringBuilder();
+ // TODO: Make these static
Pattern harmlessFoundry = Pattern.compile("^[-a-zA-Z0-9_]+$");
Pattern harmlessLayer = Pattern.compile("^[-a-zA-Z0-9_:]+$");
Iterator<String> iter;
diff --git a/src/main/java/de/ids_mannheim/korap/KorapNode.java b/src/main/java/de/ids_mannheim/korap/KorapNode.java
index 1155cae..14c57c3 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapNode.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapNode.java
@@ -4,9 +4,17 @@
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
import java.io.IOException;
import java.net.URI;
+import de.ids_mannheim.korap.KorapIndex;
+import org.apache.lucene.store.MMapDirectory;
+
+
/**
* Standalone REST-Service for the Lucene Search Backend.
*
@@ -17,14 +25,30 @@
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/";
+ // Logger
+ private final static Logger log = LoggerFactory.getLogger(KorapNode.class);
+
+ // Index
+ private static KorapIndex index;
+
+ /*
+ Todo: Use korap.config for paths to
+ indexDirectory
+ */
+ private static String path = new String("/home/ndiewald/Repositories/korap/KorAP-modules/KorAP-lucene-index/sandbox/index");
+
+
+
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
+
// create a resource config that scans for JAX-RS resources and providers
// in de.ids_mannheim.korap.server package
- final ResourceConfig rc = new ResourceConfig().packages("de.ids_mannheim.korap.server");
+ final ResourceConfig rc =
+ new ResourceConfig().packages("de.ids_mannheim.korap.server");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
@@ -42,6 +66,36 @@
// WADL available at BASE_URI + application.wadl
System.in.read();
server.stop();
- }
-}
+ };
+ public static KorapIndex getIndex () {
+ if (index != null)
+ return index;
+
+ try {
+ File file = new File(path);
+
+ log.info("Loading index from {}", path);
+ if (!file.exists()) {
+ log.error("Index not found at {}", path);
+ return null;
+ };
+
+ System.out.println("Loading index from " + path);
+
+ index = new KorapIndex(new MMapDirectory(file));
+ return index;
+ /*
+ // Temporarily!
+ static String path = new String();
+
+ this.index = new KorapIndex(new MMapDirectory(f));
+ return this.index;
+ */
+ }
+ catch (IOException e) {
+ log.error("Index not loadable at {}: {}", path, e.getMessage());
+ };
+ return null;
+ };
+};
diff --git a/src/main/java/de/ids_mannheim/korap/server/Resource.java b/src/main/java/de/ids_mannheim/korap/server/Resource.java
new file mode 100644
index 0000000..f71e9cb
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/server/Resource.java
@@ -0,0 +1,157 @@
+package de.ids_mannheim.korap.server;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.core.MultivaluedMap;
+
+import de.ids_mannheim.korap.KorapNode;
+import de.ids_mannheim.korap.KorapIndex;
+import de.ids_mannheim.korap.KorapSearch;
+import de.ids_mannheim.korap.KorapMatch;
+import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.util.QueryException;
+
+import java.util.List;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+/**
+ * Root resource (exposed at root path)
+ *
+ * @author Nils Diewald
+ *
+ * Look at http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
+ */
+@Path("/")
+public class Resource {
+
+ static Pattern p = Pattern.compile("\\s*(?i:false|null)\\s*");
+
+ private static boolean isNull (String value) {
+ if (value == null)
+ return true;
+
+ Matcher m = p.matcher(value);
+ if (m.matches())
+ return true;
+
+ return false;
+ };
+
+ /**
+ * Search the lucene index.
+ *
+ * @param json JSON-LD string with search and potential meta filters.
+ */
+ @POST
+ @Path("/search")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public String search (String json) {
+
+ // Get index
+ KorapIndex index = KorapNode.getIndex();
+
+ // Search index
+ if (index != null)
+ return new KorapSearch(json).run(index).toJSON();
+
+ // Response with error message
+ KorapResult kr = new KorapResult();
+ kr.setError("Index not found");
+ return kr.toJSON();
+ };
+
+ @GET
+ @Path("/match/{matchID}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public String match (@PathParam("matchID") String id,
+ @Context UriInfo uri) {
+
+ // Get index
+ KorapIndex index = KorapNode.getIndex();
+
+ // Search index
+ if (index != null) {
+
+ // Get query parameters
+ MultivaluedMap<String,String> qp = uri.getQueryParameters();
+
+ boolean includeSpans = false,
+ includeHighlights = true,
+ extendToSentence = false,
+ info = false;
+
+ // Optional query parameter "info" for more information on the match
+ if (!isNull(qp.getFirst("info")))
+ info = true;
+
+ // Optional query parameter "spans" for span information inclusion
+ if (!isNull(qp.getFirst("spans"))) {
+ includeSpans = true;
+ info = true;
+ };
+
+ // Optional query parameter "highlights" for highlight information inclusion
+ String highlights = qp.getFirst("highlights");
+ if (highlights != null && isNull(highlights))
+ includeHighlights = false;
+
+ // Optional query parameter "extended" for sentence expansion
+ if (!isNull(qp.getFirst("extended")))
+ extendToSentence = true;
+
+ List<String> foundries = qp.get("foundry");
+ List<String> layers = qp.get("layer");
+
+ try {
+ // Get match info
+ return index.getMatchInfo(
+ id,
+ "tokens",
+ info,
+ foundries,
+ layers,
+ includeSpans,
+ includeHighlights,
+ extendToSentence
+ ).toJSON();
+ }
+
+ // Nothing found
+ catch (QueryException qe) {
+ KorapMatch km = new KorapMatch();
+ km.setError(qe.getMessage());
+ return km.toJSON();
+ }
+ };
+
+ // Response with error message
+ KorapMatch km = new KorapMatch();
+ km.setError("Index not found");
+ return km.toJSON();
+ };
+
+ @POST
+ @Path("/collection")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public String collection (String json) {
+
+ // Get index
+ KorapIndex index = KorapNode.getIndex();
+
+ if (index == null)
+ return "{\"documents\" : -1, error\" : \"No index given\" }";
+
+ return "{}";
+ };
+};
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
index 6de0cdd..7c2b69a 100644
--- a/src/main/resources/log4j.properties
+++ b/src/main/resources/log4j.properties
@@ -22,10 +22,10 @@
#log4j.logger.de.ids_mannheim.korap.KorapFilter = TRACE, stdout
#log4j.logger.de.ids_mannheim.korap.KorapCollection = TRACE, stdout
-
# Results:
#log4j.logger.de.ids_mannheim.korap.KorapIndex = TRACE, stdout
-# log4j.logger.de.ids_mannheim.korap.KorapMatch = TRACE, stdout
+#log4j.logger.de.ids_mannheim.korap.KorapNode = TRACE, stdout
+#log4j.logger.de.ids_mannheim.korap.KorapMatch = TRACE, stdout
#log4j.logger.de.ids_mannheim.korap.index.PositionsToOffset = TRACE, stdout
#log4j.logger.de.ids_mannheim.korap.index.TestSegmentIndex = TRACE, stdout