1 16 17 18 package org.apache.catalina.servlets; 19 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.catalina.CometProcessor; 30 31 32 35 public abstract class CometServlet 36 extends HttpServlet implements CometProcessor { 37 38 public void begin(HttpServletRequest request, HttpServletResponse response) 39 throws IOException , ServletException { 40 request.setAttribute("org.apache.tomcat.comet", Boolean.TRUE); 41 } 42 43 public void end(HttpServletRequest request, HttpServletResponse response) 44 throws IOException , ServletException { 45 request.removeAttribute("org.apache.tomcat.comet"); 46 } 47 48 public void error(HttpServletRequest request, HttpServletResponse response) 49 throws IOException , ServletException { 50 end(request, response); 51 } 52 53 public boolean read(HttpServletRequest request, HttpServletResponse response) 54 throws IOException , ServletException { 55 InputStream is = request.getInputStream(); 56 byte[] buf = new byte[512]; 57 do { 58 int n = is.read(buf); 59 if (n > 0) { 60 } else if (n < 0) { 62 return false; 63 } 64 } while (is.available() > 0); 65 return true; 66 } 67 68 protected void service(HttpServletRequest request, HttpServletResponse response) 69 throws IOException , ServletException { 70 71 if (request.getAttribute("org.apache.tomcat.comet.support") == Boolean.TRUE) { 72 begin(request, response); 73 } else { 74 begin(request, response); 76 boolean error = true; 77 try { 78 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 request, HttpServletResponse response, int timeout) 92 throws IOException , ServletException , UnsupportedOperationException { 93 if (request.getAttribute("org.apache.tomcat.comet.timeout.support") == Boolean.TRUE) { 94 request.setAttribute("org.apache.tomcat.comet.timeout",new Integer (timeout)); 95 } else { 96 throw new UnsupportedOperationException (); 97 } 98 } 99 100 101 } 102 | Popular Tags |