2 // ========================================================================
3 // Copyright (c) 1995-2014 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.eclipse.jetty.server.Request;
30 /* ------------------------------------------------------------ */
33 * A ScopedHandler is a HandlerWrapper where the wrapped handlers
34 * each define a scope.
36 * <p>When {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
37 * is called on the first ScopedHandler in a chain of HandlerWrappers,
38 * the {@link #doScope(String, Request, HttpServletRequest, HttpServletResponse)} method is
39 * called on all contained ScopedHandlers, before the
40 * {@link #doHandle(String, Request, HttpServletRequest, HttpServletResponse)} method
41 * is called on all contained handlers.</p>
43 * <p>For example if Scoped handlers A, B & C were chained together, then
44 * the calling order would be:</p>
55 * <p>If non scoped handler X was in the chained A, B, X & C, then
56 * the calling order would be:</p>
69 * <p>A typical usage pattern is:</p>
71 * private static class MyHandler extends ScopedHandler
73 * public void doScope(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
78 * super.doScope(target,request,response);
86 * public void doHandle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
91 * super.doHandle(target,request,response);
95 * cleanupMyHandling();
101 public abstract class ScopedHandler extends HandlerWrapper
103 private static final ThreadLocal<ScopedHandler> __outerScope= new ThreadLocal<ScopedHandler>();
104 protected ScopedHandler _outerScope;
105 protected ScopedHandler _nextScope;
107 /* ------------------------------------------------------------ */
109 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
112 protected void doStart() throws Exception
116 _outerScope=__outerScope.get();
117 if (_outerScope==null)
118 __outerScope.set(this);
122 _nextScope= getChildHandlerByClass(ScopedHandler.class);
127 if (_outerScope==null)
128 __outerScope.set(null);
132 /* ------------------------------------------------------------ */
136 public final void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
140 if (_outerScope==null)
141 doScope(target,baseRequest,request, response);
143 doHandle(target,baseRequest,request, response);
147 /* ------------------------------------------------------------ */
151 public abstract void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
152 throws IOException, ServletException;
154 /* ------------------------------------------------------------ */
158 public final void nextScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
159 throws IOException, ServletException
161 // this method has been manually inlined in several locations, but
162 // is called protected by an if(never()), so your IDE can find those
163 // locations if this code is changed.
164 if (_nextScope!=null)
165 _nextScope.doScope(target,baseRequest,request, response);
166 else if (_outerScope!=null)
167 _outerScope.doHandle(target,baseRequest,request, response);
169 doHandle(target,baseRequest,request, response);
172 /* ------------------------------------------------------------ */
174 * Do the handler work within the scope.
176 public abstract void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
177 throws IOException, ServletException;
179 /* ------------------------------------------------------------ */
181 * Do the handler work within the scope.
183 public final void nextHandle(String target, final Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
185 // this method has been manually inlined in several locations, but
186 // is called protected by an if(never()), so your IDE can find those
187 // locations if this code is changed.
188 if (_nextScope!=null && _nextScope==_handler)
189 _nextScope.doHandle(target,baseRequest,request, response);
190 else if (_handler!=null)
191 _handler.handle(target,baseRequest, request, response);
194 /* ------------------------------------------------------------ */
195 protected boolean never()