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.
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.AsyncContext;
24 import javax.servlet.DispatcherType;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.eclipse.jetty.server.Request;
32 /* ------------------------------------------------------------ */
33 /** A handler wrapper that provides the framework to asynchronously
34 * delay the handling of a request. While it uses standard servlet
35 * API for asynchronous servlets, it adjusts the dispatch type of the
36 * request so that it does not appear to be asynchronous during the
40 public class AsyncDelayHandler extends HandlerWrapper
42 public final static String AHW_ATTR = "o.e.j.s.h.AsyncHandlerWrapper";
44 /* ------------------------------------------------------------ */
46 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
48 if (!isStarted() || _handler==null)
51 // Get the dispatcher types
52 DispatcherType ctype = baseRequest.getDispatcherType();
53 DispatcherType dtype = (DispatcherType)baseRequest.getAttribute(AHW_ATTR);
54 Object async_context_path=null;
55 Object async_path_info=null;
56 Object async_query_string=null;
57 Object async_request_uri=null;
58 Object async_servlet_path=null;
60 // Is this request a restarted one?
61 boolean restart=false;
64 // fake the dispatch type to the original
65 baseRequest.setAttribute(AHW_ATTR,null);
66 baseRequest.setDispatcherType(dtype);
69 async_context_path=baseRequest.getAttribute(AsyncContext.ASYNC_CONTEXT_PATH);
70 baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,null);
71 async_path_info=baseRequest.getAttribute(AsyncContext.ASYNC_PATH_INFO);
72 baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,null);
73 async_query_string=baseRequest.getAttribute(AsyncContext.ASYNC_QUERY_STRING);
74 baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,null);
75 async_request_uri=baseRequest.getAttribute(AsyncContext.ASYNC_REQUEST_URI);
76 baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,null);
77 async_servlet_path=baseRequest.getAttribute(AsyncContext.ASYNC_SERVLET_PATH);
78 baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,null);
81 // Should we handle this request now?
82 if (!startHandling(baseRequest,restart))
84 // No, so go async and remember dispatch type
85 AsyncContext context = baseRequest.startAsync();
86 baseRequest.setAttribute(AHW_ATTR,ctype);
88 delayHandling(baseRequest, context);
95 _handler.handle(target,baseRequest, request, response);
102 baseRequest.setDispatcherType(ctype);
103 baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,async_context_path);
104 baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,async_path_info);
105 baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,async_query_string);
106 baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,async_request_uri);
107 baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,async_servlet_path);
110 // signal the request is leaving the handler
111 endHandling(baseRequest);
116 /* ------------------------------------------------------------ */
117 /** Called to indicate that a request has been presented for handling
118 * @param request The request to handle
119 * @param restart True if this request is being restarted after a delay
120 * @return True if the request should be handled now
122 protected boolean startHandling(Request request, boolean restart)
127 /* ------------------------------------------------------------ */
128 /** Called to indicate that a requests handling is being delayed/
129 * The implementation should arrange for context.dispatch() to be
130 * called when the request should be handled. It may also set
131 * timeouts on the context.
133 * @param request The request to be delayed
134 * @param context The AsyncContext of the delayed request
136 protected void delayHandling(Request request,AsyncContext context)
141 /* ------------------------------------------------------------ */
142 /** Called to indicated the handling of the request is ending.
143 * This is only the end of the current dispatch of the request and
144 * if the request is asynchronous, it may be handled again.
145 * @param request The request
147 protected void endHandling(Request request)