diff options
Diffstat (limited to 'src/com/p4square/grow/GrowProcessComponent.java')
-rw-r--r-- | src/com/p4square/grow/GrowProcessComponent.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/com/p4square/grow/GrowProcessComponent.java b/src/com/p4square/grow/GrowProcessComponent.java index 4196a5e..eb92840 100644 --- a/src/com/p4square/grow/GrowProcessComponent.java +++ b/src/com/p4square/grow/GrowProcessComponent.java @@ -4,9 +4,21 @@ 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; @@ -16,6 +28,10 @@ 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; /** @@ -37,6 +53,13 @@ public class GrowProcessComponent extends Component { // 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(), false, + ChallengeScheme.HTTP_BASIC, BACKEND_REALM, verifier); + auth.setNext(backend); + getDefaultHost().attach("/backend", auth); } @Override @@ -56,4 +79,68 @@ public class GrowProcessComponent extends Component { super.start(); } + + /** + * Stand-alone main for testing. + */ + public static void main(String[] args) throws Exception { + // Start the HTTP Server + final GrowProcessComponent component = new GrowProcessComponent(); + component.getServers().add(Protocol.HTTP, 8085); + component.getClients().add(Protocol.HTTP); + component.getClients().add(Protocol.HTTPS); + component.getClients().add(Protocol.FILE); + //component.getClients().add(new Client(null, Arrays.asList(Protocol.HTTPS), "org.restlet.ext.httpclient.HttpClientHelper")); + + // 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); + } + + // Load an optional config file from the first argument. + component.mConfig.setDomain("dev"); + if (args.length == 1) { + component.mConfig.updateConfig(args[0]); + } + + // 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); + } + } } |