From aa3b4ce2b1a1ddd5ea27d312e179a06c5b5fe2d1 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 30 Mar 2009 01:46:15 +0000 Subject: [PATCH] improved javadocs git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@41 7e9141cc-0065-0410-87d8-b60c137991c4 --- jetty-eclipse-codetemplates.xml | 50 +++++++++++- .../jetty/embedded/ManyConnectors.java | 8 +- .../eclipse/jetty/embedded/ManyContexts.java | 48 +++++++---- .../eclipse/jetty/embedded/ManyHandlers.java | 81 ++++++++++++++++--- .../eclipse/jetty/embedded/OneContext.java | 14 +++- .../server/handler/HandlerCollection.java | 9 +-- 6 files changed, 170 insertions(+), 40 deletions(-) diff --git a/jetty-eclipse-codetemplates.xml b/jetty-eclipse-codetemplates.xml index dcb3710c469..c846c3c47b3 100644 --- a/jetty-eclipse-codetemplates.xml +++ b/jetty-eclipse-codetemplates.xml @@ -1,4 +1,50 @@ - \ No newline at end of file diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java index 47ad16416d0..714a5f007c9 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java @@ -20,8 +20,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.bio.SocketConnector; @@ -29,6 +27,12 @@ import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.util.thread.QueuedThreadPool; + +/* ------------------------------------------------------------ */ +/** + * A Jetty server can have multiple connectors. + * + */ public class ManyConnectors { public static void main(String[] args) diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java index 5b456da32ba..cbb926425a7 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java @@ -31,6 +31,14 @@ import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.nio.SelectChannelConnector; + +/* ------------------------------------------------------------ */ +/** + * 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 static void main(String[] args) @@ -42,22 +50,25 @@ public class ManyContexts server.setConnectors(new Connector[]{connector}); ContextHandler context0 = new ContextHandler(); - context0.setContextPath("/zero"); - Handler handler0=new HelloHandler(); + context0.setContextPath("/"); + Handler handler0=new HelloHandler("Root Context"); context0.setHandler(handler0); ContextHandler context1 = new ContextHandler(); - context1.setContextPath("/one"); - Handler handler1=new HelloHandler(); + context1.setContextPath("/context"); + Handler handler1=new HelloHandler("A Context"); context1.setHandler(handler1); + + ContextHandler context2 = new ContextHandler(); + context2.setContextPath("/context"); + context2.setVirtualHosts(new String[]{"127.0.0.2"}); + Handler handler2=new HelloHandler("A Virtual Context"); + context2.setHandler(handler2); ContextHandlerCollection contexts = new ContextHandlerCollection(); - contexts.setHandlers(new Handler[]{context0,context1}); + contexts.setHandlers(new Handler[]{context0,context1,context2}); - HandlerCollection handlers = new HandlerCollection(); - handlers.setHandlers(new Handler[]{contexts,new DefaultHandler()}); - - server.setHandler(handlers); + server.setHandler(contexts); server.start(); server.join(); @@ -65,16 +76,23 @@ public class ManyContexts public static class HelloHandler extends AbstractHandler { - static int h=0; - int hello=h++; - + String _welcome; + + HelloHandler(String welcome) + { + _welcome=welcome; + } + public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + ((Request)request).setHandled(true); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("text/html"); - response.getWriter().println("

Hello OneContext "+hello+"

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

"+_welcome+" "+request.getContextPath()+"

"); + response.getWriter().println("root context
"); + response.getWriter().println("normal context
"); + response.getWriter().println("virtual context
"); + } } diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java index cfe5fc4fb58..16b9545021d 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java @@ -13,47 +13,105 @@ package org.eclipse.jetty.embedded; +import java.io.File; import java.io.IOException; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.HttpConnection; +import org.eclipse.jetty.server.NCSARequestLog; import org.eclipse.jetty.server.Request; 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.HandlerCollection; -import org.eclipse.jetty.server.nio.SelectChannelConnector; +import org.eclipse.jetty.server.handler.HandlerList; +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: + * + * Multiple handlers may be combined with: + */ public class ManyHandlers { public static void main(String[] args) throws Exception { - Server server = new Server(); - Connector connector=new SelectChannelConnector(); - connector.setPort(8080); - server.setConnectors(new Connector[]{connector}); + Server server = new Server(8080); + // create the handlers Handler param=new ParamHandler(); + HandlerWrapper wrapper = new HandlerWrapper() + { + public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + request.setAttribute("welcome","Hello"); + super.handle(target,request,response); + } + }; Handler hello=new HelloHandler(); + Handler dft=new DefaultHandler(); + RequestLogHandler log=new RequestLogHandler(); + // configure logs + log.setRequestLog(new NCSARequestLog(File.createTempFile("demo","log").getAbsolutePath())); + + // create the handler collections HandlerCollection handlers = new HandlerCollection(); - handlers.setHandlers(new Handler[]{param,hello}); + HandlerList list = new HandlerList(); + + // link them all together + wrapper.addHandler(hello); + list.setHandlers(new Handler[]{param,wrapper,dft}); + handlers.setHandlers(new Handler[]{list,log}); + server.setHandler(handlers); server.start(); server.join(); } + public static class ParamHandler extends AbstractHandler { public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - System.err.println(request.getParameterMap()); + Map params = request.getParameterMap(); + if (params.size()>0) + { + response.setContentType("text/plain"); + response.getWriter().println(JSON.toString(params)); + ((Request)request).setHandled(true); + } } } @@ -61,11 +119,10 @@ public class ManyHandlers { public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println("

Hello ManyHandler

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

"+request.getAttribute("welcome") +" ManyHandler

"); + ((Request)request).setHandled(true); } } } diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java index 9e750451540..468b51b706f 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java @@ -19,15 +19,21 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.server.nio.SelectChannelConnector; + +/* ------------------------------------------------------------ */ +/** + * A {@link ContextHandler} provides a common environment for + * multiple Handlers, such as: URI context path, class loader, + * static resource base. + * + * Typically a ContextHandler is used only when multiple contexts + * are likely. + */ public class OneContext { public static void main(String[] args) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java index 51fefd5fb61..3d6df494840 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/HandlerCollection.java @@ -27,12 +27,11 @@ import org.eclipse.jetty.util.MultiException; /* ------------------------------------------------------------ */ /** A collection of handlers. *

- * For derived HandlerCollections, the order or manner of calling - * the contained handlers is not defined. The default implementations - * calls all handlers in list order, regardless of - * the response status or exceptions. + * The default implementations calls all handlers in list order, + * regardless of the response status or exceptions. Derived implementation + * may alter the order or the conditions of calling the contained + * handlers. *

- * * * @org.apache.xbean.XBean */