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 ListHandler} 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 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
75 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[]
95 { param, wrapper, dft });
96 handlers.setHandlers(new Handler[]
97 { list, log });
98
99 server.setHandler(handlers);
100
101 server.start();
102 server.join();
103 }
104
105 public static class ParamHandler extends AbstractHandler
106 {
107 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
108 {
109 Map params = request.getParameterMap();
110 if (params.size() > 0)
111 {
112 response.setContentType("text/plain");
113 response.getWriter().println(JSON.toString(params));
114 ((Request)request).setHandled(true);
115 }
116 }
117 }
118 }