Example usage for com.squareup.okhttp RequestBody RequestBody

List of usage examples for com.squareup.okhttp RequestBody RequestBody

Introduction

In this page you can find the example usage for com.squareup.okhttp RequestBody RequestBody.

Prototype

RequestBody

Source Link

Usage

From source file:org.wildfly.kubernetes.configmap.ConfigMapOperations.java

License:Open Source License

public RequestBody createJsonPayload(String namespace, String name, Map<String, String> labels,
        Collection<Path> files) throws IOException {
    return new RequestBody() {
        @Override//from w  ww.ja v  a  2  s .  c  o m
        public MediaType contentType() {
            return MediaType.parse("application/json");
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            ModelNode model = new ModelNode().setEmptyObject();
            model.get("kind").set("ConfigMap");
            model.get("apiVersion").set("v1");
            ModelNode metadataNode = model.get("metadata").setEmptyObject();
            metadataNode.get("name").set(name);
            metadataNode.get("namespace").set(namespace);
            if (!labels.isEmpty()) {
                ModelNode labelsNode = model.get("labels").setEmptyObject();
                for (Map.Entry<String, String> label : labels.entrySet()) {
                    labelsNode.get(label.getKey()).set(label.getValue());
                }
            }
            ModelNode dataNode = model.get("data").setEmptyObject();
            for (Path file : files) {
                dataNode.get(file.getFileName().toString())
                        .set(new String(Files.readAllBytes(file), StandardCharsets.UTF_8));
            }
            try (PrintWriter out = new PrintWriter(sink.outputStream(), true)) {
                model.writeJSONString(out, true);
            }
        }
    };
}