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:ch.vorburger.mariadb4j.Util.java

/**
 * Extract files from a package on the classpath into a directory.
 * //from w  w w  .j av a 2s . c o  m
 * @param packagePath e.g. "com/stuff" (always forward slash not backslash, never dot)
 * @param toDir directory to extract to
 * @return int the number of files copied
 * @throws java.io.IOException if something goes wrong, including if nothing was found on
 *             classpath
 */
public static int extractFromClasspathToFile(String packagePath, File toDir) throws IOException {
    String locationPattern = "classpath*:" + packagePath + "/**";
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resourcePatternResolver.getResources(locationPattern);
    if (resources.length == 0) {
        throw new IOException("Nothing found at " + locationPattern);
    }
    int counter = 0;
    for (Resource resource : resources) {
        if (resource.isReadable()) { // Skip hidden or system files
            final URL url = resource.getURL();
            String path = url.toString();
            if (!path.endsWith("/")) { // Skip directories
                int p = path.lastIndexOf(packagePath) + packagePath.length();
                path = path.substring(p);
                final File targetFile = new File(toDir, path);
                long len = resource.contentLength();
                if (!targetFile.exists() || targetFile.length() != len) { // Only copy new files
                    tryN(5, 500, new Procedure<IOException>() {

                        @Override
                        public void apply() throws IOException {
                            FileUtils.copyURLToFile(url, targetFile);
                        }
                    });
                    counter++;
                }
            }
        }
    }
    if (counter > 0) {
        Object[] info = new Object[] { counter, locationPattern, toDir };
        logger.info("Unpacked {} files from {} to {}", info);
    }
    return counter;
}

From source file:ch.vorburger.mifos.wiki.ZWikiJRstConverter.java

public boolean convertPage(File inputFile) throws Exception {
    File outputFile = new File(inputFile.getParentFile(), inputFile.getName().replace(".rst", "") + ".xml");
    try {/*from www .  j  a  va 2  s .co  m*/
        JRSTOptions options = new JRSTOptions();
        options.lenientTitle = true;
        options.otherKindsOfTitleLevels = true;
        options.keepLevel = true;
        options.newlinesInXML = false;
        JRST.generate("xml", inputFile, "UTF-8", outputFile, "UTF-8", Overwrite.ALLTIME, options);
        ++successfulPages;
        return true;
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
        ++failedPages;
        failedPageNames.add(inputFile);

        URL failedConversionTemplate = getClass().getResource("/FailedConversionTemplate.xml");
        FileUtils.copyURLToFile(failedConversionTemplate, outputFile);

        return false;
    }
}

From source file:hudson.cli.ClientAuthenticationCacheTest.java

@Ignore("TODO fails unless CLICommand.main patched to replace (auth==Jenkins.ANONYMOUS) with (auth instanceof AnonymousAuthenticationToken), not just (Jenkins.ANONYMOUS.equals(auth)), since SecurityFilters.groovy sets userAttribute='anonymous,' so UserAttributeEditor.setAsText configures AnonymousProcessingFilter with a token with an empty authority which fails AbstractAuthenticationToken.equals")
@Test/*from   w w  w.ja  va2 s  . com*/
public void overHttp() throws Exception {
    File jar = tmp.newFile("jenkins-cli.jar");
    FileUtils.copyURLToFile(r.jenkins.getJnlpJars("jenkins-cli.jar").getURL(), jar);
    r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
    r.jenkins.setAuthorizationStrategy(new FullControlOnceLoggedInAuthorizationStrategy());
    r.jenkins.setSlaveAgentPort(-1);
    assertCLI(0, "Authenticated as: anonymous", jar, "who-am-i");
    assertCLI(0, null, jar, "login", "--username", "admin", "--password", "admin");
    try {
        assertCLI(0, "Authenticated as: admin", jar, "who-am-i");
    } finally {
        assertCLI(0, null, jar, "logout");
    }
}

From source file:Data.c_PriceDB.java

public boolean loadPricesDB(ActionListener listener) {
    boolean success = true;
    try {//from   ww  w  . j  a  v a2 s .c  o  m
        FileUtils.copyURLToFile(new URL("http://www.magictraders.com/pricelists/current-magic-excel.txt"),
                new File(PRICES_FILE));
        listener.actionPerformed(new ActionEvent(this, Action.ACTION_FILE_LOAD_DONE, ""));
        success = updatePrices(listener, PRICES_FILE);
    } catch (Exception ex) {
        for (StackTraceElement elem : ex.getStackTrace()) {
            System.err.print(elem.toString() + "\n");
        }
        success = false;
    }
    return success;
}

From source file:com.pason.plugins.artifactorypolling.CheckoutTask.java

/**
 * Function that does the heavy lifting of the checkout. First it gets the metadata about
 * an artifact which includes all the files that are included in that artifact. Then it 
 * proceeds to download each of those files.
 * @param workspace The file where the downloads will go to
 * @param channel The channel back to the job
 * @return True if successful, false otherwise
 * @throws IOException//from w  ww.  jav a 2  s . c o m
 * @throws InterruptedException
 */
public Boolean invoke(File workspace, VirtualChannel channel) throws IOException, InterruptedException {
    if (doDownload) {
        // Delete any artifacts of previous builds
        File checkoutDir = new File(workspace, localDirectory);
        checkoutDir.mkdirs();
        FileUtils.cleanDirectory(checkoutDir);

        ArtifactoryAPI api = new ArtifactoryAPI(artifactoryURL);
        JSONArray json = api.getArtifactFiles(repo, groupID, artifactID, artifact.getVersion());

        String groupURLPart = groupID.replace('.', '/');
        for (int i = 0; i < json.size(); i++) {
            String uri = json.getString(i);
            LOGGER.log(FINE, "Found child URI: " + uri);
            String resourceURL = artifactoryURL + repo + '/' + groupURLPart + '/' + artifactID + '/'
                    + artifact.getVersion() + '/' + uri;
            LOGGER.log(FINE, "Starting download of " + uri);
            FileUtils.copyURLToFile(new URL(resourceURL), new File(checkoutDir, uri));
            LOGGER.log(FINE, "Finished downloading: " + uri);
        }
    }

    return true;
}

From source file:com.redhat.jenkins.plugins.ci.integration.FedMsgMessagingPluginIntegrationTest.java

@Test
public void testTriggeringUsingFedMsgLogger() throws Exception {

    FreeStyleJob jobA = jenkins.jobs.create();
    jobA.configure();//from  w  ww  .  j a v a2s  .  c om
    jobA.addShellStep("echo CI_MESSAGE = $CI_MESSAGE");
    CIEventTrigger ciEvent = new CIEventTrigger(jobA);
    ciEvent.selector.set("topic = 'org.fedoraproject.dev.logger.log'");
    CIEventTrigger.MsgCheck check = ciEvent.addMsgCheck();
    check.expectedValue.set(".+compose_id.+message.+");
    check.field.set("compose");
    jobA.save();
    // Allow for connection
    elasticSleep(5000);

    File privateKey = File.createTempFile("ssh", "key");
    FileUtils.copyURLToFile(FedmsgRelayContainer.class.getResource("FedmsgRelayContainer/unsafe"), privateKey);
    Files.setPosixFilePermissions(privateKey.toPath(), singleton(OWNER_READ));

    File ssh = File.createTempFile("jenkins", "ssh");
    FileUtils.writeStringToFile(ssh, "#!/bin/sh\n" + "exec ssh -o StrictHostKeyChecking=no -i "
            + privateKey.getAbsolutePath() + " fedmsg2@" + fedmsgRelay.getIpAddress()
            //+ " fedmsg-logger --message=\\\"This is a message.\\\"");
            + " fedmsg-logger " + " \"$@\"");
    //+ "--message=\\\'{\\\"compose\\\": "
    //+ "{\\\"compose_id\\\": \\\"This is a message.\\\"}}\\\' --json-input");
    Files.setPosixFilePermissions(ssh.toPath(), new HashSet<>(Arrays.asList(OWNER_READ, OWNER_EXECUTE)));

    System.out.println(FileUtils.readFileToString(ssh));
    ProcessBuilder gitLog1Pb = new ProcessBuilder(ssh.getAbsolutePath(),
            "--message='{\"compose\": " + "{\"compose_id\": \"This is a message.\"}}\'", "--json-input");
    String output = stringFrom(logProcessBuilderIssues(gitLog1Pb, "ssh"));
    System.out.println(output);

    jobA.getLastBuild().shouldSucceed().shouldExist();
    assertThat(jobA.getLastBuild().getConsole(), containsString("This is a message"));

}

From source file:com.gst.infrastructure.core.boot.EmbeddedTomcatWithSSLConfiguration.java

public File getFile(Resource resource) throws IOException {
    try {//from  w w  w .j  a  v a 2 s .c  o  m
        return resource.getFile();
    } catch (IOException e) {
        // Uops.. OK, try again (below)
    }

    try {
        URL url = resource.getURL();
        /**
         * // If this creates filenames that are too long on Win, // then
         * could just use resource.getFilename(), // even though not unique,
         * real risk prob. min.bon String tempDir =
         * System.getProperty("java.io.tmpdir"); tempDir = tempDir + "/" +
         * getClass().getSimpleName() + "/"; String path = url.getPath();
         * String uniqName = path.replace("file:/", "").replace('!', '_');
         * String tempFullPath = tempDir + uniqName;
         **/
        // instead of File.createTempFile(prefix?, suffix?);
        File targetFile = new File(resource.getFilename());
        long len = resource.contentLength();
        if (!targetFile.exists() || targetFile.length() != len) { // Only
                                                                  // copy
                                                                  // new
                                                                  // files
            FileUtils.copyURLToFile(url, targetFile);
        }
        return targetFile;
    } catch (IOException e) {
        // Uops.. erm, give up:
        throw new IOException("Cannot obtain a File for Resource: " + resource.toString(), e);
    }

}

From source file:eu.linda.analytics.formats.RDFInputFormat.java

@Override
public Rengine importData4R(String query_id, boolean isForRDFOutput, Analytics analytics) {
    System.out.println(System.getProperty("java.library.path"));
    System.out.println("R_HOME" + System.getenv().get("R_HOME"));

    Rengine re = Rengine.getMainEngine();
    if (re == null) {
        //            re = new Rengine(new String[]{"--vanilla"}, false, null);
        String newargs[] = { "--no-save" };
        re = new Rengine(newargs, false, null);

    }/*w w w  .  j a  v  a  2  s .c  om*/

    if (!re.waitForR()) {
        System.out.println("Cannot load R");
        System.out.println("is alive Rengine??" + re.isAlive());
    }

    String queryURI = connectionController.getQueryURI(query_id);

    helpfulFunctions.nicePrintMessage("import data from uri " + queryURI);
    try {
        float timeToGetQuery = 0;
        long startTimeToGetQuery = System.currentTimeMillis();
        URL url = new URL(queryURI);

        if (!helpfulFunctions.isURLResponsive(url)) {
            re.eval(" is_query_responsive <-FALSE ");
            System.out.println("is_query_responsive <-FALSE ");

        } else {
            re.eval("is_query_responsive <-TRUE  ");
            System.out.println("is_query_responsive <-TRUE ");

            File tmpfile4lindaquery = File.createTempFile("tmpfile4lindaquery" + query_id, ".tmp");
            FileUtils.copyURLToFile(url, tmpfile4lindaquery);

            re.eval(" loaded_data <- read.csv(file='" + tmpfile4lindaquery
                    + "', header=TRUE, sep=',', na.strings='---');");
            System.out.println(" loaded_data <- read.csv(file='" + tmpfile4lindaquery
                    + "', header=TRUE, sep=',', na.strings='---');");

            FileInputStream fis = null;
            try {

                fis = new FileInputStream(tmpfile4lindaquery);
                System.out.println("fis.getChannel().size() " + fis.getChannel().size());
                analytics.setData_size(analytics.getData_size() + fis.getChannel().size());
            } finally {
                fis.close();
            }
        }

        // Get elapsed time in milliseconds
        long elapsedTimeToGetQueryMillis = System.currentTimeMillis() - startTimeToGetQuery;
        // Get elapsed time in seconds
        timeToGetQuery = elapsedTimeToGetQueryMillis / 1000F;
        System.out.println("timeToGetQuery" + timeToGetQuery);
        analytics.setTimeToGet_data(analytics.getTimeToGet_data() + timeToGetQuery);

        connectionController.updateLindaAnalyticsInputDataPerformanceTime(analytics);

    } catch (Exception ex) {
        Logger.getLogger(ArffInputFormat.class.getName()).log(Level.SEVERE, null, ex);
    }
    return re;
}

From source file:api.wiki.WikiNameApi2.java

private TreeMap<String, String> getGenderNames(String title) {
    String query = BASE_URL + "&list=categorymembers&cmlimit=500&cmtitle=" + title.replaceAll("\\s+", "_")
            + "&cmprop=title";
    TreeMap<String, String> values = new TreeMap<>();
    try {//from w w  w .j  a  v  a  2 s  . c  o  m
        URL url = new URL(query);
        File file = File.createTempFile("WIKI_", title);
        FileUtils.copyURLToFile(url, file);
        String s = FileUtils.readFileToString(file);
        JSONObject j = new JSONObject(s);
        if (j.has("query-continue")) {
            values.putAll(getGenderNameContinue(title, j.getJSONObject("query-continue")
                    .getJSONObject("categorymembers").getString("cmcontinue")));
        }
        JSONArray json = j.getJSONObject("query").getJSONArray("categorymembers");
        for (int i = 0; i < json.length(); i++) {
            String value = json.getJSONObject(i).getString("title");
            String key = value;
            if (key.contains("(")) {
                key = key.substring(0, key.indexOf("("));
            }
            values.put(key, value);
        }

    } catch (IOException ex) {
        Logger.getLogger(WikiNameApi2.class.getName()).log(Level.SEVERE, null, ex);
    }

    return values;
}

From source file:ca.weblite.xmlvm.XMLVMTest.java

@Test
public void testSingleXMLVM() throws Exception {
    org.apache.tools.ant.Project proj = new org.apache.tools.ant.Project();
    File basedir = new File("/Volumes/Windows VMS/NetbeansProjects/HelloWorldCN1");
    proj.setBasedir(basedir.getAbsolutePath());
    XMLVM xmlvm = new XMLVM();
    xmlvm.setProject(proj);// w ww.  j a va 2s  .com
    File srcdir = File.createTempFile("input", "dir");
    srcdir.delete();
    srcdir.mkdir();
    File comDir = new File(srcdir, "com");
    File myCompanyDir = new File(comDir, "mycompany");
    File myappDir = new File(myCompanyDir, "myapp");
    myappDir.mkdirs();
    File inputClassFile = new File(myappDir, "MyApplication.class");

    FileUtils.copyURLToFile(this.getClass().getResource("resources/MyApplication.clazz"), inputClassFile);

    File destDir = File.createTempFile("output", "dir");
    destDir.delete();
    destDir.mkdir();

    //XMLVM xmlvm = new XMLVM();
    File[] results = xmlvm.generateSingleXMLVM(srcdir, inputClassFile, destDir);
    assertEquals(2, results.length);
    assertEquals("com_mycompany_myapp_MyApplication.xmlvm", results[0].getName());
    assertEquals("org_xmlvm_ConstantPool.xmlvm", results[1].getName());

}