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.jboss.tools.project.examples.internal.offline.ExtractScriptJob.java

@Override
protected IStatus run(IProgressMonitor monitor) {
    File offlineScript = OfflineUtil.getGoOfflineScript();
    File grapeConfigXml = OfflineUtil.getGrapeConfigXml();
    //No need to overwrite existing script if not in dev mode
    boolean devMode = offlineScript.getName().contains(".qualifier");//$NON-NLS-1$
    URL sourceUrl = null;//from  www .  j ava2s.com
    String base = "platform:/plugin/org.jboss.tools.project.examples/offline/";//$NON-NLS-1$
    if (devMode || !offlineScript.exists()) {
        try {
            URL scriptUrl = new URL(base + "go_offline.groovy"); //$NON-NLS-1$
            sourceUrl = FileLocator.resolve(scriptUrl);
            FileUtils.copyURLToFile(sourceUrl, offlineScript);
        } catch (Exception e) {
            Status error = new Status(IStatus.ERROR, ProjectExamplesActivator.PLUGIN_ID,
                    "Impossible to copy the go_offline script", e); //$NON-NLS-1$
            return error;
        }
    }
    if (devMode || !grapeConfigXml.exists()) {
        try {
            URL grapeConfigUrl = new URL(base + "jbossToolsGrapeConfig.xml"); //$NON-NLS-1$
            sourceUrl = FileLocator.resolve(grapeConfigUrl);
            FileUtils.copyURLToFile(sourceUrl, grapeConfigXml);
        } catch (Exception e) {
            Status error = new Status(IStatus.ERROR, ProjectExamplesActivator.PLUGIN_ID,
                    "Impossible to copy jbossToolsGrapeConfig.xml", e); //$NON-NLS-1$
            return error;
        }
    }
    return Status.OK_STATUS;
}

From source file:org.jenkinsci.modules.optpluginhelper.PluginHelper.java

/**
 * List all the optional plugins (while populating the staging area with any new ones we discover).
 *
 * @return the list of optional plugins available from all the current defined {@link PluginSource} extensions.
 *///from   w ww .  j ava 2 s.  com
private List<File> listPlugins() {
    // TODO figure out what to do if two sources provide different versions of the same plugin, currently undefined
    List<File> result = new ArrayList<File>();
    final Jenkins jenkins = Jenkins.getInstance();
    if (jenkins == null) {
        return result;
    }
    File baseDir = new File(jenkins.root, OPTIONAL_PLUGIN_DIR);
    if (baseDir.exists() && !baseDir.isDirectory()) {
        LOGGER.log(Level.SEVERE, "Optional plugin working directory {0} exists and is not a directory",
                baseDir);
        return result;
    }
    if (!baseDir.isDirectory()) {
        if (!baseDir.mkdirs()) {
            LOGGER.log(Level.SEVERE, "Could not create optional plugin working directory {0}", baseDir);
            return result;
        }
    }
    for (URL resource : PluginSource.allPlugins()) {
        try {
            final String externalForm = resource.toExternalForm();
            ExtractedPluginMetadata metadata = extractedPluginMetadataMap.get(externalForm);
            if (metadata != null) {
                File archive = new File(baseDir, metadata.shortName + ".jpi");
                if (archive.isFile() && archive.length() == metadata.length
                        && Util.getDigestOf(archive).equals(metadata.digest)) {
                    result.add(archive);
                    continue;
                }
            }
            final URLConnection connection = resource.openConnection();
            long lastModified = connection.getLastModified();
            long size = connection.getContentLength();
            String path = resource.getPath();
            String fileName = FilenameUtils.getBaseName(path);
            boolean nameCheck = false;
            if (StringUtils.isBlank(fileName)) {
                nameCheck = true;
                fileName = Util.getDigestOf(resource.toString());
            }
            File file = new File(baseDir, fileName + ".jpi");
            if (file.isFile() && (file.lastModified() == lastModified || lastModified == 0)
                    && file.length() == size) {
                final String fileDigest = Util.getDigestOf(file);
                final String resourceDigest;
                final InputStream stream = connection.getInputStream();
                try {
                    resourceDigest = Util.getDigestOf(stream);
                } finally {
                    IOUtils.closeQuietly(stream);
                }
                if (fileDigest.equals(resourceDigest)) {
                    result.add(file);
                    extractedPluginMetadataMap.put(externalForm, new ExtractedPluginMetadata(file));
                    continue;
                }
            }
            FileUtils.copyURLToFile(resource, file);
            if (nameCheck) {
                final String shortName = jenkins.getPluginManager().getPluginStrategy().getShortName(file);
                if (!fileName.equals(shortName)) {
                    File newFile = new File(baseDir, shortName + ".jpi");
                    if (!newFile.isFile() || !Util.getDigestOf(newFile).equals(Util.getDigestOf(file))) {
                        FileUtils.moveFile(file, newFile);
                    }
                    file = newFile;
                }
            }
            if (lastModified != 0) {
                if (!file.setLastModified(lastModified)) {
                    LOGGER.log(Level.FINE, "Couldn't set last modified on {0}", file);
                }
            }
            result.add(file);
            extractedPluginMetadataMap.put(externalForm, new ExtractedPluginMetadata(file));
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, String.format("Could not process optional plugin from %s", resource), e);
        }
    }

    LOGGER.log(Level.FINE, "List of plugins: " + result);
    return result;
}

From source file:org.jenkinsci.sshd.SshdTest.java

/**
 * Get plaintext Private Key File/*from  www.  j  a  v a2s  .  c o  m*/
 */
private File getPrivateKey() {
    if (privateKey == null) {
        try {
            privateKey = File.createTempFile("ssh", "key");
            privateKey.deleteOnExit();
            FileUtils.copyURLToFile(SshdTest.class.getResource("/sshd/unsafe"), privateKey);
            Files.setPosixFilePermissions(privateKey.toPath(), EnumSet.of(OWNER_READ));
        } catch (IOException e) {
            throw new RuntimeException(
                    "Not able to get the plaintext SSH key file. Missing file, wrong file permissions?!");
        }
    }
    return privateKey;
}

From source file:org.jenkinsci.sshd.SshdTest.java

/**
 * Get encrypted Private Key File//w  w  w. j  ava 2 s.c om
 */
private File getEncryptedPrivateKey() {
    if (privateKeyEnc == null) {
        try {
            privateKeyEnc = File.createTempFile("ssh_enc", "key");
            privateKeyEnc.deleteOnExit();
            FileUtils.copyURLToFile(SshdTest.class.getResource("/sshd/unsafe_enc_key"), privateKeyEnc);
            Files.setPosixFilePermissions(privateKeyEnc.toPath(), EnumSet.of(OWNER_READ));
        } catch (IOException e) {
            throw new RuntimeException(
                    "Not able to get the encrypted SSH key file. Missing file, wrong file permissions?!");
        }
    }
    return privateKeyEnc;
}

From source file:org.jflicks.update.system.SystemUpdate.java

/**
 * {@inheritDoc}/*w ww. j a va  2s. c  o  m*/
 */
public boolean update(UpdateState us) {

    boolean result = false;

    if (us != null) {

        result = true;
        if (us.getUpdateCount() > 0) {

            File[] array = us.getFiles();
            File wdir = us.getWorkingDirectory();
            String dir = us.getBundleDirectory();
            String sourceURL = us.getSourceURL();
            if ((array != null) && (wdir != null) && (sourceURL != null) && (dir != null)) {

                File bdir = new File(dir);
                for (int i = 0; i < array.length; i++) {

                    String fname = array[i].getName();
                    File wfile = new File(wdir, fname);
                    try {

                        URL url = new URL(sourceURL + fname);
                        FileUtils.copyURLToFile(url, wfile);

                    } catch (IOException ex) {

                        LogUtil.log(LogUtil.WARNING, ex.getMessage());
                        result = false;
                        break;
                    }
                }

                if (result) {

                    // Ok we have things downloaded.
                    File[] bundles = wdir.listFiles(new BundleFilter());
                    if ((bundles != null) && (bundles.length > 0)) {

                        for (int i = 0; i < bundles.length; i++) {

                            try {

                                FileUtils.copyFileToDirectory(bundles[i], bdir, false);

                            } catch (IOException ex) {

                                LogUtil.log(LogUtil.WARNING, ex.getMessage());
                                result = false;
                            }
                        }
                    }
                }
            }
        }
    }

    return (result);
}

From source file:org.jnotary.service.test.RestTest.java

@SuppressWarnings("static-access")
public static void initExternalFiles() throws IOException {
    URL keyStorePath = RestTest.class.getClassLoader().getSystemResource("Notary1.p12");
    System.out.println("Copied " + keyStorePath);
    FileUtils.copyURLToFile(keyStorePath, tmpP12);

    URL clientStorePath = RestTest.class.getClassLoader().getSystemResource("Client1.p12");
    System.out.println("Copied " + clientStorePath);
    FileUtils.copyURLToFile(clientStorePath, clientP12);

    URL clientCertPath = RestTest.class.getClassLoader().getSystemResource("Client1.cer");
    System.out.println("Copied " + clientCertPath);
    FileUtils.copyURLToFile(clientCertPath, clientCer);

    URL signedFile = RestTest.class.getClassLoader().getSystemResource("test.p7s");
    System.out.println("Copied " + signedFile);
    FileUtils.copyURLToFile(signedFile, testP7S);

    URL trustedRootStorePath = RestTest.class.getClassLoader().getSystemResource("trustedroots.jks");
    System.out.println("Copied " + trustedRootStorePath);
    FileUtils.copyURLToFile(trustedRootStorePath, tmpJKS);
}

From source file:org.jsweet.jaxrs.swagger.Main.java

public static void main(String... args) {
    try {//from  ww  w .  j  a  va 2 s  . c o  m
        String output = args[0];
        if (null == output) {
            output = "swagger.json";
        }

        Server server = new Server(0);
        JAXRSApplication application = new JAXRSApplication();
        ServletContextHandler context = new ServletContextHandler();
        context.addServlet(
                new ServletHolder(new ServletContainer(new ResourceConfig(application.getClasses()))), "/*");
        server.setHandler(context);
        server.start();
        FileUtils
                .copyURLToFile(
                        new URL("http", "localhost",
                                ((ServerConnector) server.getConnectors()[0]).getLocalPort(), "/swagger.json"),
                        new File(output));
        server.stop();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jvnet.hudson.test.HudsonTestCase.java

/**
 * If this test harness is launched for a Hudson plugin, locate the <tt>target/test-classes/the.hpl</tt>
 * and add a recipe to install that to the new Hudson.
 *
 * <p>/*from w  ww . j a  v  a 2s  .co m*/
 * This file is created by <tt>maven-hpi-plugin</tt> at the testCompile phase when the current
 * packaging is <tt>hpi</tt>.
 */
protected void recipeLoadCurrentPlugin() throws Exception {
    Enumeration<URL> e = getClass().getClassLoader().getResources("the.hpl");
    if (!e.hasMoreElements())
        return; // nope

    final URL hpl = e.nextElement();

    if (e.hasMoreElements()) {
        // this happens if one plugin produces a test jar and another plugin depends on it.
        // I can't think of a good way to make this work, so for now, just detect that and report an error.
        URL hpl2 = e.nextElement();
        throw new Error("We have both " + hpl + " and " + hpl2);
    }

    recipes.add(new Runner() {
        @Override
        public void decorateHome(HudsonTestCase testCase, File home) throws Exception {
            // make the plugin itself available
            Manifest m = new Manifest(hpl.openStream());
            String shortName = m.getMainAttributes().getValue("Short-Name");
            if (shortName == null)
                throw new Error(hpl + " doesn't have the Short-Name attribute");
            FileUtils.copyURLToFile(hpl, new File(home, "plugins/" + shortName + ".hpl"));

            // make dependency plugins available
            // TODO: probably better to read POM, but where to read from?
            // TODO: this doesn't handle transitive dependencies

            // Tom: plugins are now searched on the classpath first. They should be available on
            // the compile or test classpath. As a backup, we do a best-effort lookup in the Maven repository
            // For transitive dependencies, we could evaluate Plugin-Dependencies transitively. 

            String dependencies = m.getMainAttributes().getValue("Plugin-Dependencies");
            if (dependencies != null) {
                MavenEmbedder embedder = new MavenEmbedder(null);
                embedder.setClassLoader(getClass().getClassLoader());
                embedder.start();
                for (String dep : dependencies.split(",")) {
                    String[] tokens = dep.split(":");
                    String artifactId = tokens[0];
                    String version = tokens[1];
                    File dependencyJar = null;
                    // need to search multiple group IDs
                    // TODO: extend manifest to include groupID:artifactID:version
                    Exception resolutionError = null;
                    for (String groupId : new String[] { "org.jvnet.hudson.plugins",
                            "org.jvnet.hudson.main" }) {

                        // first try to find it on the classpath.
                        // this takes advantage of Maven POM located in POM
                        URL dependencyPomResource = getClass()
                                .getResource("/META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
                        if (dependencyPomResource != null) {
                            // found it
                            dependencyJar = Which.jarFile(dependencyPomResource);
                            break;
                        } else {
                            Artifact a;
                            a = embedder.createArtifact(groupId, artifactId, version,
                                    "compile"/*doesn't matter*/, "hpi");
                            try {
                                embedder.resolve(a,
                                        Arrays.asList(embedder.createRepository(
                                                "http://maven.glassfish.org/content/groups/public/", "repo")),
                                        embedder.getLocalRepository());
                                dependencyJar = a.getFile();
                            } catch (AbstractArtifactResolutionException x) {
                                // could be a wrong groupId
                                resolutionError = x;
                            }
                        }
                    }
                    if (dependencyJar == null)
                        throw new Exception("Failed to resolve plugin: " + dep, resolutionError);

                    File dst = new File(home, "plugins/" + artifactId + ".hpi");
                    if (!dst.exists() || dst.lastModified() != dependencyJar.lastModified()) {
                        FileUtils.copyFile(dependencyJar, dst);
                    }
                }
                embedder.stop();
            }
        }
    });
}

From source file:org.jvnet.hudson.test.JenkinsRule.java

/**
 * If this test harness is launched for a Jenkins plugin, locate the <tt>target/test-classes/the.jpl</tt>
 * and add a recipe to install that to the new Jenkins.
 *
 * <p>/*  ww  w.  j  av a 2s  .  co  m*/
 * This file is created by <tt>maven-hpi-plugin</tt> at the testCompile phase when the current
 * packaging is <tt>jpi</tt>.
 */
public void recipeLoadCurrentPlugin() throws Exception {
    final Enumeration<URL> jpls = getClass().getClassLoader().getResources("the.jpl");
    final Enumeration<URL> hpls = getClass().getClassLoader().getResources("the.hpl");

    final List<URL> all = Collections.list(jpls);
    all.addAll(Collections.list(hpls));

    if (all.isEmpty())
        return; // nope

    recipes.add(new JenkinsRecipe.Runner() {
        private File home;
        private final List<Jpl> jpls = new ArrayList<Jpl>();

        @Override
        public void decorateHome(JenkinsRule testCase, File home) throws Exception {
            this.home = home;
            this.jpls.clear();

            for (URL hpl : all) {
                Jpl jpl = new Jpl(hpl);
                jpl.loadManifest();
                jpls.add(jpl);
            }

            for (Jpl jpl : jpls) {
                jpl.resolveDependencies();
            }
        }

        class Jpl {
            final URL jpl;
            Manifest m;
            private String shortName;

            Jpl(URL jpl) {
                this.jpl = jpl;
            }

            void loadManifest() throws IOException {
                m = new Manifest(jpl.openStream());
                shortName = m.getMainAttributes().getValue("Short-Name");
                if (shortName == null)
                    throw new Error(jpl + " doesn't have the Short-Name attribute");
                FileUtils.copyURLToFile(jpl, new File(home, "plugins/" + shortName + ".jpl"));
            }

            void resolveDependencies() throws Exception {
                // make dependency plugins available
                // TODO: probably better to read POM, but where to read from?
                // TODO: this doesn't handle transitive dependencies

                // Tom: plugins are now searched on the classpath first. They should be available on
                // the compile or test classpath. As a backup, we do a best-effort lookup in the Maven repository
                // For transitive dependencies, we could evaluate Plugin-Dependencies transitively.
                String dependencies = m.getMainAttributes().getValue("Plugin-Dependencies");
                if (dependencies != null) {
                    DEPENDENCY: for (String dep : dependencies.split(",")) {
                        String suffix = ";resolution:=optional";
                        boolean optional = dep.endsWith(suffix);
                        if (optional) {
                            dep = dep.substring(0, dep.length() - suffix.length());
                        }
                        String[] tokens = dep.split(":");
                        String artifactId = tokens[0];
                        String version = tokens[1];

                        for (Jpl other : jpls) {
                            if (other.shortName.equals(artifactId))
                                continue DEPENDENCY; // resolved from another JPL file
                        }

                        File dependencyJar = resolveDependencyJar(artifactId, version);
                        if (dependencyJar == null) {
                            if (optional) {
                                System.err.println("cannot resolve optional dependency " + dep + " of "
                                        + shortName + "; skipping");
                                continue;
                            }
                            throw new IOException("Could not resolve " + dep + " in "
                                    + System.getProperty("java.class.path"));
                        }

                        File dst = new File(home, "plugins/" + artifactId + ".jpi");
                        if (!dst.exists() || dst.lastModified() != dependencyJar.lastModified()) {
                            try {
                                FileUtils.copyFile(dependencyJar, dst);
                            } catch (ClosedByInterruptException x) {
                                throw new AssumptionViolatedException("copying dependencies was interrupted",
                                        x);
                            }
                        }
                    }
                }
            }
        }

        /**
         * Lazily created embedder.
         */
        private MavenEmbedder embedder;

        private MavenEmbedder getMavenEmbedder() throws MavenEmbedderException, IOException {
            if (embedder == null)
                embedder = MavenUtil.createEmbedder(
                        new StreamTaskListener(System.out, Charset.defaultCharset()), (File) null, null);
            return embedder;
        }

        private @CheckForNull File resolveDependencyJar(String artifactId, String version) throws Exception {
            // try to locate it from manifest
            Enumeration<URL> manifests = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
            while (manifests.hasMoreElements()) {
                URL manifest = manifests.nextElement();
                InputStream is = manifest.openStream();
                Manifest m = new Manifest(is);
                is.close();

                if (artifactId.equals(m.getMainAttributes().getValue("Short-Name")))
                    return Which.jarFile(manifest);
            }

            // For snapshot plugin dependencies, an IDE may have replaced ~/.m2/repository//${artifactId}.hpi with /${artifactId}-plugin/target/classes/
            // which unfortunately lacks META-INF/MANIFEST.MF so try to find index.jelly (which every plugin should include) and thus the ${artifactId}.hpi:
            Enumeration<URL> jellies = getClass().getClassLoader().getResources("index.jelly");
            while (jellies.hasMoreElements()) {
                URL jellyU = jellies.nextElement();
                if (jellyU.getProtocol().equals("file")) {
                    File jellyF = new File(jellyU.toURI());
                    File classes = jellyF.getParentFile();
                    if (classes.getName().equals("classes")) {
                        File target = classes.getParentFile();
                        if (target.getName().equals("target")) {
                            File hpi = new File(target, artifactId + ".hpi");
                            if (hpi.isFile()) {
                                return hpi;
                            }
                        }
                    }
                }
            }

            // need to search multiple group IDs
            // TODO: extend manifest to include groupID:artifactID:version
            Exception resolutionError = null;
            for (String groupId : PLUGIN_GROUPIDS) {

                // first try to find it on the classpath.
                // this takes advantage of Maven POM located in POM
                URL dependencyPomResource = getClass()
                        .getResource("/META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
                if (dependencyPomResource != null) {
                    // found it
                    return Which.jarFile(dependencyPomResource);
                } else {

                    try {
                        // currently the most of the plugins are still hpi
                        return resolvePluginFile(artifactId, version, groupId, "hpi");
                    } catch (AbstractArtifactResolutionException x) {
                        try {
                            // but also try with the new jpi
                            return resolvePluginFile(artifactId, version, groupId, "jpi");
                        } catch (AbstractArtifactResolutionException x2) {
                            // could be a wrong groupId
                            resolutionError = x;
                        }
                    }

                }
            }

            throw new Exception("Failed to resolve plugin: " + artifactId + " version " + version,
                    resolutionError);
        }

        private @CheckForNull File resolvePluginFile(String artifactId, String version, String groupId,
                String type) throws Exception {
            final Artifact jpi = getMavenEmbedder().createArtifact(groupId, artifactId, version,
                    "compile"/*doesn't matter*/, type);
            getMavenEmbedder().resolve(jpi,
                    Arrays.asList(getMavenEmbedder()
                            .createRepository("http://maven.glassfish.org/content/groups/public/", "repo")),
                    embedder.getLocalRepository());
            return jpi.getFile();

        }
    });
}

From source file:org.jvnet.hudson.test.TestPluginManager.java

/**
 * Dynamically load a detached plugin that would not otherwise get loaded.
 * Will only work in Jenkins 2.x.//from  w  ww  . jav a 2s.c o m
 * May be called at any time after Jenkins starts up (do not use from {@link #loadBundledPlugins()}.
 * You may need to first install any transitive dependencies.
 * @param shortName {@code cvs} for example
 */
public void installDetachedPlugin(String shortName) throws Exception {
    URL r = TestPluginManager.class.getClassLoader()
            .getResource("WEB-INF/detached-plugins/" + shortName + ".hpi");
    Assert.assertNotNull("could not find " + shortName, r);
    File f = new File(rootDir, shortName + ".hpi");
    FileUtils.copyURLToFile(r, f);
    dynamicLoad(f);
}