View Javadoc

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