forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebUIServer.java
More file actions
executable file
·218 lines (199 loc) · 5.78 KB
/
Copy pathWebUIServer.java
File metadata and controls
executable file
·218 lines (199 loc) · 5.78 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package javaforce.webui;
/** WebUI Server.
*
* WebUI uses a WebSocket to communicate between the server and client side to create
* rich interfaces that are programmed like standard desktop applications.
* No ugly javascript, css or html knowledge needed.
* WebUI is not dependant on AWT or any related APIs and should work with the Java base module.
*
* Browser support:
*
* Internet Explorer is NOT supported
* Edge 12+
* FireFox 28+
* Chrome 21+
*
* @author pquiring
*/
import java.io.*;
import java.util.*;
import javaforce.*;
import javaforce.ui.*;
import javaforce.service.*;
public class WebUIServer implements WebHandler, WebSocketHandler {
private WebServer web;
private WebUIHandler handler;
/** Enable debug log messages. */
public static boolean debug = false;
/** Enable transaction id in WebSocket messages for debugging. */
public static boolean debug_tid = false;
/** Start WebUI Server on non-secure port. */
public void start(WebUIHandler handler, int port) {
start(handler, port, null);
}
/** Start WebUI Server on secure port using provided SSL keys. */
public void start(WebUIHandler handler, int port, KeyMgmt keys) {
this.handler = handler;
if (web != null) stop();
web = new WebServer();
web.setWebSocketHandler(this);
web.start(this, port, keys);
JFLog.log("WebUI Server starting on port " + port + "...");
}
public void stop() {
if (web != null) {
web.stop();
web = null;
}
}
public void setClientVerify(boolean state) {
web.setClientVerify(state);
}
public byte[] getResource(String name) {
InputStream is = getClass().getClassLoader().getResourceAsStream(name);
if (is == null) {
JFLog.log("WebUIServer:Resource not found:" + name);
return null;
}
return JF.readAll(is);
}
public void doPost(WebRequest req, WebResponse res) {
doGet(req, res);
}
public void doGet(WebRequest req, WebResponse res) {
String url = req.getURL();
String url_params = url + "?" + req.getQueryString();
byte[] data = null;
/*
if (url.endsWith(".html")) {
res.setContentType("text/html"); //default
}
*/
if (url.endsWith(".css")) {
res.setContentType("text/css");
}
if (url.endsWith(".png")) {
res.setContentType("image/png");
}
if (url.endsWith(".js")) {
res.setContentType("text/javascript");
}
if (url.equals("/")) {
url = "/webui.html";
String browser = req.getHeader("User-Agent");
if (browser != null) {
if (browser.indexOf("Trident") != -1) {
//Internet Explorer - not supported
url = "/webui-ie.html";
}
}
}
if (url.startsWith("/static/")) {
// url = /static/r#
Resource r = Resource.getResource(url.substring(8));
if (r != null) {
data = r.data;
res.setContentType(r.mime);
}
} else if (url.startsWith("/user/")) {
data = handler.getResource(url_params);
res.addHeader("Cache-Control: no-store");
} else if (url.startsWith("/api/")) {
data = handler.getResource(url_params);
res.addHeader("Cache-Control: no-store");
} else {
if (url.equals("/favicon.ico")) {
url = "/webui.png";
}
data = getResource("javaforce/webui/static" + url);
}
if (data == null) {
res.setStatus(404, "Resource not found");
return;
}
try {
res.write(data);
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<WebUIClient> clients = new ArrayList<WebUIClient>();
public WebUIClient getClient(WebSocket sock) {
int cnt = clients.size();
for(int a=0;a<cnt;a++) {
WebUIClient client = clients.get(a);
if (client.socket == sock) {
return client;
}
}
return null;
}
public WebUIClient getClient(String hash) {
int cnt = clients.size();
for(int a=0;a<cnt;a++) {
WebUIClient client = clients.get(a);
if (client.hash.equals(hash)) {
return client;
}
}
return null;
}
public boolean doWebSocketConnect(WebSocket sock) {
try {
WebUIClient client = new WebUIClient(sock, handler);
clients.add(client);
handler.clientConnected(client);
return true;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
public void doWebSocketClosed(WebSocket sock) {
WebUIClient client = getClient(sock);
clients.remove(client);
handler.clientDisconnected(client);
}
public void doWebSocketMessage(WebSocket sock, byte[] data, int type) {
WebUIClient client = getClient(sock);
if (client == null) {
JFLog.log("Unknown Socket:" + sock);
return;
}
if (type == WebSocket.TYPE_BINARY || type == WebSocket.TYPE_CONT) {
client.dispatchData(data);
return;
}
String msg = new String(data);
if (debug) JFLog.log("RECV=" + msg);
//decode JSON
JSON.Element json;
try {
json = JSON.parse(msg);
} catch (Exception e) {
e.printStackTrace();
return;
}
String id = "";
String event = "";
ArrayList<String> args = new ArrayList<String>();
int cnt = json.children.size();
for(int a=0;a<cnt;a++) {
JSON.Element e = json.children.get(a);
if (e.key.equals("id")) {
id = e.value;
}
else if (e.key.equals("event")) {
event = e.value;
}
else {
args.add(e.key + "=" + e.value);
}
}
try {
client.dispatchEvent(id, event, args.toArray(new String[args.size()]));
} catch (Exception e) {
e.printStackTrace();
}
}
}