View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.embedded;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.util.Map;
19  
20  import javax.servlet.ServletException;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.eclipse.jetty.server.Handler;
25  import org.eclipse.jetty.server.NCSARequestLog;
26  import org.eclipse.jetty.server.Request;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.server.handler.AbstractHandler;
29  import org.eclipse.jetty.server.handler.DefaultHandler;
30  import org.eclipse.jetty.server.handler.HandlerCollection;
31  import org.eclipse.jetty.server.handler.HandlerList;
32  import org.eclipse.jetty.server.handler.HandlerWrapper;
33  import org.eclipse.jetty.server.handler.RequestLogHandler;
34  import org.eclipse.jetty.util.ajax.JSON;
35  
36  
37  /* ------------------------------------------------------------ */
38  /**
39   * Frequently many handlers are combined together to handle
40   * different aspects of a request.  A handler may:<ul>
41   * <li>handle the request and completely generate the response
42   * <li>partially handle the request, but defer response generation
43   * to another handler.
44   * <li>select another handler to pass the request to.
45   * <li>use business logic to decide to do one of the above.
46   * </ul>
47   * 
48   * Multiple handlers may be combined with:<ul>
49   * <li>{@link HandlerWrapper} which will nest one handler inside another. 
50   * In this example, the HelloHandler is nested inside a HandlerWrapper
51   * that sets the greeting as a request attribute.
52   * <li>{@link ListHandler} which will call a collection of handlers
53   * until the request is marked as handled.  In this example, a list
54   * is used to combine the param handler (which only handles the 
55   * request if there are parameters) and the wrapper handler. 
56   * Frequently handler lists are terminated with the {@link DefaultHandler},
57   * which will generate a suitable 404 response if the request has
58   * not been handled.
59   * <li>{@link HandlerCollection} which will call each handler
60   * regardless if the request has been handled or not. Typically
61   * this is used to always pass a request to the logging handler.
62   * </ul>
63   */
64  public class ManyHandlers
65  {
66      public static void main(String[] args)
67          throws Exception
68      {
69          Server server = new Server(8080);
70          
71          // create the handlers
72          Handler param=new ParamHandler();
73          HandlerWrapper wrapper = new HandlerWrapper()
74          {
75              public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
76              {
77                  request.setAttribute("welcome","Hello");
78                  super.handle(target,baseRequest,request, response);
79              }
80          };
81          Handler hello=new HelloHandler();
82          Handler dft=new DefaultHandler();
83          RequestLogHandler log=new RequestLogHandler();
84          
85          // configure logs
86          log.setRequestLog(new NCSARequestLog(File.createTempFile("demo","log").getAbsolutePath()));
87          
88          // create the handler collections
89          HandlerCollection handlers = new HandlerCollection();
90          HandlerList list = new HandlerList();
91          
92          // link them all together
93          wrapper.setHandler(hello);
94          list.setHandlers(new Handler[]{param,wrapper,dft});
95          handlers.setHandlers(new Handler[]{list,log});
96          
97          server.setHandler(handlers);
98          
99          server.start();
100         server.join();
101     }
102 
103     
104     public static class ParamHandler extends AbstractHandler
105     {
106         public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
107         {
108             Map params = request.getParameterMap();
109             if (params.size()>0)
110             {
111                 response.setContentType("text/plain");
112                 response.getWriter().println(JSON.toString(params));
113                 ((Request)request).setHandled(true);
114             }
115         }
116     }
117     
118     public static class HelloHandler extends AbstractHandler
119     {
120         public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
121         {
122             response.setContentType("text/html");
123             response.setStatus(HttpServletResponse.SC_OK);
124             response.getWriter().println("<h1>"+request.getAttribute("welcome") +" ManyHandler</h1>");
125             ((Request)request).setHandled(true);
126         }
127     }
128 }