From: Jim Procter Date: Thu, 25 Jun 2015 15:20:41 +0000 (+0100) Subject: JAL-1794 simple file server class - configured for 8080 in current working directory X-Git-Tag: Release_2_10_0~564^2 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=be5be9d97cdb7248784777fa17face7d345bdd33 JAL-1794 simple file server class - configured for 8080 in current working directory --- diff --git a/utils/JettyExamplesDir.java b/utils/JettyExamplesDir.java new file mode 100644 index 0000000..58f7694 --- /dev/null +++ b/utils/JettyExamplesDir.java @@ -0,0 +1,73 @@ +// +//======================================================================== +//Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. +//------------------------------------------------------------------------ +//All rights reserved. This program and the accompanying materials +//are made available under the terms of the Eclipse Public License v1.0 +//and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +//You may elect to redistribute this code under either of these licenses. +//======================================================================== +// + + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.ResourceHandler; + +public class JettyExamplesDir +{ + + /** + * Simple Jetty FileServer. This is a simple example of Jetty configured as a + * FileServer. + */ + + public static void main(String[] args) throws Exception + { + // Create a basic Jetty server object that will listen on port 8080. Note + // that if you set this to port 0 + // then a randomly available port will be assigned that you can either look + // in the logs for the port, + // or programmatically obtain it for use in test cases. + Server server = new Server(8080); + + // Create the ResourceHandler. It is the object that will actually handle + // the request for a given file. It is + // a Jetty Handler object so it is suitable for chaining with other handlers + // as you will see in other examples. + ResourceHandler resource_handler = new ResourceHandler(); + // Configure the ResourceHandler. Setting the resource base indicates where + // the files should be served out of. + // In this example it is the current directory but it can be configured to + // anything that the jvm has access to. + resource_handler.setDirectoriesListed(true); + resource_handler.setWelcomeFiles(new String[] + { "applets.html" }); + resource_handler.setResourceBase("."); + + // Add the ResourceHandler to the server. + // GzipHandler gzip = new GzipHandler(); + // server.setHandler(gzip); + HandlerList handlers = new HandlerList(); + handlers.setHandlers(new Handler[] + { resource_handler, new DefaultHandler() }); + server.setHandler(handlers); + + // Start things up! By using the server.join() the server thread will join + // with the current thread. + // See + // "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" + // for more details. + server.start(); + server.join(); +} +} \ No newline at end of file