-
Notifications
You must be signed in to change notification settings - Fork 5
/
ApplicationServer.java
236 lines (216 loc) · 11.6 KB
/
ApplicationServer.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.genericsystem.reactor.appserver;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.genericsystem.common.AbstractBackEnd;
import org.genericsystem.common.AbstractCache;
import org.genericsystem.common.AbstractWebSocketsServer;
import org.genericsystem.common.GSBuffer;
import org.genericsystem.common.Root;
import org.genericsystem.reactor.HtmlDomNode;
import org.genericsystem.reactor.Model;
import org.genericsystem.reactor.ViewContext.RootViewContext;
import org.genericsystem.reactor.appserver.WebAppsConfig.SimpleWebAppConfig;
import org.genericsystem.reactor.gs.GSApp;
import org.genericsystem.reactor.html.HtmlApp;
/**
* @author Nicolas Feybesse
*
*/
public class ApplicationServer extends AbstractBackEnd {
protected static Logger log = LoggerFactory.getLogger(ApplicationServer.class);
protected Map<String, PersistentApplication<?>> apps = new HashMap<>();
public ApplicationServer(WebAppsConfig options) {
super(options.getHost(), options.getPort());
log.info("Load config : \n" + options.encodePrettily());
for (String directoryPath : options.getPersistentDirectoryPaths()) {
String path = directoryPath != null ? directoryPath : "/";
Root root = buildRoot(directoryPath, options.getClasses(directoryPath), options.getEngineClass(directoryPath));
log.info("Starts " + root.getClass().getSimpleName() + " with path : " + path + " and persistence directory path : " + directoryPath);
if (directoryPath == null)
directoryPath = "/";
roots.put(path, root);
}
for (String applicationPath : options.getApplicationsPaths()) {
String directoryPath = options.getPersistentDirectoryPath(applicationPath);
String path = directoryPath != null ? directoryPath : "/";
apps.put(applicationPath, new PersistentApplication(options.getApplicationClass(applicationPath), options.getModelClass(applicationPath), roots.get(path), options.getRootId()));
}
}
public static void sartSimpleWebApp(String[] mainArgs, Class<? extends HtmlApp<?>> htmlAppClass, Class<? extends Model> modelClass, String homePersistentDirectoryPath) {
new ApplicationServer(new SimpleWebAppConfig(mainArgs, htmlAppClass, modelClass, homePersistentDirectoryPath)).start();
}
public static void sartSimpleGenericApp(String[] mainArgs, Class<? extends GSApp> htmlAppClass, String homePersistentDirectoryPath) {
new ApplicationServer(new SimpleWebAppConfig(mainArgs, htmlAppClass, homePersistentDirectoryPath)).start();
}
protected Root buildRoot(String persistentDirectoryPath, Set<Class<?>> userClasses, Class<? extends Root> applicationClass) {
try {
return applicationClass.getConstructor(String.class, Class[].class).newInstance(persistentDirectoryPath, userClasses.toArray(new Class[userClasses.size()]));
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
// protected PersistentApplication buildApp(Class<? extends TagTree> applicationClass, String persistentDirectoryPath, List<Class<?>> userClasses, Class<? extends Model> modelClass, Root engine, String rootId) {
// return new PersistentApplication(applicationClass, modelClass, engine, rootId);
// }
private class WebSocketsServer extends AbstractWebSocketsServer {
public WebSocketsServer(String host, int port) {
super(host, port);
}
@Override
public Handler<Buffer> getHandler(String path, ServerWebSocket socket) {
PersistentApplication<?> application = apps.get(path);
if (application == null) {
throw new IllegalStateException("Unable to load an application with path : " + path);
}
AbstractCache cache = application.getEngine().newCache();
RootViewContext<?> rootViewContext = cache.safeSupply(() -> application.init(socket));
// log.info("Open new socket : " + socket);
return buffer -> {
// log.info("Receive new message for socket : " + socket);
GSBuffer gsBuffer = new GSBuffer(buffer);
String message = gsBuffer.getString(0, gsBuffer.length());
JsonObject json = new JsonObject(message);
HtmlDomNode node = rootViewContext.getNodeById(json.getString(HtmlDomNode.ID));
if (node != null) {
cache.safeConsum((x) -> node.handleMessage(json));
} else
log.info("Can't find node id : " + json.getString(HtmlDomNode.ID));
};
}
@Override
public void addHttpHandler(HttpServer httpServer) {
httpServer.requestHandler(request -> {
// log.info("Request received with path : " + request.path());
String[] items = request.path().substring(1).split("/");
// log.info("Request received with splited items : " + Arrays.toString(items));
String appPath = items.length == 0 ? "" : items[0];
if (appPath.endsWith(".js") || appPath.endsWith(".css") || appPath.endsWith(".ico") || appPath.endsWith(".jpg") || appPath.endsWith(".png"))
appPath = "";
// log.info("Request received with application path : " + appPath);
PersistentApplication<?> application = apps.get("/" + appPath);
if (application == null) {
request.response().end("No application is configured with path : /" + appPath);
log.info("No application is configured with path : /" + appPath);
return;
}
// log.info("Request detected for application : " + application.getApplicationClass().getName());
int shift = appPath.isEmpty() ? 0 : 1;
shift += items.length > shift ? 1 : 0;
String resourceToServe = request.path().substring(appPath.length() + shift);
// log.info("Resource to serve : " + resourceToServe);
if ("".equals(resourceToServe)) {
String indexHtml = "<!DOCTYPE html>";
indexHtml += "<html>";
indexHtml += "<head>";
indexHtml += "<meta charset=\"UTF-8\">";
indexHtml += "<LINK rel=stylesheet type=\"text/css\" href=\"" + (appPath.isEmpty() ? "" : ("/" + appPath)) + "/" + application.getApplicationClass().getSimpleName().toLowerCase() + ".css\"/>";
indexHtml += "<script>";
indexHtml += "var serviceLocation = \"ws://\" + document.location.host + \"" + request.path() + "\";";
indexHtml += "</script>";
indexHtml += "<script type=\"text/javascript\" src=\"" + (appPath.isEmpty() ? "" : ("/" + appPath)) + "/" + application.getApplicationClass().getSimpleName().toLowerCase() + ".js\"></script>";
indexHtml += "</head>";
indexHtml += "<body onload=\"connect();\" id=\"" + application.getRootId() + "\">";
indexHtml += "</body>";
indexHtml += "</html>";
request.response().end(indexHtml);
} else {
InputStream input = application.getApplicationClass().getResourceAsStream("/" + resourceToServe);
if (input == null) {
if (resourceToServe.endsWith(".css")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard reactor.css instead");
input = ApplicationServer.class.getResourceAsStream("/reactor.css");
} else if (resourceToServe.endsWith(".js")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard script.js instead");
input = ApplicationServer.class.getResourceAsStream("/script.js");
} else if (resourceToServe.endsWith("favicon.ico")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get the reactor standard favicon.ico instead");
input = ApplicationServer.class.getResourceAsStream("/favicon.ico");
} else if (resourceToServe.endsWith(".ico") || resourceToServe.endsWith(".jpg") || resourceToServe.endsWith(".png")) {
log.warn("Unable to find resource : /" + resourceToServe + ", get nothing instead");
request.response().end();
return;
} else
throw new IllegalStateException("Unable to find resource : " + resourceToServe);
}
if (resourceToServe.endsWith(".ico")) {
MultiMap headers = request.response().headers();
Buffer buffer = makeBuffer(input);
headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon");
headers.add(HttpHeaders.CONTENT_LENGTH, Integer.toString(buffer.length()));
headers.add(HttpHeaders.CACHE_CONTROL, "public, max-age=" + 86400);
request.response().end(buffer);
} else if (resourceToServe.endsWith(".jpg") || resourceToServe.endsWith(".png")) {
// TODO
MultiMap headers = request.response().headers();
Buffer buffer = makeBuffer(input);
// headers.add(HttpHeaders.CONTENT_TYPE, "image/x-icon");
headers.add(HttpHeaders.CONTENT_LENGTH, Integer.toString(buffer.length()));
headers.add(HttpHeaders.CACHE_CONTROL, "public, max-age=" + 86400);
request.response().end(buffer);
} else {
String result = new BufferedReader(new InputStreamReader(input)).lines().collect(Collectors.joining("\n"));
request.response().end(result);
}
}
});
}
private Buffer makeBuffer(InputStream input) {
Buffer buffer = Buffer.buffer();
int read;
byte[] data = new byte[4096];
try {
while ((read = input.read(data, 0, data.length)) != -1) {
if (read == data.length) {
buffer.appendBytes(data);
} else {
byte[] slice = new byte[read];
System.arraycopy(data, 0, slice, 0, slice.length);
buffer.appendBytes(slice);
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
return buffer;
}
@Override
public void start() {
super.start();
String logo = "\n";
logo += ("____________________________________________________________________________________________________________\n");
logo += ("|___________________________________________________________________________________________________________|\n");
logo += ("|___________________________________________________________________________________________________________|\n");
logo += ("|____________| ____ _ ____ __ /______________|\n");
logo += ("|____________| / ___)___ _ _____ ___ /_)__ / ___)_ __ ___ / / ___ ____ /_______________|\n");
logo += ("|____________| / /___/ __)/ \\/ / __)/ _ )/ |/ _)/___ \\/ \\/ ) __)/___)/ __)/ ) /________________|\n");
logo += ("|____________| / /_ / __)/ / __)/ \\/ / /_ ___/ /\\ (__ / /_ / __)/ / / / /_________________|\n");
logo += ("|____________| \\____(____(_/\\_(____(_/\\_(__(____(____/ \\ (____(____(____(_/_/_/ /__________________|\n");
logo += ("|____________| /_/ /___________________|\n");
logo += ("|____________|_________________________________________________________________________/____________________|\n");
logo += ("|___________________________________________________________________________________________________________|\n");
logo += ("|___________________________________________________________________________________________________________| \n");
log.info(logo);
log.info("Generic System Server is ready!");
}
}
@Override
protected WebSocketsServer buildWebSocketsServer(String host, int port) {
return new WebSocketsServer(host, port);
}
}