Search in sources :

Example 1 with RequestNameValue

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

the class HttpclientExecutor method prepareHeaders.

public void prepareHeaders() {
    ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
    List<RequestNameValue> headerList = request.getHeaderNameValueList();
    String contentType = request.getContentType();
    String contentEncoding = request.getContentEncoding();
    String contentTypeHeaderName = ForestHeader.CONTENT_TYPE;
    String contentEncodingHeaderName = ForestHeader.CONTENT_ENCODING;
    if (headerList != null && !headerList.isEmpty()) {
        for (RequestNameValue nameValue : headerList) {
            String name = nameValue.getName();
            if (ForestHeader.CONTENT_TYPE.equalsIgnoreCase(name)) {
                contentTypeHeaderName = name;
            } else if (ForestHeader.CONTENT_ENCODING.equalsIgnoreCase(name)) {
                contentEncodingHeaderName = name;
            } else {
                httpRequest.setHeader(name, MappingTemplate.getParameterValue(jsonConverter, nameValue.getValue()));
            }
        }
    }
    if (StringUtils.isNotEmpty(contentType)) {
        httpRequest.setHeader(contentTypeHeaderName, contentType);
    }
    if (StringUtils.isNotEmpty(contentEncoding)) {
        httpRequest.setHeader(contentEncodingHeaderName, contentEncoding);
    }
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue)

Example 2 with RequestNameValue

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

the class DefaultFormConvertor method encodeRequestBody.

@Override
public byte[] encodeRequestBody(ForestBody body, Charset charset) {
    List<RequestNameValue> nameValueList = new LinkedList<>();
    if (charset == null) {
        charset = StandardCharsets.UTF_8;
    }
    for (ForestRequestBody bodyItem : body) {
        if (bodyItem instanceof SupportFormUrlEncoded) {
            nameValueList.addAll(((SupportFormUrlEncoded) bodyItem).getNameValueList(configuration));
        }
    }
    nameValueList = processFromNameValueList(nameValueList, configuration);
    String strBody = formUrlEncodedString(nameValueList, charset);
    byte[] bytes = strBody.getBytes(charset);
    return bytes;
}
Also used : ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) SupportFormUrlEncoded(com.dtflys.forest.http.body.SupportFormUrlEncoded) LinkedList(java.util.LinkedList)

Example 3 with RequestNameValue

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

the class DefaultFormConvertor method processFromNameValueList.

/**
 * 处理Form表单中的键值对列表
 * @param nameValueList 键值对列表
 * @return 处理过的新键值对列表
 */
protected List<RequestNameValue> processFromNameValueList(List<RequestNameValue> nameValueList, ForestConfiguration configuration) {
    List<RequestNameValue> newNameValueList = new LinkedList<>();
    for (RequestNameValue nameValue : nameValueList) {
        String name = nameValue.getName();
        Object value = nameValue.getValue();
        int target = nameValue.getTarget();
        processFormItem(newNameValueList, configuration, name, value, target);
    }
    return newNameValueList;
}
Also used : RequestNameValue(com.dtflys.forest.utils.RequestNameValue) LinkedList(java.util.LinkedList)

Example 4 with RequestNameValue

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

the class TestForestConfiguration method testDefaultHeaders.

@Test
public void testDefaultHeaders() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    List<RequestNameValue> defaultHeaders = new LinkedList<>();
    defaultHeaders.add(new RequestNameValue("Accept", "text/html", TARGET_HEADER));
    configuration.setDefaultHeaders(defaultHeaders);
    assertEquals(defaultHeaders, configuration.getDefaultHeaders());
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with RequestNameValue

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

the class ForestMethod method getNameValueListFromObjectWithJSON.

private List<RequestNameValue> getNameValueListFromObjectWithJSON(MappingParameter parameter, ForestConfiguration configuration, Object obj, ForestRequestType type) {
    Map<String, Object> propMap = ReflectUtils.convertObjectToMap(obj, configuration);
    List<RequestNameValue> nameValueList = new ArrayList<>();
    for (Map.Entry<String, Object> entry : propMap.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        if (value != null) {
            RequestNameValue nameValue = new RequestNameValue(name, value, parameter.isUnknownTarget() ? type.getDefaultParamTarget() : parameter.getTarget(), parameter.getPartContentType());
            nameValueList.add(nameValue);
        }
    }
    return nameValueList;
}
Also used : RequestNameValue(com.dtflys.forest.utils.RequestNameValue) ForestQueryMap(com.dtflys.forest.http.ForestQueryMap)

Aggregations

RequestNameValue (com.dtflys.forest.utils.RequestNameValue)18 LinkedList (java.util.LinkedList)7 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)6 Map (java.util.Map)3 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 ForestQueryMap (com.dtflys.forest.http.ForestQueryMap)2 ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Test (org.junit.Test)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 VariableScope (com.dtflys.forest.config.VariableScope)1 ForestAddress (com.dtflys.forest.http.ForestAddress)1