1
//========================================================================
2
//Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3
//------------------------------------------------------------------------
4
//Licensed under the Apache License, Version 2.0 (the "License");
5
//you may not use this file except in compliance with the License.
6
//You may obtain a copy of the License at
7
//http://www.apache.org/licenses/LICENSE-2.0
8
//Unless required by applicable law or agreed to in writing, software
9
//distributed under the License is distributed on an "AS IS" BASIS,
10
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
//See the License for the specific language governing permissions and
12
//limitations under the License.
13
//========================================================================
17
import java.io.IOException;
19
import javax.servlet.ServletException;
20
import javax.servlet.http.HttpServlet;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
24
import org.mortbay.util.ajax.Continuation;
25
import org.mortbay.util.ajax.ContinuationSupport;
28
* This servlet implements the Comet API from tc6.x with the exception of the read method.
33
public class CometServlet extends HttpServlet
35
public void begin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
37
request.setAttribute("org.apache.tomcat.comet",Boolean.TRUE);
40
public void end(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
44
request.removeAttribute("org.apache.tomcat.comet");
46
Continuation continuation=ContinuationSupport.getContinuation(request,request);
47
if (continuation.isPending())
48
continuation.resume();
52
public void error(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
54
end(request,response);
57
public boolean read(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
59
throw new UnsupportedOperationException();
62
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
66
// TODO: wrap response so we can reset timeout on writes.
68
Continuation continuation=ContinuationSupport.getContinuation(request,request);
70
if (!continuation.isPending())
71
begin(request,response);
73
Integer timeout=(Integer)request.getAttribute("org.apache.tomcat.comet.timeout");
74
boolean resumed=continuation.suspend(timeout==null?60000:timeout.intValue());
77
error(request,response);
81
public void setTimeout(HttpServletRequest request, HttpServletResponse response, int timeout) throws IOException, ServletException,
82
UnsupportedOperationException
84
request.setAttribute("org.apache.tomcat.comet.timeout",new Integer(timeout));