Added API URL versioning.

Change-Id: I0000000000000000000000000000000000000000
diff --git a/core/Changes b/core/Changes
index f9c688b..0a2d333 100644
--- a/core/Changes
+++ b/core/Changes
@@ -1,3 +1,7 @@
+version 0.61.1
+28/08/2018
+    - Added API URL versioning (margaretha)
+
 version 0.61.0
 13/08/2018
 	- Updated Krill and Koral versions enabling VC caching and referencing (margaretha)
diff --git a/core/pom.xml b/core/pom.xml
index 7f9e282..3b7af23 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>de.ids_mannheim.korap</groupId>
 	<artifactId>Kustvakt-core</artifactId>
-	<version>0.61.0</version>
+	<version>0.61.1</version>
 
 	<properties>
 		<java.version>1.8</java.version>
diff --git a/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index ecd0e75..0a80e90 100644
--- a/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -1,14 +1,16 @@
 package de.ids_mannheim.korap.config;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -79,6 +81,8 @@
     private String baseURL;
     private Properties properties;
 
+    private Set<String> version;
+    
     // deprec?!
     private final BACKENDS DEFAULT_ENGINE = BACKENDS.LUCENE;
 
@@ -96,6 +100,13 @@
      * @throws Exception
      */
     protected void load (Properties properties) throws Exception {
+        String version = properties.getProperty("supported.api.version", "");
+        if (version.isEmpty()){
+            throw new IllegalArgumentException("supported.api.version must be set in the .conf file");
+        }
+        this.version = Arrays.stream(version.split(" ")).collect(Collectors.toSet());
+        
+        
         baseURL = properties.getProperty("kustvakt.base.url", "/api/*");
         maxhits = new Integer(properties.getProperty("maxhits", "50000"));
         returnhits = new Integer(properties.getProperty("returnhits", "50000"));
@@ -152,7 +163,6 @@
         // passcodeSaltField =
         // properties.getProperty("security.passcode.salt",
         // "accountCreation");
-
     }
 
     /**
diff --git a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
index a93dbc2..1194bd9 100644
--- a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
+++ b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
@@ -25,7 +25,8 @@
     public static final int NOT_ALLOWED = 109;
     public static final int HTTPS_REQUIRED = 110;
     public static final int INVALID_ALGORITHM = 111;
-
+    public static final int UNSUPPORTED_API_VERSION = 112;
+    
     /**
      * 200 status codes general JSON serialization error
      */
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/APIVersionFilter.java b/core/src/main/java/de/ids_mannheim/korap/web/APIVersionFilter.java
new file mode 100644
index 0000000..886e475
--- /dev/null
+++ b/core/src/main/java/de/ids_mannheim/korap/web/APIVersionFilter.java
@@ -0,0 +1,57 @@
+package de.ids_mannheim.korap.web;
+
+import java.util.List;
+
+import javax.ws.rs.core.PathSegment;
+import javax.ws.rs.ext.Provider;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import com.sun.jersey.spi.container.ContainerRequest;
+import com.sun.jersey.spi.container.ContainerRequestFilter;
+import com.sun.jersey.spi.container.ContainerResponseFilter;
+import com.sun.jersey.spi.container.ResourceFilter;
+
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+
+/** Checks API version in URL path. 
+ * 
+ * @author margaretha
+ *
+ */
+@Component
+@Provider
+public class APIVersionFilter implements ContainerRequestFilter, ResourceFilter {
+
+    @Autowired
+    private KustvaktConfiguration config;
+    @Autowired
+    private CoreResponseHandler kustvaktResponseHandler;
+
+    @Override
+    public ContainerRequestFilter getRequestFilter () {
+        return this;
+    }
+
+    @Override
+    public ContainerResponseFilter getResponseFilter () {
+        return null;
+    }
+
+    @Override
+    public ContainerRequest filter (ContainerRequest request) {
+        List<PathSegment> pathSegments = request.getPathSegments();
+        String version = pathSegments.get(0).getPath();
+
+        if (!config.getVersion().contains(version)) {
+            throw kustvaktResponseHandler.throwit(
+                    new KustvaktException(StatusCodes.UNSUPPORTED_API_VERSION,
+                            "API " + version + " is unsupported.", version));
+        }
+        return request;
+    }
+
+}