@Override public URI getURI() throws IOException { return this.gzipped.getURI(); }
@Override public URI getURI() throws IOException { return this.original.getURI(); }
@Override public URI getURI() throws IOException { return this.encoded.getURI(); }
@Override public URI getURI() throws IOException { return this.original.getURI(); }
@Override public URI getURI() throws IOException { return this.encoded.getURI(); }
@Override public URI getURI() throws IOException { return this.delegate.getURI(); }
@Override public URI getURI() throws IOException { return this.gzipped.getURI(); }
/** * Retrieve the URL from the given resource as System ID. * <p>Returns {@code null} if it cannot be opened. */ @Nullable private static String getSystemId(Resource resource) { try { return resource.getURI().toString(); } catch (IOException ex) { return null; } }
/** * Get the XSLT {@link Source} for the XSLT template under the {@link #setUrl configured URL}. * @return the Source object */ protected Source getStylesheetSource() { String url = getUrl(); Assert.state(url != null, "'url' not set"); if (logger.isDebugEnabled()) { logger.debug("Applying stylesheet [" + url + "]"); } try { Resource resource = obtainApplicationContext().getResource(url); return new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString()); } catch (IOException ex) { throw new ApplicationContextException("Can't load XSLT stylesheet from '" + url + "'", ex); } }
@Before public void setup() throws IOException { converter = new SourceHttpMessageConverter<>(); Resource external = new ClassPathResource("external.txt", getClass()); bodyExternal = "<!DOCTYPE root SYSTEM \"http://192.168.28.42/1.jsp\" [" + " <!ELEMENT root ANY >\n" + " <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]><root>&ext;</root>"; }
@Test public void readByteChannel() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> result = DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ), this.bufferFactory, 3); verifyReadData(result); }
@Test public void readXmlRootElementExternalEntityEnabled() throws Exception { Resource external = new ClassPathResource("external.txt", getClass()); String content = "<!DOCTYPE root [" + " <!ELEMENT external ANY >\n" + " <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + " <rootElement><external>&ext;</external></rootElement>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); this.converter.setProcessExternalEntities(true); RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage); assertEquals("Foo Bar", rootElement.external); }
@Test public void readXmlRootElementExternalEntityDisabled() throws Exception { Resource external = new ClassPathResource("external.txt", getClass()); String content = "<!DOCTYPE root SYSTEM \"http://192.168.28.42/1.jsp\" [" + " <!ELEMENT external ANY >\n" + " <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + " <rootElement><external>&ext;</external></rootElement>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); converter.setSupportDtd(true); RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage); assertEquals("", rootElement.external); }
@Test public void readAsynchronousFileChannel() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel( () -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ), this.bufferFactory, 3); verifyReadData(flux); }
@Test @SuppressWarnings("unchecked") public void readXmlRootElementExternalEntityEnabled() throws Exception { Resource external = new ClassPathResource("external.txt", getClass()); String content = "<!DOCTYPE root [" + " <!ELEMENT external ANY >\n" + " <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + " <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); Jaxb2CollectionHttpMessageConverter<?> c = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() { @Override protected XMLInputFactory createXmlInputFactory() { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true); return inputFactory; } }; Collection<RootElement> result = c.read(rootElementListType, null, inputMessage); assertEquals(1, result.size()); assertEquals("Foo Bar", result.iterator().next().external); }
@Test @SuppressWarnings("unchecked") public void readXmlRootElementExternalEntityDisabled() throws Exception { Resource external = new ClassPathResource("external.txt", getClass()); String content = "<!DOCTYPE root [" + " <!ELEMENT external ANY >\n" + " <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + " <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8")); converter = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() { @Override protected XMLInputFactory createXmlInputFactory() { XMLInputFactory inputFactory = super.createXmlInputFactory(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true); return inputFactory; } }; try { Collection<RootElement> result = converter.read(rootElementListType, null, inputMessage); assertEquals(1, result.size()); assertEquals("", result.iterator().next().external); } catch (HttpMessageNotReadableException ex) { // Some parsers raise an exception } }
@Test public void readByteChannelCancel() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> result = DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ), this.bufferFactory, 3); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(); }
@Test public void resourceInjection() throws IOException { System.setProperty("logfile", "do_not_delete_me.txt"); try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class)) { ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class); Resource resource = new ClassPathResource("do_not_delete_me.txt"); assertEquals(resource, resourceInjectionBean.resource); assertEquals(resource.getURL(), resourceInjectionBean.url); assertEquals(resource.getURI(), resourceInjectionBean.uri); assertEquals(resource.getFile(), resourceInjectionBean.file); assertArrayEquals(FileCopyUtils.copyToByteArray(resource.getInputStream()), FileCopyUtils.copyToByteArray(resourceInjectionBean.inputStream)); assertEquals(FileCopyUtils.copyToString(new EncodedResource(resource).getReader()), FileCopyUtils.copyToString(resourceInjectionBean.reader)); } finally { System.getProperties().remove("logfile"); } }
@Test public void readAsynchronousFileChannelPosition() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel( () -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ), 9, this.bufferFactory, 3); StepVerifier.create(flux) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void readAsynchronousFileChannelCancel() throws Exception { URI uri = this.resource.getURI(); Flux<DataBuffer> flux = DataBufferUtils.readAsynchronousFileChannel( () -> AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ), this.bufferFactory, 3); StepVerifier.create(flux) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(); }