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:com.liferay.blade.samples.integration.test.utils.BladeCLIUtil.java

public static String getLatestBladeCLIJar() throws Exception {
    if (bladeJar == null) {
        URL url = new URL(System.getProperty("bladeURL"));
        File file = new File("blade.jar");

        FileUtils.copyURLToFile(url, file);

        Domain jar = Domain.domain(file);

        int bundleVersion = new Version(jar.getBundleVersion()).getMajor();

        if (bundleVersion != 3) {
            throw new Exception("Expecting bladejar with major version 3, found version: " + bundleVersion);
        }/*from ww  w .  j a va 2s  .c om*/

        bladeJar = file;
    }

    return bladeJar.getCanonicalPath();
}

From source file:com.boozallen.cognition.ingest.storm.bolt.starter.CsvUrlBolt.java

@Override
protected void execute(Tuple tuple, RecordCollector collector) {
    String metadataJson = new String((byte[]) tuple.getValue(0));
    File tempFile = null;/*from ww w  .  ja va 2  s  .  c  o m*/

    try {
        FileMetadata fileMetadata = FileMetadata.parseJson(metadataJson);

        String filename = fileMetadata.getFilename();
        String fileUrl = fileMetadata.getFileUrl();
        String fileType = fileMetadata.getFileType();

        if (isBlank(filename) || isBlank(fileUrl) || isBlank(fileType)) {
            LOGGER.error("Incomplete file metadata. Requires: filename, fileUrl and fileType. {}",
                    fileMetadata);
            throw new FailedException("Incomplete file metadata: " + fileMetadata);
        }

        tempFile = File.createTempFile("csv", null);
        FileUtils.copyURLToFile(new URL(fileUrl), tempFile);

        try (FileReader fileReader = new FileReader(tempFile);) {

            CsvLogRecordParser parser = new CsvLogRecordParser(csvParserConfig);
            parser.parse(fileReader, fileType, logRecord -> {
                logRecord.setValue("filename", filename);
                logRecord.setValue("fileType", fileType);
                logRecord.setValue("fileUrl", fileUrl);
                collector.emit(logRecord);
            });
        }
    } catch (IOException e) {
        LOGGER.error(metadataJson, e);
        throw new FailedException(e);
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}

From source file:jatoo.maven.plugin.set_license.SetLicenseFileMojo.java

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
    final Log log = getLog();

    final URL licenseResource;

    if (license == null) {

        if (licenseFile == null) {
            throw new MojoExecutionException("both parameters 'license' and 'licenseFile' are missing");
        }/*  w ww . ja  v a 2  s . c om*/

        try {
            licenseResource = licenseFile.toURI().toURL();
        } catch (MalformedURLException e) {
            throw new MojoExecutionException("error getting license file (" + licenseFile + ")", e);
        }
    }

    else {
        try {
            licenseResource = getClass().getResource("licenses/" + license + "/LICENSE").toURI().toURL();
        } catch (MalformedURLException | URISyntaxException e) {
            throw new MojoExecutionException("error getting license (" + license + ")", e);
        }
    }

    log.info("setting license file:");
    log.info("from: " + licenseResource);
    log.info("to:   " + new File(project.getBasedir(), "LICENSE").getAbsolutePath());

    try {
        FileUtils.copyURLToFile(licenseResource, new File(project.getBasedir(), "LICENSE"));
    } catch (IOException e) {
        throw new MojoExecutionException("error copying license file", e);
    }
}

From source file:net.fabricmc.loom.task.DownloadTask.java

@TaskAction
public void download() {
    try {/*from w  w w .j  a v  a 2 s .  co  m*/
        LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class);

        downloadMcJson(extension, getLogger());

        Gson gson = new Gson();
        Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class);

        if (!Constants.MINECRAFT_CLIENT_JAR.get(extension).exists() || !Checksum
                .equals(Constants.MINECRAFT_CLIENT_JAR.get(extension), version.downloads.get("client").sha1)) {
            this.getLogger().lifecycle(":downloading client");
            FileUtils.copyURLToFile(new URL(version.downloads.get("client").url),
                    Constants.MINECRAFT_CLIENT_JAR.get(extension));
        }

        if (!Constants.MINECRAFT_SERVER_JAR.get(extension).exists() || !Checksum
                .equals(Constants.MINECRAFT_SERVER_JAR.get(extension), version.downloads.get("server").sha1)) {
            this.getLogger().lifecycle(":downloading server");
            FileUtils.copyURLToFile(new URL(version.downloads.get("server").url),
                    Constants.MINECRAFT_SERVER_JAR.get(extension));
        }

        if (!Constants.POMF_DIR.get(extension).exists()) {
            Constants.POMF_DIR.get(extension).mkdir();
        }

        if (!Constants.MAPPINGS_ZIP.get(extension).exists() && extension.hasPomf()) {
            this.getLogger().lifecycle(":downloading mappings");
            try {
                FileUtils.copyURLToFile(
                        new URL("http://modmuss50.me:8080/job/FabricMC/job/pomf/job/" + extension.version + "/"
                                + extension.pomfVersion + "/artifact/build/libs/pomf-enigma-"
                                + extension.version + "." + extension.pomfVersion + ".zip"),
                        Constants.MAPPINGS_ZIP.get(extension));
            } catch (Exception e) {
                throw new RuntimeException("Failed to download mappings", e);
            }

        }
        if (!extension.hasPomf()) {
            if (Constants.MAPPINGS_DIR_LOCAL.get(extension).exists()) {
                if (Constants.MAPPINGS_TINY_GZ_LOCAL.get(extension).exists()
                        && Constants.MAPPINGS_ZIP_LOCAL.get(extension).exists()) {
                    this.getLogger().lifecycle(":using local mappings!");
                    extension.localMappings = true;

                    //We delete this to make sure they are always re extracted.
                    deleteIfExists(Constants.MAPPINGS_ZIP.get(extension));
                    deleteIfExists(Constants.MAPPINGS_TINY_GZ.get(extension));
                    deleteIfExists(Constants.MAPPINGS_TINY.get(extension));
                    deleteIfExists(Constants.MAPPINGS_DIR.get(extension));

                    Constants.MAPPINGS_TINY_GZ = Constants.MAPPINGS_TINY_GZ_LOCAL;
                    Constants.MAPPINGS_ZIP = Constants.MAPPINGS_ZIP_LOCAL;
                }
            }
        }

        if (!Constants.MAPPINGS_TINY.get(extension).exists() && extension.hasPomf()) {
            if (!Constants.MAPPINGS_TINY_GZ.get(extension).exists() && !extension.localMappings) {
                getLogger().lifecycle(":downloading tiny mappings");
                try {
                    FileUtils.copyURLToFile(
                            new URL("http://modmuss50.me:8080/job/FabricMC/job/pomf/job/" + extension.version
                                    + "/" + extension.pomfVersion + "/artifact/build/libs/pomf-tiny-"
                                    + extension.version + "." + extension.pomfVersion + ".gz"),
                            Constants.MAPPINGS_TINY_GZ.get(extension));
                } catch (Exception e) {
                    throw new RuntimeException("Failed to download mappings", e);
                }
            }
            GZIPInputStream gzipInputStream = new GZIPInputStream(
                    new FileInputStream(Constants.MAPPINGS_TINY_GZ.get(extension)));
            FileOutputStream fileOutputStream = new FileOutputStream(Constants.MAPPINGS_TINY.get(extension));
            int length;
            byte[] buffer = new byte[1024];
            while ((length = gzipInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }
            gzipInputStream.close();
            fileOutputStream.close();
        }

        DependencyHandler dependencyHandler = getProject().getDependencies();

        if (getProject().getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES)
                .getState() == Configuration.State.UNRESOLVED) {
            for (Version.Library library : version.libraries) {
                if (library.allowed() && library.getFile(extension) != null) {
                    // By default, they are all available on all sides
                    String configName = Constants.CONFIG_MC_DEPENDENCIES;
                    if (library.name.contains("java3d") || library.name.contains("paulscode")
                            || library.name.contains("lwjgl") || library.name.contains("twitch")
                            || library.name.contains("jinput") || library.name.contains("text2speech")
                            || library.name.contains("objc")) {
                        configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT;
                    }
                    dependencyHandler.add(configName, library.getArtifactName());
                }
            }
        }

        if (getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES)
                .getState() == Configuration.State.UNRESOLVED) {
            version.libraries.stream().filter(lib -> lib.natives != null)
                    .forEach(lib -> dependencyHandler.add(Constants.CONFIG_NATIVES, lib.getArtifactName()));
        }

        // Force add LaunchWrapper
        dependencyHandler.add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:launchwrapper:1.12");

        Version.AssetIndex assetIndex = version.assetIndex;

        File assets = new File(extension.getFabricUserCache(), "assets-" + extension.version);
        if (!assets.exists()) {
            assets.mkdirs();
        }

        File assetsInfo = new File(assets, "indexes" + File.separator + assetIndex.id + ".json");
        if (!assetsInfo.exists() || !Checksum.equals(assetsInfo, assetIndex.sha1)) {
            this.getLogger().lifecycle(":downloading asset index");
            FileUtils.copyURLToFile(new URL(assetIndex.url), assetsInfo);
        }

        ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName());
        progressLogger.start("Downloading assets...", "assets");
        AssetIndex index = new Gson().fromJson(new FileReader(assetsInfo), AssetIndex.class);
        Map<String, AssetObject> parent = index.getFileMap();
        final int totalSize = parent.size();
        int position = 0;
        this.getLogger().lifecycle(":downloading assets...");
        for (Map.Entry<String, AssetObject> entry : parent.entrySet()) {
            AssetObject object = entry.getValue();
            String sha1 = object.getHash();
            File file = new File(assets,
                    "objects" + File.separator + sha1.substring(0, 2) + File.separator + sha1);
            if (!file.exists() || !Checksum.equals(file, sha1)) {
                this.getLogger().debug(":downloading asset " + entry.getKey());
                FileUtils.copyURLToFile(new URL(Constants.RESOURCES_BASE + sha1.substring(0, 2) + "/" + sha1),
                        file);
            }
            String assetName = entry.getKey();
            int end = assetName.lastIndexOf("/") + 1;
            if (end > 0) {
                assetName = assetName.substring(end, assetName.length());
            }
            progressLogger.progress(assetName + " - " + position + "/" + totalSize + " ("
                    + (int) ((position / (double) totalSize) * 100) + "%) assets downloaded");
            position++;
        }
        progressLogger.completed();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.rptools.tokentool.AppSetup.java

public static void installDefaultOverlays() throws IOException {
    // Create the overlay directory
    File overlayDir = AppConstants.OVERLAY_DIR;
    overlayDir.mkdirs();/*from   www.j a  v a 2  s.  com*/

    // Copy default overlays from resources
    // https://dzone.com/articles/get-all-classes-within-package
    Reflections reflections = new Reflections(DEFAULT_OVERLAYS, new ResourcesScanner());
    Set<String> resourcePathSet = reflections.getResources(Pattern.compile(".*"));

    for (String resourcePath : resourcePathSet) {
        URL inputUrl = AppSetup.class.getClassLoader().getResource(resourcePath);
        String resourceName = resourcePath.substring(DEFAULT_OVERLAYS.length());

        try {
            log.info("Installing overlay: " + resourceName);
            FileUtils.copyURLToFile(inputUrl, new File(overlayDir, resourceName));
        } catch (IOException e) {
            log.error("ERROR writing " + inputUrl);
            log.error(e);
        }
    }
}

From source file:cz.cas.lib.proarc.common.imports.InputUtilsTest.java

private File createTestFile(String templatePath, File file) throws IOException {
    URL resource = TiffImporterTest.class.getResource(templatePath);
    assertNotNull(resource);//  www .ja  v a 2s  . c o  m
    FileUtils.copyURLToFile(resource, file);
    return file;
}

From source file:minecrunch_updater.Minecrunch_updater.java

private static void Update() throws MalformedURLException, IOException {

    // Get system properties
    String os = System.getProperty("os.name");
    String home = System.getProperty("user.home");
    String dir;/* w w w . ja  v a  2  s .c  o m*/
    String newlib = null;

    if (os.contains("Windows")) {
        dir = home + "\\.minecrunch\\";
        newlib = dir + "\\lib\\";
    } else {
        dir = home + "/.minecrunch/";
        newlib = dir + "/lib/";
    }

    // Check for .minecrunch directory if it doesn't exist create main directory, sub-directories, then download libraries and resources 
    URL lib1 = new URL("http://www.minecrunch.net/download/minecrunch_installer/lib/commons-io-2.4.jar");
    URL lib2 = new URL("http://www.minecrunch.net/download/minecrunch_installer/lib/json-simple-1.1.1.jar");
    URL lib3 = new URL("http://www.minecrunch.net/download/minecrunch_installer/lib/zip4j_1.3.2.jar");
    URL jar1 = new URL("http://www.minecrunch.net/download/minecrunch_installer/minecrunch_launcher.jar");

    File flib1 = new File(newlib + "commons-io-2.4.jar");
    File flib2 = new File(newlib + "json-simple-1.1.1.jar");
    File flib3 = new File(newlib + "zip4j_1.3.2.jar");
    File fjar1 = new File(dir + "minecrunch_launcher.jar");

    FileUtils.copyURLToFile(lib1, flib1);
    FileUtils.copyURLToFile(lib2, flib2);
    FileUtils.copyURLToFile(lib3, flib3);
    FileUtils.copyURLToFile(jar1, fjar1);

    Component frame = null;
    JOptionPane.showMessageDialog(frame, "Update complete.");

    try {
        Process proc = Runtime.getRuntime().exec("java -jar " + dir + "minecrunch_launcher.jar");
        System.exit(0);
    } catch (IOException ex) {
        Logger.getLogger(Minecrunch_updater.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.liferay.blade.cli.InitCommand.java

public void execute() throws Exception {
    final List<String> args = _options._arguments();

    String name = args.size() > 0 ? args.get(0) : null;

    File destDir = name != null ? new File(_blade.getBase(), name) : _blade.getBase();

    trace("Using destDir " + destDir);

    if (destDir.exists() && !destDir.isDirectory()) {
        addError(destDir.getAbsolutePath() + " is not a directory.");
        return;// ww w.  ja  va2 s. c om
    }

    if (destDir.exists()) {
        if (isPluginsSDK(destDir)) {
            if (!isPluginsSDK70(destDir)) {
                if (_options.upgrade()) {
                    trace("Found plugins-sdk 6.2, upgraded to 7.0, moving contents to new subdirectory "
                            + "and initing workspace.");

                    File sdk7zip = new File(_blade.getCacheDir(), _PLUGINS_SDK_7_ZIP);

                    if (!sdk7zip.exists()) {
                        FileUtils.copyURLToFile(new URL(_PLUGINS_SDK_7_URL), sdk7zip);
                    }

                    try {
                        Util.unzip(sdk7zip, destDir, "com.liferay.portal.plugins.sdk-7.0/");
                    } catch (Exception e) {
                        addError("Opening zip file error, " + "please delete zip file: " + sdk7zip.getPath());
                        return;
                    }

                    for (String fileName : _SDK_6_GA5_FILES) {
                        File file = new File(destDir, fileName);

                        if (file.exists()) {
                            file.delete();
                        }
                    }
                } else {
                    addError("Unable to run blade init in plugins sdk 6.2, please add -u (--upgrade)"
                            + " if you want to upgrade to 7.0");
                    return;
                }
            }

            trace("Found plugins-sdk, moving contents to new subdirectory " + "and initing workspace.");

            File pluginsSdkDir = new File(destDir, "plugins-sdk");

            moveContentsToDir(destDir, pluginsSdkDir);
        } else if (destDir.list().length > 0) {
            if (_options.force()) {
                trace("Files found, initing anyways.");
            } else {
                addError(destDir.getAbsolutePath() + " contains files, please move them before continuing "
                        + "or use -f (--force) option to init workspace " + "anyways.");
                return;
            }
        }
    }

    ProjectTemplatesArgs projectTemplatesArgs = new ProjectTemplatesArgs();

    if (name == null) {
        name = destDir.getName();
    }

    File destParentDir = destDir.getParentFile();

    projectTemplatesArgs.setDestinationDir(destParentDir);

    if (_options.force() || _options.upgrade()) {
        projectTemplatesArgs.setForce(true);
    }

    projectTemplatesArgs.setName(name);
    projectTemplatesArgs.setTemplate("workspace");

    new ProjectTemplates(projectTemplatesArgs);
}

From source file:jenkins.security.CustomClassFilterTest.java

@Test
public void dynamicLoad() throws Exception {
    assertBlacklisted("not yet enabled via plugin", ScriptException.class, true);
    assertBlacklisted("not yet disabled via plugin", TreeStringBuilder.class, false);
    File jpi = tmp.newFile("custom-class-filter.jpi");
    FileUtils.copyURLToFile(CustomClassFilterTest.class.getResource("/plugins/custom-class-filter.jpi"), jpi);
    r.jenkins.pluginManager.dynamicLoad(jpi);
    assertBlacklisted("enabled via plugin", ScriptException.class, false);
    assertBlacklisted("disabled via plugin", TreeStringBuilder.class, true);
}

From source file:me.ardacraft.blocksapi.update.Update.java

public void download(UpdateUrl u) {
    URL url = u.getUrl();/*  w  ww  .ja va  2s  .c  o  m*/
    if (url == null) {
        return;
    }
    try {
        File download = FileHelper.getOrCreateFile(FileHelper.getBlockPackFolder(), u.getFileName());
        FileUtils.copyURLToFile(url, download);
    } catch (IOException e) {
        e.printStackTrace();
    }
}