Example usage for com.google.common.io Files toString

List of usage examples for com.google.common.io Files toString

Introduction

In this page you can find the example usage for com.google.common.io Files toString.

Prototype

public static String toString(File file, Charset charset) throws IOException 

Source Link

Usage

From source file:io.parallec.core.util.PcFileNetworkIoUtils.java

/**
 * Read file content to string./*from w  w  w .ja va  2 s .c o m*/
 *
 * @param filePath
 *            the file path
 * @return the string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static String readFileContentToString(String filePath) throws IOException {
    String content = "";
    content = Files.toString(new File(filePath), Charsets.UTF_8);
    return content;
}

From source file:se.kth.hopsworks.rest.AgentService.java

@POST
@Path("/register")
@Consumes(MediaType.APPLICATION_JSON)/* ww w. j a  v  a 2s .c o m*/
@Produces(MediaType.APPLICATION_JSON)
public Response register(@Context HttpServletRequest req, String jsonString) throws AppException {
    JSONObject json = new JSONObject(jsonString);
    String pubAgentCert = "no certificate";
    String caPubCert = "no certificate";
    if (json.has("csr")) {
        String csr = json.getString("csr");
        try {
            pubAgentCert = PKIUtils.signWithServerCertificate(csr, settings.getIntermediateCaDir(),
                    settings.getHopsworksMasterPasswordSsl());
            caPubCert = Files.toString(new File(settings.getIntermediateCaDir() + "/certs/ca-chain.cert.pem"),
                    Charsets.UTF_8);
        } catch (IOException | InterruptedException ex) {
            Logger.getLogger(AgentService.class.getName()).log(Level.SEVERE, null, ex);
            throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ex.toString());
        }
    }

    if (json.has("host-id") && json.has("agent-password")) {
        String hostId = json.getString("host-id");
        Host host;
        try {
            host = hostEJB.findByHostId(hostId);
            String agentPassword = json.getString("agent-password");
            host.setAgentPassword(agentPassword);
            host.setRegistered(true);
            hostEJB.storeHost(host, true);
        } catch (Exception ex) {
            Logger.getLogger(AgentService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    CsrDTO dto = new CsrDTO(caPubCert, pubAgentCert);
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(dto).build();
}

From source file:org.t3as.snomedct.client.cmdline.Main.java

private static void processFiles(final Options opts, final SnomedClient client) throws IOException {
    // read each file and call the web service
    for (final File f : opts.files) {
        final String input = Files.toString(f, Charsets.UTF_8);
        System.out.printf("%s:\n", f);
        System.out.println(callService(input, opts, client));
    }/* www.j  a  va2s.  c  o  m*/
}

From source file:com.hubspot.jinjava.loader.FileLocator.java

@Override
public String getString(String name, Charset encoding, JinjavaInterpreter interpreter) throws IOException {
    File file = resolveFileName(name);

    if (!file.exists() || !file.isFile()) {
        throw new ResourceNotFoundException("Couldn't find resource: " + file);
    }//from  ww w. ja v a  2  s .  c  o m

    return Files.toString(file, encoding);
}

From source file:playn.java.JavaAssetManager.java

@Override
protected void doGetText(String path, ResourceCallback<String> callback) {
    try {//  w w w  .j  av  a  2s  .co m
        callback.done(Files.toString(new File(pathPrefix, path), Charsets.UTF_8));
    } catch (Throwable e) {
        callback.error(e);
    }
}

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

public JCEFileKeyProvider(File keyStoreFile, File keyStorePasswordFile,
        Map<String, File> aliasPasswordFileMap) {
    super();/*from   w  w w  .  ja  va  2s .co  m*/
    this.aliasPasswordFileMap = aliasPasswordFileMap;
    this.keyStorePasswordFile = keyStorePasswordFile;
    try {
        ks = KeyStore.getInstance("jceks");
        keyStorePassword = Files.toString(keyStorePasswordFile, Charsets.UTF_8).trim().toCharArray();
        ks.load(new FileInputStream(keyStoreFile), keyStorePassword);
    } catch (Exception ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:org.obeonetwork.dsl.uml2.profile.design.profiletodsl.ManifestTools.java

/**
 * Add a given bundle to the require bundle of a manifest file.
 * //from w  w  w  .  j  a v  a  2s.  c o  m
 * @param bundle
 *            the bundle name
 * @param version
 *            the bundle version, can be <code>null</code>
 * @return the new manifest file content
 * @throws IOException
 */
public String addRequireBundle(String bundle, Version version) throws IOException {
    String originalContent = Files.toString(manifestFile, Charsets.UTF_8);
    String updatedFileContent = addRequireBundle(bundle, version, originalContent);
    if (!originalContent.equals(updatedFileContent)) {
        Files.write(updatedFileContent, manifestFile, Charsets.UTF_8);
        return updatedFileContent;
    }
    return originalContent;
}

From source file:com.netflix.simianarmy.client.chef.ChefContext.java

@Override
protected void createClient() {
    MonkeyConfiguration config = configuration();
    String privateKey = null;/*w w w .j  a  va 2s .co m*/
    String chefURL = config.getStrOrElse("simianarmy.client.chef.url", "http://127.0.0.1:4242");
    String chefClient = config.getStrOrElse("simianarmy.client.chef.clientname", "root");

    //copied code from from SshConfig
    String chefKeyPath = config.getStrOrElse("simianarmy.client.chef.key", null);
    if (chefKeyPath != null) {
        chefKeyPath = chefKeyPath.trim();
        if (chefKeyPath.startsWith("~/")) {
            String home = System.getProperty("user.home");
            if (!Strings.isNullOrEmpty(home)) {
                if (!home.endsWith("/")) {
                    home += "/";
                }
                chefKeyPath = home + chefKeyPath.substring(2);
            }
        }

        try {
            privateKey = Files.toString(new File(chefKeyPath), Charsets.UTF_8);
        } catch (IOException e) {
            throw new IllegalStateException("Unable to read the specified Chef key: " + chefKeyPath, e);
        }
    }

    org.jclouds.chef.ChefContext context = ContextBuilder.newBuilder("chef").endpoint(chefURL)
            .credentials(chefClient, privateKey).buildView(org.jclouds.chef.ChefContext.class);

    final ChefClient client = new ChefClient(context);
    setCloudClient(client);
}

From source file:org.jclouds.virtualbox.functions.YamlImagesFromFileConfig.java

@Override
public String get() {
    try {/*from   w  w w.  j  a v  a  2  s.  c o  m*/
        File yamlFile = new File(yamlFilePath);
        String yamlDesc = null;
        // if the yaml file does not exist just use default-images.yaml
        if (!yamlFile.exists()) {
            yamlDesc = Strings2.toStringAndClose(getClass().getResourceAsStream("/default-images.yaml"));
        } else {
            yamlDesc = Files.toString(yamlFile, Charsets.UTF_8);
        }
        checkNotNull(yamlDesc, "yaml descriptor");
        return yamlDesc;
    } catch (IOException e) {
        throw new RuntimeException("error reading yaml file");
    }
}

From source file:org.bonitasoft.studio.common.FileUtil.java

public static void replaceStringInFile(File file, String match, String replacingString) {
    try {//from   w  w w .j a v  a 2s  .c  om
        final Charset utf8 = Charset.forName("UTF-8");
        Files.write(Files.toString(file, utf8).replace(match, replacingString), file, utf8);
    } catch (final IOException e) {
        BonitaStudioLog.error(e);
    }

}