]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/IdleTimeoutHandler.java
Merge "upd: remove 'browser install'"
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / IdleTimeoutHandler.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.AsyncEvent;
24 import javax.servlet.AsyncListener;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.io.EndPoint;
30 import org.eclipse.jetty.server.HttpChannel;
31 import org.eclipse.jetty.server.HttpConnection;
32 import org.eclipse.jetty.server.Request;
33
34 /**
35  * Handler to adjust the idle timeout of requests while dispatched.
36  * Can be applied in jetty.xml with
37  * <pre>
38  *   &lt;Get id='handler' name='Handler'/>
39  *   &lt;Set name='Handler'>
40  *     &lt;New id='idleTimeoutHandler' class='org.eclipse.jetty.server.handler.IdleTimeoutHandler'>
41  *       &lt;Set name='Handler'>&lt;Ref id='handler'/>&lt;/Set>
42  *       &lt;Set name='IdleTimeoutMs'>5000&lt;/Set>
43  *     &lt;/New>
44  *   &lt;/Set>
45  * </pre>
46  */
47 public class IdleTimeoutHandler extends HandlerWrapper
48 {
49     private long _idleTimeoutMs = 1000;
50     private boolean _applyToAsync = false;
51     
52     public boolean isApplyToAsync()
53     {
54         return _applyToAsync;
55     }
56
57     /**
58      * Should the adjusted idle time be maintained for asynchronous requests
59      * @param applyToAsync true if alternate idle timeout is applied to asynchronous requests
60      */
61     public void setApplyToAsync(boolean applyToAsync)
62     {
63         _applyToAsync = applyToAsync;
64     }
65
66     public long getIdleTimeoutMs()
67     {
68         return _idleTimeoutMs;
69     }
70
71     /**
72      * @param idleTimeoutMs The idle timeout in MS to apply while dispatched or async
73      */
74     public void setIdleTimeoutMs(long idleTimeoutMs)
75     {
76         this._idleTimeoutMs = idleTimeoutMs;
77     }
78     
79    
80     @Override
81     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
82     {
83         final HttpChannel<?> channel = baseRequest.getHttpChannel();
84         final long idle_timeout=baseRequest.getHttpChannel().getIdleTimeout();
85         channel.setIdleTimeout(_idleTimeoutMs);
86         
87         try
88         {
89             super.handle(target,baseRequest,request,response);
90         }
91         finally
92         {
93                 if (_applyToAsync && request.isAsyncStarted())
94                 {
95                     request.getAsyncContext().addListener(new AsyncListener()
96                     {
97                         @Override
98                         public void onTimeout(AsyncEvent event) throws IOException
99                         {                            
100                         }
101                         
102                         @Override
103                         public void onStartAsync(AsyncEvent event) throws IOException
104                         {
105                         }
106                         
107                         @Override
108                         public void onError(AsyncEvent event) throws IOException
109                         {
110                             channel.setIdleTimeout(idle_timeout);
111                         }
112                         
113                         @Override
114                         public void onComplete(AsyncEvent event) throws IOException
115                         {
116                             channel.setIdleTimeout(idle_timeout);
117                         }
118                     });
119                 }
120                 else 
121                     channel.setIdleTimeout(idle_timeout);
122         }
123     }
124 }