]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/AsyncDelayHandler.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / AsyncDelayHandler.java
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.server.handler;
20
21 import java.io.IOException;
22
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;
28
29 import org.eclipse.jetty.server.Request;
30
31
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
37  * delayed dispatch.
38  * 
39  */
40 public class AsyncDelayHandler extends HandlerWrapper
41 {
42     public final static String AHW_ATTR = "o.e.j.s.h.AsyncHandlerWrapper";
43
44     /* ------------------------------------------------------------ */
45     @Override
46     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
47     {
48         if (!isStarted() || _handler==null)
49             return;
50         
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;
59         
60         // Is this request a restarted one?
61         boolean restart=false;
62         if (dtype!=null)
63         {
64             // fake the dispatch type to the original
65             baseRequest.setAttribute(AHW_ATTR,null);
66             baseRequest.setDispatcherType(dtype);
67             restart=true;
68             
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);
79         }
80         
81         // Should we handle this request now?
82         if (!startHandling(baseRequest,restart))
83         {
84             // No, so go async and remember dispatch type
85             AsyncContext context = baseRequest.startAsync();
86             baseRequest.setAttribute(AHW_ATTR,ctype);
87
88             delayHandling(baseRequest, context);
89             return;
90         }
91
92         // Handle the request
93         try
94         {
95             _handler.handle(target,baseRequest, request, response);
96         }
97         finally
98         {
99             if(restart)
100             {
101                 // reset the request
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);
108             }
109             
110             // signal the request is leaving the handler
111             endHandling(baseRequest);
112         }
113         
114     }
115     
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
121      */
122     protected boolean startHandling(Request request, boolean restart)
123     {
124         return true;
125     }
126
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. 
132      * 
133      * @param request The request to be delayed
134      * @param context The AsyncContext of the delayed request
135      */
136     protected void delayHandling(Request request,AsyncContext context)
137     {
138         context.dispatch();
139     }
140     
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
146      */
147     protected void endHandling(Request request)
148     {
149         
150     }
151     
152     
153 }