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

org.logdoc.fairhttp.flow.FairResponse Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package org.logdoc.fairhttp.flow;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.logdoc.fairhttp.diag.CallData;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Denis Danilin | [email protected]
 * 08.01.2023 19:03
 * fairhttp ☭ sweat and blood
 */
public final class FairResponse {
    public int code;
    public String message;
    public byte[] body;

    public final Map headers = new HashMap<>();
    public CallData callData;

    public String asString() {
        if (body == null)
            return null;

        return new String(body, StandardCharsets.UTF_8);
    }

    public JsonNode asJson() {
        if (body != null)
            try {
                return new ObjectMapper().readTree(body);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        return null;
    }

    public Document asXml() {
        if (body != null)
            try {
                return DocumentBuilderFactory.newInstance().newDocumentBuilder()
                        .parse(new InputSource(new ByteArrayInputStream(body)));
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }

        return null;
    }

    public  T fromJson(final Class cls) {
        if (body == null)
            return null;

        final ObjectMapper m = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            return m.treeToValue(m.readTree(body), cls);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy