use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class HttpclientBodyBuilder method setFileBody.
@Override
protected void setFileBody(T httpReq, ForestRequest request, Charset charset, String contentType, LifeCycleHandler lifeCycleHandler) {
String boundary = request.getBoundary();
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
if (StringUtils.isNotEmpty(boundary)) {
entityBuilder.setBoundary(boundary);
}
// 解决文件名乱码问题
ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
Charset httpCharset = charset;
Charset itemCharset = StandardCharsets.UTF_8;
if (charset != null) {
itemCharset = charset;
}
boolean needSetMode = false;
for (ForestRequestBody item : request.body()) {
if (item instanceof NameValueRequestBody) {
needSetMode = true;
NameValueRequestBody nameValueItem = (NameValueRequestBody) item;
String name = nameValueItem.getName();
Object value = nameValueItem.getValue();
String partContentType = nameValueItem.getContentType();
addMultipart(entityBuilder, name, value, partContentType, itemCharset, jsonConverter);
} else if (item instanceof ObjectRequestBody) {
Object obj = ((ObjectRequestBody) item).getObject();
if (obj == null) {
continue;
}
needSetMode = true;
Map<String, Object> attrs = jsonConverter.convertObjectToMap(obj);
for (Map.Entry<String, Object> entry : attrs.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
addMultipart(entityBuilder, name, value, null, itemCharset, jsonConverter);
}
}
}
if (needSetMode) {
entityBuilder.setCharset(httpCharset);
entityBuilder.setMode(HttpMultipartMode.RFC6532);
}
List<ForestMultipart> multiparts = request.getMultiparts();
for (ForestMultipart multipart : multiparts) {
String name = multipart.getName();
String fileName = multipart.getOriginalFileName();
String partContentType = multipart.getContentType();
ContentType ctype = null;
if (StringUtils.isNotEmpty(partContentType)) {
ctype = ContentType.create(partContentType, httpCharset);
}
if (ctype == null) {
String mimeType = URLConnection.guessContentTypeFromName(fileName);
if (mimeType == null) {
// guess this is a video uploading
ctype = ContentType.create(com.dtflys.forest.backend.ContentType.MULTIPART_FORM_DATA, httpCharset);
} else {
ctype = ContentType.create(mimeType);
}
}
AbstractContentBody contentBody = null;
if (multipart.isFile()) {
contentBody = new HttpclientMultipartFileBody(request, multipart.getFile(), ctype, fileName, lifeCycleHandler);
} else {
contentBody = new HttpclientMultipartCommonBody(request, multipart, ctype, fileName, lifeCycleHandler);
}
entityBuilder.addPart(name, contentBody);
}
HttpEntity entity = entityBuilder.build();
httpReq.setEntity(entity);
}
use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class ForestBody method setNameValue.
public Object setNameValue(String key, Object value) {
if (key == null) {
return null;
}
NameValueRequestBody body = getNameValueBody(key);
Object oldValue = null;
if (key != null) {
oldValue = body.getValue();
body.setValue(value);
}
return oldValue;
}
use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class ForestBody method nameValuesMap.
/**
* 获取请求体中的键值对
* <p>仅包含{@link NameValueRequestBody}类型请求体项数据
*
* @return 键值对类型请求体项组成的 {@link Map}
*/
public Map<String, Object> nameValuesMap() {
Map<String, Object> map = new LinkedHashMap<>();
for (ForestRequestBody body : bodyItems) {
if (body instanceof NameValueRequestBody) {
NameValueRequestBody nameValueRequestBody = (NameValueRequestBody) body;
String name = nameValueRequestBody.getName();
if (!map.containsKey(name)) {
map.put(name, nameValueRequestBody.getValue());
}
}
}
return map;
}
Aggregations