/
Cafe.java
126 lines (103 loc) · 3.79 KB
/
Cafe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cafe.web.view;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.security.enterprise.SecurityContext;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import cafe.model.entity.Coffee;
@Named
@SessionScoped
public class Cafe implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private String baseUri;
private transient Client client;
@Inject
private transient SecurityContext securityContext;
@Inject
private transient CafeJwtUtil jwtUtil;
@Inject
private transient CafeRequestFilter filter;
@NotNull
@NotEmpty
protected String name;
@NotNull
protected Double price;
protected List<Coffee> coffeeList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public List<Coffee> getCoffeeList() {
return coffeeList;
}
public String getLoggedOnUser() {
return securityContext.getCallerPrincipal().getName();
}
public boolean isDisabledForDeletion() {
return !jwtUtil.isUserInAdminGroup();
}
@PostConstruct
private void init() {
try {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
.getRequest();
InetAddress inetAddress = InetAddress.getByName(request.getServerName());
baseUri = FacesContext.getCurrentInstance().getExternalContext().getRequestScheme() + "://"
+ inetAddress.getHostName() + ":"
+ FacesContext.getCurrentInstance().getExternalContext().getRequestServerPort()
+ "/javaee-cafe/rest/coffees";
this.client = ClientBuilder.newBuilder().hostnameVerifier(new HostnameVerifier() {
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();
}
}
private void getAllCoffees() {
this.coffeeList = this.client.target(this.baseUri).path("/").request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Coffee>>() {
});
}
public void addCoffee() {
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();
}
public void removeCoffee(String coffeeId) {
this.client.target(baseUri).path(coffeeId).request().delete();
this.getAllCoffees();
}
}