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