Search in sources :

Example 26 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class FusedForeignSourceRepositoryTest method simpleSnapshotTest.

@Test
public void simpleSnapshotTest() throws URISyntaxException {
    Requisition pendingReq = new Requisition("test");
    pendingReq.putNode(createNode("1"));
    m_pending.save(pendingReq);
    m_pending.flush();
    pendingReq = m_pending.getRequisition(pendingReq.getForeignSource());
    final File pendingSnapshot = RequisitionFileUtils.createSnapshot(m_pending, pendingReq.getForeignSource(), pendingReq.getDate());
    m_repository.importResourceRequisition(new FileSystemResource(pendingSnapshot));
    assertFalse(pendingSnapshot.exists());
    final URL pendingUrl = m_pending.getRequisitionURL(pendingReq.getForeignSource());
    final File pendingFile = new File(pendingUrl.toURI());
    assertFalse(pendingFile.exists());
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) URL(java.net.URL) Test(org.junit.Test)

Example 27 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class RequisitionFileUtils method getLatestPendingOrSnapshotRequisition.

public static Requisition getLatestPendingOrSnapshotRequisition(final ForeignSourceRepository foreignSourceRepository, final String foreignSource) {
    Requisition newest = foreignSourceRepository.getRequisition(foreignSource);
    for (final File snapshotFile : findSnapshots(foreignSourceRepository, foreignSource)) {
        if (newest == null || isNewer(snapshotFile, newest.getDate())) {
            newest = JaxbUtils.unmarshal(Requisition.class, snapshotFile);
            newest.setResource(new FileSystemResource(snapshotFile));
        }
    }
    return newest;
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition)

Example 28 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class MicroblogClient method main.

public static void main(final String[] args) throws Exception {
    System.out.println("=== Configure Microblog Authentication ===");
    System.out.println("");
    final String configPath = System.getProperty("opennms.home") + File.separator + "etc" + File.separator + "microblog-configuration.xml";
    final File configFile = new File(configPath);
    if (!configFile.exists())
        usage();
    String profile = null;
    if (args.length > 0) {
        profile = args[0];
    }
    try {
        final MicroblogClient client = new MicroblogClient(new FileSystemResource(configFile));
        int step = 1;
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        if (!client.hasOAuth(profile)) {
            System.out.println("This utility is for connecting OpenNMS notifications with Twitter, or any other");
            System.out.println("microblog site which uses OAuth.  These examples will use Twitter URLs, but you");
            System.out.println("should be able to do the equivalent with any Twitter-compatible site like identi.ca.");
            System.out.println("");
            System.out.println("If you wish to use a username and password instead, just enter them into your");
            System.out.println("microblog-configuration.xml file.");
            System.out.println("");
            System.out.println("Step " + step++ + ".  Go to https://twitter.com/oauth_clients/new and create a Twitter");
            System.out.println("\"application\" for your OpenNMS install.  If you have already created an application,");
            System.out.println("you can get the info you need for the next steps at https://dev.twitter.com/apps/");
            System.out.println("instead.  Make sure you go to 'Keys and Access Tokens' and configure the 'Access Level'");
            System.out.println("to allow 'Read, Write and Access direct messages'.");
            System.out.println("");
            System.out.print("Step " + step++ + ".  Enter your consumer key: ");
            final String consumerKey = br.readLine();
            System.out.println("");
            System.out.print("Step " + step++ + ".  Enter your consumer secret: ");
            final String consumerSecret = br.readLine();
            System.out.println("");
            client.getProfile(profile).setOauthConsumerKey(consumerKey);
            client.getProfile(profile).setOauthConsumerSecret(consumerSecret);
            if (!client.hasOAuth(profile)) {
                System.err.println("Something went wrong, either your consumer key or secret were empty.  Bailing.");
                System.exit(1);
            }
        }
        final MicroblogAuthorization auth = client.requestAuthorization(profile);
        System.out.println("Step " + step++ + ".  Go to " + auth.getUrl());
        System.out.println("in your browser and authorize OpenNMS.");
        System.out.println("");
        System.out.print("Step " + step++ + ".  Type your PIN from the web page, or hit ENTER if there is no PIN: ");
        final String pin = br.readLine();
        AccessToken token = null;
        if (pin == null || pin.length() == 0 || !pin.matches("^[0-9]*$")) {
            System.err.println("No pin, or pin input was not numeric.  Trying pinless auth.");
            token = auth.retrieveToken();
        } else {
            token = auth.retrieveToken(pin);
        }
        System.out.println("");
        System.out.println("Step " + step + ".  There is no step " + step++ + ".");
        System.out.println("");
        System.out.print("Saving tokens to " + configPath + "... ");
        client.saveAccessToken(profile, token);
        System.out.println("done");
        System.out.println("");
    } catch (final Exception e) {
        System.err.println("Failed to get access token.");
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new RuntimeException(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) AccessToken(twitter4j.auth.AccessToken) BufferedReader(java.io.BufferedReader) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) MicroblogAuthorizationException(org.opennms.netmgt.notifd.MicroblogAuthorization.MicroblogAuthorizationException) IOException(java.io.IOException)

Example 29 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class SyslogConfigDaoTest method testFiltersAndReload.

/**
     * Test filters and reload.
     *
     * @throws Exception the exception
     */
@Test
public void testFiltersAndReload() throws Exception {
    File configFile = new File("target/syslog-northbounder-test.xml");
    FileWriter writer = new FileWriter(configFile);
    writer.write(xmlWithFilters);
    writer.close();
    Resource resource = new FileSystemResource(configFile);
    SyslogNorthbounderConfigDao dao = new SyslogNorthbounderConfigDao();
    dao.setConfigResource(resource);
    dao.afterPropertiesSet();
    assertNotNull(dao.getConfig());
    SyslogDestination dst = dao.getConfig().getSyslogDestination("test-host");
    assertNotNull(dst);
    assertEquals(2, dst.getFilters().size());
    assertEquals(true, dst.getFilters().get(0).isEnabled());
    assertEquals(false, dst.getFilters().get(1).isEnabled());
    writer = new FileWriter(configFile);
    writer.write(xmlNoUeis);
    writer.close();
    dao.reload();
    dst = dao.getConfig().getSyslogDestination("test-host");
    assertNotNull(dst);
    assertTrue(dst.getFilters().isEmpty());
    configFile.delete();
}
Also used : FileWriter(java.io.FileWriter) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Test(org.junit.Test)

Example 30 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class PollOutagesConfigManagerTest method setUp.

@Before
public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    String xml = "<?xml version=\"1.0\"?>\n" + "<outages>\n" + "   <outage name=\"one\" type=\"weekly\">\n" + "       <time day=\"sunday\" begins=\"12:30:00\" ends=\"12:45:00\"/>\n" + "       <time day=\"sunday\" begins=\"13:30:00\" ends=\"14:45:00\"/>\n" + "       <time day=\"monday\" begins=\"13:30:00\" ends=\"14:45:00\"/>\n" + "       <time day=\"tuesday\" begins=\"13:00:00\" ends=\"14:45:00\"/>\n" + "       <interface address=\"192.168.0.1\"/>\n" + "       <interface address=\"192.168.0.36\"/>\n" + "       <interface address=\"192.168.0.38\"/>\n" + "   </outage>\n" + "\n" + "   <outage name=\"two\" type=\"monthly\">\n" + "       <time day=\"1\" begins=\"23:30:00\" ends=\"23:45:00\"/>\n" + "       <time day=\"15\" begins=\"21:30:00\" ends=\"21:45:00\"/>\n" + "       <time day=\"15\" begins=\"23:30:00\" ends=\"23:45:00\"/>\n" + "       <interface address=\"192.168.100.254\"/>\n" + "       <interface address=\"192.168.101.254\"/>\n" + "       <interface address=\"192.168.102.254\"/>\n" + "       <interface address=\"192.168.103.254\"/>\n" + "       <interface address=\"192.168.104.254\"/>\n" + "       <interface address=\"192.168.105.254\"/>\n" + "       <interface address=\"192.168.106.254\"/>\n" + "       <interface address=\"192.168.107.254\"/>\n" + "   </outage>\n" + "\n" + "   <outage name=\"three\" type=\"specific\">\n" + "       <time begins=\"21-Feb-2005 05:30:00\" ends=\"21-Feb-2005 15:00:00\"/>\n" + "       <interface address=\"192.168.0.1\"/>\n" + "   </outage>\n";
    StringBuffer sb = new StringBuffer(xml);
    // Fake a really big poll-outages.xml
    for (int i = 1; i <= 10000; i++) {
        sb.append("<outage name=\"o" + i + "\" type=\"specific\">\n");
        sb.append("<time begins=\"21-Feb-2005 05:30:00\" ends=\"21-Feb-2005 15:00:00\"/>\n");
        sb.append("<node id=\"" + i + "\"/>");
        sb.append("</outage>");
    }
    sb.append("</outages>\n");
    m_manager = new PollOutagesConfigManager() {

        @Override
        public void update() throws IOException {
        }
    };
    FileWriter w = new FileWriter(m_configFile);
    w.write(sb.toString());
    w.close();
    m_manager.setConfigResource(new FileSystemResource(m_configFile));
    m_manager.afterPropertiesSet();
    assertEquals(10003, m_manager.getOutages().size());
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) FileSystemResource(org.springframework.core.io.FileSystemResource) Before(org.junit.Before)

Aggregations

FileSystemResource (org.springframework.core.io.FileSystemResource)128 File (java.io.File)66 Test (org.junit.Test)41 Resource (org.springframework.core.io.Resource)35 Before (org.junit.Before)21 ClassPathResource (org.springframework.core.io.ClassPathResource)13 IOException (java.io.IOException)11 FileWriter (java.io.FileWriter)10 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)8 FileOutputStream (java.io.FileOutputStream)7 HashMap (java.util.HashMap)7 Properties (java.util.Properties)7 InputStreamResource (org.springframework.core.io.InputStreamResource)7 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6 DefaultDataCollectionConfigDao (org.opennms.netmgt.config.DefaultDataCollectionConfigDao)6 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)5 FileReader (java.io.FileReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Ehcache (net.sf.ehcache.Ehcache)4