Java Code Examples for org.springframework.core.io.Resource#getURI()

The following examples show how to use org.springframework.core.io.Resource#getURI() . 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: Jaxb2CollectionHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: Jaxb2CollectionHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: PackageMetadataService.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
String computeFilename(Resource resource) throws IOException {
	URI uri = resource.getURI();
	StringBuilder stringBuilder = new StringBuilder();
	String scheme = uri.getScheme();
	if (scheme.equals("file")) {
		stringBuilder.append("file");
		if (uri.getPath() != null) {
			stringBuilder.append(uri.getPath().replaceAll("/", "_"));
		}
		else {
			String relativeFilename = uri.getSchemeSpecificPart().replaceAll("^./", "/dot/");
			stringBuilder.append(relativeFilename.replaceAll("/", "_"));
		}
	}
	else if (scheme.equals("http") || scheme.equals("https")) {
		stringBuilder.append(uri.getHost()).append(uri.getPath().replaceAll("/", "_"));
	}
	else {
		logger.warn("Package repository with scheme " + scheme
				+ " is not supported.  Skipping processing this repository.");
	}
	return stringBuilder.toString();
}
 
Example 4
Source File: Jaxb2CollectionHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 5
Source File: Jaxb2CollectionHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
	}
}
 
Example 6
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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>";
}
 
Example 9
Source File: Jaxb2CollectionHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
	}
}
 
Example 10
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readXmlRootElementExternalEntityDisabled() throws Exception {
	Resource external = new ClassPathResource("external.txt", getClass());
	String content =  "<!DOCTYPE root SYSTEM \"https://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);
}
 
Example 11
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws IOException {
	converter = new SourceHttpMessageConverter<>();
	Resource external = new ClassPathResource("external.txt", getClass());

	bodyExternal = "<!DOCTYPE root SYSTEM \"https://192.168.28.42/1.jsp\" [" +
			"  <!ELEMENT root ANY >\n" +
			"  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]><root>&ext;</root>";
}
 
Example 12
Source File: WxMediaUtils.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
public static String resourcePath(Resource resource) {
    URI uri = null;
    try {
        uri = resource.getURI();
    } catch (IOException e) {
        return resource.getFilename();
    }
    String path = uri.getPath();
    if (File.pathSeparator == "/") {
        return path;
    } else {
        return path.substring(1);
    }
}
 
Example 13
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
	converter = new SourceHttpMessageConverter<Source>();
	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>";
}
 
Example 16
Source File: Jaxb2CollectionHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
	}
}
 
Example 17
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 18
Source File: PropertiesRewriter.java    From n2o-framework with Apache License 2.0 4 votes vote down vote up
private static void createFile(Resource resource) throws IOException {
    File file = new File(resource.getURI());
    file.getParentFile().mkdirs();
    file.createNewFile();
}