/** * Prepare a script for execution in an iframe environment * * @param script The script to modify * @return The modified script */ public static String remoteEval(String script) { // We need to define the "r" redirector variable inside the eval as well // as it can't access the global "r" defined in the response closure. return "r._eval(\"var r=dwr._[dwr.engine._instanceId]; " + JavascriptUtil.escapeJavaScript(script) + "\");"; }
/** * Evade the 2 connection limit by sending scripts to the wrong window and having that use * window.name to push it to the right window * * @param original The script that we wish to be proxied * @param windowName The window to which we wish the window to be proxied * @return A script to send to a different window to cause proxying */ public static ScriptBuffer createForeignWindowProxy(String windowName, ScriptBuffer original) { String proxy = JavascriptUtil.escapeJavaScript(original.toString()); ScriptBuffer reply = new ScriptBuffer(); reply.appendCall("r.handleForeign", windowName, proxy); reply.appendData(proxy); return reply; }
/** * Call dwr.engine.remote.handleServerException() in the browser * * @param batchId The identifier of the batch that we are handling a response for * @param ex The exception from which we make a reply * @return The script to send to the browser */ public static String getRemoteHandleBatchExceptionScript(String batchId, Exception ex) { StringBuffer reply = new StringBuffer(); String output = JavascriptUtil.escapeJavaScript(ex.getMessage()); String params = "{ name:'" + ex.getClass().getName() + "', message:'" + output + "' }"; if (batchId != null) { params += ", '" + batchId + "'"; } reply.append(ProtocolConstants.SCRIPT_CALL_REPLY).append("\r\n"); reply.append("r.handleBatchException(").append(params).append(");\r\n"); return reply.toString(); }
/** * Convert a map of strings to objects to json * * @param map object to convert * @param sb StringBuffer to append to */ private void toJSONString(Map<String, Object> map, StringBuffer sb) { boolean first = true; sb.append('{'); for (Map.Entry<String, Object> entry : map.entrySet()) { if (first) first = false; else sb.append(','); sb.append('"'); if (entry.getKey() == null) sb.append("null"); else sb.append(JavascriptUtil.escapeJavaScript(entry.getKey())); sb.append('"').append(':'); sb.append(toJSONString(entry.getValue())); } sb.append('}'); }
/** * Convert all other objects to json * * @param object object to convert * @param sb StringBuffer to append to */ private void toJSONString(Object object, StringBuffer sb) { if (object == null) sb.append("null"); else sb.append('"').append(JavascriptUtil.escapeJavaScript(object.toString())).append('"'); }
/** * Take an XML string, and convert it into some Javascript that when executed will return a DOM * object that represents the same XML object * * @param xml The XML string to convert * @return The Javascript */ public static String xmlStringToJavascriptDomDocument(String xml) { String xmlout = JavascriptUtil.escapeJavaScript(xml); return "r.toDomDocument(\"" + xmlout + "\")"; }
/* (non-Javadoc) * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext) */ public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws ConversionException { URI uri = (URI) data; String escaped = JavascriptUtil.escapeJavaScript(uri.toString()); return new NonNestedOutboundVariable('\"' + escaped + '\"'); }