Skip to content

Commit

Permalink
make sessionbean cafe as stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
majguo committed Sep 30, 2020
1 parent e96999b commit a1b73b7
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 46 deletions.
15 changes: 10 additions & 5 deletions 1-start/src/main/java/cafe/web/view/Cafe.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,9 +61,14 @@ public void setPrice(Double price) {
}

public List<Coffee> getCoffeeList() {
this.getAllCoffees();
return coffeeList;
}

public String getHostName() {
return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : "";
}

@PostConstruct
private void init() {
try {
Expand All @@ -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();
Expand All @@ -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("/");
}
}
3 changes: 3 additions & 0 deletions 1-start/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
crossorigin="anonymous" />
</h:head>
<h:body>
<div>
<h:outputText value="#{cafe.hostName}" />
</div>
<h:form>
<div class="container">
<h2>
Expand Down
15 changes: 10 additions & 5 deletions 2-simple/src/main/java/cafe/web/view/Cafe.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,9 +61,14 @@ public void setPrice(Double price) {
}

public List<Coffee> getCoffeeList() {
this.getAllCoffees();
return coffeeList;
}

public String getHostName() {
return "true".equals(System.getenv("SHOW_HOST_NAME")) ? System.getenv("HOSTNAME") : "";
}

@PostConstruct
private void init() {
try {
Expand All @@ -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();
Expand All @@ -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("/");
}
}
3 changes: 3 additions & 0 deletions 2-simple/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
crossorigin="anonymous" />
</h:head>
<h:body>
<div>
<h:outputText value="#{cafe.hostName}" />
</div>
<h:form>
<div class="container">
<h2>
Expand Down
17 changes: 11 additions & 6 deletions 3-integration/aad-ldap/src/main/java/cafe/web/view/Cafe.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void setPrice(Double price) {
}

public List<Coffee> getCoffeeList() {
this.getAllCoffees();
return coffeeList;
}

Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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("/");
}
}
3 changes: 3 additions & 0 deletions 3-integration/aad-ldap/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
crossorigin="anonymous" />
</h:head>
<h:body>
<div>
<h:outputText value="#{cafe.hostName}" />
</div>
<h:form>
<div class="container">
<h2>
Expand Down
17 changes: 11 additions & 6 deletions 3-integration/aad-oidc/src/main/java/cafe/web/view/Cafe.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,6 +72,7 @@ public void setPrice(Double price) {
}

public List<Coffee> getCoffeeList() {
this.getAllCoffees();
return coffeeList;
}

Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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("/");
}
}
3 changes: 3 additions & 0 deletions 3-integration/aad-oidc/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
crossorigin="anonymous" />
</h:head>
<h:body>
<div>
<h:outputText value="#{cafe.hostName}" />
</div>
<h:form>
<div class="container">
<h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,14 +61,14 @@ public void setPrice(Double price) {
}

public List<Coffee> 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 {
Expand All @@ -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();
Expand All @@ -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("/");
}
}
2 changes: 1 addition & 1 deletion 3-integration/connect-db/mssql/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</h:head>
<h:body>
<div>
<h:outputText value="#{cafe.podName}" />
<h:outputText value="#{cafe.hostName}" />
</div>
<h:form>
<div class="container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,14 +61,14 @@ public void setPrice(Double price) {
}

public List<Coffee> 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 {
Expand All @@ -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();
Expand All @@ -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("/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</h:head>
<h:body>
<div>
<h:outputText value="#{cafe.podName}" />
<h:outputText value="#{cafe.hostName}" />
</div>
<h:form>
<div class="container">
Expand Down
Loading

0 comments on commit a1b73b7

Please sign in to comment.