Search in sources :

Example 6 with ForestDataType

use of com.dtflys.forest.utils.ForestDataType in project forest by dromara.

the class AbstractBodyBuilder method buildBody.

/**
 * 构建请求体
 * @param httpRequest 后端http请求对象
 * @param request Forest请求对象
 * @param lifeCycleHandler 生命周期处理器
 */
@Override
public void buildBody(T httpRequest, ForestRequest request, LifeCycleHandler lifeCycleHandler) {
    String contentType = request.getContentType();
    if (StringUtils.isEmpty(contentType)) {
        contentType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
    }
    String[] typeGroup = contentType.split(";[ ]*charset=");
    String mineType = typeGroup[0];
    String strCharset = request.getCharset();
    Charset charset = null;
    boolean mergeCharset = typeGroup.length > 1;
    if (StringUtils.isEmpty(strCharset)) {
        if (typeGroup.length > 1) {
            strCharset = typeGroup[1];
            charset = Charset.forName(strCharset);
        } else {
            charset = StandardCharsets.UTF_8;
        }
    } else {
        charset = Charset.forName(strCharset);
    }
    if (StringUtils.isEmpty(mineType)) {
        mineType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
    }
    ContentType mineContentType = new ContentType(mineType);
    String ctypeWithoutParams = mineContentType.toStringWithoutParameters();
    ForestEncoder encoder = request.getEncoder();
    if (encoder != null) {
        byte[] bodyBytes = encoder.encodeRequestBody(request, charset);
        setBinaryBody(httpRequest, request, charset, ctypeWithoutParams, bodyBytes, mergeCharset);
        return;
    }
    ForestBody reqBody = request.getBody();
    boolean needRequestBody = request.getType().isNeedBody() || !reqBody.isEmpty() || !request.getMultiparts().isEmpty();
    if (needRequestBody) {
        ForestDataType bodyType = request.bodyType();
        if (bodyType == null || bodyType == ForestDataType.AUTO) {
            bodyType = mineContentType.bodyType();
        }
        if (bodyType == ForestDataType.MULTIPART) {
            setFileBody(httpRequest, request, charset, ctypeWithoutParams, lifeCycleHandler);
            return;
        }
        ForestEncoder bodyEncoder = (ForestEncoder) request.getConfiguration().getConverterMap().get(bodyType);
        if (bodyEncoder == null) {
            bodyEncoder = (ForestEncoder) request.getConfiguration().getConverterMap().get(ForestDataType.TEXT);
        }
        byte[] bodyBytes = bodyEncoder.encodeRequestBody(request, charset);
        setBinaryBody(httpRequest, request, charset, ctypeWithoutParams, bodyBytes, mergeCharset);
    }
}
Also used : ForestEncoder(com.dtflys.forest.converter.ForestEncoder) ForestBody(com.dtflys.forest.http.ForestBody) ContentType(com.dtflys.forest.backend.ContentType) Charset(java.nio.charset.Charset) ForestDataType(com.dtflys.forest.utils.ForestDataType)

Example 7 with ForestDataType

use of com.dtflys.forest.utils.ForestDataType in project forest by dromara.

the class TestForestConfiguration method testConverterMap.

@Test
public void testConverterMap() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    assertNotNull(configuration.getConverterMap());
    Map<ForestDataType, ForestConverter> converterMap = new HashMap<>();
    converterMap.put(ForestDataType.JSON, new ForestFastjsonConverter());
    configuration.setConverterMap(converterMap);
    assertEquals(converterMap, configuration.getConverterMap());
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) HashMap(java.util.HashMap) ForestConverter(com.dtflys.forest.converter.ForestConverter) ForestDataType(com.dtflys.forest.utils.ForestDataType) Test(org.junit.Test)

Aggregations

ForestDataType (com.dtflys.forest.utils.ForestDataType)7 ForestConverter (com.dtflys.forest.converter.ForestConverter)3 ContentType (com.dtflys.forest.backend.ContentType)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 ForestRequest (com.dtflys.forest.http.ForestRequest)2 ObjectWithReflectiveEqualsHashCodeToString (org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 AddressSource (com.dtflys.forest.callback.AddressSource)1 OnError (com.dtflys.forest.callback.OnError)1 OnLoadCookie (com.dtflys.forest.callback.OnLoadCookie)1 OnProgress (com.dtflys.forest.callback.OnProgress)1 OnRedirection (com.dtflys.forest.callback.OnRedirection)1 OnSaveCookie (com.dtflys.forest.callback.OnSaveCookie)1 OnSuccess (com.dtflys.forest.callback.OnSuccess)1 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 VariableScope (com.dtflys.forest.config.VariableScope)1