Example usage for org.apache.commons.io FileUtils openOutputStream

List of usage examples for org.apache.commons.io FileUtils openOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openOutputStream.

Prototype

public static FileOutputStream openOutputStream(File file) throws IOException 

Source Link

Document

Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

Usage

From source file:org.eclipse.smila.blackboard.test.TestPersistingBlackboard.java

/**
 * Test setting attachment from file.//  w  w  w  .  j a  v  a 2 s.c o m
 * 
 * @throws Exception
 *           the exception
 */
public void testSetAttachmentFromFile() throws Exception {
    final String attachment1 = "testSetAttachmentFromFile1";
    final File tempDir = WorkspaceHelper.createWorkingDir(BUNDLE_ID);
    final File attachmentFile = new File(tempDir, attachment1);
    final OutputStream output = FileUtils.openOutputStream(attachmentFile);
    IOUtils.write(attachment1.getBytes(), output);
    IOUtils.closeQuietly(output);

    final String id = setTestRecord();
    _blackboard.setAttachmentFromFile(id, attachment1, attachmentFile);

    final byte[] storageAttachment1 = _blackboard.getAttachment(id, attachment1);
    assertEquals(storageAttachment1.length, attachment1.getBytes().length);

    try {
        _blackboard.setAttachmentFromFile(id, attachment1, new File("unexistingFile"));
        fail("Must throw BlackboardAccessException");
    } catch (final BlackboardAccessException e) {
        ; // ok
    }
}

From source file:org.esa.s2tbx.dataio.NativeLibraryLoader.java

/**
 * Loads library either from the current JAR archive, or from file system
 * <p>//from  ww  w  . j a  va  2  s  .  co m
 * The file from JAR is copied into system temporary directory and then loaded.
 *
 * @param path          The path from which the load is attempted
 * @param libraryName   The name of the library to be loaded (without extension)
 * @throws IOException              If temporary file creation or read/write operation fails
 */
public static void loadLibrary(String path, String libraryName) throws IOException {
    path = URLDecoder.decode(path, "UTF-8");
    String libPath = "/lib/" + NativeLibraryLoader.getOSFamily() + "/" + System.mapLibraryName(libraryName);
    if (path.contains(".jar!")) {
        try (InputStream in = NativeLibraryLoader.class.getResourceAsStream(libPath)) {
            File fileOut = new File(System.getProperty("java.io.tmpdir"), libPath);
            try (OutputStream out = FileUtils.openOutputStream(fileOut)) {
                IOUtils.copy(in, out);
            }
            path = fileOut.getAbsolutePath();
        }
    } else {
        path = new File(path, libPath).getAbsolutePath();
    }
    System.load(path);
}

From source file:org.fabrician.enabler.util.DockerfileBuildLock.java

private DockerfileBuildLock(String dockerImageName, File dockerFilePath) throws Exception {
    this.dockerImageName = dockerImageName;
    this.dockerFilePath = dockerFilePath;

    byte[] docker_bytes = FileUtils.readFileToByteArray(dockerFilePath);
    // we create a hash file name from the image and dockerfile content to build with...
    HashFunction hf = Hashing.md5();//from  ww  w.  jav  a2  s  .  c  o m
    HashCode hc = hf.newHasher().putString(dockerImageName, Charsets.UTF_8).putBytes(docker_bytes).hash();
    String dockerFileHash = BaseEncoding.base64Url().encode(hc.asBytes());
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    lock_file = new File(tmpDir, dockerFileHash + ".dockerfile_lck");
    logger.info("Attempt to acquire Dockerfile build lock at path :" + lock_file);
    lock_channel = FileUtils.openOutputStream(lock_file).getChannel();
    lock = lock_channel.tryLock();

    if (lock == null) {
        throw new Exception("Can't create exclusive build lock for image [" + dockerImageName
                + "] for Dockerfile [" + dockerFilePath + "]");
    } else {
        logger.info("Acquired Dockerfile build lock at lock path : [" + lock_file + "]");
    }
}

From source file:org.fuin.kickstart4j.Utils.java

/**
 * Copies a file from an URL to a local destination.
 * <code>IOException</code>s are mapped into a <code>RuntimeException</code>
 * .//from  w  w w . j av a2 s . co m
 * 
 * @param listener
 *            Monitor to use - Can be <code>null</code> if no progress
 *            information is needed.
 * @param srcFileUrl
 *            Source file URL.
 * @param destFile
 *            Destination file.
 * @param fileNo
 *            Number of the current file.
 * @param fileSize
 *            File size.
 * 
 * @throws FileNotFoundException
 *             The <code>srcFileUrl</code> was not found.
 */
public static void copyURLToFile(final FileCopyProgressListener listener, final URL srcFileUrl,
        final File destFile, final int fileNo, final int fileSize) throws FileNotFoundException {

    if (listener != null) {
        listener.updateFile(srcFileUrl.toString(), destFile.toString(), fileNo, fileSize);
    }
    try {
        final InputStream input = new FileCopyProgressInputStream(listener, srcFileUrl.openStream(), fileSize);
        try {
            final FileOutputStream output = FileUtils.openOutputStream(destFile);
            try {
                IOUtils.copy(input, output);
            } finally {
                IOUtils.closeQuietly(output);
            }
        } finally {
            IOUtils.closeQuietly(input);
        }
    } catch (final FileNotFoundException ex) {
        throw ex;
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

public String saveSpMetadataFile(String spMetadataFileName, InputStream input) {
    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        IOUtils.closeQuietly(input);/*w  ww.  j a  va 2  s .c o m*/
        String errorMessage = "Failed to save SP meta-data file due to undefined IDP root folder";
        log.error(errorMessage);
        throw new InvalidConfigurationException(errorMessage);
    }

    String idpMetadataFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB2_IDP_TEMPMETADATA_FOLDER + File.separator;
    String tempFileName = getTempMetadataFilename(idpMetadataFolder, spMetadataFileName);
    File spMetadataFile = new File(idpMetadataFolder + tempFileName);

    FileOutputStream os = null;
    try {
        os = FileUtils.openOutputStream(spMetadataFile);
        IOUtils.copy(input, os);
        os.flush();
    } catch (IOException ex) {
        log.error("Failed to write SP meta-data file '{0}'", ex, spMetadataFile);
        return null;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(input);
    }

    return tempFileName;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

public String saveFilterCert(String filterCertFileName, InputStream input) {
    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        IOUtils.closeQuietly(input);//from ww  w .j  av a2s  .  com
        throw new InvalidConfigurationException(
                "Failed to save filter certificate file due to undefined IDP root folder");
    }

    String idpMetadataFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB2_IDP_METADATA_FOLDER + File.separator + "credentials" + File.separator;
    File filterCertFile = new File(idpMetadataFolder + filterCertFileName);

    FileOutputStream os = null;
    try {
        os = FileUtils.openOutputStream(filterCertFile);
        IOUtils.copy(input, os);
        os.flush();
    } catch (IOException ex) {
        log.error("Failed to write  filter certificate file '{0}'", ex, filterCertFile);
        return null;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(input);
    }

    return filterCertFile.getAbsolutePath();
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

public String saveProfileConfigurationCert(String profileConfigurationCertFileName, InputStream stream) {
    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        IOUtils.closeQuietly(stream);/*from w  w w.j ava2  s. c  om*/
        throw new InvalidConfigurationException(
                "Failed to save Profile Configuration file due to undefined IDP root folder");
    }

    String idpMetadataFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB2_IDP_METADATA_FOLDER + File.separator + "credentials" + File.separator;
    File filterCertFile = new File(idpMetadataFolder + profileConfigurationCertFileName);

    FileOutputStream os = null;
    try {
        os = FileUtils.openOutputStream(filterCertFile);
        IOUtils.copy(stream, os);
        os.flush();
    } catch (IOException ex) {
        log.error("Failed to write  Profile Configuration  certificate file '{0}'", ex, filterCertFile);
        return null;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(stream);
    }

    return filterCertFile.getAbsolutePath();

}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

public boolean saveMetadataFile(String metadataFileName, InputStream stream) {
    if (applicationConfiguration.getShibboleth2FederationRootDir() == null) {
        IOUtils.closeQuietly(stream);/*from  w  ww  . j av a2  s.co m*/
        throw new InvalidConfigurationException(
                "Failed to save meta-data file due to undefined federation root folder");
    }

    String idpMetadataFolderName = applicationConfiguration.getShibboleth2FederationRootDir() + File.separator
            + SHIB2_IDP_METADATA_FOLDER + File.separator;
    File idpMetadataFolder = new File(idpMetadataFolderName);
    if (!idpMetadataFolder.exists()) {
        idpMetadataFolder.mkdirs();
    }
    File spMetadataFile = new File(idpMetadataFolderName + metadataFileName);

    FileOutputStream os = null;
    try {
        os = FileUtils.openOutputStream(spMetadataFile);
        IOUtils.copy(stream, os);
        os.flush();
    } catch (IOException ex) {
        log.error("Failed to write meta-data file '{0}'", ex, spMetadataFile);
        return false;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(stream);
    }

    return true;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth3ConfService.java

public String saveSpMetadataFile(String spMetadataFileName, InputStream input) {
    //TODO: change for IDP3
    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        IOUtils.closeQuietly(input);/*  w  w w  .  j a  v  a2s  .  c o  m*/
        String errorMessage = "Failed to save SP meta-data file due to undefined IDP root folder";
        log.error(errorMessage);
        throw new InvalidConfigurationException(errorMessage);
    }

    String idpMetadataFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB3_IDP_TEMPMETADATA_FOLDER + File.separator;
    String tempFileName = getTempMetadataFilename(idpMetadataFolder, spMetadataFileName);
    File spMetadataFile = new File(idpMetadataFolder + tempFileName);

    FileOutputStream os = null;
    try {
        os = FileUtils.openOutputStream(spMetadataFile);
        IOUtils.copy(input, os);
        os.flush();
    } catch (IOException ex) {
        log.error("Failed to write SP meta-data file '{0}'", ex, spMetadataFile);
        return null;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(input);
    }

    return tempFileName;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth3ConfService.java

public String saveFilterCert(String filterCertFileName, InputStream input) {
    //TODO: change for IDP3
    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        IOUtils.closeQuietly(input);// ww w  .  jav a 2 s.co m
        throw new InvalidConfigurationException(
                "Failed to save filter certificate file due to undefined IDP root folder");
    }

    String idpMetadataFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB3_IDP_METADATA_FOLDER + File.separator + "credentials" + File.separator;
    File filterCertFile = new File(idpMetadataFolder + filterCertFileName);

    FileOutputStream os = null;
    try {
        os = FileUtils.openOutputStream(filterCertFile);
        IOUtils.copy(input, os);
        os.flush();
    } catch (IOException ex) {
        log.error("Failed to write  filter certificate file '{0}'", ex, filterCertFile);
        return null;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(input);
    }

    return filterCertFile.getAbsolutePath();
}