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

org.catools.ws.rest.CHttpRestJsonClient Maven / Gradle / Ivy

package org.catools.ws.rest;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.catools.common.json.CJsonUtil;
import org.catools.common.tests.CTest;
import org.catools.ws.enums.CHttpRequestType;
import org.catools.ws.session.CSession;
import org.json.JSONObject;

public abstract class CHttpRestJsonClient extends CHttpRestClient {
    private JSONObject entity;

    public CHttpRestJsonClient(T testInstance, CHttpRequestType requestType, boolean useHttps, String targetURL) {
        this(testInstance, testInstance instanceof CSession ? (CSession)testInstance : null, requestType, useHttps, targetURL);
    }

    public CHttpRestJsonClient(T testInstance, CSession session, CHttpRequestType requestType, boolean useHttps, String targetURL) {
        super(testInstance, session, requestType, useHttps, targetURL);
        request.getHeaders().setContentType(ContentType.APPLICATION_JSON);
    }

    public JSONObject getEntity() {
        return entity;
    }

    public CHttpRestJsonClient setEntity(Object obj) {
        if (obj instanceof JSONObject) {
            this.entity = (JSONObject) obj;
        } else if (obj instanceof String) {
            this.entity = new JSONObject((String) obj);
        } else if (obj != null) {
            this.entity = new JSONObject(CJsonUtil.toString(obj));
        } else {
            this.entity = null;
        }
        return this;
    }

    @Override
    public HttpEntity buildHttpEntity() {
        if (entity != null) {
            return new StringEntity(entity.toString(), ContentType.APPLICATION_JSON);
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy