@Override protected boolean removeAttr(Gadget gadget, Attr attr, Uri ctx) { if ("img".equalsIgnoreCase(attr.getOwnerElement().getNodeName()) && "src".equalsIgnoreCase(attr.getName())) { try { Uri uri = Uri.parse(attr.getValue()); ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList( gadget, ImmutableList.of(uri)).get(0); proxiedUri.setHtmlTagContext(attr.getOwnerElement().getNodeName().toLowerCase()); attr.setValue(imageRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { // Invalid Uri, remove. return true; } } return false; } }
@Override protected boolean removeAttr(Gadget gadget, Attr attr, Uri ctx) { if ("img".equalsIgnoreCase(attr.getOwnerElement().getNodeName()) && "src".equalsIgnoreCase(attr.getName())) { try { Uri uri = Uri.parse(attr.getValue()); ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList( gadget, ImmutableList.of(uri)).get(0); proxiedUri.setHtmlTagContext(attr.getOwnerElement().getNodeName().toLowerCase()); attr.setValue(imageRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { // Invalid Uri, remove. return true; } } return false; } }
@Override protected boolean removeTag(Gadget gadget, Element elem, Uri ctx) { if (!elem.getNodeName().equalsIgnoreCase("link")) { return false; } boolean hasType = false; for (Attr attr : toList(elem.getAttributes())) { if ("rel".equalsIgnoreCase(attr.getName())) { hasType |= "stylesheet".equalsIgnoreCase(attr.getValue()); } else if ("type".equalsIgnoreCase(attr.getName())) { hasType |= "text/css".equalsIgnoreCase(attr.getValue()); } else if ("href".equalsIgnoreCase(attr.getName())) { try { ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList(gadget, ImmutableList.of(Uri.parse(attr.getValue()))).get(0); proxiedUri.setHtmlTagContext(elem.getNodeName().toLowerCase()); attr.setValue(cssImportRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { return true; } } } return !hasType; }
@Override protected boolean removeAttr(Gadget gadget, Attr attr, Uri ctx) { if ("img".equalsIgnoreCase(attr.getOwnerElement().getNodeName()) && "src".equalsIgnoreCase(attr.getName())) { try { Uri uri = Uri.parse(attr.getValue()); ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList( gadget, ImmutableList.of(uri)).get(0); proxiedUri.setHtmlTagContext(attr.getOwnerElement().getNodeName().toLowerCase()); attr.setValue(imageRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { // Invalid Uri, remove. return true; } } return false; } }
@Override protected boolean removeTag(Gadget gadget, Element elem, Uri ctx) { if (!elem.getNodeName().equalsIgnoreCase("link")) { return false; } boolean hasType = false; for (Attr attr : toList(elem.getAttributes())) { if ("rel".equalsIgnoreCase(attr.getName())) { hasType |= "stylesheet".equalsIgnoreCase(attr.getValue()); } else if ("type".equalsIgnoreCase(attr.getName())) { hasType |= "text/css".equalsIgnoreCase(attr.getValue()); } else if ("href".equalsIgnoreCase(attr.getName())) { try { ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList(gadget, ImmutableList.of(Uri.parse(attr.getValue()))).get(0); proxiedUri.setHtmlTagContext(elem.getNodeName().toLowerCase()); attr.setValue(cssImportRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { return true; } } } return !hasType; }
@Override protected boolean removeTag(Gadget gadget, Element elem, Uri ctx) { if (!elem.getNodeName().equalsIgnoreCase("link")) { return false; } boolean hasType = false; for (Attr attr : toList(elem.getAttributes())) { if ("rel".equalsIgnoreCase(attr.getName())) { hasType |= "stylesheet".equalsIgnoreCase(attr.getValue()); } else if ("type".equalsIgnoreCase(attr.getName())) { hasType |= "text/css".equalsIgnoreCase(attr.getValue()); } else if ("href".equalsIgnoreCase(attr.getName())) { try { ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList(gadget, ImmutableList.of(Uri.parse(attr.getValue()))).get(0); proxiedUri.setHtmlTagContext(elem.getNodeName().toLowerCase()); attr.setValue(cssImportRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { return true; } } } return !hasType; }
@Test public void makeSingleNoMime() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); List<Uri> output = Lists.newArrayList(uri); Integer refresh = new Integer(0); expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); expect(proxyUri.setSanitizeContent(true)).andReturn(proxyUri).once(); replay(proxyUri); SanitizingProxyUriManager rewriter = makeRewriter(null); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(1, returned.size()); verify(proxyUri); }
@Test public void makeSingleNoMime() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); List<Uri> output = Lists.newArrayList(uri); Integer refresh = new Integer(0); expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); expect(proxyUri.setSanitizeContent(true)).andReturn(proxyUri).once(); replay(proxyUri); SanitizingProxyUriManager rewriter = makeRewriter(null); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(1, returned.size()); verify(proxyUri); }
@Test public void makeSingleExpectedMime() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); List<Uri> output = Lists.newArrayList(uri); Integer refresh = new Integer(0); String mime = "my/mime"; expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); expect(proxyUri.setSanitizeContent(true)).andReturn(proxyUri).once(); expect(proxyUri.setRewriteMimeType(mime)).andReturn(proxyUri).once(); replay(proxyUri); SanitizingProxyUriManager rewriter = makeRewriter(mime); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(1, returned.size()); verify(proxyUri); }
@Test public void makeSingleExpectedMime() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); List<Uri> output = Lists.newArrayList(uri); Integer refresh = new Integer(0); String mime = "my/mime"; expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); expect(proxyUri.setSanitizeContent(true)).andReturn(proxyUri).once(); expect(proxyUri.setRewriteMimeType(mime)).andReturn(proxyUri).once(); replay(proxyUri); SanitizingProxyUriManager rewriter = makeRewriter(mime); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(1, returned.size()); verify(proxyUri); }
@Test public void makeList() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); ProxyUri proxyUri2 = createMock(ProxyUri.class); List<ProxyUri> input = Lists.newArrayList(proxyUri, proxyUri2); Uri uri2 = new UriBuilder().toUri(); List<Uri> output = Lists.newArrayList(uri, uri2); Integer refresh = new Integer(0); String mime = "my/mime"; expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); expect(proxyUri.setSanitizeContent(true)).andReturn(proxyUri).once(); expect(proxyUri.setRewriteMimeType(mime)).andReturn(proxyUri).once(); expect(proxyUri2.setSanitizeContent(true)).andReturn(proxyUri2).once(); expect(proxyUri2.setRewriteMimeType(mime)).andReturn(proxyUri2).once(); replay(proxyUri, proxyUri2); SanitizingProxyUriManager rewriter = makeRewriter(mime); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(2, returned.size()); verify(proxyUri, proxyUri2); }
@Test public void makeList() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); ProxyUri proxyUri2 = createMock(ProxyUri.class); List<ProxyUri> input = Lists.newArrayList(proxyUri, proxyUri2); Uri uri2 = new UriBuilder().toUri(); List<Uri> output = Lists.newArrayList(uri, uri2); Integer refresh = new Integer(0); String mime = "my/mime"; expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); expect(proxyUri.setSanitizeContent(true)).andReturn(proxyUri).once(); expect(proxyUri.setRewriteMimeType(mime)).andReturn(proxyUri).once(); expect(proxyUri2.setSanitizeContent(true)).andReturn(proxyUri2).once(); expect(proxyUri2.setRewriteMimeType(mime)).andReturn(proxyUri2).once(); replay(proxyUri, proxyUri2); SanitizingProxyUriManager rewriter = makeRewriter(mime); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(2, returned.size()); verify(proxyUri, proxyUri2); }
@Test public void makeSingleNoMime() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); List<Uri> output = Lists.newArrayList(uri); Integer refresh = new Integer(0); expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); SanitizingProxyUriManager rewriter = makeRewriter(null); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(1, returned.size()); assertEquals("1", returned.get(0).getQueryParameter(Param.SANITIZE.getKey())); assertNull(returned.get(0).getQueryParameter(Param.REWRITE_MIME_TYPE.getKey())); }
@Test public void makeSingleExpectedMime() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); List<Uri> output = Lists.newArrayList(uri); Integer refresh = new Integer(0); String mime = "my/mime"; expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); SanitizingProxyUriManager rewriter = makeRewriter(mime); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(1, returned.size()); assertEquals("1", returned.get(0).getQueryParameter(Param.SANITIZE.getKey())); assertEquals(mime, returned.get(0).getQueryParameter(Param.REWRITE_MIME_TYPE.getKey())); }
@Test public void makeList() throws Exception { Capture<List<ProxyUri>> uriCapture = new Capture<List<ProxyUri>>(); Capture<Integer> intCapture = new Capture<Integer>(); List<ProxyUri> input = Lists.newArrayList(proxyUri); Uri uri2 = new UriBuilder().toUri(); List<Uri> output = Lists.newArrayList(uri, uri2); Integer refresh = new Integer(0); String mime = "my/mime"; expect(uriManager.make(capture(uriCapture), capture(intCapture))) .andReturn(output).once(); replay(uriManager); SanitizingProxyUriManager rewriter = makeRewriter(mime); List<Uri> returned = rewriter.make(input, refresh); verify(uriManager); assertSame(uriCapture.getValue(), input); assertSame(intCapture.getValue(), refresh); assertEquals(2, returned.size()); assertEquals("1", returned.get(0).getQueryParameter(Param.SANITIZE.getKey())); assertEquals(mime, returned.get(0).getQueryParameter(Param.REWRITE_MIME_TYPE.getKey())); assertEquals("1", returned.get(1).getQueryParameter(Param.SANITIZE.getKey())); assertEquals(mime, returned.get(1).getQueryParameter(Param.REWRITE_MIME_TYPE.getKey())); }