/
AppPageGeneric.java
493 lines (382 loc) · 13.3 KB
/
AppPageGeneric.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package org.rapidoid.app.builtin;
/*
* #%L
* rapidoid-app
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Scaffold;
import org.rapidoid.annotation.Since;
import org.rapidoid.aop.AOP;
import org.rapidoid.app.AppClasses;
import org.rapidoid.app.AppGUI;
import org.rapidoid.app.AppScreens;
import org.rapidoid.app.Apps;
import org.rapidoid.app.Scaffolding;
import org.rapidoid.appctx.AppCtx;
import org.rapidoid.appctx.Scan;
import org.rapidoid.beany.Beany;
import org.rapidoid.cls.Cls;
import org.rapidoid.config.Conf;
import org.rapidoid.html.Cmd;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.ATag;
import org.rapidoid.html.tag.FormTag;
import org.rapidoid.http.HttpExchange;
import org.rapidoid.http.HttpNotFoundException;
import org.rapidoid.http.HttpSuccessException;
import org.rapidoid.oauth.OAuth;
import org.rapidoid.oauth.OAuthProvider;
import org.rapidoid.pages.Pages;
import org.rapidoid.pages.impl.ComplexView;
import org.rapidoid.plugins.db.DB;
import org.rapidoid.plugins.languages.Languages;
import org.rapidoid.security.Secure;
import org.rapidoid.util.English;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
public class AppPageGeneric extends AppGUI implements ComplexView {
protected static final String[] themes = { "default", "1", "2", "3", "4", "5", "none" };
protected static final Pattern ENTITY_VIEW = Pattern.compile("^/(\\w+?)/(\\d+)/?$");
protected static final Pattern ENTITY_EDIT = Pattern.compile("^/edit(\\w+?)/(\\d+)/?$");
protected static final Pattern ENTITY_NEW = Pattern.compile("^/new(\\w+?)/?$");
protected static final Pattern ENTITY_LIST = Pattern.compile("^/(\\w+?)/?$");
protected static final AppScreens APP_SCREENS = Cls.customizable(AppScreens.class);
protected static final Class<?>[] BUILT_IN_SCREENS = { SearchScreenBuiltIn.class, SettingsScreenBuiltIn.class,
DebugUserInfoScreenBuiltIn.class, DeleteAllDataScreenBuiltIn.class };
protected final HttpExchange x;
protected final AppClasses appCls;
protected final Object app;
protected final Object screen;
public AppPageGeneric(HttpExchange x, AppClasses appCls, Object app) {
this.x = x;
this.appCls = appCls;
this.app = app;
this.screen = Apps.wireExchange(getScreen(), x);
if (appCls.main != null) {
Method init = Cls.findMethod(appCls.main, "init");
if (init != null) {
AOP.invoke(x, init, app);
}
}
U.must(screen != null, "Cannot find a screen to process the request!");
}
@Override
public Object[] getSubViews() {
return new Object[] { app, screen };
}
public String title() {
return Pages.titleOf(x, app);
}
public Object head() {
String theme = config("theme", null);
return theme != null ? link().href(themeUrl(theme)).rel("stylesheet") : null;
}
protected String themeUrl(String theme) {
if (theme.startsWith("bootswatch:")) {
theme = theme.substring("bootswatch:".length());
return "//maxcdn.bootstrapcdn.com/bootswatch/3.3.0/" + theme + "/bootstrap.min.css";
} else {
return "/bootstrap/css/theme-" + theme + ".css";
}
}
public Object content() {
Map<String, Class<?>> mainScreens = filterScreens();
Object pageContent = null;
int activeIndex = -1;
pageContent = pageContent();
Class<?>[] screens = APP_SCREENS.constructScreens(mainScreens);
List<Class<?>> scaffolding = Scan.annotated(Scaffold.class);
Object[] menuItems = new Object[screens.length + scaffolding.size()];
activeIndex = setupMenuItems(x.path(), screens, menuItems, scaffolding);
boolean showNavbar = Apps.config(app, "navbar", true);
Object result;
String modal = Beany.getPropValue(screen, "modal", null);
Object modalContent = modal != null ? Beany.getPropValue(screen, modal, null) : null;
if (showNavbar) {
ATag brand = a(Pages.titleOf(x, app)).href("/");
Tag userMenu = userMenu();
Tag debugMenu = Conf.dev() ? debugMenu() : null;
FormTag searchForm = searchForm();
Tag navMenu = navbarMenu(true, activeIndex, menuItems);
Object[] navbarContent = arr(navMenu, debugMenu, userMenu, searchForm);
result = navbarPage(isFluid(), brand, navbarContent, arr(pageContent, modalContent));
} else {
result = cleanPage(isFluid(), arr(pageContent, modalContent));
}
Pages.store(x, screen);
return result;
}
protected Object genericScreen() {
String path = x.path();
if (path.equals("/")) {
return appCls.main != null ? app : new TheDefaultApp();
}
for (Class<?> scr : BUILT_IN_SCREENS) {
if (Apps.screenUrl(scr).equals(path)) {
return Cls.newInstance(scr);
}
}
if (!x.query().isEmpty()) {
return null;
}
Matcher m = ENTITY_EDIT.matcher(path);
if (m.find()) {
String type = m.group(1);
String id = m.group(2);
Class<?> entityType = Scaffolding.getScaffoldingEntity(type);
if (entityType == null) {
return null;
}
Object entity = DB.getIfExists(entityType, id);
String entityClass = Cls.entityName(entity);
String reqType = U.capitalized(type);
if (entityClass.equals(reqType)) {
return new EditEntityScreenGeneric(entityType);
}
}
m = ENTITY_NEW.matcher(path);
if (m.find()) {
String type = m.group(1);
Class<?> entityType = Scaffolding.getScaffoldingEntity(type);
if (entityType == null) {
return null;
}
return new NewEntityScreenGeneric(entityType);
}
m = ENTITY_VIEW.matcher(path);
if (m.find()) {
String type = m.group(1);
String id = m.group(2);
Class<?> entityType = Scaffolding.getScaffoldingEntity(type);
if (entityType == null) {
return null;
}
Object entity = DB.getIfExists(entityType, id);
String entityClass = Cls.entityName(entity);
String reqType = U.capitalized(type);
if (entityClass.equals(reqType)) {
return new ViewEntityScreenGeneric(entityType);
}
}
m = ENTITY_LIST.matcher(path);
if (m.find()) {
String type = m.group(1);
String type2 = U.or(Languages.pluralToSingular(type), type);
Class<?> entityType = Scaffolding.getScaffoldingEntity(type2);
if (entityType == null) {
return null;
}
return new ListEntityScreenGeneric(entityType);
}
return null;
}
protected Object pageContent() {
Object cnt = Pages.contentOf(x, screen);
if (cnt != null) {
return cnt;
} else {
if (screen != null && (screen != app || x.uri().equals("/") || x.uri().equals("/index.html"))) {
return "No content available!";
} else {
throw x.notFound();
}
}
}
protected FormTag searchForm() {
return addon("search") ? navbarSearchForm("/search") : null;
}
protected boolean addon(String name) {
return Apps.addon(app, name);
}
protected <T> T config(String name, T defaultValue) {
return Apps.config(app, name, defaultValue);
}
protected int setupMenuItems(String currentUrl, Class<?>[] screens, Object[] menuItems, List<Class<?>> scaffolding) {
int activeIndex = -1;
int k = 0;
int i = 0;
for (; i < screens.length; i++) {
Class<?> scr = screens[i];
String name = Apps.screenName(scr);
if (name.equalsIgnoreCase("about") || name.equalsIgnoreCase("help") || name.equalsIgnoreCase("settings")
|| name.equalsIgnoreCase("search") || name.equalsIgnoreCase("profile")) {
break;
}
String title = Cls.getFieldValue(scr, "title", UTILS.camelPhrase(name));
String url = Apps.screenUrl(scr);
menuItems[k++] = a(title).href(url);
if (url.equals(currentUrl)) {
activeIndex = i;
}
}
for (int j = 0; j < scaffolding.size(); j++) {
Class<?> scaff = scaffolding.get(j);
String name = English.plural(scaff.getSimpleName());
String title = UTILS.camelSplit(name);
String url = "/" + name.toLowerCase();
menuItems[k++] = a(title).href(url);
if (url.equals(currentUrl)) {
activeIndex = i + j;
}
}
for (; i < screens.length; i++) {
Class<?> scr = screens[i];
String name = Apps.screenName(scr);
String title = Cls.getFieldValue(scr, "title", UTILS.camelPhrase(name));
String url = Apps.screenUrl(scr);
menuItems[k++] = a(title).href(url);
if (url.equals(currentUrl)) {
activeIndex = i + scaffolding.size();
}
}
return activeIndex;
}
protected Tag userMenu() {
Tag dropdownMenu = null;
if (addon("auth") || addon("googleLogin") || addon("facebookLogin") || addon("linkedinLogin")
|| addon("githubLogin")) {
if (AppCtx.isLoggedIn()) {
dropdownMenu = loggedInUserMenu();
} else {
dropdownMenu = loggedOutUserMenu();
}
}
return dropdownMenu;
}
protected ATag[] themesMenuOpttions() {
ATag[] themess = new ATag[themes.length];
Tag eye = awesome("eye");
for (int i = 0; i < themes.length; i++) {
String thm = themes[i];
String js = U.format("document.cookie='THEME=%s; path=/'; location.reload();", thm);
themess[i] = a_void(eye, NBSP, "Theme " + U.capitalized(thm)).onclick(js);
}
return themess;
}
protected Tag loggedOutUserMenu() {
Tag ga = null, fb = null, li = null, gh = null;
if (addon("googleLogin")) {
ga = loginMenuItem(OAuthProvider.GOOGLE);
}
if (addon("facebookLogin")) {
fb = loginMenuItem(OAuthProvider.FACEBOOK);
}
if (addon("linkedinLogin")) {
li = loginMenuItem(OAuthProvider.LINKEDIN);
}
if (addon("githubLogin")) {
gh = loginMenuItem(OAuthProvider.GITHUB);
}
return navbarDropdown(false, a_awesome("user", "Sign in", caret()), ga, fb, li, gh);
}
protected ATag loginMenuItem(OAuthProvider provider) {
String text = "Sign in with " + provider.getName();
String js = U.format("_popup('%s');", OAuth.getLoginURL(x, provider, null));
return a_awesome(provider.getName().toLowerCase(), text).onclick(js);
}
protected Tag loggedInUserMenu() {
ATag profile = a_awesome("user", userDisplay(), caret());
ATag settings = addon("settings") ? a_awesome("cog", " Settings").href("/settings") : null;
ATag logout = a_awesome("sign-out", "Logout").href("/_logout");
return navbarDropdown(false, profile, settings, logout);
}
protected String userDisplay() {
String username = AppCtx.username();
int pos = username.indexOf('@');
return pos > 0 ? username.substring(0, pos) : username;
}
protected Tag debugMenu() {
ATag debug = a_awesome("bug", " ", caret());
ATag userInfo = a_awesome("bug", "User info").href("/debuguserinfo");
ATag delAll = a_awesome("bug", "Delete All Data").href("/deletealldata");
Tag sep = menuDivider();
String theme = config("theme", null);
ATag[] themesOpts = theme == null ? themesMenuOpttions() : null;
Tag themeSep = themesOpts != null ? sep : null;
return navbarDropdown(false, debug, debugLoginUrl("admin"), debugLoginUrl("manager"),
debugLoginUrl("moderator"), debugLoginUrl("foo"), debugLoginUrl("bar"), sep, userInfo, themeSep,
themesOpts, sep, delAll);
}
protected ATag debugLoginUrl(String username) {
return a_awesome("bug", "Sign in as " + U.capitalized(username)).href("/_debugLogin?user=" + username);
}
protected boolean isFluid() {
return config("fluid", true);
}
protected Object getScreen() {
// TODO use screens.get(...) instead of iteration
for (Class<?> screen : appCls.screens.values()) {
if (Apps.screenUrl(screen).equals(x.path())) {
x.authorize(screen);
return Cls.newInstance(screen);
}
}
return U.or(genericScreen(), app);
}
protected Map<String, Class<?>> filterScreens() {
Object[] screensConfig = config("screens", null);
if (screensConfig == null) {
screensConfig = appCls.screens.values().toArray();
}
Map<String, Class<?>> filtered = new LinkedHashMap<String, Class<?>>();
for (Object scr : screensConfig) {
if (scr instanceof Class<?>) {
Class<?> cls = (Class<?>) scr;
Class<?> screenCls = appCls.screens.get(cls.getSimpleName());
if (isScreenAllowed(screenCls)) {
filtered.put(cls.getSimpleName(), screenCls);
}
} else if (scr instanceof String) {
String name = U.capitalized((String) scr);
Class<?> screenCls = appCls.screens.get(name);
if (isScreenAllowed(screenCls)) {
filtered.put(name, screenCls);
}
} else {
throw U.rte("Expected class or string to represent a screen, but found: %s", scr);
}
}
return filtered;
}
protected boolean isScreenAllowed(Class<?> screenCls) {
return Secure.canAccessClass(AppCtx.username(), screenCls);
}
public void on(String cmd, Object[] args) {
try {
Pages.callCmdHandler(x, screen, new Cmd(cmd, false, args));
} catch (Exception e) {
Throwable cause = UTILS.rootCause(e);
if (cause instanceof HttpSuccessException || cause instanceof HttpNotFoundException) {
Pages.store(x, screen);
}
throw U.rte(e);
}
}
@Override
public String toString() {
return "AppPageGeneric [screen=" + screen + "]";
}
}