Search in sources :

Example 11 with RequestNameValue

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

the class NameValueRequestBody method getNameValueList.

@Override
public List<RequestNameValue> getNameValueList(ForestConfiguration configuration) {
    List<RequestNameValue> nameValueList = new ArrayList<>(1);
    nameValueList.add(new RequestNameValue(name, value, MappingParameter.TARGET_BODY));
    return nameValueList;
}
Also used : RequestNameValue(com.dtflys.forest.utils.RequestNameValue) ArrayList(java.util.ArrayList)

Example 12 with RequestNameValue

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

the class OkHttp3Executor method prepareHeaders.

protected void prepareHeaders(Request.Builder builder) {
    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 {
                builder.addHeader(name, MappingTemplate.getParameterValue(jsonConverter, nameValue.getValue()));
            }
        }
    }
    if (StringUtils.isNotEmpty(contentType)) {
        builder.addHeader(contentTypeHeaderName, contentType);
    }
    if (StringUtils.isNotEmpty(contentEncoding)) {
        builder.addHeader(contentEncodingHeaderName, contentEncoding);
    }
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue)

Example 13 with RequestNameValue

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

the class DefaultFormConvertor method encodeToString.

@Override
public String encodeToString(Object obj) {
    ForestJsonConverter jsonConverter = configuration.getJsonConverter();
    Map<String, Object> map = jsonConverter.convertObjectToMap(obj);
    List<RequestNameValue> nameValueList = new LinkedList<>();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        RequestNameValue nameValue = new RequestNameValue(entry.getKey(), MappingParameter.TARGET_BODY);
        nameValue.setValue(entry.getValue());
        nameValueList.add(nameValue);
    }
    nameValueList = processFromNameValueList(nameValueList, configuration);
    return formUrlEncodedString(nameValueList, StandardCharsets.UTF_8);
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 14 with RequestNameValue

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

the class DefaultFormConvertor method processFormItem.

/**
 * 处理Form表单中的项
 * @param newNameValueList 键值对列表
 * @param configuration Forest配置
 * @param name 表单项目名
 * @param value 表单项目值
 * @param target 请求目标位置
 */
protected void processFormItem(List<RequestNameValue> newNameValueList, ForestConfiguration configuration, String name, Object value, int target) {
    if (StringUtils.isEmpty(name) && value == null) {
        return;
    }
    if (value != null) {
        Class itemClass = value.getClass();
        boolean needCollapse = false;
        if (value instanceof Collection) {
            Collection collection = (Collection) value;
            if (collection.size() <= 8) {
                for (Object item : collection) {
                    if (!ReflectUtils.isPrimaryType(item.getClass())) {
                        needCollapse = true;
                        break;
                    }
                }
            }
        } else if (itemClass.isArray() && !ReflectUtils.isPrimaryArrayType(itemClass)) {
            needCollapse = true;
        }
        if (needCollapse) {
            if (value instanceof Collection) {
                processFormCollectionItem(newNameValueList, configuration, name, (Collection) value, target);
            } else if (itemClass.isArray()) {
                processFormArrayItem(newNameValueList, configuration, name, value, target);
            }
        } else if (ReflectUtils.isPrimaryType(itemClass) || ReflectUtils.isPrimaryArrayType(itemClass) || value instanceof Collection) {
            newNameValueList.add(new RequestNameValue(name, value, target));
        } else if (value instanceof Map) {
            processFormMapItem(newNameValueList, configuration, name, (Map) value, target);
        } else {
            Map<String, Object> itemAttrs = ReflectUtils.convertObjectToMap(value, configuration);
            for (Map.Entry<String, Object> entry : itemAttrs.entrySet()) {
                String subAttrName = entry.getKey();
                Object subAttrValue = entry.getValue();
                String subName = name + "." + subAttrName;
                processFormItem(newNameValueList, configuration, subName, subAttrValue, target);
            }
        }
    }
}
Also used : RequestNameValue(com.dtflys.forest.utils.RequestNameValue) Collection(java.util.Collection) Map(java.util.Map)

Example 15 with RequestNameValue

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

the class DefaultFormConvertor method formUrlEncodedString.

private String formUrlEncodedString(List<RequestNameValue> nameValueList, Charset charset) {
    ForestJsonConverter jsonConverter = configuration.getJsonConverter();
    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < nameValueList.size(); i++) {
        RequestNameValue nameValue = nameValueList.get(i);
        if (!nameValue.isInBody()) {
            continue;
        }
        String name = nameValue.getName();
        Object value = nameValue.getValue();
        strBuilder.append(name);
        if (value != null) {
            value = MappingTemplate.getFormValueString(jsonConverter, value);
            strBuilder.append("=").append(URLEncoder.FORM_VALUE.encode(String.valueOf(value), charset));
        }
        if (i < nameValueList.size() - 1) {
            strBuilder.append("&");
        }
    }
    return strBuilder.toString();
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue)

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