diff --git a/examples/embedded/src/main/java/HelloWorld.java b/examples/embedded/src/main/java/HelloWorld.java index a6c45680475..2061bf57f47 100644 --- a/examples/embedded/src/main/java/HelloWorld.java +++ b/examples/embedded/src/main/java/HelloWorld.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // import java.io.IOException; @@ -29,23 +29,30 @@ import org.eclipse.jetty.server.handler.AbstractHandler; public class HelloWorld extends AbstractHandler { @Override - public void handle(String target, - Request baseRequest, - HttpServletRequest request, - HttpServletResponse response) - throws IOException, ServletException + public void handle( String target, + Request baseRequest, + HttpServletRequest request, + HttpServletResponse response ) throws IOException, + ServletException { - response.setContentType("text/html;charset=utf-8"); + // Declare response encoding and types + response.setContentType("text/html; charset=utf-8"); + + // Declare response status code response.setStatus(HttpServletResponse.SC_OK); - baseRequest.setHandled(true); + + // Write back response response.getWriter().println("

Hello World

"); + + // Inform jetty that this request has now been handled + baseRequest.setHandled(true); } - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { Server server = new Server(8080); server.setHandler(new HelloWorld()); - + server.start(); server.join(); } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/DumpServlet.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/DumpServlet.java index 6f26857357b..aa528134e88 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/DumpServlet.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/DumpServlet.java @@ -1,24 +1,25 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; import java.io.IOException; +import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -28,26 +29,31 @@ import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class DumpServlet extends HttpServlet { - public DumpServlet() - { - } - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + protected void doGet( HttpServletRequest request, + HttpServletResponse response ) throws ServletException, + IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println("

DumpServlet

");
-        response.getWriter().println("requestURI=" + request.getRequestURI());
-        response.getWriter().println("contextPath=" + request.getContextPath());
-        response.getWriter().println("servletPath=" + request.getServletPath());
-        response.getWriter().println("pathInfo=" + request.getPathInfo());
-        response.getWriter().println("session=" + request.getSession(true).getId());
-        
-        String r=request.getParameter("resource");
-        if (r!=null)
-            response.getWriter().println("resource("+r+")=" + getServletContext().getResource(r));
-            
-        response.getWriter().println("
"); + + PrintWriter out = response.getWriter(); + + out.println("

DumpServlet

"); + out.println("
");
+        out.println("requestURI=" + request.getRequestURI());
+        out.println("contextPath=" + request.getContextPath());
+        out.println("servletPath=" + request.getServletPath());
+        out.println("pathInfo=" + request.getPathInfo());
+        out.println("session=" + request.getSession(true).getId());
+
+        String r = request.getParameter("resource");
+        if (r != null)
+        {
+            out.println("resource(" + r + ")="
+                    + getServletContext().getResource(r));
+        }
+
+        out.println("
"); } } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServer.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServer.java index 9b488db47b3..bbe60329a14 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServer.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServer.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; @@ -25,24 +25,23 @@ import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.util.thread.QueuedThreadPool; public class ExampleServer { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { Server server = new Server(); - ServerConnector connector=new ServerConnector(server); + ServerConnector connector = new ServerConnector(server); connector.setPort(8080); - server.setConnectors(new Connector[]{connector}); - + server.setConnectors(new Connector[] { connector }); + ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/hello"); - context.addServlet(HelloServlet.class,"/"); - + context.addServlet(HelloServlet.class, "/"); + HandlerCollection handlers = new HandlerCollection(); - handlers.setHandlers(new Handler[]{context,new DefaultHandler()}); + handlers.setHandlers(new Handler[] { context, new DefaultHandler() }); server.setHandler(handlers); server.start(); diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServerXml.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServerXml.java index 91fdaa51f54..fc89ed4b5ea 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServerXml.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ExampleServerXml.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; @@ -21,11 +21,20 @@ package org.eclipse.jetty.embedded; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.xml.XmlConfiguration; +/** + * Configures and Starts a Jetty server from an XML declaration. + *

+ * See exampleserver.xml + *

+ */ public class ExampleServerXml { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { - Resource fileserver_xml = Resource.newSystemResource("exampleserver.xml"); - XmlConfiguration.main(fileserver_xml.getFile().getAbsolutePath()); + // Find Jetty XML (in classpath) that configures and starts Server. + Resource serverXml = Resource.newSystemResource("exampleserver.xml"); + XmlConfiguration.main(serverXml.getFile().getAbsolutePath()); } } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/FastFileServer.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/FastFileServer.java index 5bbbaf872b6..c6f66d1b78f 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/FastFileServer.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/FastFileServer.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; @@ -39,31 +39,32 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerList; -import org.eclipse.jetty.server.handler.ResourceHandler; -import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.resource.Resource; -/* ------------------------------------------------------------ */ -/** Fast FileServer. - * - *

This example shows how to use the Jetty APIs for sending static - * as fast as possible using various strategies for small, medium and - * large content.

- *

The Jetty {@link DefaultServlet} does all this and more, and to - * a lesser extent so does the {@link ResourceHandler}, so unless you - * have exceptional circumstances it is best to use those classes for - * static content

+/** + * Fast FileServer. + *

+ * This example shows how to use the Jetty APIs for sending static as fast as + * possible using various strategies for small, medium and large content. + *

+ *

+ * The Jetty {@link DefaultServlet} does all this and more, and to a lesser + * extent so does the {@link ResourceHandler}, so unless you have exceptional + * circumstances it is best to use those classes for static content + *

*/ public class FastFileServer { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { Server server = new Server(8080); - + HandlerList handlers = new HandlerList(); - handlers.setHandlers(new Handler[] { new FastFileHandler(new File(".")), new DefaultHandler() }); + handlers.setHandlers(new Handler[] { + new FastFileHandler(new File(System.getProperty("user.dir"))), + new DefaultHandler() }); server.setHandler(handlers); server.start(); @@ -72,65 +73,74 @@ public class FastFileServer static class FastFileHandler extends AbstractHandler { - private final MimeTypes _mimeTypes = new MimeTypes(); - private final File _dir; - - FastFileHandler(File dir) + private final MimeTypes mimeTypes = new MimeTypes(); + private final File dir; + + private FastFileHandler( File dir ) { - _dir=dir; + this.dir = dir; } - + @Override - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle( String target, + Request baseRequest, + HttpServletRequest request, + HttpServletResponse response ) throws IOException, + ServletException { - // define small medium and large. - // This should be turned for your content, JVM and OS, but we will huge HTTP response buffer size as a measure - final int SMALL=response.getBufferSize(); - final int MEDIUM=8*SMALL; - - + // define small medium and large. + // This should be turned for your content, JVM and OS, but we will + // huge HTTP response buffer size as a measure + final int SMALL = response.getBufferSize(); + final int MEDIUM = 8 * SMALL; + // What file to serve? - final File file = new File(_dir,request.getPathInfo()); - + final File file = new File(this.dir, request.getPathInfo()); + // Only handle existing files if (!file.exists()) return; // we will handle this request baseRequest.setHandled(true); - + // Handle directories if (file.isDirectory()) { if (!request.getPathInfo().endsWith(URIUtil.SLASH)) { - response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH))); + response.sendRedirect(response.encodeRedirectURL(URIUtil + .addPaths(request.getRequestURI(), URIUtil.SLASH))); return; } - String listing = Resource.newResource(file).getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/") > 0); - response.setContentType("text/html; charset=UTF-8"); + String listing = Resource.newResource(file).getListHTML( + request.getRequestURI(), + request.getPathInfo().lastIndexOf("/") > 0); + response.setContentType("text/html; charset=utf-8"); response.getWriter().println(listing); return; } + + // Set some content headers. - // Set some content headers - // Jetty DefaultServlet will cache formatted date strings, but we will reformat for each request here - response.setDateHeader("Last-Modified",file.lastModified()); - response.setDateHeader("Content-Length",file.length()); - response.setContentType(_mimeTypes.getMimeByExtension(file.getName())); - - - + // Jetty DefaultServlet will cache formatted date strings, but we + // will reformat for each request here + response.setDateHeader("Last-Modified", file.lastModified()); + response.setDateHeader("Content-Length", file.length()); + response.setContentType(mimeTypes.getMimeByExtension(file.getName())); + // send "small" files blocking directly from an input stream - if (file.length() - * See fileserver.xml + * This server is identical to {@link FileServer}, except that it is configured + * via an {@link XmlConfiguration} config file that does the identical work. + *

+ *

+ * See fileserver.xml + *

*/ public class FileServerXml { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { - Resource fileserver_xml = Resource.newSystemResource("fileserver.xml"); - XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream()); - Server server = (Server)configuration.configure(); + Resource fileserverXml = Resource.newSystemResource("fileserver.xml"); + XmlConfiguration configuration = new XmlConfiguration( + fileserverXml.getInputStream()); + Server server = (Server) configuration.configure(); server.start(); server.join(); } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java index d98e3c36a85..a56bdf60d71 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java @@ -1,24 +1,25 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; import java.io.IOException; +import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -29,35 +30,42 @@ import org.eclipse.jetty.server.handler.AbstractHandler; public class HelloHandler extends AbstractHandler { - final String _greeting; - final String _body; + final String greeting; + final String body; public HelloHandler() { - _greeting="Hello World"; - _body=null; + this("Hello World"); } - public HelloHandler(String greeting) + public HelloHandler( String greeting ) { - _greeting=greeting; - _body=null; + this(greeting, null); } - public HelloHandler(String greeting,String body) + public HelloHandler( String greeting, String body ) { - _greeting=greeting; - _body=body; + this.greeting = greeting; + this.body = body; } - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle( String target, + Request baseRequest, + HttpServletRequest request, + HttpServletResponse response ) throws IOException, + ServletException { - response.setContentType("text/html;charset=utf-8"); + response.setContentType("text/html; charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); - baseRequest.setHandled(true); - response.getWriter().println("

"+_greeting+"

"); - if (_body!=null) - response.getWriter().println(_body); + PrintWriter out = response.getWriter(); + + out.println("

" + greeting + "

"); + if (body != null) + { + out.println(body); + } + + baseRequest.setHandled(true); } } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java index d0a595fc07c..4b95e7cadcf 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; @@ -28,22 +28,26 @@ import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { - String greeting = "Hello"; + final String greeting; public HelloServlet() { + this("Hello"); } - public HelloServlet(String hi) + public HelloServlet( String greeting ) { - greeting = hi; + this.greeting = greeting; } @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + protected void doGet( HttpServletRequest request, + HttpServletResponse response ) throws ServletException, + IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println("

" + greeting + " from HelloServlet

"); + response.getWriter().println( + "

" + greeting + " from HelloServlet

"); } } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java index eb4d6223ba0..d9e85c591a5 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java @@ -18,6 +18,8 @@ package org.eclipse.jetty.embedded; +import java.io.File; +import java.io.FileNotFoundException; import java.lang.management.ManagementFactory; import org.eclipse.jetty.deploy.DeploymentManager; @@ -34,7 +36,6 @@ import org.eclipse.jetty.server.SecureRequestCustomizer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.SslConnectionFactory; -import org.eclipse.jetty.server.handler.AsyncDelayHandler; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; @@ -45,14 +46,36 @@ import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; import org.eclipse.jetty.webapp.Configuration; +/** + * Starts the Jetty Distribution's demo-base directory using entirely + * embedded jetty techniques. + */ public class LikeJettyXml { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { - String jetty_home = System.getProperty("jetty.home","../../jetty-distribution/target/distribution"); - String jetty_base = System.getProperty("jetty.home","../../jetty-distribution/target/distribution/demo-base"); - System.setProperty("jetty.home",jetty_home); - System.setProperty("jetty.base",jetty_base); + // Path to as-built jetty-distribution directory + String jettyHomeBuild = "../../jetty-distribution/target/distribution"; + + // Find jetty home and base directories + String homePath = System.getProperty("jetty.home", jettyHomeBuild); + File homeDir = new File(homePath); + if (!homeDir.exists()) + { + throw new FileNotFoundException(homeDir.getAbsolutePath()); + } + String basePath = System.getProperty("jetty.base", homeDir + "/demo-base"); + File baseDir = new File(basePath); + if(!baseDir.exists()) + { + throw new FileNotFoundException(baseDir.getAbsolutePath()); + } + + // Configure jetty.home and jetty.base system properties + String jetty_home = homeDir.getAbsolutePath(); + String jetty_base = baseDir.getAbsolutePath(); + System.setProperty("jetty.home", jetty_home); + System.setProperty("jetty.base", jetty_base); // === jetty.xml === @@ -88,14 +111,15 @@ public class LikeJettyXml server.setDumpBeforeStop(false); server.setStopAtShutdown(true); - // === jetty-jmx.xml === - MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); + MBeanContainer mbContainer = new MBeanContainer( + ManagementFactory.getPlatformMBeanServer()); server.addBean(mbContainer); // === jetty-http.xml === - ServerConnector http = new ServerConnector(server,new HttpConnectionFactory(http_config)); + ServerConnector http = new ServerConnector(server, + new HttpConnectionFactory(http_config)); http.setPort(8080); http.setIdleTimeout(30000); server.addConnector(http); @@ -109,10 +133,8 @@ public class LikeJettyXml sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g"); sslContextFactory.setTrustStorePath(jetty_home + "/etc/keystore"); sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); - sslContextFactory.setExcludeCipherSuites( - "SSL_RSA_WITH_DES_CBC_SHA", - "SSL_DHE_RSA_WITH_DES_CBC_SHA", - "SSL_DHE_DSS_WITH_DES_CBC_SHA", + sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", + "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", @@ -133,7 +155,9 @@ public class LikeJettyXml // === jetty-deploy.xml === DeploymentManager deployer = new DeploymentManager(); deployer.setContexts(contexts); - deployer.setContextAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/servlet-api-[^/]*\\.jar$"); + deployer.setContextAttribute( + "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", + ".*/servlet-api-[^/]*\\.jar$"); WebAppProvider webapp_provider = new WebAppProvider(); webapp_provider.setMonitoredDirName(jetty_base + "/webapps"); @@ -146,9 +170,10 @@ public class LikeJettyXml server.addBean(deployer); // === setup jetty plus == - Configuration.ClassList.setServerDefault(server) - .addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", - "org.eclipse.jetty.plus.webapp.EnvConfiguration","org.eclipse.jetty.plus.webapp.PlusConfiguration"); + Configuration.ClassList.setServerDefault(server).addAfter( + "org.eclipse.jetty.webapp.FragmentConfiguration", + "org.eclipse.jetty.plus.webapp.EnvConfiguration", + "org.eclipse.jetty.plus.webapp.PlusConfiguration"); // === jetty-stats.xml === StatisticsHandler stats = new StatisticsHandler(); @@ -188,6 +213,7 @@ public class LikeJettyXml login.setRefreshInterval(0); server.addBean(login); + // Start the server server.start(); server.join(); diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java index 78e595909b5..96fb2fd7069 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java @@ -1,23 +1,26 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; +import java.io.File; +import java.io.FileNotFoundException; + import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -27,70 +30,89 @@ import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.util.ssl.SslContextFactory; -/* ------------------------------------------------------------ */ /** * A Jetty server with multiple connectors. */ public class ManyConnectors { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { - // Since this example shows off SSL configuration, we need a keystore with the appropriate key. These two - // lines are purely a hack to get access to a keystore that we use in many unit tests and should probably be - // a direct path to your own keystore (used on line 29). - String jetty_home = System.getProperty("jetty.home","../../jetty-distribution/target/distribution"); - System.setProperty("jetty.home", jetty_home); + // Since this example shows off SSL configuration, we need a keystore + // with the appropriate key. These lookup of jetty.home is purely a hack + // to get access to a keystore that we use in many unit tests and should + // probably be a direct path to your own keystore. - // Create a basic jetty server object without declaring the port. Since we are configuring connectors - // directly we'll be setting ports on those connectors. + String jettyDistKeystore = "../../jetty-distribution/target/distribution/etc/keystore"; + String keystorePath = System.getProperty( + "example.keystore", jettyDistKeystore); + File keystoreFile = new File(keystorePath); + if (!keystoreFile.exists()) + { + throw new FileNotFoundException(keystoreFile.getAbsolutePath()); + } + + // Create a basic jetty server object without declaring the port. Since + // we are configuring connectors directly we'll be setting ports on + // those connectors. Server server = new Server(); // HTTP Configuration - // HttpConfiguration is a collection of configuration information appropriate for http and https. The default - // scheme for http is http of course, as the default for secured http is https but - // we show setting the scheme to show it can be done. The port for secured communication is also set here. + // HttpConfiguration is a collection of configuration information + // appropriate for http and https. The default scheme for http is + // http of course, as the default for secured http is + // https but we show setting the scheme to show it can be + // done. The port for secured communication is also set here. HttpConfiguration http_config = new HttpConfiguration(); http_config.setSecureScheme("https"); http_config.setSecurePort(8443); http_config.setOutputBufferSize(32768); // HTTP connector - // The first server connector we create is the one for http, passing in the http configuration we configured - // above so it can get things like the output buffer size, etc. We also set the port (8080) and configure an - // idle timeout. - ServerConnector http = new ServerConnector(server,new HttpConnectionFactory(http_config)); + // The first server connector we create is the one for http, passing in + // the http configuration we configured above so it can get things like + // the output buffer size, etc. We also set the port (8080) and + // configure an idle timeout. + ServerConnector http = new ServerConnector(server, + new HttpConnectionFactory(http_config)); http.setPort(8080); http.setIdleTimeout(30000); - + // SSL Context Factory for HTTPS and SPDY - // SSL requires a certificate so we configure a factory for ssl contents with information pointing to what - // keystore the ssl connection needs to know about. Much more configuration is available the ssl context, - // including things like choosing the particular certificate out of a keystore to be used. + // SSL requires a certificate so we configure a factory for ssl contents + // with information pointing to what keystore the ssl connection needs + // to know about. Much more configuration is available the ssl context, + // including things like choosing the particular certificate out of a + // keystore to be used. SslContextFactory sslContextFactory = new SslContextFactory(); - sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore"); + sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath()); sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g"); // HTTPS Configuration - // A new HttpConfiguration object is needed for the next connector and you can pass the old one as an - // argument to effectively clone the contents. On this HttpConfiguration object we add a - // SecureRequestCustomizer which is how a new connector is able to resolve the https connection before - // handing control over to the Jetty Server. + // A new HttpConfiguration object is needed for the next connector and + // you can pass the old one as an argument to effectively clone the + // contents. On this HttpConfiguration object we add a + // SecureRequestCustomizer which is how a new connector is able to + // resolve the https connection before handing control over to the Jetty + // Server. HttpConfiguration https_config = new HttpConfiguration(http_config); https_config.addCustomizer(new SecureRequestCustomizer()); // HTTPS connector - // We create a second ServerConnector, passing in the http configuration we just made along with the - // previously created ssl context factory. Next we set the port and a longer idle timeout. + // We create a second ServerConnector, passing in the http configuration + // we just made along with the previously created ssl context factory. + // Next we set the port and a longer idle timeout. ServerConnector https = new ServerConnector(server, - new SslConnectionFactory(sslContextFactory,"http/1.1"), - new HttpConnectionFactory(https_config)); + new SslConnectionFactory(sslContextFactory, "http/1.1"), + new HttpConnectionFactory(https_config)); https.setPort(8443); https.setIdleTimeout(500000); - // Here you see the server having multiple connectors registered with it, now requests can flow into the server - // from both http and https urls to their respective ports and be processed accordingly by jetty. A simple - // handler is also registered with the server so the example has something to pass requests off to. + // Here you see the server having multiple connectors registered with + // it, now requests can flow into the server from both http and https + // urls to their respective ports and be processed accordingly by jetty. + // A simple handler is also registered with the server so the example + // has something to pass requests off to. // Set the connectors server.setConnectors(new Connector[] { http, https }); diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java index 5fb6da9fd74..efb6a2527a2 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; @@ -25,7 +25,7 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection; public class ManyContexts { - public static void main(String[] args) throws Exception + public static void main( String[] args ) throws Exception { Server server = new Server(8080); @@ -35,16 +35,17 @@ public class ManyContexts ContextHandler contextFR = new ContextHandler("/fr"); contextFR.setHandler(new HelloHandler("Bonjoir")); - + ContextHandler contextIT = new ContextHandler("/it"); contextIT.setHandler(new HelloHandler("Bongiorno")); ContextHandler contextV = new ContextHandler("/"); - contextV.setVirtualHosts(new String[]{ "127.0.0.2" }); + contextV.setVirtualHosts(new String[] { "127.0.0.2" }); contextV.setHandler(new HelloHandler("Virtual Hello")); ContextHandlerCollection contexts = new ContextHandlerCollection(); - contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV }); + contexts.setHandlers(new Handler[] { context, contextFR, contextIT, + contextV }); server.setHandler(contexts); diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java index 32f199fa4ae..7e6d2edee6f 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java @@ -1,19 +1,19 @@ // -// ======================================================================== -// Copyright (c) 1995-2014 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. +// ======================================================================== +// Copyright (c) 1995-2014 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 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 +// 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. -// ======================================================================== +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== // package org.eclipse.jetty.embedded; @@ -38,7 +38,6 @@ import org.eclipse.jetty.server.handler.HandlerWrapper; import org.eclipse.jetty.server.handler.RequestLogHandler; import org.eclipse.jetty.util.ajax.JSON; -/* ------------------------------------------------------------ */ /** * Frequently many handlers are combined together to handle different aspects of * a request. A handler may: @@ -49,7 +48,6 @@ import org.eclipse.jetty.util.ajax.JSON; *
  • select another handler to pass the request to. *
  • use business logic to decide to do one of the above. * - * * Multiple handlers may be combined with: *