FileServerApp.java

/***************************************************************************
   Copyright 2012 Emily Estes

   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.
***************************************************************************/
package net.metanotion.web.examples;


import net.metanotion.io.JavaFileSystem;

import net.metanotion.simpletemplate.DefaultServer;
import net.metanotion.simpletemplate.ResourceFactory;
import net.metanotion.simpletemplate.StaticResources;

import net.metanotion.util.Message;
import net.metanotion.web.RequestObject;
import net.metanotion.web.SessionHandler;
import net.metanotion.web.concrete.DefaultDispatcher;
import net.metanotion.web.servlets.ServerUtil;

/** This is a web app server that serves up static files from a local folder to visitors. */
public final class FileServerApp implements SessionHandler {
	/** This is just a constant for the port the server should listen on for this app. */
	public static final int SERVER_PORT = 8080;

	/** This is the startup of the app server. While other examples have yet to use any command line arguments
	this example does. The first argument is assumed to be a local folder containing files to serve up.
		@param args The command line arguments for this application. Only the first one is used: The folder to serve
			HTTP requests from.
	*/
	public static void main(final String[] args) {
		try {
			/** We need to represent our folder as a file system for the static file server component. */
			final JavaFileSystem files = new JavaFileSystem(args[0]);

			/** The static file server component is implemented by an instance of StaticResources.
			StaticResources implements the ResourceFactory interface, we will see this interface again when
			looking at the templating system. */
			final ResourceFactory resources = new StaticResources(files);

			/** The "default server" is an implementation of the "Default" component that uses a ResourceFactory. */
			final DefaultServer fileServer = new DefaultServer(resources);

			ServerUtil.launchJettyServer(SERVER_PORT, new DefaultDispatcher(), new FileServerApp(fileServer));
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	/** This is the implementation of the Default component we're using. In this case it's wrapping our file server library. */
	private final DefaultServer resources;

	/** Create a new instance of this application.
		@param resources The component for serving up the files requested.
	*/
	public FileServerApp(DefaultServer resources) {
		this.resources = resources;
	}

	/** Since the only component our app implementations is Default, all the messagees will be for instances of the
	Default component. And we're not implementing sessions, so we can simply dispatch our messages to the DefaultServer.
		@param m The message generated by the dispatcher for this web application(provided to the web server by our code).
		@param ro The HTTP request object representing the details of this HTTP request.
		@return The HTTP response.
	*/
	@Override public Object dispatch(Message m, RequestObject ro) {
		return m.call(resources);
	}
}