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:com.microsoft.azure.credentials.AuthFile.java

/**
 * Parses an auth file and read into an AuthFile object.
 * @param file the auth file to read// w  w  w . ja va  2  s.  c om
 * @return the AuthFile object created
 * @throws IOException thrown when the auth file or the certificate file cannot be read or parsed
 */
static AuthFile parse(File file) throws IOException {
    String content = Files.toString(file, Charsets.UTF_8).trim();

    AuthFile authFile;
    if (isJsonBased(content)) {
        authFile = ADAPTER.deserialize(content, AuthFile.class);
        Map<String, String> endpoints = ADAPTER.deserialize(content, new TypeToken<Map<String, String>>() {
        }.getType());
        authFile.environment.endpoints().putAll(endpoints);
    } else {
        // Set defaults
        Properties authSettings = new Properties();
        authSettings.put(CredentialSettings.AUTH_URL.toString(),
                AzureEnvironment.AZURE.activeDirectoryEndpoint());
        authSettings.put(CredentialSettings.BASE_URL.toString(),
                AzureEnvironment.AZURE.resourceManagerEndpoint());
        authSettings.put(CredentialSettings.MANAGEMENT_URI.toString(),
                AzureEnvironment.AZURE.managementEndpoint());
        authSettings.put(CredentialSettings.GRAPH_URL.toString(), AzureEnvironment.AZURE.graphEndpoint());
        authSettings.put(CredentialSettings.VAULT_SUFFIX.toString(),
                AzureEnvironment.AZURE.keyVaultDnsSuffix());

        // Load the credentials from the file
        StringReader credentialsReader = new StringReader(content);
        authSettings.load(credentialsReader);
        credentialsReader.close();

        authFile = new AuthFile();
        authFile.clientId = authSettings.getProperty(CredentialSettings.CLIENT_ID.toString());
        authFile.tenantId = authSettings.getProperty(CredentialSettings.TENANT_ID.toString());
        authFile.clientSecret = authSettings.getProperty(CredentialSettings.CLIENT_KEY.toString());
        authFile.clientCertificate = authSettings.getProperty(CredentialSettings.CLIENT_CERT.toString());
        authFile.clientCertificatePassword = authSettings
                .getProperty(CredentialSettings.CLIENT_CERT_PASS.toString());
        authFile.subscriptionId = authSettings.getProperty(CredentialSettings.SUBSCRIPTION_ID.toString());
        authFile.environment.endpoints().put(Endpoint.MANAGEMENT.identifier(),
                authSettings.getProperty(CredentialSettings.MANAGEMENT_URI.toString()));
        authFile.environment.endpoints().put(Endpoint.ACTIVE_DIRECTORY.identifier(),
                authSettings.getProperty(CredentialSettings.AUTH_URL.toString()));
        authFile.environment.endpoints().put(Endpoint.RESOURCE_MANAGER.identifier(),
                authSettings.getProperty(CredentialSettings.BASE_URL.toString()));
        authFile.environment.endpoints().put(Endpoint.GRAPH.identifier(),
                authSettings.getProperty(CredentialSettings.GRAPH_URL.toString()));
        authFile.environment.endpoints().put(Endpoint.KEYVAULT.identifier(),
                authSettings.getProperty(CredentialSettings.VAULT_SUFFIX.toString()));
    }
    authFile.authFilePath = file.getParent();

    return authFile;
}

From source file:com.google.dart.tools.debug.core.sourcemaps.SourceMap.java

public static SourceMap createFrom(File file) throws IOException {
    String contents = Files.toString(file, Charsets.UTF_8);

    try {/*from   ww  w .  ja v a  2 s . c o  m*/
        return createFrom(Path.fromOSString(file.getAbsolutePath()), contents);
    } catch (JSONException e) {
        throw new IOException(e);
    }
}

From source file:org.jclouds.compute.ComputeTestUtils.java

public static Map<String, String> setupKeyPair() {
    String secretKeyFile;//  ww  w.j a  v  a 2 s . c o m
    try {
        secretKeyFile = checkNotNull(System.getProperty("test.ssh.keyfile"), "test.ssh.keyfile");
    } catch (NullPointerException e) {
        secretKeyFile = System.getProperty("user.home") + "/.ssh/id_rsa";
    }
    checkSecretKeyFile(secretKeyFile);
    try {
        String secret = Files.toString(new File(secretKeyFile), Charsets.UTF_8);
        assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n" + secret;
        return ImmutableMap.<String, String>of("private", secret, "public",
                Files.toString(new File(secretKeyFile + ".pub"), Charsets.UTF_8));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.github.tomakehurst.wiremock.common.TextFile.java

public String readContents() {
    try {//from  ww  w  . ja v a 2 s . co m
        final String json = Files.toString(file, UTF_8);
        return json;
    } catch (final IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:org.glowroot.agent.config.ConfigFileUtil.java

public static ObjectNode getRootObjectNode(File file) {
    String content;// ww  w. j a v a 2 s  .  c  o  m
    try {
        content = Files.toString(file, UTF_8);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return mapper.createObjectNode();
    }
    ObjectNode rootObjectNode = null;
    try {
        JsonNode rootNode = mapper.readTree(content);
        if (rootNode instanceof ObjectNode) {
            rootObjectNode = (ObjectNode) rootNode;
        }
    } catch (IOException e) {
        logger.warn("error processing config file: {}", file.getAbsolutePath(), e);
        writeBackupFile(file);
    }
    return rootObjectNode == null ? mapper.createObjectNode() : rootObjectNode;
}

From source file:org.infinitest.intellij.WhenDiscoveringInfinitestJars.java

private static String mavenVersion() {
    try {/*from  w  w  w .j a va2  s  .  c om*/
        String pomXml = Files.toString(new File("pom.xml"), UTF_8);
        return substringBetween(pomXml, "<version>", "</version>");
    } catch (IOException e) {
        throw new RuntimeException("Unable to read maven version", e);
    }
}

From source file:org.cloudifysource.cosmo.orchestrator.workflow.RuoteWorkflow.java

public static RuoteWorkflow createFromFile(String path, RuoteRuntime runtime) {
    Preconditions.checkNotNull(path);// w w  w.  j a  va 2 s.com
    Preconditions.checkNotNull(runtime);
    try {
        final String workflow = Files.toString(new File(path), Charsets.UTF_8);
        return new RuoteWorkflow(workflow, runtime);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.glowroot.testing.Util.java

static void updateLibVersion(String modulePath, String property, String version) throws IOException {
    File pomFile = new File(BASE_DIR + "/" + modulePath + "/pom.xml");
    String pom = Files.toString(pomFile, UTF_8);
    pom = pom.replaceAll("<" + property + ">.*", "<" + property + ">" + version + "</" + property + ">");
    Files.write(pom, pomFile, UTF_8);
    System.out.println(property + " : " + version);
    report.println(property + " : " + version);
}

From source file:ch.acanda.eclipse.pmd.builder.ViolationProcessor.java

public void annotate(final IFile file, final Iterable<RuleViolation> violations)
        throws CoreException, IOException {
    MarkerUtil.removeAllMarkers(file);//from  w  w  w . j  a  v  a2s  . c  om
    if (!Iterables.isEmpty(violations)) {
        final String content = Files.toString(file.getRawLocation().toFile(),
                Charset.forName(file.getCharset()));
        for (final RuleViolation violation : violations) {
            MarkerUtil.addMarker(file, content, violation);
        }
    }
}

From source file:fr.mc.app.tool.EnvironmentParser.java

public EnvironmentParser() {
    JSONParser jsonParser = new JSONParser();

    //String workingDir = System.getProperty("user.dir");
    //System.out.println("Current working directory : " + workingDir);

    try {//from   www. j a  v a 2s .  c  o  m
        File file = new File(filepath);

        if (file.exists()) {
            String fileContent = Files.toString(file, Charset.defaultCharset());
            JSONObject fileContentAsJSONObject = (JSONObject) jsonParser.parse(fileContent);
            JSONArray environmentsAsObject = (JSONArray) fileContentAsJSONObject.get("environments");
            ArrayList<Environment> newEnvironmentsCollection = new ArrayList<>();

            for (Object environmentAsObject : environmentsAsObject) {
                JSONObject environmentAsJSONObject = (JSONObject) environmentAsObject;
                Environment environment = new Environment();
                environment.setClient_name((String) environmentAsJSONObject.get("client_name"));
                environment.setResponse_type((String) environmentAsJSONObject.get("response_type"));
                environment.setClient_id((String) environmentAsJSONObject.get("client_id"));
                environment.setClient_secret((String) environmentAsJSONObject.get("client_secret"));
                environment.setRedirect_uri((String) environmentAsJSONObject.get("redirect_uri"));
                environment.setDisplay((String) environmentAsJSONObject.get("display"));
                environment.setScope((String) environmentAsJSONObject.get("scope"));
                newEnvironmentsCollection.add(environment);
            }
            environments = newEnvironmentsCollection;
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EnvironmentParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(EnvironmentParser.class.getName()).log(Level.SEVERE, null, ex);
    }
}