Skip to content
Snippets Groups Projects
Commit 5bb1ca2a authored by Andreas Schärtl's avatar Andreas Schärtl
Browse files

uloapi: set up json routes

parent 7470b452
Branches
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@
<configuration>
<archive>
<manifest>
<mainClass>info.mathhub.uloapi.Main</mainClass>
<mainClass>info.mathhub.uloapi.rest.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
......@@ -41,7 +41,7 @@
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>info.mathhub.uloapi.Main</mainClass>
<mainClass>info.mathhub.uloapi.rest.Main</mainClass>
</configuration>
</plugin>
</plugins>
......@@ -51,7 +51,7 @@
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5</version>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
......@@ -68,5 +68,10 @@
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package info.mathhub.uloapi;
import spark.*;
/**
* This class contains all routes of our application.
*/
public class Routes {
private Routes() {};
public static final Route index = (Request request, Response response) -> {
return "<h1>Index /</h1>";
};
public static Route createError(int status) {
return (Request request, Response response) -> {
response.status(status);
return String.format("Error %d", status);
};
}
}
package info.mathhub.uloapi.query;
public class Query {
public Statistics getStatistics() {
return new Statistics(10);
}
}
package info.mathhub.uloapi.query;
/**
* Contains statistic information about the backing storage.
*/
public class Statistics {
/**
* The number of triplets in the backing storage.
*/
public long numTriplets;
Statistics(int numTriplets) {
this.numTriplets = numTriplets;
}
}
package info.mathhub.uloapi.rest;
import spark.Filter;
import spark.Request;
import spark.Response;
public class Filters {
private Filters() {};
public static final Filter setJsonContentType = (Request request, Response response) -> {
response.type("application/json");
};
}
package info.mathhub.uloapi.rest;
import com.google.gson.Gson;
import spark.ResponseTransformer;
/**
* A {@link spark.ResponseTransformer} that converts everything to
* JSON.
*/
public class JsonTransformer implements ResponseTransformer {
private final Gson instance = new Gson();
@Override
public String render(Object model) throws Exception {
return this.instance.toJson(model);
}
}
package info.mathhub.uloapi;
package info.mathhub.uloapi.rest;
import static spark.Spark.*;
/**
* Entry point of the JSON/REST API for the underlying ULO/RDF store.
*/
public class Main {
private Main() {};
public static void main(String[] args) {
get("/", Routes.index);
get("/favicon.ico", Routes.createError(404));
get("/statistics", Routes.statistics, new JsonTransformer());
get("/name/:name", Routes.name, new JsonTransformer());
notFound(Routes.notFound);
after(Filters.setJsonContentType);
}
}
package info.mathhub.uloapi.rest;
import info.mathhub.uloapi.query.Query;
import org.eclipse.jetty.http.HttpStatus;
import spark.*;
import java.util.HashMap;
import java.util.Map;
/**
* This class contains all routes of our application.
*/
public class Routes {
private Routes() {};
public static final Route index = (Request request, Response response) -> {
return "{}";
};
public static final Route statistics = (Request request, Response response) -> {
return new Query().getStatistics();
};
public static final Route name = (Request request, Response response) -> {
final Map<String, Object> json = new HashMap<>();
json.put("name", request.params(":name"));
json.put("attributes", request.attributes());
json.put("ip", request.ip());
return json;
};
public static final Route notFound = (Request request, Response response) -> {
return createError(HttpStatus.NOT_FOUND_404);
};
public static final Route notAcceptable = (Request request, Response response) -> {
return createError(HttpStatus.NOT_ACCEPTABLE_406);
};
private static Route createError(int status) {
return (Request request, Response response) -> {
response.status(status);
return String.format("Error %d", status);
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment