From 3b73517b00af061c5a060fd161f25a3f7e590087 Mon Sep 17 00:00:00 2001 From: Bipin kumar Date: Sun, 21 May 2023 15:15:40 +0530 Subject: [PATCH] =?UTF-8?q?JAVA-21440:=20Changes=20made=20for=20upgrade=20?= =?UTF-8?q?jooby=20module=20to=202.16.1=20to=20make=20j=E2=80=A6=20(#14064?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * JAVA-21440: Chnages made for upgrade jooby module to 2.16.1 to make java-17 compatable * JAVA-21440: commenting redis code as this will only work in the local or infra should have a redis setup --- web-modules/jooby/conf/application.conf | 4 +- web-modules/jooby/pom.xml | 59 ++++-- web-modules/jooby/public/form.html | 21 +- .../src/main/java/com/baeldung/jooby/App.java | 186 +++++++++++------- .../com/baeldung/jooby/mvc/GetController.java | 27 +-- .../baeldung/jooby/mvc/PostController.java | 13 +- .../java/com/baeldung/jooby/AppLiveTest.java | 35 ++-- .../java/com/baeldung/jooby/AppUnitTest.java | 15 +- web-modules/pom.xml | 2 +- 9 files changed, 218 insertions(+), 144 deletions(-) diff --git a/web-modules/jooby/conf/application.conf b/web-modules/jooby/conf/application.conf index 2f89e0eb6b..7da56eef6b 100644 --- a/web-modules/jooby/conf/application.conf +++ b/web-modules/jooby/conf/application.conf @@ -1,2 +1,4 @@ #application.secret = 2o128940921eo298e21 -#db = /url/to/the/datastore \ No newline at end of file +#db = /url/to/the/datastore + +#redis = "redis://localhost:6379" \ No newline at end of file diff --git a/web-modules/jooby/pom.xml b/web-modules/jooby/pom.xml index 6c2ce3a5c5..024a41e1d9 100644 --- a/web-modules/jooby/pom.xml +++ b/web-modules/jooby/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.baeldung.jooby jooby @@ -14,26 +14,25 @@ 1.0.0-SNAPSHOT - - - - org.jooby - modules - ${jooby.version} - pom - - - - - org.jooby - jooby-netty + io.jooby + jooby ${jooby.version} - org.jooby - jooby-jedis + io.jooby + jooby-test + ${jooby.version} + + + io.jooby + jooby-jetty + ${jooby.version} + + + io.jooby + jooby-redis ${jooby.version} @@ -42,23 +41,43 @@ test ${rest-assured.version} + + com.squareup.okhttp3 + okhttp + ${squareup.okhttp.version} + - org.apache.maven.plugins maven-shade-plugin ${maven-shade-plugin.version} + + io.jooby + jooby-maven-plugin + ${jooby.version} + + + maven-compiler-plugin + ${maven-compiler.version} + + + -parameters + + + - 1.1.3 + 2.16.1 3.1.1 com.baeldung.jooby.App - 2.4.3 + 3.2.4 + 3.8.1 + 4.9.1 \ No newline at end of file diff --git a/web-modules/jooby/public/form.html b/web-modules/jooby/public/form.html index a23620812a..bb9a705b9a 100644 --- a/web-modules/jooby/public/form.html +++ b/web-modules/jooby/public/form.html @@ -1,17 +1,18 @@ - -Insert title here + + Insert title here -
- - - - - - -
+
+
+
+
+
+
+

+ +
\ No newline at end of file diff --git a/web-modules/jooby/src/main/java/com/baeldung/jooby/App.java b/web-modules/jooby/src/main/java/com/baeldung/jooby/App.java index 94a24048c2..87899944ee 100644 --- a/web-modules/jooby/src/main/java/com/baeldung/jooby/App.java +++ b/web-modules/jooby/src/main/java/com/baeldung/jooby/App.java @@ -1,95 +1,135 @@ package com.baeldung.jooby; -import org.jooby.Jooby; -import org.jooby.Mutant; -import org.jooby.Session; -import org.jooby.jedis.Redis; -import org.jooby.jedis.RedisSessionStore; - import com.baeldung.jooby.bean.Employee; +import io.jooby.Jooby; +import io.jooby.ServerOptions; +import io.jooby.Session; +import io.jooby.SessionStore; +import io.jooby.redis.RedisModule; +import io.jooby.redis.RedisSessionStore; +import io.lettuce.core.RedisClient; + public class App extends Jooby { + { + setServerOptions(new ServerOptions().setPort(8080) + .setSecurePort(8433)); + } - { - port(8080); - securePort(8443); - } - { - get("/", () -> "Hello World!"); - } + { + get("/", ctx -> "Hello World!"); + } - { - get("/user/{id}", req -> "Hello user : " + req.param("id").value()); - get("/user/:id", req -> "Hello user: " + req.param("id").value()); - get("/uid:{id}", req -> "Hello User with id : uid" + req.param("id").value()); - } + { + get("/user/{id}", ctx -> "Hello user : " + ctx.path("id") + .value()); + get("/user/:id", ctx -> "Hello user: " + ctx.path("id") + .value()); + get("/uid:{id}", ctx -> "Hello User with id : uid = " + ctx.path("id") + .value()); + } - { - onStart(() -> { - System.out.println("starting app"); - }); + { + onStarting(() -> { + System.out.println("starting app"); + }); - onStop(() -> { - System.out.println("stopping app"); - }); + onStop(() -> { + System.out.println("stopping app"); + }); - onStarted(() -> { - System.out.println("app started"); - }); - } + onStarted(() -> { + System.out.println("app started"); + }); + } - { - get("/login", () -> "Hello from Baeldung"); - } + { + get("/login", ctx -> "Hello from Baeldung"); + } - { - post("/save", req -> { - Mutant token = req.param("token"); - return token.intValue(); - }); - } + { + post("/save", ctx -> { + String userId = ctx.query("id") + .value(); + return userId; + }); + } - { - { - assets("/employee", "form.html"); - } + { + { + assets("/employee", "public/form.html"); + } - post("/submitForm", req -> { - Employee employee = req.params(Employee.class); - // TODO - return "empoyee data saved successfullly"; - }); - } + post("/submitForm", ctx -> { + Employee employee = ctx.path(Employee.class); + // TODO + return "employee data saved successfully"; + }); - { - get("/filter", (req, resp, chain) -> { - // TODO - // resp.send(...); - chain.next(req, resp); - }); - get("/filter", (req, resp) -> { - resp.send("filter response"); - }); - } + } - { -// cookieSession(); - -// use(new Redis()); -// -// session(RedisSessionStore.class); - get("/session", req -> { - Session session = req.session(); - session.set("token", "value"); - return session.get("token").value(); - }); - } + { + decorator(next -> ctx -> { + System.out.println("first"); + // Moves execution to next handler: second + return next.apply(ctx); + }); + decorator(next -> ctx -> { + System.out.println("second"); + // Moves execution to next handler: third + return next.apply(ctx); + }); - public static void main(final String[] args) { + get("/handler", ctx -> { + return "third"; + }); + } - run(App::new, args); - } + { + get("/sessionInMemory", ctx -> { + Session session = ctx.session(); + session.put("token", "value"); + return session.get("token") + .value(); + }); + } + + { + String secret = "super secret token"; + + setSessionStore(SessionStore.signed(secret)); + + get("/signedSession", ctx -> { + Session session = ctx.session(); + session.put("token", "value"); + return session.get("token").value(); + }); + } + + { + get("/sessionInMemoryRedis", ctx -> { + Session session = ctx.session(); + session.put("token", "value"); + return session.get("token") + .value(); + }); + } + + /* This will work once redis is installed locally + { + install(new RedisModule("redis")); + setSessionStore(new RedisSessionStore(require(RedisClient.class))); + get("/redisSession", ctx -> { + Session session = ctx.session(); + session.put("token", "value"); + return session.get("token"); + }); + }*/ + + public static void main(final String[] args) { + runApp(args, App::new); + } } diff --git a/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/GetController.java b/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/GetController.java index a2c51bae70..bd43fb1717 100644 --- a/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/GetController.java +++ b/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/GetController.java @@ -1,22 +1,23 @@ package com.baeldung.jooby.mvc; -import org.jooby.Result; -import org.jooby.Results; -import org.jooby.mvc.GET; -import org.jooby.mvc.Path; +import java.util.HashMap; + +import io.jooby.ModelAndView; +import io.jooby.annotations.GET; +import io.jooby.annotations.Path; @Path("/hello") public class GetController { - @GET - public String hello() { - return "Hello Baeldung"; - } + @GET + public String hello() { + return "Hello Baeldung"; + } - @GET - @Path("/home") - public Result home() { - return Results.html("welcome").put("model", new Object()); - } + @GET + @Path("/home") + public ModelAndView home() { + return new ModelAndView("welcome.html", new HashMap<>()); + } } diff --git a/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/PostController.java b/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/PostController.java index df00e47da5..0f014580f9 100644 --- a/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/PostController.java +++ b/web-modules/jooby/src/main/java/com/baeldung/jooby/mvc/PostController.java @@ -1,14 +1,13 @@ package com.baeldung.jooby.mvc; -import org.jooby.mvc.POST; -import org.jooby.mvc.Path; +import io.jooby.annotations.POST; +import io.jooby.annotations.Path; @Path("/submit") public class PostController { - @POST - public String hello() { - return "Submit Baeldung"; - } - + @POST + public String hello() { + return "Submit Baeldung"; + } } diff --git a/web-modules/jooby/src/test/java/com/baeldung/jooby/AppLiveTest.java b/web-modules/jooby/src/test/java/com/baeldung/jooby/AppLiveTest.java index 1bd12f8bb3..18a505b406 100644 --- a/web-modules/jooby/src/test/java/com/baeldung/jooby/AppLiveTest.java +++ b/web-modules/jooby/src/test/java/com/baeldung/jooby/AppLiveTest.java @@ -1,21 +1,32 @@ package com.baeldung.jooby; -import static io.restassured.RestAssured.get; -import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.jooby.test.JoobyRule; -import org.junit.ClassRule; -import org.junit.Test; +import java.io.IOException; +import org.junit.jupiter.api.Test; + +import io.jooby.JoobyTest; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +@JoobyTest(value = App.class, port = 8080) public class AppLiveTest { - @ClassRule - public static JoobyRule app = new JoobyRule(new App()); + static OkHttpClient client = new OkHttpClient(); - @Test - public void given_defaultUrl_expect_fixedString() { - get("/").then().assertThat().body(equalTo("Hello World!")).statusCode(200) - .contentType("text/html;charset=UTF-8"); - } + @Test + public void given_defaultUrl_expect_fixedString() { + Request request = new Request.Builder().url("http://localhost:8080") + .build(); + try (Response response = client.newCall(request) + .execute()) { + assertEquals("Hello World!", response.body() + .string()); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/web-modules/jooby/src/test/java/com/baeldung/jooby/AppUnitTest.java b/web-modules/jooby/src/test/java/com/baeldung/jooby/AppUnitTest.java index 9bca30e2c1..98184e11f3 100644 --- a/web-modules/jooby/src/test/java/com/baeldung/jooby/AppUnitTest.java +++ b/web-modules/jooby/src/test/java/com/baeldung/jooby/AppUnitTest.java @@ -2,15 +2,16 @@ package com.baeldung.jooby; import static org.junit.Assert.assertEquals; -import org.jooby.test.MockRouter; import org.junit.Test; +import io.jooby.MockRouter; + public class AppUnitTest { - @Test - public void given_defaultUrl_with_mockrouter_expect_fixedString() throws Throwable { - String result = new MockRouter(new App()).get("/"); - assertEquals("Hello World!", result); - } - + @Test + public void given_defaultUrl_with_mockrouter_expect_fixedString() { + MockRouter router = new MockRouter(new App()); + assertEquals("Hello World!", router.get("/") + .value()); + } } diff --git a/web-modules/pom.xml b/web-modules/pom.xml index 7283df64fd..5d71f105b8 100644 --- a/web-modules/pom.xml +++ b/web-modules/pom.xml @@ -24,7 +24,7 @@ javax-servlets javax-servlets-2 jee-7 - + jooby linkrest