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