All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.athaydes.rawhttp.reqinedit.js.JsLoader Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package com.athaydes.rawhttp.reqinedit.js;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

final class JsLoader {

    /**
     * Returns two JS Objects, the first being the public environment,
     * the second being the private environment.
     *
     * @param projectDir where the HTTP file is located
     * @return the public and private environments
     */
    static List getJsObjects(@Nullable File projectDir) {
        if (projectDir == null) {
            projectDir = new File(".");
        }

        try {
            return load(new File(projectDir, "rest-client.env.json"),
                    new File(projectDir, "http-client.env.json"),
                    new File(projectDir, "rest-client.private.env.json"),
                    new File(projectDir, "http-client.private.env.json"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static List load(File restClientEnv, File httpClientEnv,
                             File privateRestClientEnv, File privateHttpClientEnv) throws IOException {
        List jsonObjects = new ArrayList<>(2);
        if (restClientEnv.isFile()) {
            jsonObjects.add(read(restClientEnv));
        } else if (httpClientEnv.isFile()) {
            jsonObjects.add(read(httpClientEnv));
        } else {
            jsonObjects.add("{}");
        }

        if (privateRestClientEnv.isFile()) {
            jsonObjects.add(read(privateRestClientEnv));
        } else if (privateHttpClientEnv.isFile()) {
            jsonObjects.add(read(privateHttpClientEnv));
        } else {
            jsonObjects.add("{}");
        }

        return jsonObjects;
    }

    private static String read(File file) throws IOException {
        return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy