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   * Frequently many handlers are combined together to handle different aspects of
39   * a request. A handler may:
40   * <ul>
41   * <li>handle the request and completely generate the response
42   * <li>partially handle the request, but defer response generation to another
43   * 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:
49   * <ul>
50   * <li>{@link HandlerWrapper} which will nest one handler inside another. In
51   * this example, the HelloHandler is nested inside a HandlerWrapper that sets
52   * the greeting as a request attribute.
53   * <li>{@link HandlerList} which will call a collection of handlers until the
54   * request is marked as handled. In this example, a list is used to combine the
55   * param handler (which only handles the request if there are parameters) and
56   * the wrapper handler. Frequently handler lists are terminated with the
57   * {@link DefaultHandler}, which will generate a suitable 404 response if the
58   * request has not been handled.
59   * <li>{@link HandlerCollection} which will call each handler regardless if the
60   * request has been handled or not. Typically this is used to always pass a
61   * request to the logging handler.
62   * </ul>
63   */
64  public class ManyHandlers
65  {
66      public static void main(String[] args) throws Exception
67      {
68          Server server = new Server(8080);
69  
70          // create the handlers
71          Handler param = new ParamHandler();
72          HandlerWrapper wrapper = new HandlerWrapper()
73          {
74              @Override
75              public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
76                      ServletException
77              {
78                  request.setAttribute("welcome","Hello");
79                  super.handle(target,baseRequest,request,response);
80              }
81          };
82          Handler hello = new HelloHandler();
83          Handler dft = new DefaultHandler();
84          RequestLogHandler log = new RequestLogHandler();
85  
86          // configure logs
87          log.setRequestLog(new NCSARequestLog(File.createTempFile("demo","log").getAbsolutePath()));
88  
89          // create the handler collections
90          HandlerCollection handlers = new HandlerCollection();
91          HandlerList list = new HandlerList();
92  
93          // link them all together
94          wrapper.setHandler(hello);
95          list.setHandlers(new Handler[]
96          { param, wrapper, dft });
97          handlers.setHandlers(new Handler[]
98          { list, log });
99  
100         server.setHandler(handlers);
101 
102         server.start();
103         server.join();
104     }
105 
106     public static class ParamHandler extends AbstractHandler
107     {
108         public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
109         {
110             Map params = request.getParameterMap();
111             if (params.size() > 0)
112             {
113                 response.setContentType("text/plain");
114                 response.getWriter().println(JSON.toString(params));
115                 ((Request)request).setHandled(true);
116             }
117         }
118     }
119 }