Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9fd87d353e893e9c9512e728b057beee054729c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server.handler;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;


/* ------------------------------------------------------------ */
/** A handler wrapper that provides the framework to asynchronously
 * delay the handling of a request.  While it uses standard servlet 
 * API for asynchronous servlets, it adjusts the dispatch type of the 
 * request so that it does not appear to be asynchronous during the
 * delayed dispatch.
 * 
 */
public class AsyncDelayHandler extends HandlerWrapper
{
    public final static String AHW_ATTR = "o.e.j.s.h.AsyncHandlerWrapper";

    /* ------------------------------------------------------------ */
    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        if (!isStarted() || _handler==null)
            return;
        
        // Get the dispatcher types
        DispatcherType ctype = baseRequest.getDispatcherType();
        DispatcherType dtype = (DispatcherType)baseRequest.getAttribute(AHW_ATTR);
        Object async_context_path=null;
        Object async_path_info=null;
        Object async_query_string=null;
        Object async_request_uri=null;
        Object async_servlet_path=null;
        
        // Is this request a restarted one?
        boolean restart=false;
        if (dtype!=null)
        {
            // fake the dispatch type to the original
            baseRequest.setAttribute(AHW_ATTR,null);
            baseRequest.setDispatcherType(dtype);
            restart=true;
            
            async_context_path=baseRequest.getAttribute(AsyncContext.ASYNC_CONTEXT_PATH);
            baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,null);
            async_path_info=baseRequest.getAttribute(AsyncContext.ASYNC_PATH_INFO);
            baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,null);
            async_query_string=baseRequest.getAttribute(AsyncContext.ASYNC_QUERY_STRING);
            baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,null);
            async_request_uri=baseRequest.getAttribute(AsyncContext.ASYNC_REQUEST_URI);
            baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,null);
            async_servlet_path=baseRequest.getAttribute(AsyncContext.ASYNC_SERVLET_PATH);
            baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,null);
        }
        
        // Should we handle this request now?
        if (!startHandling(baseRequest,restart))
        {
            // No, so go async and remember dispatch type
            AsyncContext context = baseRequest.startAsync();
            baseRequest.setAttribute(AHW_ATTR,ctype);

            delayHandling(baseRequest, context);
            return;
        }

        // Handle the request
        try
        {
            _handler.handle(target,baseRequest, request, response);
        }
        finally
        {
            if(restart)
            {
                // reset the request
                baseRequest.setDispatcherType(ctype);
                baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,async_context_path);
                baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO,async_path_info);
                baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,async_query_string);
                baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,async_request_uri);
                baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH,async_servlet_path);
            }
            
            // signal the request is leaving the handler
            endHandling(baseRequest);
        }
        
    }
    
    /* ------------------------------------------------------------ */
    /** Called to indicate that a request has been presented for handling
     * @param request The request to handle
     * @param restart True if this request is being restarted after a delay
     * @return True if the request should be handled now
     */
    protected boolean startHandling(Request request, boolean restart)
    {
        return true;
    }

    /* ------------------------------------------------------------ */
    /** Called to indicate that a requests handling is being delayed/
     * The implementation should arrange for context.dispatch() to be
     * called when the request should be handled.  It may also set
     * timeouts on the context. 
     * 
     * @param request The request to be delayed
     * @param context The AsyncContext of the delayed request
     */
    protected void delayHandling(Request request,AsyncContext context)
    {
        context.dispatch();
    }
    
    /* ------------------------------------------------------------ */
    /** Called to indicated the handling of the request is ending.
     * This is only the end of the current dispatch of the request and
     * if the request is asynchronous, it may be handled again.
     * @param request The request
     */
    protected void endHandling(Request request)
    {
        
    }
    
    
}

Back to the top