ConsoleProxyServlet extends HttpServlet { public static final String escapeHTML(String content) { if (content == null || content.isEmpty()) return content; StringBuilder sb = new StringBuilder(); for (int i = 0; i < content.length(); i++) { char c = content.charAt(i); switch (c) { case '<': sb.append("&lt;"); break; case '>': sb.append("&gt;"); break; case '&': sb.append("&amp;"); break; case '"': sb.append("&quot;"); break; case ' ': sb.append("&nbsp;"); break; default: sb.append(c); break; } } return sb.toString(); } ConsoleProxyServlet(); @Override void init(ServletConfig config); static Ternary<String, String, String> parseHostInfo(String hostInfo); static String genAccessTicket(String host, String port, String sid, String tag); static String genAccessTicket(String host, String port, String sid, String tag, Date normalizedHashTime); boolean verifyUser(Long userId); static final String escapeHTML(String content); static final Logger s_logger; }
@Test public void escapeHTML() { Assert.assertNull(ConsoleProxyServlet.escapeHTML(null)); Assert.assertEquals("", ConsoleProxyServlet.escapeHTML("")); Assert.assertEquals("&lt;strangevmname&gt;", ConsoleProxyServlet.escapeHTML("<strangevmname>")); Assert.assertEquals("&quot;strange&nbsp;vm&quot;", ConsoleProxyServlet.escapeHTML("\"strange vm\"")); Assert.assertEquals("Nothing-extraordinary-anyway.", ConsoleProxyServlet.escapeHTML("Nothing-extraordinary-anyway.")); }