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;
}
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);
}
}
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);
}
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);
}
}
}
}
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();
}
Aggregations