private String rewrite(Gadget gadget, String content) throws Exception { MutableContent mc = new MutableContent(parser, content); rewriter.rewrite(gadget, mc); return mc.getContent(); }
Document doc = content.getDocument(); Map<PipelinedData, Node> pipelineNodes = parsePipelinedData(gadget, doc); Node node = nodeEntry.getValue(); node.getParentNode().removeChild(node); MutableContent.notifyEdit(doc); content.addPipelinedData(key, entry.getValue()); script.append("opensocial.data.DataContext.putDataSet(\"") .append(key) MutableContent.notifyEdit(doc);
@Test public void modifyContentReflectedInTreeUtf8() throws Exception { String theContent = "N\uFFFDW C\uFFFDNT\uFFFDNT"; assertEquals(0, mhc.getNumChanges()); mhc.setContent(theContent); assertEquals(1, mhc.getNumChanges()); assertEquals(theContent, new String(IOUtils.toByteArray(mhc.getContentBytes()), "UTF8")); Document document = mhc.getDocument(); assertEquals(1, document.getChildNodes().getLength()); assertEquals(theContent, document.getChildNodes().item(0).getTextContent()); mhc.documentChanged(); assertEquals(2, mhc.getNumChanges()); }
public boolean rewrite(HttpRequest request, HttpResponse original, MutableContent content) throws RewritingException { String mimeType = RewriterUtils.getMimeType(request, original); if (OS_TEMPLATE_MIME.equalsIgnoreCase(mimeType)) { content.setContent(converter.domToJson(content.getContent())); return true; } return false; }
public void rewrite(Gadget gadget, MutableContent mc) { Document doc = mc.getDocument(); NodeList list = doc.getElementsByTagName("base"); for (int i = 0; i < list.getLength(); i++) { Element baseElement = (Element) list.item(i); baseElement.getParentNode().removeChild(baseElement); if (baseElement.hasAttribute("href") && logger.isLoggable(Level.FINE)) { logger.fine("Removing base tag pointing to: " + baseElement.getAttribute("href") + " for gadget: " + gadget.getContext().getUrl().toString()); } } mc.documentChanged(); }
@Test public void modifyBytesReflectedInContentAndTree() throws Exception { assertEquals(0, mhc.getNumChanges()); mhc.setContentBytes("NEW CONTENT".getBytes("UTF8"), Charsets.UTF_8); assertEquals(1, mhc.getNumChanges()); Document document = mhc.getDocument(); assertEquals(1, document.getChildNodes().getLength()); assertEquals("NEW CONTENT", document.getChildNodes().item(0).getTextContent()); assertEquals("NEW CONTENT", mhc.getContent()); assertEquals(1, mhc.getNumChanges()); InputStream is = mhc.getContentBytes(); assertEquals("NEW CONTENT", new String(IOUtils.toByteArray(is), "UTF8")); assertEquals(1, mhc.getNumChanges()); }
@Test public void sanitizationBypassAllowed() throws Exception { String markup = "<p foo=\"bar\"><b>Parag</b><!--raph--></p>"; // Create a rewriter that would strip everything GadgetRewriter rewriter = createRewriter(set(), set()); MutableContent mc = new MutableContent(parser, markup); Document document = mc.getDocument(); // Force the content to get re-serialized MutableContent.notifyEdit(document); String fullMarkup = mc.getContent(); Element paragraphTag = (Element) document.getElementsByTagName("p").item(0); // Mark the paragraph tag element as trusted SanitizingGadgetRewriter.bypassSanitization(paragraphTag, true); rewriter.rewrite(gadget, mc); // The document should be unchanged assertEquals(fullMarkup, mc.getContent()); }
@Test public void sanitizationBypassOnlySelf() throws Exception { String markup = "<p foo=\"bar\"><b>Parag</b><!--raph--></p>"; // Create a rewriter that would strip everything GadgetRewriter rewriter = createRewriter(set(), set()); MutableContent mc = new MutableContent(parser, markup); Document document = mc.getDocument(); Element paragraphTag = (Element) document.getElementsByTagName("p").item(0); // Mark the paragraph tag element as trusted SanitizingGadgetRewriter.bypassSanitization(paragraphTag, false); rewriter.rewrite(gadget, mc); // The document should be unchanged String content = mc.getContent(); Matcher matcher = BODY_REGEX.matcher(content); matcher.matches(); assertEquals("<p foo=\"bar\"></p>", matcher.group(1)); }
Document doc = content.getDocument(); if (doc == null) { throw new RewritingException("content.getDocument is null. Content: " + content.getContent(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR); switch(visitor.visit(gadget, visiting)) { case MODIFY: content.documentChanged(); mutated = true; break; List<Node> nodesReserved = reservations.get(visitor); if (nodesReserved != null && visitor.revisit(gadget, nodesReserved)) { content.documentChanged(); mutated = true;
Map<String, Object> pipelinedData = content.getPipelinedData(); MutableContent.notifyEdit(content.getDocument());
Document doc = mc.getDocument(); cajoledData = doc.adoptNode(cajoledOutput); createContainerFor(doc, cajoledData); mc.documentChanged(); cajoledData = cajoledOutput; createContainerFor(doc, cajoledData); mc.documentChanged(); safe = true; HtmlSerialization.attach(doc, htmlSerializer, null); if (!safe) { mc.setContent(""); return;
String rewriteHelper(GadgetRewriter rewriter, String s) throws Exception { MutableContent mc = rewriteContent(rewriter, s, null); String rewrittenContent = mc.getContent(); // Strip around the HTML tags for convenience int htmlTagIndex = rewrittenContent.indexOf("<HTML>"); if (htmlTagIndex != -1) { return rewrittenContent.substring(htmlTagIndex + 6, rewrittenContent.lastIndexOf("</HTML>")); } return rewrittenContent; }
@Before public void setUp() throws Exception { Injector injector = Guice.createInjector(new ParseModule(), new PropertiesModule()); mhc = new MutableContent(injector.getInstance(GadgetHtmlParser.class), "DEFAULT VIEW"); }
public static void notifyEdit(Document doc) { MutableContent mc = (MutableContent) doc.getUserData(MUTABLE_CONTENT_LISTENER); if (mc != null) { mc.documentChanged(); } }
/** * Sets content to new byte array, with unspecified charset. It is * recommended to use the {@code setContentBytes(byte[], Charset)} API instead, * where possible. * @param newBytes New content. */ public final void setContentBytes(byte[] newBytes) { setContentBytes(newBytes, null); }
private void putPipelinedData(String key, JSONObject data) { content.addPipelinedData(key, data); }
@Override public void rewrite(Gadget gadget, MutableContent content) throws RewritingException { if (gadget.sanitizeOutput()) { boolean sanitized = false; try { super.rewrite(gadget, content); sanitized = true; } finally { // Defensively clean the content in case of failure if (!sanitized) { content.setContent(""); } } } }
@Test public void modifyBytesReflectedInContentAndTree() throws Exception { assertEquals(0, mhc.getNumChanges()); mhc.setContentBytes("NEW CONTENT".getBytes("UTF8"), Charsets.UTF_8); assertEquals(1, mhc.getNumChanges()); Document document = mhc.getDocument(); assertEquals(1, document.getChildNodes().getLength()); assertEquals("NEW CONTENT", document.getChildNodes().item(0).getTextContent()); assertEquals("NEW CONTENT", mhc.getContent()); assertEquals(1, mhc.getNumChanges()); InputStream is = mhc.getContentBytes(); assertEquals("NEW CONTENT", new String(IOUtils.toByteArray(is), "UTF8")); assertEquals(1, mhc.getNumChanges()); }
public void rewrite(Gadget gadget, MutableContent mc) { Document doc = mc.getDocument(); NodeList list = doc.getElementsByTagName("base"); for (int i = 0; i < list.getLength(); i++) { Element baseElement = (Element) list.item(i); baseElement.getParentNode().removeChild(baseElement); if (baseElement.hasAttribute("href") && logger.isLoggable(Level.FINE)) { logger.fine("Removing base tag pointing to: " + baseElement.getAttribute("href") + " for gadget: " + gadget.getContext().getUrl().toString()); } } mc.documentChanged(); }
@Test public void sanitizationBypassAllowed() throws Exception { String markup = "<p foo=\"bar\"><b>Parag</b><!--raph--></p>"; // Create a rewriter that would strip everything GadgetRewriter rewriter = createRewriter(set(), set()); MutableContent mc = new MutableContent(parser, markup); Document document = mc.getDocument(); // Force the content to get re-serialized MutableContent.notifyEdit(document); String fullMarkup = mc.getContent(); Element paragraphTag = (Element) document.getElementsByTagName("p").item(0); // Mark the paragraph tag element as trusted SanitizingGadgetRewriter.bypassSanitization(paragraphTag, true); rewriter.rewrite(gadget, mc); // The document should be unchanged assertEquals(fullMarkup, mc.getContent()); }