Example usage for org.apache.commons.io FileUtils copyURLToFile

List of usage examples for org.apache.commons.io FileUtils copyURLToFile

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyURLToFile.

Prototype

public static void copyURLToFile(URL source, File destination) throws IOException 

Source Link

Document

Copies bytes from the URL source to a file destination.

Usage

From source file:org.geoserver.config.GeoServerPersisterTest.java

@Test
public void testModifyStyleWithResourceChangeWorkspace() throws Exception {
    testAddStyle();/*from  w ww .  j  a  v a  2 s.  c  om*/

    //copy an sld with its resource into place
    FileUtils.copyURLToFile(getClass().getResource("burg.sld"),
            new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"));
    FileUtils.copyURLToFile(getClass().getResource("burg02.svg"),
            new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"));

    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg").exists());

    StyleInfo s = catalog.getStyleByName("foostyle");
    s.setWorkspace(catalog.getDefaultWorkspace());
    catalog.save(s);

    assertFalse(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists());
    assertFalse(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg").exists());

    assertTrue(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/burg02.svg").exists());
}

From source file:org.geoserver.config.hibernate.HibGeoServerLoader.java

/**
 * Copies a well known style out to the data directory and adds a catalog entry for it.
 *///www  . j  ava  2s.  co  m
private void initializeStyle(Catalog catalog, String styleName, String sld) {

    // copy the file out to the data directory if necessary
    try {
        if (resourceLoader.find("styles", sld) == null) {
            FileUtils.copyURLToFile(getClass().getResource(sld), new File(resourceLoader.find("styles"), sld));
        }
    } catch (Throwable ex) {
        LOGGER.log(Level.SEVERE, "Could not import SLD " + styleName, ex);
        return;
    }

    // create a style for it
    StyleInfo s = catalog.getFactory().createStyle();
    s.setName(styleName);
    s.setFilename(sld);
    catalog.add(s);
}

From source file:org.geoserver.jdbcconfig.catalog.JDBGeoserverResourcePersisterTest.java

@Test
public void testModifyStyleChangeWorkspace() throws Exception {
    addStyle();//from w w  w  . ja  v  a2  s.co m

    // copy an sld into place
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("default_line.sld"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"));

    assertTrue(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld").exists());

    StyleInfo s = catalog.getStyleByName("foostyle");
    s.setWorkspace(catalog.getDefaultWorkspace());
    catalog.save(s);

    assertFalse(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld").exists());
    assertTrue(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld")
            .exists());
}

From source file:org.geoserver.jdbcconfig.catalog.JDBGeoserverResourcePersisterTest.java

@Test
public void testModifyStyleChangeWorkspaceToGlobal() throws Exception {
    addStyleWithWorkspace();/*w w  w .  j  a  va  2  s.c om*/

    // copy an sld into place
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("default_line.sld"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld"));

    assertTrue(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld")
            .exists());

    StyleInfo s = catalog.getStyleByName("foostyle");
    s.setWorkspace(null);
    catalog.save(s);

    assertTrue(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld").exists());
    assertFalse(
            new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld")
                    .exists());
}

From source file:org.geoserver.jdbcconfig.catalog.JDBGeoserverResourcePersisterTest.java

@Test
public void testModifyStyleWithResourceChangeWorkspace() throws Exception {
    addStyle();/*from  w w w  .j  ava2 s .  c o m*/

    // copy an sld with its resource into place
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg.sld"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"));
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg02.svg"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"));

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());

    StyleInfo s = catalog.getStyleByName("foostyle");
    s.setWorkspace(catalog.getDefaultWorkspace());
    catalog.save(s);

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());

    assertThat(
            new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg02.svg"),
            fileExists());
}

From source file:org.geoserver.jdbcconfig.catalog.JDBGeoserverResourcePersisterTest.java

@Test
public void testModifyStyleWithResourcesInParentDirChangeWorkspace() throws Exception {
    addStyle();/*from ww w . j a v  a  2 s . c  o m*/

    // If a relative URI with parent references is used, give up on trying to copy the resource.
    // The style will break but copying arbitrary files from parent directories around is a bad
    // idea. Handle the rest normally. KS

    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burgParentReference.sld"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"));
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg02.svg"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"));
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg02.svg"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "burg03.svg"));

    new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg03.svg").delete();

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "burg03.svg"), fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg03.svg"),
            not(fileExists()));

    StyleInfo s = catalog.getStyleByName("foostyle");

    s.setWorkspace(catalog.getDefaultWorkspace());
    catalog.save(s);

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "burg03.svg"), fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());

    assertThat(
            new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg02.svg"),
            fileExists());
}

From source file:org.geoserver.jdbcconfig.catalog.JDBGeoserverResourcePersisterTest.java

@Test
public void testModifyStyleWithResourcesAbsoluteChangeWorkspace() throws Exception {
    addStyle();/*from w ww. j  a v a  2s.  com*/

    // If an absolute uri is used, don't copy it anywhere. The reference is absolute
    // so it will still work.

    File styleFile = new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld");
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burgParentReference.sld"), styleFile);
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg02.svg"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"));
    File target = new File(testSupport.getResourceLoader().getBaseDirectory(), "burg03.svg");
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg02.svg"), target);

    // Insert an absolute path to test
    String content = new String(Files.readAllBytes(styleFile.toPath()), StandardCharsets.UTF_8);
    content = content.replaceAll("./burg03.svg", "http://doesnotexist.example.org/burg03.svg");
    Files.write(styleFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
    new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg03.svg").delete();

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());
    assertThat(target, fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg03.svg"),
            not(fileExists()));

    StyleInfo s = catalog.getStyleByName("foostyle");

    s.setWorkspace(catalog.getDefaultWorkspace());
    catalog.save(s);

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            not(fileExists()));
    assertThat(target, fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());

    assertThat(
            new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs" + target.getPath()),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(),
            "workspaces/gs/styles" + target.getPath()), not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg02.svg"),
            fileExists());
}

From source file:org.geoserver.jdbcconfig.catalog.JDBGeoserverResourcePersisterTest.java

@Test
public void testModifyStyleWithResourcesRemoteChangeWorkspace() throws Exception {
    addStyle();// w w  w  .  j ava2s  . c  o  m

    // If an absolute uri is used, don't copy it anywhere. The reference is absolute
    // so it will still work.

    File styleFile = new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld");
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burgRemoteReference.sld"), styleFile);
    FileUtils.copyURLToFile(GeoServerPersistersTest.class.getResource("burg02.svg"),
            new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"));

    new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg03.svg").delete();
    new File(testSupport.getResourceLoader().getBaseDirectory(), "burg03.svg").delete();

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "burg03.svg"), not(fileExists()));

    StyleInfo s = catalog.getStyleByName("foostyle");

    s.setWorkspace(catalog.getDefaultWorkspace());
    catalog.save(s);

    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/foostyle.sld"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "styles/burg02.svg"), fileExists());

    assertThat(
            new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/foostyle.sld"),
            fileExists());
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(),
            "workspaces/gs/example.com/burg03.svg"), not(fileExists()));
    assertThat(new File(testSupport.getResourceLoader().getBaseDirectory(), "workspaces/gs/styles/burg02.svg"),
            fileExists());
}

From source file:org.geoserver.logging.LoggingStartupContextListenerTest.java

@Test
public void testLogLocationFromServletContext() throws Exception {
    File tmp = File.createTempFile("log", "tmp", new File("target"));
    tmp.delete();/*from   ww  w  .j a v  a2s  . com*/
    tmp.mkdirs();

    File logs = new File(tmp, "logs");
    assertTrue(logs.mkdirs());

    FileUtils.copyURLToFile(getClass().getResource("logging.xml"), new File(tmp, "logging.xml"));

    MockServletContext context = new MockServletContext();
    context.setInitParameter("GEOSERVER_DATA_DIR", tmp.getPath());
    context.setInitParameter("GEOSERVER_LOG_LOCATION", new File(tmp, "foo.log").getAbsolutePath());

    Logger logger = Logger.getRootLogger();
    assertNull("Expected geoserverlogfile to be null.  But was: " + logger.getAppender("geoserverlogfile"),
            logger.getAppender("geoserverlogfile"));

    String rel = System.getProperty(LoggingUtils.RELINQUISH_LOG4J_CONTROL);
    System.setProperty(LoggingUtils.RELINQUISH_LOG4J_CONTROL, "false");
    try {
        new LoggingStartupContextListener().contextInitialized(new ServletContextEvent(context));
    } finally {
        System.setProperty(LoggingUtils.RELINQUISH_LOG4J_CONTROL, "rel");
    }

    Appender appender = logger.getAppender("geoserverlogfile");
    assertNotNull(appender);
    assertTrue(appender instanceof FileAppender);

    assertEquals(new File(tmp, "foo.log").getCanonicalPath(), ((FileAppender) appender).getFile());
}

From source file:org.geoserver.python.datastore.PythonDataStoreAdapterTest.java

@BeforeClass
public static void setUpData() throws Exception {
    GeoServerResourceLoader loader = new GeoServerResourceLoader(new File("target"));
    py = new Python(loader);

    File f = new File("target", "foo_process.py");
    FileUtils.copyURLToFile(PythonDataStoreAdapterTest.class.getResource("foo_datastore.py"), f);
    adapter = new PythonDataStoreAdapter(f, py);
}