1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 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 * Frequently many handlers are combined together to handle different aspects of
43 * a request. A handler may:
44 * <ul>
45 * <li>handle the request and completely generate the response
46 * <li>partially handle the request, but defer response generation to another
47 * handler.
48 * <li>select another handler to pass the request to.
49 * <li>use business logic to decide to do one of the above.
50 * </ul>
51 * Multiple handlers may be combined with:
52 * <ul>
53 * <li>{@link HandlerWrapper} which will nest one handler inside another. In
54 * this example, the HelloHandler is nested inside a HandlerWrapper that sets
55 * the greeting as a request attribute.
56 * <li>{@link HandlerList} which will call a collection of handlers until the
57 * request is marked as handled. In this example, a list is used to combine the
58 * param handler (which only handles the request if there are parameters) and
59 * the wrapper handler. Frequently handler lists are terminated with the
60 * {@link DefaultHandler}, which will generate a suitable 404 response if the
61 * request has not been handled.
62 * <li>{@link HandlerCollection} which will call each handler regardless if the
63 * request has been handled or not. Typically this is used to always pass a
64 * request to the logging handler.
65 * </ul>
66 */
67 public class ManyHandlers
68 {
69 /**
70 * Produce output that lists all of the request parameters
71 */
72 public static class ParamHandler extends AbstractHandler
73 {
74 public void handle( String target,
75 Request baseRequest,
76 HttpServletRequest request,
77 HttpServletResponse response ) throws IOException,
78 ServletException
79 {
80 Map<String, String[]> params = request.getParameterMap();
81 if (params.size() > 0)
82 {
83 response.setContentType("text/plain");
84 response.getWriter().println(JSON.toString(params));
85 baseRequest.setHandled(true);
86 }
87 }
88 }
89
90 /**
91 * Add a request attribute, but produce no output.
92 */
93 public static class WelcomeWrapHandler extends HandlerWrapper
94 {
95 @Override
96 public void handle( String target,
97 Request baseRequest,
98 HttpServletRequest request,
99 HttpServletResponse response ) throws IOException,
100 ServletException
101 {
102 request.setAttribute("welcome", "Hello");
103 super.handle(target, baseRequest, request, response);
104 }
105 }
106
107 public static void main( String[] args ) throws Exception
108 {
109 Server server = new Server(8080);
110
111 // create the handlers
112 Handler param = new ParamHandler();
113 HandlerWrapper wrapper = new WelcomeWrapHandler();
114 Handler hello = new HelloHandler();
115 Handler dft = new DefaultHandler();
116 RequestLogHandler requestLog = new RequestLogHandler();
117
118 // configure request logging
119 File requestLogFile = File.createTempFile("demo", "log");
120 NCSARequestLog ncsaLog = new NCSARequestLog(
121 requestLogFile.getAbsolutePath());
122 requestLog.setRequestLog(ncsaLog);
123
124 // create the handler collections
125 HandlerCollection handlers = new HandlerCollection();
126 HandlerList list = new HandlerList();
127
128 // link them all together
129 wrapper.setHandler(hello);
130 list.setHandlers(new Handler[] { param, wrapper, dft });
131 handlers.setHandlers(new Handler[] { list, requestLog });
132
133 // Handler tree looks like the following
134 // <pre>
135 // Server
136 // + HandlerCollection
137 // . + HandlerList
138 // . | + param (ParamHandler)
139 // . | + wrapper (WelcomeWrapHandler)
140 // . | | \ hello (HelloHandler)
141 // . | \ dft (DefaultHandler)
142 // . \ requestLog (RequestLogHandler)
143 // </pre>
144
145 server.setHandler(handlers);
146
147 server.start();
148 server.join();
149 }
150 }