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

The following examples show how to use org.springframework.core.io.Resource#getURI() . These examples are extracted from open source projects. 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
@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
@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
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
@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
@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 6
@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 7
@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 8
@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 9
@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 10
@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 11
@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 12
@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 13
Source Project: FastBootWeixin   File: WxMediaUtils.java    License: 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 14
@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 15
@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 16
@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 17
@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 18
private static void createFile(Resource resource) throws IOException {
    File file = new File(resource.getURI());
    file.getParentFile().mkdirs();
    file.createNewFile();
}