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