Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Wilkins2013-11-08 04:09:05 +0000
committerGreg Wilkins2013-11-08 04:09:05 +0000
commitcbd6c11ab30ba63a1d6717d199c69dc9ea8fb276 (patch)
treec2ddd90d65d48eea652a1524494ab56b6e9ad4d4 /jetty-server
parentd9f818bee9f38aed5abd439b6b292376e45da922 (diff)
downloadorg.eclipse.jetty.project-cbd6c11ab30ba63a1d6717d199c69dc9ea8fb276.tar.gz
org.eclipse.jetty.project-cbd6c11ab30ba63a1d6717d199c69dc9ea8fb276.tar.xz
org.eclipse.jetty.project-cbd6c11ab30ba63a1d6717d199c69dc9ea8fb276.zip
added IdleTimeoutHandler
Diffstat (limited to 'jetty-server')
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/handler/IdleTimeoutHandler.java134
1 files changed, 134 insertions, 0 deletions
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/IdleTimeoutHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/IdleTimeoutHandler.java
new file mode 100644
index 0000000000..045b567c9f
--- /dev/null
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/IdleTimeoutHandler.java
@@ -0,0 +1,134 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2013 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.AsyncEvent;
+import javax.servlet.AsyncListener;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.io.EndPoint;
+import org.eclipse.jetty.server.HttpConnection;
+import org.eclipse.jetty.server.Request;
+
+/**
+ * Handler to adjust the idle timeout of requests while dispatched.
+ * Can be applied in jetty.xml with
+ * <pre>
+ * &lt;Get id='handler' name='Handler'/>
+ * &lt;Set name='Handler>
+ * &lt;New id='idleTimeoutHandler' class='org.eclipse.jetty.server.handler.IdleTimeoutHandler'>
+ * &lt;Set name='Handler'>&lt;Ref id='handler'/>&lt;/Set>
+ * &lt;Set name='IdleTimeoutMs'>5000&lt;/Set>
+ * &lt;/New>
+ * &lt;/Set>
+ * </pre>
+ */
+public class IdleTimeoutHandler extends HandlerWrapper
+{
+ private long _idleTimeoutMs = 1000;
+ private boolean _applyToAsync = false;
+
+ public boolean isApplyToAsync()
+ {
+ return _applyToAsync;
+ }
+
+ /**
+ * Should the adjusted idle time be maintained for asynchronous requests
+ * @param applyToAsync true if alternate idle timeout is applied to asynchronous requests
+ */
+ public void setApplyToAsync(boolean applyToAsync)
+ {
+ _applyToAsync = applyToAsync;
+ }
+
+ public long getIdleTimeoutMs()
+ {
+ return _idleTimeoutMs;
+ }
+
+ /**
+ * @param idleTimeoutMs The idle timeout in MS to apply while dispatched or async
+ */
+ public void setIdleTimeoutMs(long idleTimeoutMs)
+ {
+ this._idleTimeoutMs = idleTimeoutMs;
+ }
+
+
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+ {
+ HttpConnection connection = HttpConnection.getCurrentConnection();
+ final EndPoint endp = connection==null?null:connection.getEndPoint();
+
+ final long idle_timeout;
+ if (endp==null)
+ idle_timeout=-1;
+ else
+ {
+ idle_timeout=endp.getIdleTimeout();
+ endp.setIdleTimeout(_idleTimeoutMs);
+ }
+
+ try
+ {
+ super.handle(target,baseRequest,request,response);
+ }
+ finally
+ {
+ if (endp!=null)
+ {
+ if (_applyToAsync && request.isAsyncStarted())
+ {
+ request.getAsyncContext().addListener(new AsyncListener()
+ {
+ @Override
+ public void onTimeout(AsyncEvent event) throws IOException
+ {
+ }
+
+ @Override
+ public void onStartAsync(AsyncEvent event) throws IOException
+ {
+ }
+
+ @Override
+ public void onError(AsyncEvent event) throws IOException
+ {
+ endp.setIdleTimeout(idle_timeout);
+ }
+
+ @Override
+ public void onComplete(AsyncEvent event) throws IOException
+ {
+ endp.setIdleTimeout(idle_timeout);
+ }
+ });
+ }
+ else
+ endp.setIdleTimeout(idle_timeout);
+ }
+ }
+ }
+}

Back to the top