Search in sources :

Example 21 with UrlResource

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();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) InputStream(java.io.InputStream) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Example 22 with UrlResource

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();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) InputStream(java.io.InputStream) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Example 23 with UrlResource

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));
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) URL(java.net.URL)

Example 24 with UrlResource

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);
}
Also used : MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) UrlResource(org.springframework.core.io.UrlResource) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BeansException(org.springframework.beans.BeansException) BeansException(org.springframework.beans.BeansException)

Example 25 with UrlResource

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);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) EncodedResource(org.springframework.core.io.support.EncodedResource) Resource(org.springframework.core.io.Resource) Document(org.w3c.dom.Document) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)68 Test (org.junit.Test)36 Resource (org.springframework.core.io.Resource)31 URL (java.net.URL)25 ArrayList (java.util.ArrayList)10 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 ClassPathResource (org.springframework.core.io.ClassPathResource)10 GenericBean (org.springframework.tests.sample.beans.GenericBean)9 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)8 InputStream (java.io.InputStream)6 FileSystemResource (org.springframework.core.io.FileSystemResource)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 JUnitDNSServer (org.opennms.core.test.dns.annotations.JUnitDNSServer)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 JAXBContext (javax.xml.bind.JAXBContext)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 BeansException (org.springframework.beans.BeansException)4