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.geopublishing.atlasViewer.dp.layer.DpLayerRaster_Reader.java

/**
 * This method is caching the geotools object, and can be uncached by
 * calling uncache()/*  www  .j  a v  a2 s  .  c om*/
 */
@Override
public AbstractGridCoverage2DReader getGeoObject() {

    try {

        if (gc == null) {

            /**
             * Can we define transparent colors here?
             */
            GeneralParameterValue[] readParams = null;

            URL url;
            url = getUrl();

            /**
             * GEOTiffReader fails for jar:file:....jar!bla.tif URLs :-(
             */
            if (url.getProtocol().startsWith("jar")
            // && url.getPath().startsWith("file")
            ) {
                // We copy the Tiff to the local temp dir
                File inTemp = new File(IOUtil.getTempDir(), getFilename());
                LOGGER.debug("Workaround for the GeoTiffReader bug, new source = " + inTemp);
                if (!inTemp.exists()) {
                    /**
                     * This is a work-around for GeoTiffReader problems for
                     * jar:// URLs We just all files to the local tempdir.
                     */
                    LOGGER.debug("Local copy does not exist, so we copy " + url + " to " + inTemp);
                    FileUtils.copyURLToFile(url, inTemp);

                    // Try to copy pending world files...
                    for (WORLD_POSTFIXES pf : GeoImportUtil.WORLD_POSTFIXES.values()) {
                        final URL src = IOUtil.changeUrlExt(url, pf.toString());

                        // clean = false, because we only clean
                        // filenames on import
                        IOUtil.copyURLNoException(src, IOUtil.getTempDir(), false);
                    }

                    // Copy optional .prj file to data directory
                    IOUtil.copyURLNoException(IOUtil.changeUrlExt(url, "prj"), IOUtil.getTempDir(), false);

                    // Copy optional .prj file to data directory
                    IOUtil.copyURLNoException(IOUtil.changeUrlExt(url, "tfw"), IOUtil.getTempDir(), false);

                    // Copy optional .sld file to data directory
                    IOUtil.copyURLNoException(IOUtil.changeUrlExt(url, "sld"), IOUtil.getTempDir(), false);

                }

                LOGGER.info("Changed the URL from " + url + " to " + inTemp);
                url = DataUtilities.fileToURL(inTemp);
            }

            final String filename = getFilename().toLowerCase();

            // ****************************************************************************
            // Check if the ending suggests a GeoTIFF
            // ****************************************************************************
            for (GEOTIFF_POSTFIXES ending : GeoImportUtil.GEOTIFF_POSTFIXES.values()) {
                if (filename.endsWith(ending.toString())) {

                    gc = new GeoTiffReader(url);
                    setType(DpEntryType.RASTER_GEOTIFF);
                }
            }

            // ****************************************************************************
            // Check if the ending suggests a Arc/Info ASCII Grid
            // ****************************************************************************
            for (ARCASCII_POSTFIXES ending : GeoImportUtil.ARCASCII_POSTFIXES.values()) {
                if (filename.endsWith(ending.toString())) {
                    gc = new ArcGridReader(url);
                    setType(DpEntryType.RASTER_ARCASCII);
                }
            }
            // ****************************************************************************
            // Check if the ending suggests normal image file with worldfile
            // ****************************************************************************
            for (IMAGE_POSTFIXES ending : GeoImportUtil.IMAGE_POSTFIXES.values()) {
                if (filename.endsWith(ending.toString())) {
                    gc = new WorldImageReader(url);
                    setType(DpEntryType.RASTER_IMAGEWORLD);
                }
            }

            if (gc == null)
                throw (new IllegalArgumentException("File doesn't seem to be a GeoTIFF nor a GIF"));

            // Create an Envelope that contains all information of the
            // raster
            // MS-01.sc
            // Envelope e = gc.getEnvelope();
            // envelope = new com.vividsolutions.jts.geom.Envelope(e
            // .getUpperCorner().getOrdinate(0), // X1
            // e.getLowerCorner().getOrdinate(0), // X2
            // e.getUpperCorner().getOrdinate(1), // Y1dddd
            // e.getLowerCorner().getOrdinate(1) // Y2
            // );

            // GridEnvelope e = gc.getOriginalGridRange();
            // envelope = new com.vividsolutions.jts.geom.Envelope(
            // e.getHigh(0), // X1
            // e.getLow(0), // X2
            // e.getHigh(1), // Y1
            // e.getLow(1) // Y2
            //
            // ServiceInfo info = gc.getInfo();
            //
            // Object source = gc.getSource();
            //
            // LOGGER.debug(info);
            // LOGGER.debug(source);
            // envelope = new com.vividsolutions.jts.geom.Envelope(
            // e.getHigh(0), // X1
            // e.getLow(0), // X2
            // e.getHigh(1), // Y1
            // e.getLow(1) // Y2
            // );

            double[] lower = gc.getOriginalEnvelope().getLowerCorner().getCoordinate();
            double[] upper = gc.getOriginalEnvelope().getUpperCorner().getCoordinate();
            envelope = new Envelope(lower[0], lower[1], upper[0], upper[1]);

            // LOGGER.info("Evaluated the following Enveloper for GeoTiffReader "
            // + envelope);

            crs = gc.getCrs();

            // Object object = gc.getProperties().get("GC_NODATA");
            // gc.getSampleDimension(0).getNoDataValues();
            // log.debug(object);
        }

        return gc;

    } catch (Exception e) {
        throw new RuntimeException("Exception while accessing the GeoObject for " + getId() + " " + this, e);
    }
}

From source file:org.geopublishing.atlasViewer.dp.layer.DpLayerVectorFeatureSource.java

@Override
public void exportWithGUI(Component owner) throws IOException {
    AtlasExportTask exportTask = new AtlasExportTask(owner, getTitle().toString()) {

        @Override/*from www. jav a 2 s  .co m*/
        protected Boolean doInBackground() throws Exception {

            setPrefix("Exporting ");

            try {
                exportDir = AVSwingUtil.selectExportDir(owner, getAtlasConfig());

                if (exportDir == null) {
                    return false;
                }

                URL url = AVSwingUtil.getUrl(DpLayerVectorFeatureSource.this, owner);
                final File file = new File(exportDir, getFilename());

                // ****************************************************************************
                // Copy main file and possibly throw an Exception
                // ****************************************************************************
                publish(file.getAbsolutePath());
                FileUtils.copyURLToFile(AVSwingUtil.getUrl(DpLayerVectorFeatureSource.this, owner), file);

                // Try to copy any pending files...
                for (SHP_POSTFIXES pf : GeoImportUtil.SHP_POSTFIXES.values()) {
                    final File changeFileExt = IOUtil.changeFileExt(file, pf.toString());
                    publish(changeFileExt.getAbsolutePath());
                    AtlasConfig.exportURLtoFileNoEx(IOUtil.changeUrlExt(url, pf.toString()), changeFileExt);
                }

                AtlasConfig.exportURLtoFileNoEx(IOUtil.changeUrlExt(url, "prj"),
                        IOUtil.changeFileExt(file, "prj"));

                AtlasConfig.exportURLtoFileNoEx(IOUtil.changeUrlExt(url, "sld"),
                        IOUtil.changeFileExt(file, "sld"));

                AtlasConfig.exportURLtoFileNoEx(IOUtil.changeUrlExt(url, "shp.xml"),
                        IOUtil.changeFileExt(file, "shp.xml"));

                // publish("done");

                success = true;
            } catch (Exception e) {
                done();
            }
            return success;
        }

    };

    exportTask.execute();
}

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

/**
 * Copies a well known style out to the data directory and adds a catalog entry for it.
 *///from  w  w  w  .  j av  a2 s .  c o m
void initializeStyle(Catalog catalog, String styleName, String sld) throws IOException {

    //copy the file out to the data directory if necessary
    if (resourceLoader.find("styles", sld) == null) {
        FileUtils.copyURLToFile(GeoServerLoader.class.getResource(sld),
                new File(resourceLoader.findOrCreateDirectory("styles"), sld));
    }

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

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

@Test
public void testModifyStyleChangeWorkspace2() throws Exception {
    testAddStyle();//from w  w  w.ja v a2  s  .  c om

    // copy an sld into place
    FileUtils.copyURLToFile(getClass().getResource("default_line.sld"),
            new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"));

    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").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(), "workspaces/gs/styles/foostyle.xml").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld").exists());
}

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

@Test
public void testModifyStyleChangeWorkspaceToGlobal() throws Exception {
    testAddStyleWithWorkspace();//from  ww w .  j  av  a 2 s . c o m

    // copy an sld into place
    FileUtils.copyURLToFile(getClass().getResource("default_line.sld"),
            new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld"));

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

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

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

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

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

@Test
public void testModifyStyleWithResourceChangeWorkspace() throws Exception {
    testAddStyle();//  ww  w  .  j  ava2  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"));

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

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

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());

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

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

@Test
public void testModifyStyleWithResourcesInParentDirChangeWorkspace() throws Exception {
    testAddStyle();/*from  w  w 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(getClass().getResource("burgParentReference.sld"),
            new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"));
    FileUtils.copyURLToFile(getClass().getResource("burg02.svg"),
            new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"));
    FileUtils.copyURLToFile(getClass().getResource("burg02.svg"),
            new File(testData.getDataDirectoryRoot(), "burg03.svg"));

    new File(testData.getDataDirectoryRoot(), "styles/burg03.svg").delete();

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "burg03.svg"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg03.svg"), not(fileExists()));

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

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

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "burg03.svg"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());

    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/burg03.svg"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/burg03.svg"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/burg02.svg"), fileExists());
}

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

@Test
public void testModifyStyleWithResourcesAbsoluteChangeWorkspace() throws Exception {
    testAddStyle();//from ww w.j  av a2  s. c om

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

    File styleFile = new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld");
    FileUtils.copyURLToFile(getClass().getResource("burgParentReference.sld"), styleFile);
    FileUtils.copyURLToFile(getClass().getResource("burg02.svg"),
            new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"));
    File target = new File(testData.getDataDirectoryRoot(), "burg03.svg");
    FileUtils.copyURLToFile(getClass().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(testData.getDataDirectoryRoot(), "styles/burg03.svg").delete();

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());
    assertThat(target, fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg03.svg"), not(fileExists()));

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

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

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), not(fileExists()));
    assertThat(target, fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());

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

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

@Test
public void testModifyStyleWithResourcesRemoteChangeWorkspace() throws Exception {
    testAddStyle();//from   ww w .  ja  va2 s  .  c  om

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

    File styleFile = new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld");
    FileUtils.copyURLToFile(getClass().getResource("burgRemoteReference.sld"), styleFile);
    FileUtils.copyURLToFile(getClass().getResource("burg02.svg"),
            new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"));

    new File(testData.getDataDirectoryRoot(), "styles/burg03.svg").delete();
    new File(testData.getDataDirectoryRoot(), "burg03.svg").delete();

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg03.svg"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "burg03.svg"), not(fileExists()));

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

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

    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "styles/burg02.svg"), fileExists());

    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.xml"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld"), fileExists());
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/burg03.svg"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/burg03.svg"), not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/example.com/burg03.svg"),
            not(fileExists()));
    assertThat(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/burg02.svg"), fileExists());
}

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

@Test
public void testModifyStyleChangeWorkspace2() throws Exception {
    testAddStyle();//from   ww  w  .  j  ava 2  s .c  om

    //copy an sld into place
    FileUtils.copyURLToFile(getClass().getResource("default_line.sld"),
            new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld"));

    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.xml").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "styles/foostyle.sld").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(), "workspaces/gs/styles/foostyle.xml").exists());
    assertTrue(new File(testData.getDataDirectoryRoot(), "workspaces/gs/styles/foostyle.sld").exists());
}