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