Java Code Examples for javax.xml.transform.sax.SAXSource#getInputSource()

The following examples show how to use javax.xml.transform.sax.SAXSource#getInputSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void readSAXSourceExternal() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	converter.setSupportDtd(true);
	SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
	InputSource inputSource = result.getInputSource();
	XMLReader reader = result.getXMLReader();
	reader.setContentHandler(new DefaultHandler() {
		@Override
		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			assertNotEquals("Invalid result", "Foo Bar", s);
		}
	});
	reader.parse(inputSource);
}
 
Example 2
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void readSAXSourceExternal() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	converter.setSupportDtd(true);
	SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
	InputSource inputSource = result.getInputSource();
	XMLReader reader = result.getXMLReader();
	reader.setContentHandler(new DefaultHandler() {
		@Override
		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			assertNotEquals("Invalid result", "Foo Bar", s);
		}
	});
	reader.parse(inputSource);
}
 
Example 3
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void readSAXSourceExternal() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	converter.setSupportDtd(true);
	SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
	InputSource inputSource = result.getInputSource();
	XMLReader reader = result.getXMLReader();
	reader.setContentHandler(new DefaultHandler() {
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			String s = new String(ch, start, length);
			assertNotEquals("Invalid result", "Foo Bar", s);
		}
	});
	reader.parse(inputSource);
}
 
Example 4
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readSAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
	InputSource inputSource = result.getInputSource();
	String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream()));
	assertThat("Invalid result", s, isSimilarTo(BODY));
}
 
Example 5
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readSAXSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";

	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);

	InputSource inputSource = result.getInputSource();
	XMLReader reader = result.getXMLReader();
	assertThatExceptionOfType(SAXException.class).isThrownBy(() ->
			reader.parse(inputSource))
		.withMessageContaining("DOCTYPE");
}
 
Example 6
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readSAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
	InputSource inputSource = result.getInputSource();
	String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream()));
	assertThat("Invalid result", s, isSimilarTo(BODY));
}
 
Example 7
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void readSAXSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";

	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);

	this.thrown.expect(SAXException.class);
	this.thrown.expectMessage("DOCTYPE");

	InputSource inputSource = result.getInputSource();
	XMLReader reader = result.getXMLReader();
	reader.parse(inputSource);
}
 
Example 8
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readSAXSource() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
	InputSource inputSource = result.getInputSource();
	String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream()));
	assertXMLEqual("Invalid result", BODY, s);
}
 
Example 9
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void readSAXSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";

	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);

	this.thrown.expect(SAXException.class);
	this.thrown.expectMessage("DOCTYPE");

	InputSource inputSource = result.getInputSource();
	XMLReader reader = result.getXMLReader();
	reader.parse(inputSource);
}