Commit 9ec252d0 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

An alternative web server implementation using Jersey and JAX-RS

parent 03e26d9a
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
<module>app.core</module> <module>app.core</module>
<module>FxMapControl</module> <module>FxMapControl</module>
<module>app.ui</module> <module>app.ui</module>
<!--
<module>web.server</module> <module>web.server</module>
-->
<module>web.server.jaxrs</module>
</modules> </modules>
</project> </project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tdt4140.gr1800.web.server.jaxrs.jaxrs.jaxrs</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
# web.server.jaxrs module - Web server providing a REST API to domain data, using JAX-RS implemented by Jersey
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>tdt4140-gr1800.web.server.jaxrs</artifactId>
<packaging>war</packaging>
<parent>
<groupId>tdt4140-gr1800</groupId>
<artifactId>tdt4140-gr1800</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<jetty.version>9.4.8.v20171121</jetty.version>
<jersey.version>2.26</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>tdt4140-gr1800</groupId>
<artifactId>tdt4140-gr1800.app.core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<!--
Needed as of v. 2.26
See https://stackoverflow.com/questions/44088493/jersey-stopped-working-with-injectionmanagerfactory-not-found
-->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- to use Jackson as provider for JSON media type -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin -->
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.8.v20171121</version>
<configuration>
<webApp>
<contextPath>/jax-rs</contextPath>
</webApp>
<scanIntervalSeconds>0</scanIntervalSeconds>
<stopKey>stopMe</stopKey>
<stopPort>9966</stopPort>
<systemProperties>
<systemProperty>
<name>data.locations</name>
<value>file:${project.basedir}/src/test/resources/tdt4140/gr1800/web/server/geoLocations.json</value>
</systemProperty>
</systemProperties>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId> <!-- for starting jetty for integration tests -->
<version>2.16</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package tdt4140.gr1800.web.server.jaxrs;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import tdt4140.gr1800.app.core.GeoLocation;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
import tdt4140.gr1800.app.core.Person;
import tdt4140.gr1800.app.db.IdProvider;
import tdt4140.gr1800.app.json.GeoLocationDeserializer;
import tdt4140.gr1800.app.json.GeoLocationSerializer;
import tdt4140.gr1800.app.json.GeoLocationsDeserializer;
import tdt4140.gr1800.app.json.GeoLocationsSerializer;
import tdt4140.gr1800.app.json.LatLongDeserializer;
import tdt4140.gr1800.app.json.LatLongSerializer;
import tdt4140.gr1800.app.json.PersonDeserializer;
import tdt4140.gr1800.app.json.PersonSerializer;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class GeoObjectMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
private final PersonSerializer personSerializer;
public GeoObjectMapperProvider() {
personSerializer = new PersonSerializer();
final SimpleModule module = new SimpleModule()
.addSerializer(new LatLongSerializer())
.addSerializer(new GeoLocationSerializer())
.addSerializer(new GeoLocationsSerializer())
.addSerializer(personSerializer)
.addDeserializer(LatLong.class, new LatLongDeserializer())
.addDeserializer(GeoLocation.class, new GeoLocationDeserializer())
.addDeserializer(GeoLocations.class, new GeoLocationsDeserializer())
.addDeserializer(Person.class, new PersonDeserializer());
objectMapper = new ObjectMapper()
.registerModule(module);
}
public void setPersonIdProvider(final IdProvider<Person> idProvider) {
personSerializer.setIdProvider(idProvider);
}
@Override
public ObjectMapper getContext(final Class<?> type) {
return objectMapper;
}
}
package tdt4140.gr1800.web.server.jaxrs;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
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.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Providers;
import com.fasterxml.jackson.databind.ObjectMapper;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.Person;
import tdt4140.gr1800.app.db.IDbAccess;
@Path("geo")
public class GeoService {
@Inject
IDbAccess dbAccess;
protected IDbAccess getDbAccess() {
return dbAccess;
}
@Context
private Providers providers;
@PostConstruct
protected void ensureIdProvider() {
final ContextResolver<ObjectMapper> resolver = providers.getContextResolver(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
if (resolver instanceof GeoObjectMapperProvider) {
((GeoObjectMapperProvider) resolver).setPersonIdProvider(getDbAccess().getPersonIdProvider());
}
}
// REST URL structure, according to https://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/
// persons/<id>/geoLocations/<num>/geoLocations/<num>
// GET variants
// persons: Get all Person objects. Do we allow that? Should we return a list of <id> values or all the person entities (with some subset of properties)
// persons/<id>: Get a specific Person object
// persons/name or email: Get a specific Person object, with the provided name or email (with a '@')
// persons/<id>/geoLocations: Get all the GeoLocations objects, with (some subset of) properties
// persons/<id>/geoLocations/<num>: Get a specific GeoLocations object
// persons/<id>/geoLocations/<num>/geoLocations: Get all GeoLocation objects, with (some subset of) properties
// persons/<id>/geoLocations/<num>/geoLocations/<num>: Get a specific GeoLocation object
@GET
@Path("/persons")
@Produces(MediaType.APPLICATION_JSON)
public Collection<Person> getPersons() {
// System.out.println("getPersons");
return getDbAccess().getAllPersons(false);
}
@GET
@Path("/persons/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Person getPerson(@PathParam("id") final String idOrKey) {
// System.out.println("getPerson: " + idOrKey);
Person person = null;
try {
final Integer num = Integer.valueOf(idOrKey);
person = getDbAccess().getPerson(num, false);
} catch (final NumberFormatException e) {
person = (idOrKey.contains("@") ? getDbAccess().getPersonByEmail(idOrKey, false) : getDbAccess().getPersonByName(idOrKey, false));
}
return person;
}
@GET
@Path("/persons/{id}/geoLocations")
@Produces(MediaType.APPLICATION_JSON)
public Collection<GeoLocations> getGeoLocations(@PathParam("id") final String idOrKey) throws RuntimeException {
// System.out.println("getGeoLocations: " + idOrKey);
final Person person = getDbAccess().getPerson(Integer.valueOf(idOrKey), false);
return getDbAccess().getGeoLocations(person, false);
}
@GET
@Path("/persons/{id}/geoLocations/{num}")
@Produces(MediaType.APPLICATION_JSON)
public GeoLocations getGeoLocation(@PathParam("id") final int id, @PathParam("num") final int num) throws RuntimeException {
// System.out.println("getGeoLocation: " + id + "," + num);
final Person person = getDbAccess().getPerson(id, false);
final int geoNum = 0;
for (final GeoLocations geoLocations : getDbAccess().getGeoLocations(person, false)) {
if (geoNum == num) {
return geoLocations;
}
}
return null;
}
// POST variants
// persons: Create a new Person object, with properties in the payload
// persons/<id>: Not allowed
// persons/<id>/geoLocations: Create a new GeoLocations object, with properties in the payload
// persons/<id>/geoLocations/<num>: Not allowed
// persons/<id>/geoLocations/<num>/geoLocations: Create a new GeoLocation object, with properties in the payload
// persons/<id>/geoLocations/<num>/geoLocations/<num>: Not allowed
@POST
@Path("/persons")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Person createPerson(final Person person, @Context final Providers providers) {
final Person dbPerson = getDbAccess().createPerson(person.getName(), person.getEmail());
return dbPerson;
}
// PUT variants
// persons: Not allowed
// persons/<id>: Update specific Person object
// persons/<id>/geoLocations: Not allowed
// persons/<id>/geoLocations/<num>: Update specific GeoLocations object
// persons/<id>/geoLocations/<num>/geoLocations: Not allowed
// persons/<id>/geoLocations/<num>/geoLocations/<num>: Update specific GeoLocation object
// DELETE variants
// persons: Not allowed
// persons/<id>: Delete specific Person object
// persons/<id>/geoLocations: Delete all GeoLocations objects?
// persons/<id>/geoLocations/<num>: Delete specific GeoLocations object
// persons/<id>/geoLocations/<num>/geoLocations: Delete all GeoLocation objects?
// persons/<id>/geoLocations/<num>/geoLocations/<num>: Delete specific GeoLocation object
}
package tdt4140.gr1800.web.server.jaxrs;
import java.sql.SQLException;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import tdt4140.gr1800.app.db.DbAccessImpl;
import tdt4140.gr1800.app.db.IDbAccess;
public class GeoServiceConfig extends ResourceConfig {
public GeoServiceConfig() {
packages("tdt4140.gr1800.web.server.jaxrs");
register(GeoService.class);
register(GeoObjectMapperProvider.class);
register(JacksonFeature.class);
// https://stackoverflow.com/questions/16216759/dependency-injection-with-jersey-2-0
register(new AbstractBinder() {
@Override
protected void configure() {
bind(getDbAccess()).to(IDbAccess.class);
}
});
}
private IDbAccess dbAccess;
public IDbAccess getDbAccess() {
if (dbAccess == null) {
String dbConnectionUrl = "jdbc:hsqldb:mem:" + getClass().getName();
final Object dbConnectionParam = getProperty("dbConnectionURL");
if (dbConnectionParam != null) {
dbConnectionUrl = String.valueOf(dbConnectionParam);
}
try {
final DbAccessImpl dbAccess = new DbAccessImpl(dbConnectionUrl);
dbAccess.executeStatements("schema.sql", false);
this.dbAccess = dbAccess;
} catch (final SQLException e) {
throw new RuntimeException("Error when initializing database connection (" + dbConnectionUrl + "): " + e, e);
}
}
return dbAccess;
}
// private ObjectMapper objectMapper;
//
// public ObjectMapper getObjectMapper() {
// if (objectMapper == null) {
// final PersonSerializer personSerializer = new PersonSerializer();
// personSerializer.setIdProvider(getDbAccess().getPersonIdProvider());
// final SimpleModule module = new SimpleModule()
// .addSerializer(new LatLongSerializer())
// .addSerializer(new GeoLocationSerializer())
// .addSerializer(new GeoLocationsSerializer())
// .addSerializer(personSerializer)
// .addDeserializer(LatLong.class, new LatLongDeserializer())
// .addDeserializer(GeoLocation.class, new GeoLocationDeserializer())
// .addDeserializer(GeoLocations.class, new GeoLocationsDeserializer())
// .addDeserializer(Person.class, new PersonDeserializer());
// objectMapper = new ObjectMapper()
// .registerModule(module);
// }
// return objectMapper;
// }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false" version="3.1">
<!--
https://stackoverflow.com/questions/38639425/basic-jersey-webservice-with-maven-jetty-plugin
-->
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>tdt4140.gr1800.web.server.jaxrs.GeoServiceConfig</param-value>
</init-param>
<!--
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
-->
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
package tdt4140.gr1800.web.server.jaxrs;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ValueNode;
import tdt4140.gr1800.app.core.Person;
public class GeoServiceIT {
private ObjectMapper objectMapper;
@Before
public void setUp() {
objectMapper = new GeoObjectMapperProvider().getContext(getClass());
}
private HttpURLConnection createUrlConnection(final String path) {
try {
return (HttpURLConnection) (new URL("http://localhost:8080/jax-rs/geo/" + path).openConnection());