Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • issue-10-fxmapcontrol
  • issue-13-gpx
  • issue-9-file-menu
  • master
4 results

Target

Select target project
  • bragearn/examples
  • johngu/examples
  • krisschn/examples
  • erikbd/examples
  • helenesm/examples
  • balazor/examples
  • htechter/examples
  • aslakho/examples
  • jonri/examples
  • andreski/examples
  • sigriksc/examples
  • norasbr/examples
  • jannash/examples
  • smledsaa/examples
  • mtorre/examples
  • tdt4140-staff/examples
16 results
Select Git revision
  • issue-10-fxmapcontrol
  • issue-13-gpx
  • issue-9-file-menu
  • master
  • patch-1
5 results
Show changes
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tdt4140.gr18nn.web.server</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 module - Web server providing a REST API to domain data
<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-gr18nn.web.server</artifactId>
<packaging>war</packaging>
<parent>
<groupId>tdt4140-gr18nn</groupId>
<artifactId>tdt4140-gr18nn</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>tdt4140-gr18nn</groupId>
<artifactId>tdt4140-gr18nn.app.core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</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>
<scanIntervalSeconds>0</scanIntervalSeconds>
<stopKey>stopMe</stopKey>
<stopPort>9966</stopPort>
</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.gr18nn.web.server;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import tdt4140.gr18nn.app.core.LatLong;
import tdt4140.gr18nn.app.json.LatLongDeserializer;
import tdt4140.gr18nn.app.json.LatLongSerializer;
public class LatLongServlet extends HttpServlet {
private ObjectMapper objectMapper;
@Override
public void init() throws ServletException {
objectMapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addSerializer(LatLong.class, new LatLongSerializer());
module.addDeserializer(LatLong.class, new LatLongDeserializer());
objectMapper.registerModule(module);
}
private final List<LatLong> geoLocations = new ArrayList<>();
// About REST APIs: https://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
int geoLocNum = parseNum(request.getPathInfo());
if (geoLocNum < 0) {
geoLocNum = geoLocations.size() + geoLocNum;
}
if (geoLocNum >= 0 && geoLocNum < geoLocations.size()) {
respondWithJson(response, geoLocations.get(geoLocNum));
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
protected int parseNum(String path) throws ServletException {
if (path.startsWith("/")) {
path = path.substring(1);
}
try {
return Integer.valueOf(path);
} catch (final NumberFormatException e) {
}
throw new ServletException("Illegal reference to geo-location: " + path);
}
protected void respondWithJson(final HttpServletResponse response, final Object result) {
response.setContentType("application/json");
try (OutputStream output = response.getOutputStream()) {
response.setStatus(HttpServletResponse.SC_OK);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(output, result);
} catch (final Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
final Collection<LatLong> latLongs = objectMapper.readValue(request.getInputStream(), objectMapper.getTypeFactory().constructParametricType(Collection.class, LatLong.class));
final int geoLocNum = geoLocations.size();
geoLocations.addAll(latLongs);
respondWithJson(response, geoLocNum);
}
@Override
protected void doPut(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
int geoLocNum = parseNum(request.getPathInfo());
if (geoLocNum < 0) {
geoLocNum = geoLocations.size() + geoLocNum;
}
final LatLong latLong = objectMapper.readValue(request.getInputStream(), LatLong.class);
if (geoLocNum >= 0 && geoLocNum < geoLocations.size()) {
geoLocations.set(geoLocNum, latLong);
respondWithJson(response, geoLocNum);
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
@Override
protected void doDelete(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
int geoLocNum = parseNum(request.getPathInfo());
if (geoLocNum < 0) {
geoLocNum = geoLocations.size() + geoLocNum;
}
if (geoLocNum >= 0 && geoLocNum < geoLocations.size()) {
final LatLong latLong = geoLocations.remove(geoLocNum);
respondWithJson(response, latLong);
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
}
<?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">
<servlet>
<servlet-name>LatLongs</servlet-name>
<servlet-class>tdt4140.gr18nn.web.server.LatLongServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LatLongs</servlet-name>
<url-pattern>/latLong/*</url-pattern>
</servlet-mapping>
</web-app>
package tdt4140.gr18nn.web.server;
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.ProtocolException;
import java.net.URL;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.NumericNode;
import tdt4140.gr18nn.app.core.LatLong;
import tdt4140.gr18nn.app.json.LatLongDeserializer;
import tdt4140.gr18nn.app.json.LatLongSerializer;
public class LatLongServletIT {
private ObjectMapper objectMapper;
final String baseUrl = "http://localhost:8080/latLong/";
@Before
public void setUp() {
objectMapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addSerializer(LatLong.class, new LatLongSerializer());
module.addDeserializer(LatLong.class, new LatLongDeserializer());
objectMapper.registerModule(module);
}
@Test
public void test() throws Exception {
// POST, i.e. add
final LatLong latLong = new LatLong(63, 11);
final URL postUrl = new URL(baseUrl);
final HttpURLConnection postCon = ((HttpURLConnection) postUrl.openConnection());
postCon.setRequestMethod("POST");
postCon.setDoInput(true);
doJsonOutput(postCon, Collections.singleton(latLong));
testNumContent(postCon, 0);
// GET
testGet(0, latLong);
// PUT, i.e. set
final LatLong altLatLong = new LatLong(63, 11);
final URL putUrl = new URL(baseUrl + "0");
final HttpURLConnection putCon = ((HttpURLConnection) putUrl.openConnection());
putCon.setRequestMethod("PUT");
putCon.setDoInput(true);
doJsonOutput(putCon, altLatLong);
testNumContent(putCon, 0);
// GET
testGet(0, altLatLong);
// DELETE, i.e. remove
final URL deleteUrl = new URL(baseUrl + "0");
final HttpURLConnection deleteCon = ((HttpURLConnection) deleteUrl.openConnection());
deleteCon.setRequestMethod("DELETE");
deleteCon.setDoInput(true);
testJsonContent(deleteCon, altLatLong);
}
protected void doJsonOutput(final HttpURLConnection postCon, final Object content)
throws IOException, JsonGenerationException, JsonMappingException {
postCon.setDoOutput(true);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
objectMapper.writeValue(out, content);
out.close();
final byte[] postBytes = out.toByteArray();
postCon.setFixedLengthStreamingMode(postBytes.length);
postCon.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
postCon.connect();
try (OutputStream conOut = postCon.getOutputStream()) {
conOut.write(postBytes);
}
}
protected void testGet(final int num, final LatLong latLong)
throws MalformedURLException, IOException, ProtocolException, JsonParseException, JsonMappingException {
// GET
final URL getUrl = new URL(baseUrl + num);
final HttpURLConnection getCon = ((HttpURLConnection) getUrl.openConnection());
getCon.setRequestMethod("GET");
getCon.setDoInput(true);
getCon.connect();
testJsonContent(getCon, latLong);
}
protected void testNumContent(final HttpURLConnection con, final int num) throws IOException {
try (InputStream conIn = con.getInputStream()) {
final JsonNode jsonTree = objectMapper.readTree(conIn);
Assert.assertTrue(jsonTree instanceof NumericNode);
Assert.assertEquals(num, ((NumericNode) jsonTree).asInt());
}
}
protected void testJsonContent(final HttpURLConnection getCon, final LatLong latLong)
throws IOException, JsonParseException, JsonMappingException {
try (InputStream conIn = getCon.getInputStream()) {
final LatLong getLatLong = objectMapper.readValue(conIn, LatLong.class);
Assert.assertEquals(latLong, getLatLong);
}
}
}