summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/frontend/GrowFrontend.java
blob: 74cd70454d7d6a559e5e6bab491dea55cf8a1283 (plain)
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
/*
 * Copyright 2013 Jesse Morgan <jesse@jesterpm.net>
 */

package com.p4square.grow.frontend;

import java.io.File;
import java.io.IOException;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.resource.Directory;
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);
        accountRouter.attach("/training/{chapter}", TrainingPageResource.class);
        accountRouter.attach("/training", TrainingPageResource.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);
        component.getClients().add(Protocol.FILE);
        
        // Static content
        try {
            component.getDefaultHost().attach("/images/", new FileServingApp("./build/images/"));
            component.getDefaultHost().attach("/scripts", new FileServingApp("./build/scripts"));
            component.getDefaultHost().attach("/style.css", new FileServingApp("./build/style.css"));
        } catch (IOException e) {
            cLog.error("Could not create directory for static resources: " 
                    + e.getMessage(), e);
        }

        // 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);
        }
    }
        
    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);
        }
    }
}