1 16 package org.directwebremoting.extend; 17 18 import java.io.IOException ; 19 20 import org.directwebremoting.ScriptBuffer; 21 import org.directwebremoting.dwrp.ProtocolConstants; 22 import org.directwebremoting.impl.DefaultRemoter; 23 import org.directwebremoting.proxy.ScriptProxy; 24 import org.directwebremoting.util.JavascriptUtil; 25 import org.directwebremoting.util.Logger; 26 27 34 public class EnginePrivate extends ScriptProxy 35 { 36 45 public static void remoteHandleCallback(ScriptConduit conduit, String batchId, String callId, Object data) throws IOException , MarshallException 46 { 47 ScriptBuffer script = new ScriptBuffer(); 48 script.appendScript("dwr.engine._remoteHandleCallback(\'") 49 .appendScript(batchId) 50 .appendScript("\',\'") 51 .appendScript(callId) 52 .appendScript("\',") 53 .appendData(data) 54 .appendScript(");"); 55 56 conduit.addScript(script); 57 } 58 59 67 public static void remoteHandleMarshallException(ScriptConduit conduit, String batchId, String callId, MarshallException ex) throws IOException 68 { 69 try 70 { 71 ScriptBuffer script = new ScriptBuffer(); 72 script.appendScript("dwr.engine._remoteHandleException(\'") 73 .appendScript(batchId) 74 .appendScript("\',\'") 75 .appendScript(callId) 76 .appendScript("\',") 77 .appendData(ex) 78 .appendScript(");"); 79 80 conduit.addScript(script); 81 } 82 catch (MarshallException mex) 83 { 84 ScriptBuffer script = new ScriptBuffer(); 85 script.appendScript("dwr.engine._remoteHandleException(\'") 86 .appendScript(batchId) 87 .appendScript("\',\'") 88 .appendScript(callId) 89 .appendScript("\',") 90 .appendData(ex) 91 .appendScript(");"); 92 93 addScriptWithoutException(conduit, script); 94 } 95 } 96 97 105 public static void remoteHandleException(ScriptConduit conduit, String batchId, String callId, Throwable ex) throws IOException 106 { 107 try 108 { 109 ScriptBuffer script = new ScriptBuffer(); 110 script.appendScript("dwr.engine._remoteHandleException(\'") 111 .appendScript(batchId) 112 .appendScript("\',\'") 113 .appendScript(callId) 114 .appendScript("\',") 115 .appendData(ex) 116 .appendScript(");"); 117 118 conduit.addScript(script); 119 } 120 catch (MarshallException mex) 121 { 122 ScriptBuffer script = new ScriptBuffer(); 123 script.appendScript("dwr.engine._remoteHandleException(\'") 124 .appendScript(batchId) 125 .appendScript("\',\'") 126 .appendScript(callId) 127 .appendScript("\',") 128 .appendData(ex) 129 .appendScript(");"); 130 131 addScriptWithoutException(conduit, script); 132 } 133 } 134 135 141 public static String getRemoteHandleBatchExceptionScript(String batchId, Exception ex) 142 { 143 StringBuffer reply = new StringBuffer (); 144 145 String output = JavascriptUtil.escapeJavaScript(ex.getMessage()); 146 String params = "{ name:'" + ex.getClass().getName() + "', message:'" + output + "' }"; 147 if (batchId != null) 148 { 149 params += ", '" + batchId + "'"; 150 } 151 152 reply.append(ProtocolConstants.SCRIPT_CALL_REPLY).append("\r\n"); 153 reply.append("if (window.dwr) dwr.engine._remoteHandleBatchException(").append(params).append(");\r\n"); 154 reply.append("else if (window.parent.dwr) window.parent.dwr.engine._remoteHandleBatchException(").append(params).append(");\r\n"); 155 156 return reply.toString(); 157 } 158 159 164 public static String getRemotePollCometDisabledScript(String batchId) 165 { 166 StringBuffer reply = new StringBuffer (); 167 168 String params = "{ name:'dwr.engine.pollAndCometDisabled', message:'Polling and Comet are disabled. See the server logs.' }"; 169 if (batchId != null) 170 { 171 params += ", '" + batchId + "'"; 172 } 173 174 reply.append(ProtocolConstants.SCRIPT_CALL_REPLY).append("\r\n"); 175 reply.append("if (window.dwr) dwr.engine._remotePollCometDisabled(").append(params).append(");\r\n"); 176 reply.append("else if (window.parent.dwr) window.parent.dwr.engine._remotePollCometDisabled(").append(params).append(");\r\n"); 177 178 return reply.toString(); 179 } 180 181 187 public static String xmlStringToJavascriptDom(String xml) 188 { 189 String xmlout = JavascriptUtil.escapeJavaScript(xml); 190 return "dwr.engine._unserializeDocument(\"" + xmlout + "\")"; 191 } 192 193 197 public static String getEngineInitScript() 198 { 199 return "// Provide a default path to dwr.engine\n" + 200 "if (dwr == null) var dwr = {};\n" + 201 "if (dwr.engine == null) dwr.engine = {};\n" + 202 "if (DWREngine == null) var DWREngine = dwr.engine;\n" + 203 '\n'; 204 } 205 206 210 public static String getExecuteFunctionName() 211 { 212 return "dwr.engine._execute"; 213 } 214 215 223 public static String remoteBeginIFrameResponse(String batchId, boolean useWindowParent) 224 { 225 String prefix = ""; 226 if (useWindowParent) 227 { 228 prefix = "window.parent."; 229 } 230 231 return prefix + "dwr.engine._remoteBeginIFrameResponse(this.frameElement"+(batchId == null?"":", '" + batchId+"'") + ");"; 232 } 233 234 242 public static String remoteEndIFrameResponse(String batchId, boolean useWindowParent) 243 { 244 String prefix = ""; 245 if (useWindowParent) 246 { 247 prefix = "window.parent."; 248 } 249 250 return prefix + "dwr.engine._remoteEndIFrameResponse("+(batchId == null?"":"'" + batchId+"'")+");"; 251 } 252 253 258 public static String remoteEval(String script) 259 { 260 return "window.parent.dwr.engine._eval(\"" + JavascriptUtil.escapeJavaScript(script) + "\");"; 261 } 262 263 271 private static void addScriptWithoutException(ScriptConduit conduit, ScriptBuffer script) throws IOException 272 { 273 try 274 { 275 conduit.addScript(script); 276 } 277 catch (MarshallException ex) 278 { 279 log.warn("This exception can't happen", ex); 280 } 281 } 282 283 286 private static final Logger log = Logger.getLogger(EnginePrivate.class); 287 } 288 | Popular Tags |