Example usage for com.google.common.io Resources copy

List of usage examples for com.google.common.io Resources copy

Introduction

In this page you can find the example usage for com.google.common.io Resources copy.

Prototype

public static void copy(URL from, OutputStream to) throws IOException 

Source Link

Document

Copies all bytes from a URL to an output stream.

Usage

From source file:org.apache.flume.channel.file.encryption.EncryptionTestUtils.java

public static Map<String, File> configureTestKeyStore(File baseDir, File keyStoreFile) throws IOException {
    Map<String, File> result = Maps.newHashMap();

    if (System.getProperty("java.vendor").contains("IBM")) {
        Resources.copy(Resources.getResource("ibm-test.keystore"), new FileOutputStream(keyStoreFile));
    } else {/*  w  ww  .  ja v a  2s  .c  o  m*/
        Resources.copy(Resources.getResource("sun-test.keystore"), new FileOutputStream(keyStoreFile));
    }
    /*
    Commands below:
    keytool -genseckey -alias key-0 -keypass keyPassword -keyalg AES \
      -keysize 128 -validity 9000 -keystore src/test/resources/test.keystore \
      -storetype jceks -storepass keyStorePassword
    keytool -genseckey -alias key-1 -keyalg AES -keysize 128 -validity 9000 \
      -keystore src/test/resources/test.keystore -storetype jceks \
      -storepass keyStorePassword
     */
    //  key-0 has own password, key-1 used key store password
    result.put("key-0", TestUtils.writeStringToFile(baseDir, "key-0", "keyPassword"));
    result.put("key-1", null);
    return result;
}

From source file:org.openqa.selenium.firefox.internal.ClasspathExtension.java

public void writeTo(File extensionsDir) throws IOException {
    if (!FileHandler.isZipped(loadFrom)) {
        throw new WebDriverException("Will only install zipped extensions for now");
    }//from  w ww . j a va  2s  .  com

    File holdingPen = new File(extensionsDir, "webdriver-staging");
    FileHandler.createDir(holdingPen);

    File extractedXpi = new File(holdingPen, loadFrom);
    File parentDir = extractedXpi.getParentFile();
    if (!parentDir.exists()) {
        parentDir.mkdirs();
    }

    URL resourceUrl = Resources.getResource(loadResourcesUsing, loadFrom);
    OutputStream stream = null;

    try {
        stream = new FileOutputStream(extractedXpi);
        Resources.copy(resourceUrl, stream);
    } finally {
        Closeables.close(stream, false);
    }
    new FileExtension(extractedXpi).writeTo(extensionsDir);
}

From source file:org.killbill.billing.plugin.analytics.http.StaticServlet.java

private void doHandleStaticResource(final String resourceName, final HttpServletResponse resp)
        throws IOException {
    final URL resourceUrl = Resources.getResource("static/" + resourceName);

    final String[] parts = resourceName.split("/");
    if (parts.length >= 2) {
        if (parts[0].equals("javascript")) {
            resp.setContentType("application/javascript");
        } else if (parts[0].equals("styles")) {
            resp.setContentType("text/css");
        }/*from   w  w w .jav  a 2 s .  c o  m*/
        Resources.copy(resourceUrl, resp.getOutputStream());
    } else {
        Resources.copy(resourceUrl, resp.getOutputStream());
        resp.setContentType("text/html");
    }
    resp.setStatus(HttpServletResponse.SC_OK);
}

From source file:com.thoughtworks.selenium.webdriven.commands.AttachFile.java

private File downloadFile(String name) {
    URL url = getUrl(name);/*  w w w  . j  a  v a2 s.  co  m*/

    File dir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("attachFile", "dir");
    File outputTo = new File(dir, new File(url.getFile()).getName());

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputTo);
        Resources.copy(url, fos);
    } catch (IOException e) {
        throw new SeleniumException("Can't access file to upload: " + url, e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            // Nothing sane to do. Log and continue.
            LOGGER.log(Level.WARNING, "Unable to close stream used for reading file: " + name, e);
        }
    }

    return outputTo;
}

From source file:org.apache.gobblin.util.DownloadUtils.java

/**
 * Get ivy settings file from classpath/*from w  w w  .  j a  va 2 s .c  om*/
 */
public static File getIvySettingsFile() throws IOException {
    URL settingsUrl = Thread.currentThread().getContextClassLoader().getResource(IVY_SETTINGS_FILE_NAME);
    if (settingsUrl == null) {
        throw new IOException("Failed to find " + IVY_SETTINGS_FILE_NAME + " from class path");
    }

    // Check if settingsUrl is file on classpath
    File ivySettingsFile = new File(settingsUrl.getFile());
    if (ivySettingsFile.exists()) {
        // can access settingsUrl as a file
        return ivySettingsFile;
    }

    // Create temporary Ivy settings file.
    ivySettingsFile = File.createTempFile("ivy.settings", ".xml");
    ivySettingsFile.deleteOnExit();

    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(ivySettingsFile))) {
        Resources.copy(settingsUrl, os);
    }

    return ivySettingsFile;
}

From source file:org.openqa.selenium.iphone.IPhoneSimulatorBinary.java

protected static String getIphoneSimPath() {
    String filename = "ios-sim";
    File parentDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("webdriver", "libs");
    try {//from w  w  w.  ja va2s.c  o  m
        File destination = new File(parentDir, filename);
        FileOutputStream outputStream = new FileOutputStream(destination);
        try {
            URL resource = Resources.getResource(
                    IPhoneSimulatorBinary.class.getPackage().getName().replace('.', '/') + '/' + filename);
            Resources.copy(resource, outputStream);
            FileHandler.makeExecutable(destination);
            return destination.getAbsolutePath();
        } finally {
            outputStream.close();
        }
    } catch (IOException e) {
        throw new WebDriverException(e);
    }
}

From source file:org.hawkular.metrics.clients.ptrans.ExecutableITestBase.java

@Before
public void before() throws Exception {
    ptransConfFile = temporaryFolder.newFile();
    try (FileOutputStream out = new FileOutputStream(ptransConfFile)) {
        Resources.copy(Resources.getResource("ptrans.conf"), out);
    }/* w w  w  . j av  a2s . c o  m*/

    ptransOut = temporaryFolder.newFile();
    ptransErr = temporaryFolder.newFile();

    ptransPidFile = temporaryFolder.newFile();

    ptransProcessBuilder = new ProcessBuilder();
    ptransProcessBuilder.directory(temporaryFolder.getRoot());
    ptransProcessBuilder.redirectOutput(ptransOut);
    ptransProcessBuilder.redirectError(ptransErr);

    ptransProcessBuilder.command(JAVA, "-jar", PTRANS_ALL);
}

From source file:org.hawkular.metrics.clients.ptrans.exec.ExecutableITestBase.java

@Before
public void before() throws Exception {
    ptransConfFile = temporaryFolder.newFile();
    try (FileOutputStream out = new FileOutputStream(ptransConfFile)) {
        Resources.copy(Resources.getResource("ptrans.conf"), out);
    }// w ww  .j av  a 2  s .co  m

    ptransOut = temporaryFolder.newFile();
    ptransErr = temporaryFolder.newFile();

    ptransPidFile = temporaryFolder.newFile();

    ptransProcessBuilder = new ProcessBuilder();
    ptransProcessBuilder.directory(temporaryFolder.getRoot());
    ptransProcessBuilder.redirectOutput(ptransOut);
    ptransProcessBuilder.redirectError(ptransErr);

    ptransProcessBuilder.command(JAVA, "-Xss228k", "-jar", PTRANS_ALL);
}

From source file:jp.vmi.selenium.selenese.command.AttachFile.java

@Override
protected Result executeImpl(Context context, String... curArgs) {
    String name = curArgs[ARG_FILENAME];
    File outputTo = null;//  ww  w .j  a v  a2s. c  o m
    if (name.contains("://")) {
        // process (remote) url
        URL url;
        try {
            url = new URL(name);
        } catch (MalformedURLException e) {
            return new Error("Malformed URL: " + name);
        }
        File dir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("attachFile", "dir");
        outputTo = new File(dir, new File(url.getFile()).getName());
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outputTo);
            Resources.copy(url, fos);
        } catch (IOException e) {
            return new Error("Can't access file to upload: " + url, e);
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                return new Warning("Unable to close stream used for reading file: " + name, e);
            }
        }
    } else {
        // process file besides testcase file
        outputTo = new File(FilenameUtils.getPath(context.getCurrentTestCase().getFilename()), name);
        if (!outputTo.exists()) {
            return new Error("Can't access file: " + outputTo);
        }
    }

    WebElement element = context.findElement(curArgs[ARG_LOCATOR]);
    try {
        element.clear();
    } catch (Exception e) {
        // ignore exceptions from some drivers when file-input cannot be cleared;
    }
    element.sendKeys(outputTo.getAbsolutePath());
    return SUCCESS;
}

From source file:dk.dma.epd.common.prototype.sensor.nmea.NmeaSerialSensorFactory.java

public static void unpackLibs() {
    String filename = "";
    String libDir = "";

    final String osArch = System.getProperty("os.arch");
    final String osName = System.getProperty("os.name");

    if (osName.startsWith("Windows")) {
        filename = "rxtxSerial.dll";

        if (osArch.indexOf("64") != -1) {
            libDir = "Windows/mfz-rxtx-2.2-20081207-win-x64/";
        } else {/*from   w ww .j  a va  2s.com*/
            libDir = "Windows/i368-mingw32/";
        }

    } else if (osName.equals("Linux")) {
        filename = "librxtxSerial.so";
        if (osArch.equals("amd64")) {
            libDir = "Linux/x86_64-unknown-linu-gnu/";
        } else {
            libDir = "Linux/i686-unknown-linux-gnu/";
        }

    } else if (osName.startsWith("Mac")) {
        filename = "rxtxSerial.jnilib";
        libDir = "Mac_OS_X/";

    } else {
        return;
    }

    try {
        File dest = Paths.get(EPDNATIVEPATH.toAbsolutePath().toString(), filename).toAbsolutePath().toFile();
        dest.createNewFile();

        FileOutputStream destOut = new FileOutputStream(dest);

        String resource = "/gnu/io/" + libDir + filename;
        System.out.println(resource);

        Resources.copy(NmeaSerialSensorFactory.class.getResource(resource), destOut);

        destOut.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}