From 4ab01c3787b7f973405913a28f779dc5a097eeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenzo=20Dellac=C3=A0?= Date: Fri, 21 Aug 2020 12:19:56 +0200 Subject: [PATCH] Initialize Maven project and test functionality A very simple sketch of what the basic server should be able to do has been set up with Maven. The main package and class, and important dependencies were added. The project is based on the Pippo Java framework. A very simple GET test has been implemented, to store/read/remove data from a list of users and their visited urls. This is obviously not secure and not ready for anything that can be considered pre-alpha state. --- .gitignore | 2 + pom.xml | 63 +++++++++++++++++++ .../net/mindoverflow/webmarker/WebMarker.java | 13 ++++ .../webmarker/server/WebApplication.java | 51 +++++++++++++++ .../mindoverflow/webmarker/utils/URLMap.java | 55 ++++++++++++++++ src/main/resources/META-INF/MANIFEST.MF | 0 webMarker.iml | 2 + 7 files changed, 186 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/net/mindoverflow/webmarker/WebMarker.java create mode 100644 src/main/java/net/mindoverflow/webmarker/server/WebApplication.java create mode 100644 src/main/java/net/mindoverflow/webmarker/utils/URLMap.java create mode 100644 src/main/resources/META-INF/MANIFEST.MF create mode 100644 webMarker.iml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92322c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bfd99ad --- /dev/null +++ b/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + net.mindoverflow.webmarker + WebMarker + 1.0-SNAPSHOT + jar + + + + ro.pippo + pippo + 1.13.1 + pom + + + + ro.pippo + pippo-controller + 1.13.1 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + + + + org.apache.maven.plugins + maven-assembly-plugin + + + create-my-bundle + package + + single + + + + jar-with-dependencies + + + + net.mindoverflow.webmarker.WebMarker + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/net/mindoverflow/webmarker/WebMarker.java b/src/main/java/net/mindoverflow/webmarker/WebMarker.java new file mode 100644 index 0000000..590a597 --- /dev/null +++ b/src/main/java/net/mindoverflow/webmarker/WebMarker.java @@ -0,0 +1,13 @@ +package net.mindoverflow.webmarker; + +import net.mindoverflow.webmarker.server.WebApplication; +import ro.pippo.core.Pippo; + +public class WebMarker { + + public static void main(String[] args) + { + final Pippo pippo = new Pippo(new WebApplication()); + pippo.start(); + } +} diff --git a/src/main/java/net/mindoverflow/webmarker/server/WebApplication.java b/src/main/java/net/mindoverflow/webmarker/server/WebApplication.java new file mode 100644 index 0000000..a3a75c1 --- /dev/null +++ b/src/main/java/net/mindoverflow/webmarker/server/WebApplication.java @@ -0,0 +1,51 @@ +package net.mindoverflow.webmarker.server; + +import net.mindoverflow.webmarker.utils.URLMap; +import ro.pippo.controller.ControllerApplication; +import ro.pippo.core.route.TrailingSlashHandler; + +import java.util.List; + +public class WebApplication extends ControllerApplication { + + @Override + protected void onInit() { + + GET("/save", routeContext -> routeContext.send("Please append an user ID")); + GET("/save/{id}", routeContext -> routeContext.send("Please append an URL")); + GET("/save/{id}/{url}", routeContext -> + { + int userId = routeContext.getParameter("id").toInt(); + String saveUrl = routeContext.getParameter("url").toString(); + + URLMap.saveUrl(userId, saveUrl); + routeContext.send("Saved!"); + }); + + GET("/get", routeContext -> routeContext.send("Please append an user ID")); + GET("/get/{id}", routeContext -> + { + int userId = routeContext.getParameter("id").toInt(); + + List urls = URLMap.getUserUrls(userId); + StringBuilder urlsSB = new StringBuilder(" | "); + for(String url : urls) + { + urlsSB.append(url).append(" | "); + } + routeContext.send(urlsSB.toString()); + + }); + + GET("/drop", routeContext -> routeContext.send("Please append an user ID")); + GET("/drop/{id}", routeContext -> + { + int userId = routeContext.getParameter("id").toInt(); + URLMap.dropUser(userId); + routeContext.send("Dropped!"); + + }); + + ANY("/.*", new TrailingSlashHandler(false)); // remove trailing slash + } +} diff --git a/src/main/java/net/mindoverflow/webmarker/utils/URLMap.java b/src/main/java/net/mindoverflow/webmarker/utils/URLMap.java new file mode 100644 index 0000000..45e5b70 --- /dev/null +++ b/src/main/java/net/mindoverflow/webmarker/utils/URLMap.java @@ -0,0 +1,55 @@ +package net.mindoverflow.webmarker.utils; + +import java.util.ArrayList; +import java.util.List; + +public class URLMap { + + private static List ids = new ArrayList<>(); + private static List urls = new ArrayList<>(); + + public static void saveUrl(int userId, String url) + { + ids.add(userId); + urls.add(url); + System.out.println(); + System.out.println("Saved ID: " + userId + "; URL: " + url); + System.out.println("table is now:"); + for(int pos = 0; pos < ids.size(); pos++) + { + System.out.println("ID = " + ids.get(pos) + "; URL = " + urls.get(pos)); + } + System.out.println(); + } + + public static List getUserUrls(int userId) + { + ListthisUserUrls = new ArrayList<>(); + for(int pos = 0; pos < ids.size(); pos++) + { + if(userId == ids.get(pos)) + { + thisUserUrls.add(urls.get(pos)); + } + } + + return thisUserUrls; + } + + public static void dropUser(int userId) + { + List newIds = new ArrayList<>(); + List newUrls = new ArrayList<>(); + for(int pos = 0; pos < ids.size(); pos++) + { + if(ids.get(pos) != userId) + { + newIds.add(ids.get(pos)); + newUrls.add(urls.get(pos)); + } + } + + ids = newIds; + urls = newUrls; + } +} diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e69de29 diff --git a/webMarker.iml b/webMarker.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/webMarker.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file