use of org.springframework.core.io.UrlResource in project opennms by OpenNMS.
the class DnsRequisitionUrlConnectionIT method dwoUrlAsResource.
@Test
@JUnitDNSServer(port = 9153, zones = { @DNSZone(name = "example.com", entries = { @DNSEntry(hostname = "www", data = "72.14.204.99") }) })
public void dwoUrlAsResource() throws IOException, JAXBException {
Resource resource = new UrlResource(TEST_URL);
Assert.assertEquals(TEST_URL, resource.getURL().toString());
Requisition req = null;
Assert.assertNotNull(resource);
InputStream resourceStream = resource.getInputStream();
JAXBContext context = JAXBContext.newInstance(Requisition.class);
Unmarshaller um = context.createUnmarshaller();
um.setSchema(null);
req = (Requisition) um.unmarshal(resourceStream);
Assert.assertEquals("should have 2 A records: 1 for example.com, and 1 for www.example.com", 2, req.getNodeCount());
resourceStream.close();
}
use of org.springframework.core.io.UrlResource in project opennms by OpenNMS.
the class DnsRequisitionUrlConnectionIT method dwoUrlAsResourceUsingNonMatchingExpression.
@Test
@JUnitDNSServer(port = 9153, zones = { @DNSZone(name = "example.com", entries = { @DNSEntry(hostname = "www", data = "72.14.204.99"), @DNSEntry(hostname = "monkey", data = "72.14.204.99") }) })
public void dwoUrlAsResourceUsingNonMatchingExpression() throws IOException, JAXBException {
String urlString = "dns://localhost:9153/example.com/?expression=Local.*";
Resource resource = new UrlResource(urlString);
Assert.assertEquals(urlString, resource.getURL().toString());
Requisition req = null;
Assert.assertNotNull(resource);
InputStream resourceStream = resource.getInputStream();
JAXBContext context = JAXBContext.newInstance(Requisition.class);
Unmarshaller um = context.createUnmarshaller();
um.setSchema(null);
req = (Requisition) um.unmarshal(resourceStream);
Assert.assertEquals(0, req.getNodeCount());
resourceStream.close();
}
use of org.springframework.core.io.UrlResource in project opennms by OpenNMS.
the class Provisioner method doImport.
/**
* <p>doImport</p>
*
* @param url a {@link java.lang.String} object.
*/
public void doImport(final String url, final String rescanExisting) {
try {
LOG.info("doImport: importing from url: {}, rescanExisting ? {}", url, rescanExisting);
final Resource resource;
final URL u = new URL(url);
if ("file".equals(u.getProtocol())) {
final File file = new File(u.toURI());
LOG.debug("doImport: file = {}", file);
if (file.exists()) {
resource = new FileSystemResource(file);
} else {
final String filename = file.getName();
if (filename.contains("%20")) {
resource = new FileSystemResource(new File(file.getParentFile(), filename.replace("%20", " ")));
} else {
resource = new UrlResource(url);
}
}
} else {
resource = new UrlResource(url);
}
m_stats = new TimeTrackingMonitor();
send(importStartedEvent(resource, rescanExisting));
importModelFromResource(resource, rescanExisting, m_stats);
LOG.info("Finished Importing: {}", m_stats);
send(importSuccessEvent(m_stats, url, rescanExisting));
} catch (final Throwable t) {
final String msg = "Exception importing " + url;
LOG.error("Exception importing {} using rescanExisting={}", url, rescanExisting, t);
send(importFailedEvent((msg + ": " + t.getMessage()), url, rescanExisting));
}
}
use of org.springframework.core.io.UrlResource in project ignite by apache.
the class IgniteNode method loadConfiguration.
/**
* @param springCfgPath Spring configuration file path.
* @return Tuple with grid configuration and Spring application context.
* @throws Exception If failed.
*/
public static IgniteBiTuple<IgniteConfiguration, ? extends ApplicationContext> loadConfiguration(String springCfgPath) throws Exception {
URL url;
try {
url = new URL(springCfgPath);
} catch (MalformedURLException e) {
url = IgniteUtils.resolveIgniteUrl(springCfgPath);
if (url == null)
throw new IgniteCheckedException("Spring XML configuration path is invalid: " + springCfgPath + ". Note that this path should be either absolute or a relative local file system path, " + "relative to META-INF in classpath or valid URL to IGNITE_HOME.", e);
}
GenericApplicationContext springCtx;
try {
springCtx = new GenericApplicationContext();
new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url));
springCtx.refresh();
} catch (BeansException e) {
throw new Exception("Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e);
}
Map<String, IgniteConfiguration> cfgMap;
try {
cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class);
} catch (BeansException e) {
throw new Exception("Failed to instantiate bean [type=" + IgniteConfiguration.class + ", err=" + e.getMessage() + ']', e);
}
if (cfgMap == null || cfgMap.isEmpty())
throw new Exception("Failed to find ignite configuration in: " + url);
return new IgniteBiTuple<>(cfgMap.values().iterator().next(), springCtx);
}
use of org.springframework.core.io.UrlResource in project cxf by apache.
the class ControlledValidationXmlBeanDefinitionReader method fastInfosetLoadBeanDefinitions.
private int fastInfosetLoadBeanDefinitions(EncodedResource encodedResource) throws IOException, StaleFastinfosetException, ParserConfigurationException, XMLStreamException {
URL resUrl = encodedResource.getResource().getURL();
// We don't apply the optimization to them.
if (!resUrl.getPath().endsWith(".xml")) {
throw new StaleFastinfosetException();
}
String fixmlPath = resUrl.getPath().replaceFirst("\\.xml$", ".fixml");
String protocol = resUrl.getProtocol();
// beware of the relative URL rules for jar:, which are surprising.
if ("jar".equals(protocol)) {
fixmlPath = fixmlPath.replaceFirst("^.*!", "");
}
URL fixmlUrl = new URL(resUrl, fixmlPath);
// to ensure that we aren't using a stale Fastinfoset file.
if ("file".equals(protocol)) {
URLConnection resCon = null;
URLConnection fixCon = null;
resCon = resUrl.openConnection();
fixCon = fixmlUrl.openConnection();
if (resCon.getLastModified() > fixCon.getLastModified()) {
throw new StaleFastinfosetException();
}
}
Resource newResource = new UrlResource(fixmlUrl);
Document doc = TunedDocumentLoader.loadFastinfosetDocument(fixmlUrl);
if (doc == null) {
// something caused FastinfoSet to not be able to read the doc
throw new StaleFastinfosetException();
}
return registerBeanDefinitions(doc, newResource);
}
Aggregations