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.
This commit is contained in:
Bea 2020-08-21 12:19:56 +02:00
parent 2a4db7ed68
commit 4ab01c3787
7 changed files with 186 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea/
target/

63
pom.xml Normal file
View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>net.mindoverflow.webmarker</groupId>
<artifactId>WebMarker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo</artifactId>
<version>1.13.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo-controller</artifactId>
<version>1.13.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>net.mindoverflow.webmarker.WebMarker</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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();
}
}

View File

@ -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<String> 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
}
}

View File

@ -0,0 +1,55 @@
package net.mindoverflow.webmarker.utils;
import java.util.ArrayList;
import java.util.List;
public class URLMap {
private static List<Integer> ids = new ArrayList<>();
private static List<String> 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<String> getUserUrls(int userId)
{
List<String>thisUserUrls = 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<Integer> newIds = new ArrayList<>();
List<String> 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;
}
}

View File

2
webMarker.iml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />