diff --git a/README.md b/README.md index d0d551b2b7235125d9aee0238fc8759afe31116f..b8b191c85c5c77d7aad49ff916c4089b86704919 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,41 @@ Make sure java 17 is installed. Make sure that Maven 3.8.3 is installed on your computer with `mvn --version`. If it is not, follow the instructions here: https://maven.apache.org/install.html. +Make sure you have IntelliJ Ultimate (licence from the university is free) the IDE of choice for Java installed -Test that things are up and running and can work on your machine. Run `mvn package` in the kg-shape-project directory, and you should see a build success message. -Then from the same directory, in the command line run: +## Run "normal" programs -`java -cp target/kg-shape-project-1.0-SNAPSHOT.jar com.kgshape.app.App` +Use your IDE to handle all the dependencies and just hit the play button. -You should see a Hello World message. + + +This may be useful to test small implementations. Later on the test suite will be extended to have also a server environment for testing. + +## Run "server" programs + +Sooner or later the functionality needs to be provided to our frontend. + +A running wildfly (Jakarta EE server) will take care of that. + +Checkout [DemoEndpoint.java](src/main/java/com/kgshape/app/rest/demo/DemoEndpoint.java) +as an example for a collection of rest endpoints. + +Checkout [ApplicationWideCache.java](src/main/java/com/kgshape/app/service/demo/ApplicationWideCache.java) +as an example for CDI scoped beans (meaning the server takes care of business class creation) + +Add the following run configurations to IntelliJ + +Maven: + + + + +For Debug: + + + +Execute the maven configuration, wait until it says deployed, like so: + + + +Then start the debug configuration. Now you can also debug your server application. \ No newline at end of file diff --git a/pom.xml b/pom.xml index e5075ff54a36a4fa46cbd3c36878cc0d0039c0af..13d5743db195fa9c38562fb5e0fe1fcd0260040d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,7 @@ <groupId>com.kgshape.app</groupId> <artifactId>kg-shape-project</artifactId> <version>1.0-SNAPSHOT</version> + <packaging>war</packaging> <name>kg-shape-project</name> <!-- FIXME change it to the project's website --> @@ -19,12 +20,21 @@ </properties> <dependencies> + <!-- java application server dependencies, just apis --> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.11</version> - <scope>test</scope> + <groupId>jakarta.platform</groupId> + <artifactId>jakarta.jakartaee-api</artifactId> + <version>9.1.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>jakarta.ws.rs</groupId> + <artifactId>jakarta.ws.rs-api</artifactId> + <version>3.0.0</version> + <scope>provided</scope> </dependency> + + <!-- project dependencies --> <dependency> <groupId>org.apache.jena</groupId> <artifactId>apache-jena-libs</artifactId> @@ -41,6 +51,15 @@ <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> + + <!-- test deps --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + </dependencies> <build> @@ -96,6 +115,39 @@ <target>17</target> </configuration> </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-war-plugin</artifactId> + <version>3.3.2</version> + </plugin> + + <plugin> + <groupId>org.wildfly.plugins</groupId> + <artifactId>wildfly-jar-maven-plugin</artifactId> + <version>6.1.0.Final</version> + <configuration> + <jvmArguments> + <arg>-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n</arg> + </jvmArguments> + <feature-pack-location>wildfly-preview@maven(org.jboss.universe:community-universe)</feature-pack-location> + <layers> + <layer>jaxrs-server</layer> + <layer>cdi</layer> + <layer>ee-security</layer> + <layer>bean-validation</layer> + <layer>management</layer> + <layer>jaxrs</layer> + </layers> + </configuration> + <executions> + <execution> + <goals> + <goal>package</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> </project> diff --git a/readme/debug_configuration.png b/readme/debug_configuration.png new file mode 100644 index 0000000000000000000000000000000000000000..2ec4396e37a44a02ff0a0da3093547ab54579bd6 Binary files /dev/null and b/readme/debug_configuration.png differ diff --git a/readme/run_configuration.png b/readme/run_configuration.png new file mode 100644 index 0000000000000000000000000000000000000000..6cfe652dd29ccbac52516a759552d6c7597cdbdd Binary files /dev/null and b/readme/run_configuration.png differ diff --git a/readme/running_main.png b/readme/running_main.png new file mode 100644 index 0000000000000000000000000000000000000000..eed24489147146558649707c7efaa7ff148df21b Binary files /dev/null and b/readme/running_main.png differ diff --git a/readme/server_log_deployed.png b/readme/server_log_deployed.png new file mode 100644 index 0000000000000000000000000000000000000000..a48732331cfefc2e4d3235969433b278bd6d0f0e Binary files /dev/null and b/readme/server_log_deployed.png differ diff --git a/src/main/java/com/kgshape/app/model/demo/ComplexObject.java b/src/main/java/com/kgshape/app/model/demo/ComplexObject.java new file mode 100644 index 0000000000000000000000000000000000000000..c7147f424f80f17c2068d4388b88e2d542fe7784 --- /dev/null +++ b/src/main/java/com/kgshape/app/model/demo/ComplexObject.java @@ -0,0 +1,46 @@ +package com.kgshape.app.model.demo; + +import jakarta.xml.bind.annotation.XmlRootElement; + +import java.util.concurrent.atomic.AtomicLong; + +@XmlRootElement +public class ComplexObject { + + private long id; + + private String name; + + public ComplexObject() { + + } + + public ComplexObject(final long id, final String name) { + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "ComplexObject{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/com/kgshape/app/rest/RestApplication.java b/src/main/java/com/kgshape/app/rest/RestApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..144c3c7518f7acb2223039956c15e9724e782ab5 --- /dev/null +++ b/src/main/java/com/kgshape/app/rest/RestApplication.java @@ -0,0 +1,13 @@ +package com.kgshape.app.rest; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +/** + * Declares the Rest application + * + * Needed so that the application is initialized correctly on the server + */ +@ApplicationPath("/") +public class RestApplication extends Application { +} diff --git a/src/main/java/com/kgshape/app/rest/demo/DemoEndpoint.java b/src/main/java/com/kgshape/app/rest/demo/DemoEndpoint.java new file mode 100644 index 0000000000000000000000000000000000000000..f5ef57a4621e89c37463072ab6a8f19014fa3cac --- /dev/null +++ b/src/main/java/com/kgshape/app/rest/demo/DemoEndpoint.java @@ -0,0 +1,63 @@ +package com.kgshape.app.rest.demo; + +import com.kgshape.app.model.demo.ComplexObject; +import com.kgshape.app.service.demo.ApplicationWideCache; +import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.util.logging.Logger; + +@RequestScoped +@Path("/demo") +public class DemoEndpoint { + + // get or create objects if they are needed, with inject + // ApplicationWideCache is ApplicationScoped, meaning it is created on systemstart, and stopped on system stop + @Inject + private ApplicationWideCache applicationWideCache; + + // url = localhost:8080/demo + // in general don't comment the url, use intellijs endpoints feature + @GET + public String hello() { + return "hello"; + } + + // url = localhost:8080/demo/name + // use PUT for update operations + // use POST for Insert operations + // use DELETE for delete operations + @Path("/name") + @PUT + public Response setName(@FormParam("name") final String name) { + this.applicationWideCache.setValue(name); + return Response.ok().build(); + } + + @Path("/name") + @GET + public String getName() { + return this.applicationWideCache.getValue(); + } + + @Path("/complexName") + @GET + @Produces(MediaType.APPLICATION_JSON) + public ComplexObject getComplexName() { + return new ComplexObject(1, "complex name"); + } + + @Path("/complexName") + @PUT + @Consumes(MediaType.APPLICATION_JSON) + public Response logComplexName(@NotNull final ComplexObject complexObject) { + // todo use more sophisticated logger + Logger.getLogger("DemoEndpoint").info(complexObject.toString()); + return Response.accepted().build(); + } + +} diff --git a/src/main/java/com/kgshape/app/service/demo/ApplicationWideCache.java b/src/main/java/com/kgshape/app/service/demo/ApplicationWideCache.java new file mode 100644 index 0000000000000000000000000000000000000000..99de932beeff15eabeb4178ae524d243cb3cbff8 --- /dev/null +++ b/src/main/java/com/kgshape/app/service/demo/ApplicationWideCache.java @@ -0,0 +1,17 @@ +package com.kgshape.app.service.demo; + +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class ApplicationWideCache { + + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/webapp/WEB-INF/jboss-web.xml b/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000000000000000000000000000000000000..c55c387855c87b634a8066da89ae976e3d0787e7 --- /dev/null +++ b/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,3 @@ +<jboss-web> + <context-root>/</context-root> +</jboss-web> \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..e3a61ed57d618dd28ae14c34202e71bafd80b6a5 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ +<?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" + version="3.1"> + +</web-app> \ No newline at end of file