From a1b73b7a61e8622d5309c25345331589081ad16e Mon Sep 17 00:00:00 2001 From: Jianguo Ma Date: Wed, 30 Sep 2020 14:08:51 +0800 Subject: [PATCH] make sessionbean cafe as stateless --- 1-start/src/main/java/cafe/web/view/Cafe.java | 15 ++++++++++----- 1-start/src/main/webapp/index.xhtml | 3 +++ 2-simple/src/main/java/cafe/web/view/Cafe.java | 15 ++++++++++----- 2-simple/src/main/webapp/index.xhtml | 3 +++ .../src/main/java/cafe/web/view/Cafe.java | 17 +++++++++++------ .../aad-ldap/src/main/webapp/index.xhtml | 3 +++ .../src/main/java/cafe/web/view/Cafe.java | 17 +++++++++++------ .../aad-oidc/src/main/webapp/index.xhtml | 3 +++ .../mssql/src/main/java/cafe/web/view/Cafe.java | 16 +++++++++------- .../mssql/src/main/webapp/index.xhtml | 2 +- .../openlibertyapplication-cluster.yaml | 2 +- .../src/main/java/cafe/web/view/Cafe.java | 16 +++++++++------- .../postgres/src/main/webapp/index.xhtml | 2 +- 4-finish/src/main/java/cafe/web/view/Cafe.java | 14 ++++++++------ 4-finish/src/main/webapp/index.xhtml | 2 +- 15 files changed, 84 insertions(+), 46 deletions(-) diff --git a/1-start/src/main/java/cafe/web/view/Cafe.java b/1-start/src/main/java/cafe/web/view/Cafe.java index 1cb9b15..eac7054 100644 --- a/1-start/src/main/java/cafe/web/view/Cafe.java +++ b/1-start/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -60,9 +61,14 @@ public void setPrice(Double price) { } public List getCoffeeList() { + this.getAllCoffees(); return coffeeList; } + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; + } + @PostConstruct private void init() { try { @@ -79,7 +85,6 @@ public boolean verify(String hostname, SSLSession session) { return true; } }).build(); - this.getAllCoffees(); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -92,16 +97,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/1-start/src/main/webapp/index.xhtml b/1-start/src/main/webapp/index.xhtml index 4fa5508..877ee33 100644 --- a/1-start/src/main/webapp/index.xhtml +++ b/1-start/src/main/webapp/index.xhtml @@ -13,6 +13,9 @@ crossorigin="anonymous" /> +
+ +

diff --git a/2-simple/src/main/java/cafe/web/view/Cafe.java b/2-simple/src/main/java/cafe/web/view/Cafe.java index 1cb9b15..eac7054 100644 --- a/2-simple/src/main/java/cafe/web/view/Cafe.java +++ b/2-simple/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -60,9 +61,14 @@ public void setPrice(Double price) { } public List getCoffeeList() { + this.getAllCoffees(); return coffeeList; } + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; + } + @PostConstruct private void init() { try { @@ -79,7 +85,6 @@ public boolean verify(String hostname, SSLSession session) { return true; } }).build(); - this.getAllCoffees(); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -92,16 +97,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/2-simple/src/main/webapp/index.xhtml b/2-simple/src/main/webapp/index.xhtml index 4fa5508..877ee33 100644 --- a/2-simple/src/main/webapp/index.xhtml +++ b/2-simple/src/main/webapp/index.xhtml @@ -13,6 +13,9 @@ crossorigin="anonymous" /> +
+ +

diff --git a/3-integration/aad-ldap/src/main/java/cafe/web/view/Cafe.java b/3-integration/aad-ldap/src/main/java/cafe/web/view/Cafe.java index 7aa4088..0a4681c 100644 --- a/3-integration/aad-ldap/src/main/java/cafe/web/view/Cafe.java +++ b/3-integration/aad-ldap/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -68,6 +69,7 @@ public void setPrice(Double price) { } public List getCoffeeList() { + this.getAllCoffees(); return coffeeList; } @@ -79,6 +81,10 @@ public boolean isDisabledForDeletion() { return !securityContext.isCallerInRole("admin"); } + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; + } + @PostConstruct private void init() { try { @@ -94,8 +100,7 @@ private void init() { public boolean verify(String hostname, SSLSession session) { return true; } - }).build().register(filter); - this.getAllCoffees(); + }).build().register(filter); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -108,16 +113,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/3-integration/aad-ldap/src/main/webapp/index.xhtml b/3-integration/aad-ldap/src/main/webapp/index.xhtml index a3ce84a..a0768fb 100644 --- a/3-integration/aad-ldap/src/main/webapp/index.xhtml +++ b/3-integration/aad-ldap/src/main/webapp/index.xhtml @@ -13,6 +13,9 @@ crossorigin="anonymous" /> +
+ +

diff --git a/3-integration/aad-oidc/src/main/java/cafe/web/view/Cafe.java b/3-integration/aad-oidc/src/main/java/cafe/web/view/Cafe.java index d23542d..bab07bb 100644 --- a/3-integration/aad-oidc/src/main/java/cafe/web/view/Cafe.java +++ b/3-integration/aad-oidc/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -71,6 +72,7 @@ public void setPrice(Double price) { } public List getCoffeeList() { + this.getAllCoffees(); return coffeeList; } @@ -82,6 +84,10 @@ public boolean isDisabledForDeletion() { return !jwtUtil.isUserInAdminGroup(); } + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; + } + @PostConstruct private void init() { try { @@ -97,8 +103,7 @@ private void init() { public boolean verify(String hostname, SSLSession session) { return true; } - }).build().register(filter); - this.getAllCoffees(); + }).build().register(filter); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -111,16 +116,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); - this.getAllCoffees(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/3-integration/aad-oidc/src/main/webapp/index.xhtml b/3-integration/aad-oidc/src/main/webapp/index.xhtml index a3ce84a..a0768fb 100644 --- a/3-integration/aad-oidc/src/main/webapp/index.xhtml +++ b/3-integration/aad-oidc/src/main/webapp/index.xhtml @@ -13,6 +13,9 @@ crossorigin="anonymous" /> +
+ +

diff --git a/3-integration/connect-db/mssql/src/main/java/cafe/web/view/Cafe.java b/3-integration/connect-db/mssql/src/main/java/cafe/web/view/Cafe.java index e940af5..eac7054 100644 --- a/3-integration/connect-db/mssql/src/main/java/cafe/web/view/Cafe.java +++ b/3-integration/connect-db/mssql/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -60,14 +61,14 @@ public void setPrice(Double price) { } public List getCoffeeList() { - this.getAllCoffees(); + this.getAllCoffees(); return coffeeList; } - public String getPodName() { - return "true".equals(System.getenv("SHOW_POD_NAME")) ? System.getenv("HOSTNAME") : ""; + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; } - + @PostConstruct private void init() { try { @@ -84,7 +85,6 @@ public boolean verify(String hostname, SSLSession session) { return true; } }).build(); - this.getAllCoffees(); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -97,14 +97,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/3-integration/connect-db/mssql/src/main/webapp/index.xhtml b/3-integration/connect-db/mssql/src/main/webapp/index.xhtml index af149b7..877ee33 100644 --- a/3-integration/connect-db/mssql/src/main/webapp/index.xhtml +++ b/3-integration/connect-db/mssql/src/main/webapp/index.xhtml @@ -14,7 +14,7 @@
- +
diff --git a/3-integration/connect-db/openlibertyapplication-cluster.yaml b/3-integration/connect-db/openlibertyapplication-cluster.yaml index ab8f7b7..9a84ee7 100644 --- a/3-integration/connect-db/openlibertyapplication-cluster.yaml +++ b/3-integration/connect-db/openlibertyapplication-cluster.yaml @@ -18,7 +18,7 @@ spec: # - replace "${DB_Type}" with "mssql" for testing DB connection with Azure SQL # - replace "${DB_Type}" with "postgres" for testing DB connection with Azure Database for PostgreSQL env: - - name: SHOW_POD_NAME + - name: SHOW_HOST_NAME value: 'true' - name: DB_SERVER_NAME valueFrom: diff --git a/3-integration/connect-db/postgres/src/main/java/cafe/web/view/Cafe.java b/3-integration/connect-db/postgres/src/main/java/cafe/web/view/Cafe.java index e940af5..eac7054 100644 --- a/3-integration/connect-db/postgres/src/main/java/cafe/web/view/Cafe.java +++ b/3-integration/connect-db/postgres/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -60,14 +61,14 @@ public void setPrice(Double price) { } public List getCoffeeList() { - this.getAllCoffees(); + this.getAllCoffees(); return coffeeList; } - public String getPodName() { - return "true".equals(System.getenv("SHOW_POD_NAME")) ? System.getenv("HOSTNAME") : ""; + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; } - + @PostConstruct private void init() { try { @@ -84,7 +85,6 @@ public boolean verify(String hostname, SSLSession session) { return true; } }).build(); - this.getAllCoffees(); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -97,14 +97,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/3-integration/connect-db/postgres/src/main/webapp/index.xhtml b/3-integration/connect-db/postgres/src/main/webapp/index.xhtml index af149b7..877ee33 100644 --- a/3-integration/connect-db/postgres/src/main/webapp/index.xhtml +++ b/3-integration/connect-db/postgres/src/main/webapp/index.xhtml @@ -14,7 +14,7 @@
- +
diff --git a/4-finish/src/main/java/cafe/web/view/Cafe.java b/4-finish/src/main/java/cafe/web/view/Cafe.java index 23e92c0..bab07bb 100644 --- a/4-finish/src/main/java/cafe/web/view/Cafe.java +++ b/4-finish/src/main/java/cafe/web/view/Cafe.java @@ -1,5 +1,6 @@ package cafe.web.view; +import java.io.IOException; import java.io.Serializable; import java.lang.invoke.MethodHandles; import java.net.InetAddress; @@ -71,7 +72,7 @@ public void setPrice(Double price) { } public List getCoffeeList() { - this.getAllCoffees(); + this.getAllCoffees(); return coffeeList; } @@ -83,8 +84,8 @@ public boolean isDisabledForDeletion() { return !jwtUtil.isUserInAdminGroup(); } - public String getPodName() { - return "true".equals(System.getenv("SHOW_POD_NAME")) ? System.getenv("HOSTNAME") : ""; + public String getHostName() { + return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : ""; } @PostConstruct @@ -103,7 +104,6 @@ public boolean verify(String hostname, SSLSession session) { return true; } }).build().register(filter); - this.getAllCoffees(); } catch (IllegalArgumentException | NullPointerException | WebApplicationException | UnknownHostException ex) { logger.severe("Processing of HTTP response failed."); ex.printStackTrace(); @@ -116,14 +116,16 @@ private void getAllCoffees() { }); } - public void addCoffee() { + public void addCoffee() throws IOException { Coffee coffee = new Coffee(this.name, this.price); this.client.target(baseUri).request(MediaType.APPLICATION_JSON).post(Entity.json(coffee)); this.name = null; this.price = null; + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } - public void removeCoffee(String coffeeId) { + public void removeCoffee(String coffeeId) throws IOException { this.client.target(baseUri).path(coffeeId).request().delete(); + FacesContext.getCurrentInstance().getExternalContext().redirect("/"); } } diff --git a/4-finish/src/main/webapp/index.xhtml b/4-finish/src/main/webapp/index.xhtml index 6a655d9..a0768fb 100644 --- a/4-finish/src/main/webapp/index.xhtml +++ b/4-finish/src/main/webapp/index.xhtml @@ -14,7 +14,7 @@
- +