1 package org.directwebremoting.dwrp; 2 3 import java.io.IOException ; 4 import java.io.PrintWriter ; 5 6 import javax.servlet.http.HttpServletResponse ; 7 8 import org.directwebremoting.extend.ConverterManager; 9 import org.directwebremoting.extend.EnginePrivate; 10 import org.directwebremoting.extend.ScriptConduit; 11 import org.directwebremoting.util.DebuggingPrintWriter; 12 import org.directwebremoting.util.Logger; 13 14 21 public abstract class BaseScriptConduit extends ScriptConduit 22 { 23 30 public BaseScriptConduit(HttpServletResponse response, String batchId, ConverterManager converterManager) throws IOException 31 { 32 super(RANK_FAST); 33 34 this.response = response; 35 this.batchId = batchId; 36 this.converterManager = converterManager; 37 38 response.setContentType(getOutboundMimeType()); 39 40 if (log.isDebugEnabled()) 41 { 42 out = new DebuggingPrintWriter("", response.getWriter()); 46 } 47 else 48 { 49 out = response.getWriter(); 50 } 51 52 if (out instanceof DebuggingPrintWriter) 54 { 55 DebuggingPrintWriter dpw = (DebuggingPrintWriter) out; 56 dpw.setPrefix("out(" + hashCode() + "): "); 57 } 58 59 beginStream(); 60 } 61 62 66 protected abstract String getOutboundMimeType(); 67 68 74 protected abstract void beginStream(); 75 76 81 protected abstract void endStream(); 82 83 88 public void close(int timetoNextPoll) throws IOException 89 { 90 try 91 { 92 EnginePrivate.remoteHandleCallback(this, batchId, "0", new Integer (timetoNextPoll)); 93 } 94 catch (Exception ex) 95 { 96 EnginePrivate.remoteHandleException(this, batchId, "0", ex); 97 log.warn("--Erroring: batchId[" + batchId + "] message[" + ex.toString() + ']', ex); 98 } 99 100 endStream(); 101 } 102 103 107 protected boolean flush() 108 { 109 out.flush(); 110 111 if (out.checkError()) 117 { 118 log.debug("Error writing to stream"); 119 return false; 121 } 122 123 try 124 { 125 response.flushBuffer(); 126 return true; 127 } 128 catch (IOException ex) 129 { 130 log.debug("Error writing to HTTP response:" + ex); 133 return false; 134 } 135 } 136 137 140 protected ConverterManager converterManager = null; 141 142 145 protected final HttpServletResponse response; 146 147 150 protected final PrintWriter out; 151 152 155 protected final String batchId; 156 157 160 private static final Logger log = Logger.getLogger(BaseScriptConduit.class); 161 162 165 protected static final String fourKFlushData; 166 static 167 { 168 StringBuffer buffer = new StringBuffer (409600); 169 for (int i = 0; i < 4096; i++) 170 { 171 buffer.append(" "); 172 } 173 fourKFlushData = buffer.toString(); 174 } 175 } | Popular Tags |