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
|
/*
* Copyright 2013 Jesse Morgan
*/
package com.p4square.grow;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.restlet.Application;
import org.restlet.Client;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Protocol;
import org.restlet.resource.Directory;
import org.restlet.security.ChallengeAuthenticator;
import com.p4square.grow.backend.BackendVerifier;
import com.p4square.grow.backend.GrowBackend;
import com.p4square.grow.config.Config;
import com.p4square.grow.frontend.GrowFrontend;
/**
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
public class GrowProcessComponent extends Component {
private static Logger LOG = Logger.getLogger(GrowProcessComponent.class);
private static final String BACKEND_REALM = "Grow Backend";
private final Config mConfig;
/**
* Create a new Grow Process website component combining a frontend and backend.
*/
public GrowProcessComponent() throws Exception {
this(new Config());
}
public GrowProcessComponent(Config config) {
// Clients
getClients().add(Protocol.FILE);
getClients().add(Protocol.HTTP);
getClients().add(Protocol.HTTPS);
// Prepare mConfig
mConfig = config;
// Frontend
GrowFrontend frontend = new GrowFrontend(mConfig);
getDefaultHost().attach(frontend);
// Backend
GrowBackend backend = new GrowBackend(mConfig);
getInternalRouter().attach("/backend", backend);
// Authenticated access to the backend
BackendVerifier verifier = new BackendVerifier(backend.getUserRecordProvider());
ChallengeAuthenticator auth = new ChallengeAuthenticator(getContext().createChildContext(),
false, ChallengeScheme.HTTP_BASIC, BACKEND_REALM, verifier);
auth.setNext(backend);
getDefaultHost().attach("/backend", auth);
}
@Override
public void start() throws Exception {
// Load mConfigs
mConfig.updateConfig(this.getClass().getResourceAsStream("/grow.properties"));
String configDomain = getContext().getParameters().getFirstValue("com.p4square.grow.configDomain");
if (configDomain != null) {
mConfig.setDomain(configDomain);
}
String configFilename = getContext().getParameters().getFirstValue("com.p4square.grow.configFile");
if (configFilename != null) {
mConfig.updateConfig(configFilename);
}
super.start();
}
/**
* Stand-alone main for testing.
*/
public static void main(String[] args) throws Exception {
// Load an optional config file from the first argument.
Config config = new Config();
config.setDomain("dev");
if (args.length >= 1) {
config.updateConfig(args[0]);
}
// Override domain
if (args.length == 2) {
config.setDomain(args[1]);
}
// Start the HTTP Server
final GrowProcessComponent component = new GrowProcessComponent(config);
component.getServers().add(Protocol.HTTP, 8085);
// Static content
try {
component.getDefaultHost().attach("/images/", new FileServingApp("./build/root/images/"));
component.getDefaultHost().attach("/scripts", new FileServingApp("./build/root/scripts"));
component.getDefaultHost().attach("/style.css", new FileServingApp("./build/root/style.css"));
component.getDefaultHost().attach("/favicon.ico", new FileServingApp("./build/root/favicon.ico"));
component.getDefaultHost().attach("/notfound.html", new FileServingApp("./build/root/notfound.html"));
component.getDefaultHost().attach("/error.html", new FileServingApp("./build/root/error.html"));
} catch (IOException e) {
LOG.error("Could not create directory for static resources: "
+ e.getMessage(), e);
}
// Setup shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
component.stop();
} catch (Exception e) {
LOG.error("Exception during cleanup", e);
}
}
});
LOG.info("Starting server...");
try {
component.start();
} catch (Exception e) {
LOG.fatal("Could not start: " + e.getMessage(), e);
}
}
private static class FileServingApp extends Application {
private final String mPath;
public FileServingApp(String path) throws IOException {
mPath = new File(path).getAbsolutePath();
}
@Override
public Restlet createInboundRoot() {
return new Directory(getContext(), "file://" + mPath);
}
}
}
|