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
|
/*
* Copyright 2013 Jesse Morgan <jesse@jesterpm.net>
*/
package com.p4square.grow.frontend;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.apache.log4j.Logger;
import net.jesterpm.fmfacade.FMFacade;
import net.jesterpm.fmfacade.FreeMarkerPageResource;
import com.p4square.grow.config.Config;
/**
* This is the Restlet Application implementing the Grow project front-end.
* It's implemented as an extension of FMFacade that connects interactive pages
* with various ServerResources. This class provides a main method to start a
* Jetty instance for testing.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class GrowFrontend extends FMFacade {
private static Logger cLog = Logger.getLogger(GrowFrontend.class);
private Config mConfig;
public GrowFrontend() {
mConfig = new Config();
}
public Config getConfig() {
return mConfig;
}
@Override
public void start() throws Exception {
super.start();
final String configDomain =
getContext().getParameters().getFirstValue("configDomain");
if (configDomain != null) {
mConfig.setDomain(configDomain);
}
mConfig.updateConfig(this.getClass().getResourceAsStream("/grow.properties"));
final String configFilename =
getContext().getParameters().getFirstValue("configFile");
if (configFilename != null) {
mConfig.updateConfig(configFilename);
}
}
@Override
protected Router createRouter() {
Router router = new Router(getContext());
final String loginPage = getConfig().getString("dynamicRoot", "") + "/login.html";
final LoginAuthenticator defaultGuard =
new LoginAuthenticator(getContext(), true, loginPage);
defaultGuard.setNext(FreeMarkerPageResource.class);
router.attachDefault(defaultGuard);
router.attach("/login.html", LoginPageResource.class);
final Router accountRouter = new Router(getContext());
accountRouter.attach("/assessment/question/{questionId}", SurveyPageResource.class);
accountRouter.attach("/assessment", SurveyPageResource.class);
final LoginAuthenticator accountGuard =
new LoginAuthenticator(getContext(), false, loginPage);
accountGuard.setNext(accountRouter);
router.attach("/account", accountGuard);
return router;
}
/**
* Stand-alone main for testing.
*/
public static void main(String[] args) {
// Start the HTTP Server
final Component component = new Component();
component.getServers().add(Protocol.HTTP, 8085);
component.getClients().add(Protocol.HTTP);
// Setup App
GrowFrontend app = new GrowFrontend();
// Load an optional config file from the first argument.
app.getConfig().setDomain("dev");
if (args.length == 1) {
app.getConfig().updateConfig(args[0]);
}
component.getDefaultHost().attach(app);
// Setup shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
component.stop();
} catch (Exception e) {
cLog.error("Exception during cleanup", e);
}
}
});
cLog.info("Starting server...");
try {
component.start();
} catch (Exception e) {
cLog.fatal("Could not start: " + e.getMessage(), e);
}
}
}
|