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

io.alapierre.ksef.client.http.JsonBodyHandler Maven / Gradle / Ivy

There is a newer version: 2.0.24
Show newest version
package io.alapierre.ksef.client.http;

import io.alapierre.ksef.client.JsonSerializer;

import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

/**
 * @author Karol Bryzgiel {@literal [email protected]}
 * Copyrights by original author 2022.04.24
 */
public class JsonBodyHandler implements HttpResponse.BodyHandler> {

    private final Class tClass;
    private final JsonSerializer serializer;


    public JsonBodyHandler(Class tClass, JsonSerializer serializer) {
        this.tClass = tClass;
        this.serializer = serializer;
    }

    @Override
    public HttpResponse.BodySubscriber> apply(HttpResponse.ResponseInfo responseInfo) {
        return asJson(tClass);
    }

    private  HttpResponse.BodySubscriber> asJson(Class tClass) {
        HttpResponse.BodySubscriber upstream = HttpResponse.BodySubscribers.ofString(StandardCharsets.UTF_8);
        return HttpResponse.BodySubscribers.mapping(
                upstream,
                (String body) -> body != null
                        ? serializer.fromJson(body, tClass)
                        : Optional.empty());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy