From 9234f3ec9051d74a10149732ed85729b89b0a103 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 22 Feb 2013 18:14:41 +1100 Subject: [PATCH] refined jetty embedded examples --- .../eclipse/jetty/embedded/ManyContexts.java | 49 +++++-------------- .../jetty/embedded/MinimalServlets.java | 4 +- .../jetty/embedded/OneServletContext.java | 6 +-- .../org/eclipse/jetty/embedded/OneWebApp.java | 37 ++++---------- 4 files changed, 27 insertions(+), 69 deletions(-) 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 5c396679d21..80176ef49ab 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 @@ -18,60 +18,37 @@ package org.eclipse.jetty.embedded; -import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ContextHandlerCollection; -/* ------------------------------------------------------------ */ -/** - * A {@link ContextHandlerCollection} handler may be used to direct a request to - * a specific Context. The URI path prefix and optional virtual host is used to - * select the context. - * - */ public class ManyContexts { - public final static String BODY= - "root context
"+ - "normal context
"+ - "virtual context
"; - public static void main(String[] args) throws Exception { - Server server = new Server(); - ServerConnector connector = new ServerConnector(server); - connector.setPort(8080); - server.setConnectors(new Connector[] - { connector }); + Server server = new Server(8080); - ContextHandler context0 = new ContextHandler(); - context0.setContextPath("/"); - Handler handler0 = new HelloHandler("Root Context",BODY); - context0.setHandler(handler0); + ContextHandler context = new ContextHandler("/"); + context.setContextPath("/"); + context.setHandler(new HelloHandler("Root Hello")); - ContextHandler context1 = new ContextHandler(); - context1.setContextPath("/context"); - Handler handler1 = new HelloHandler("A Context",BODY); - context1.setHandler(handler1); + ContextHandler contextFR = new ContextHandler("/fr"); + contextFR.setHandler(new HelloHandler("Bonjoir")); + + ContextHandler contextIT = new ContextHandler("/it"); + contextIT.setHandler(new HelloHandler("Bongiorno")); - ContextHandler context2 = new ContextHandler(); - context2.setContextPath("/context"); - context2.setVirtualHosts(new String[] - { "127.0.0.2" }); - Handler handler2 = new HelloHandler("A Virtual Context",BODY); - context2.setHandler(handler2); + ContextHandler contextV = new ContextHandler("/"); + contextV.setVirtualHosts(new String[]{ "127.0.0.2" }); + contextV.setHandler(new HelloHandler("Virtual Hello")); ContextHandlerCollection contexts = new ContextHandlerCollection(); - contexts.setHandlers(new Handler[] - { context0, context1, context2 }); + contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV }); server.setHandler(contexts); server.start(); - System.err.println(server.dump()); server.join(); } } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/MinimalServlets.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/MinimalServlets.java index bb2ff17b383..5d371e2b74f 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/MinimalServlets.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/MinimalServlets.java @@ -36,12 +36,12 @@ public class MinimalServlets ServletHandler handler = new ServletHandler(); server.setHandler(handler); - handler.addServletWithMapping("org.eclipse.jetty.embedded.MinimalServlets$HelloServlet","/"); + handler.addServletWithMapping(HelloServlet.class,"/*"); server.start(); server.join(); } - + public static class HelloServlet extends HttpServlet { @Override diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java index 8305e6bd202..3e91ec4c871 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java @@ -37,10 +37,8 @@ public class OneServletContext holder.setInitParameter("resourceBase","/tmp"); holder.setInitParameter("pathInfoOnly","true"); - // Serve some hello world servlets - context.addServlet(new ServletHolder(new HelloServlet()),"/*"); - context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*"); - context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*"); + // A Dump Servlet + context.addServlet(new ServletHolder(new DumpServlet()),"/*"); server.start(); server.join(); diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneWebApp.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneWebApp.java index 281ec11bab7..87a1a662a35 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneWebApp.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneWebApp.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.embedded; +import org.eclipse.jetty.security.HashLoginService; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; @@ -27,36 +28,18 @@ public class OneWebApp { public static void main(String[] args) throws Exception { - Server server = new Server(); - - ServerConnector connector = new ServerConnector(server); - connector.setPort(Integer.getInteger("jetty.port",8080).intValue()); - server.setConnectors(new Connector[] - { connector }); - - - //If you're running this from inside Eclipse, then Server.getVersion will not provide - //the correct number as there is no manifest. Use the command line instead to provide the path to the - //test webapp - String war = args.length > 0?args[0]: "../test-jetty-webapp/target/test-jetty-webapp-"+Server.getVersion(); - String path = args.length > 1?args[1]:"/"; - - System.err.println(war + " " + path); + Server server = new Server(8080); WebAppContext webapp = new WebAppContext(); - webapp.setContextPath(path); - webapp.setWar(war); - - //If the webapp contains security constraints, you will need to configure a LoginService - if (war.contains("test-jetty-webapp")) - { - org.eclipse.jetty.security.HashLoginService loginService = new org.eclipse.jetty.security.HashLoginService(); - loginService.setName("Test Realm"); - loginService.setConfig("src/test/resources/realm.properties"); - webapp.getSecurityHandler().setLoginService(loginService); - } - + webapp.setContextPath("/"); + webapp.setWar("../../tests/test-webapps/test-jetty-webapp/target/test-jetty-webapp-9.0.0-SNAPSHOT.war"); server.setHandler(webapp); + + // Configure a LoginService + HashLoginService loginService = new HashLoginService(); + loginService.setName("Test Realm"); + loginService.setConfig("src/test/resources/realm.properties"); + server.addBean(loginService); server.start(); server.join();