/
SessionBean.java
190 lines (159 loc) · 6.32 KB
/
SessionBean.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package net.clementlevallois.nocodeapp.web.front.backingbeans;
import io.mikael.urlbuilder.UrlBuilder;
import jakarta.inject.Named;
import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;
import jakarta.enterprise.context.SessionScoped;
import jakarta.faces.context.FacesContext;
import jakarta.servlet.http.HttpServletRequest;
import net.clementlevallois.nocodeapp.web.front.http.RemoteLocal;
import net.clementlevallois.nocodeapp.web.front.http.SendReport;
import jakarta.annotation.PostConstruct;
import jakarta.faces.application.FacesMessage;
import jakarta.inject.Inject;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import net.clementlevallois.nocodeapp.web.front.i18n.I18nStaticFilesResourceBundle;
/**
*
* @author LEVALLOIS
*/
@Named
@SessionScoped
public class SessionBean implements Serializable {
private String function;
private String gazeOption = "1";
private String userAgent;
private ResourceBundle localeBundle;
private boolean testServer;
private String noRobot;
private Locale currentLocale;
@Inject
ApplicationPropertiesBean applicationProperties;
public SessionBean() {
}
@PostConstruct
public void init() {
FacesContext currentInstance = FacesContext.getCurrentInstance();
if (currentInstance == null) {
currentLocale = Locale.ENGLISH;
userAgent = "unknown-user-agent";
} else {
currentLocale = currentInstance.getExternalContext().getRequestLocale();
HttpServletRequest request = (HttpServletRequest) currentInstance.getExternalContext().getRequest();
userAgent = request.getHeader("user-agent");
}
I18nStaticFilesResourceBundle staticFilesResourceBundle = new I18nStaticFilesResourceBundle();
localeBundle = staticFilesResourceBundle.simpleMethodToGetResourceBundle(currentLocale);
}
public void setApplicationProperties(ApplicationPropertiesBean applicationProperties) {
this.applicationProperties = applicationProperties;
}
public String getFunction() {
return function;
}
public void setFunction(String function) {
if (function == null) {
System.out.println("function param was null??");
return;
}
if (function.contains("=") & !function.contains("?")) {
System.out.println("weird url parameters decoded in sessionBean");
System.out.println("url param for function is: " + function);
this.function = function.split("=")[1];
} else if (function.contains("=") & function.contains("?")) {
this.function = function.split("\\?")[0];
} else {
this.function = function;
}
}
public String getGazeOption() {
return gazeOption;
}
public void setGazeOption(String gazeOption) {
this.gazeOption = gazeOption;
}
public void sendFunctionPageReport() {
SendReport send = new SendReport(applicationProperties.getMiddlewareHost(), applicationProperties.getMiddlewarePort());
send.initAnalytics(this.function, userAgent);
send.start();
}
public String logout() {
Locale persistLocale = currentLocale;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext.getCurrentInstance().getViewRoot().setLocale(persistLocale);
return "/index?faces-redirect=true";
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public String host() {
return RemoteLocal.getDomain();
}
public String getHostFunctionsAPI(Boolean urlEncode) {
URI uri;
if (RemoteLocal.isLocal()) {
uri = UrlBuilder
.empty()
.withScheme("http")
.withHost("localhost")
.withPort((Integer.valueOf(applicationProperties.getPrivateProperties().getProperty("nocode_api_port")))).toUri();
return uri.toString();
} else {
String domain;
if (System.getProperty("test") != null && System.getProperty("test").equals("yes")) {
domain = "test.nocodefunctions.com";
} else {
domain = "nocodefunctions.com";
}
UrlBuilder urlBuilder = UrlBuilder
.empty()
.withScheme("https")
.withHost(domain);
String urlString = urlBuilder.toUri().toString();
return urlEncode ? URLEncoder.encode(urlString, StandardCharsets.UTF_8) : urlString;
}
}
public void refreshLocaleBundle() {
I18nStaticFilesResourceBundle dbb = new I18nStaticFilesResourceBundle();
localeBundle = dbb.simpleMethodToGetResourceBundle(currentLocale);
}
public ResourceBundle getLocaleBundle() {
return localeBundle;
}
// redudant with RemoteLocal.isTest() but for a good reason
// we need this method here because that's going to be called in an EL in a page
// and for that it has to be in a bean, not just a static method
public boolean isTestServer() {
return RemoteLocal.isTest();
}
public void setTestServer(boolean testServer) {
this.testServer = testServer;
}
public String getNoRobot() {
String noIndex = "<meta name=\"robots\" content=\"noindex\"/>";
return noIndex;
}
public void setNoRobot(String noRobot) {
this.noRobot = noRobot;
}
public Locale getCurrentLocale() {
return currentLocale;
}
public void setCurrentLocale(Locale currentLocale) {
this.currentLocale = currentLocale;
}
public void addMessage(FacesMessage.Severity severity, String summary, String detail) {
try {
FacesContext.getCurrentInstance().
addMessage(null, new FacesMessage(severity, summary, detail));
} catch (NullPointerException e) {
System.out.println("FacesContext.getCurrentInstance was null. Detail: " + detail);
}
}
}