From 6ff26adbfa8065c057a11b02c2220752c6f81cd9 Mon Sep 17 00:00:00 2001 From: Philipp <p.gritsch@student.uibk.ac.at> Date: Thu, 25 Nov 2021 20:43:29 +0100 Subject: [PATCH] adds session demo --- pom.xml | 12 +++++ .../app/rest/demo/SessionAwareEndpoint.java | 44 +++++++++++++++++++ .../app/service/demo/DemoSessionCache.java | 31 +++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/main/java/com/kgshape/app/rest/demo/SessionAwareEndpoint.java create mode 100644 src/main/java/com/kgshape/app/service/demo/DemoSessionCache.java diff --git a/pom.xml b/pom.xml index 13d5743..6532855 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,12 @@ <artifactId>jakarta.jakartaee-api</artifactId> <version>9.1.0</version> <scope>provided</scope> + <exclusions> + <exclusion> + <groupId>javax</groupId> + <artifactId>javaee-api</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> <groupId>jakarta.ws.rs</groupId> @@ -40,6 +46,12 @@ <artifactId>apache-jena-libs</artifactId> <type>pom</type> <version>4.2.0</version> + <exclusions> + <exclusion> + <groupId>javax.annotation</groupId> + <artifactId>javax.annotation-api</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> diff --git a/src/main/java/com/kgshape/app/rest/demo/SessionAwareEndpoint.java b/src/main/java/com/kgshape/app/rest/demo/SessionAwareEndpoint.java new file mode 100644 index 0000000..da5d0c5 --- /dev/null +++ b/src/main/java/com/kgshape/app/rest/demo/SessionAwareEndpoint.java @@ -0,0 +1,44 @@ +package com.kgshape.app.rest.demo; + +import com.kgshape.app.service.demo.DemoSessionCache; + +import jakarta.enterprise.context.RequestScoped; +import jakarta.enterprise.context.SessionScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +/** + * The name of this bean is misleading, then contrary to its name, the offered endpoints + * (all which start with /sessionTest) don't need to know there statefulness + * + * This can also be seen with the {@link RequestScoped} annotation, meaning + * that this object is created again for each object + */ +@RequestScoped +@Path("/sessionTest") +public class SessionAwareEndpoint { + + /** + * The only thing which makes the whole endpoint session aware is the injection of this {@link SessionScoped} + * bean + * + * But also note that the scope of the injected Bean should be irrelevant to the development of your endpoint. + * Think of this principle like that: The injected object fulfills a contract, in this case a session wide cache + * The technique used to achieve this contract is not the business of the endpoint, but the implementation of this object + * (it could possibly be a more sophisticated service, which synchronizes sessions across multiple cluster nodes, or something + * else) + */ + @Inject + private DemoSessionCache demoSessionCache; + + /** + * Upon request contains a JSESSIONID cookie used to identify the session + * + * As long as the cookie isnt changed this session is alive (also regard session time out) + */ + @GET + public String getCounter() { + return String.valueOf(this.demoSessionCache.getAtomicInteger().getAndIncrement()); + } +} diff --git a/src/main/java/com/kgshape/app/service/demo/DemoSessionCache.java b/src/main/java/com/kgshape/app/service/demo/DemoSessionCache.java new file mode 100644 index 0000000..38bf0c9 --- /dev/null +++ b/src/main/java/com/kgshape/app/service/demo/DemoSessionCache.java @@ -0,0 +1,31 @@ +package com.kgshape.app.service.demo; + +import java.io.Serializable; +import java.util.concurrent.atomic.AtomicInteger; + +import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.SessionScoped; + +/** + * This object exists once per session + */ +@SessionScoped +public class DemoSessionCache implements Serializable { + + private AtomicInteger atomicInteger; + + /** + * Always use {@link PostConstruct} to initialize the state of your bean + * + * Reason: Objects may be recycled, leading to inconsistencies, when initializing logic is in the constructor + * (as the constructor wouldn't be called if the object would be recycled) + */ + @PostConstruct + protected void init() { + this.atomicInteger = new AtomicInteger(); + } + + public AtomicInteger getAtomicInteger() { + return this.atomicInteger; + } +} -- GitLab