protected String escapeForJavaScript(String str) { try { str = StringEscapeUtils.escapeJavaScript(str); } catch (Exception e) { logger.error("Failed to escape", e); str = null; } return str; }
@Override public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException { String charEncoding = response.getCharacterEncoding() == null ? "UTF-8" : response.getCharacterEncoding(); String s = new String(responseDraft, charEncoding); // Ugly. if (s.equalsIgnoreCase("h") || s.equals("c") || (s.equals("o\n") && r.transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET))) { return s.getBytes(); } if (!s.isEmpty()) { try { if (transport.equals(JSONP)) { return ("a" + s).getBytes(charEncoding); } else if (transport.equals(HTMLFILE)) { StringBuilder sb = new StringBuilder(); sb.append("<script>\np(") .append("\"") .append(StringEscapeUtils.escapeJavaScript("a[\"" + StringEscapeUtils.escapeJavaScript(s) + "\"]\n")) .append("\")</script>\n"); return (sb.toString()).getBytes(charEncoding); } else { return ("a[\"" + StringEscapeUtils.escapeJavaScript(s) + "\"]\n").getBytes(charEncoding); } } catch (Exception e) { logger.error("", e); return "".getBytes(); } } return s.getBytes(); } });