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