KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > servlets > CometServlet


1 /*
2  * Copyright 2006 The Apache Software Foundation.
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  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.catalina.servlets;
19
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.catalina.CometProcessor;
30
31
32 /**
33  * Helper class to implement Comet functionality.
34  */

35 public abstract class CometServlet
36     extends HttpServlet JavaDoc implements CometProcessor {
37
38     public void begin(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
39         throws IOException JavaDoc, ServletException JavaDoc {
40         request.setAttribute("org.apache.tomcat.comet", Boolean.TRUE);
41     }
42     
43     public void end(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
44         throws IOException JavaDoc, ServletException JavaDoc {
45         request.removeAttribute("org.apache.tomcat.comet");
46     }
47     
48     public void error(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
49         throws IOException JavaDoc, ServletException JavaDoc {
50         end(request, response);
51     }
52     
53     public boolean read(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
54         throws IOException JavaDoc, ServletException JavaDoc {
55         InputStream JavaDoc is = request.getInputStream();
56         byte[] buf = new byte[512];
57         do {
58             int n = is.read(buf);
59             if (n > 0) {
60                 // Do something with the data
61
} else if (n < 0) {
62                 return false;
63             }
64         } while (is.available() > 0);
65         return true;
66     }
67
68     protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69         throws IOException JavaDoc, ServletException JavaDoc {
70         
71         if (request.getAttribute("org.apache.tomcat.comet.support") == Boolean.TRUE) {
72             begin(request, response);
73         } else {
74             // No Comet support: regular servlet handling
75
begin(request, response);
76             boolean error = true;
77             try {
78                 // Loop reading data
79
while (read(request, response));
80                 error = false;
81             } finally {
82                 if (error) {
83                     error(request, response);
84                 } else {
85                     end(request, response);
86                 }
87             }
88         }
89     }
90     
91     public void setTimeout(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, int timeout)
92         throws IOException JavaDoc, ServletException JavaDoc, UnsupportedOperationException JavaDoc {
93         if (request.getAttribute("org.apache.tomcat.comet.timeout.support") == Boolean.TRUE) {
94             request.setAttribute("org.apache.tomcat.comet.timeout",new Integer JavaDoc(timeout));
95         } else {
96             throw new UnsupportedOperationException JavaDoc();
97         }
98     }
99
100
101 }
102
Popular Tags