use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class DefaultTextConverter method encodeRequestBody.
@Override
public byte[] encodeRequestBody(ForestBody body, Charset charset) {
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
StringBuilder builder = new StringBuilder();
ForestRequestBody lastBodyItem = null;
for (ForestRequestBody bodyItem : body) {
if (lastBodyItem != null && lastBodyItem instanceof NameValueRequestBody) {
builder.append("&");
}
builder.append(bodyItem.toString());
lastBodyItem = bodyItem;
}
String strBody = builder.toString();
byte[] bytes = strBody.getBytes(charset);
return bytes;
}
use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class ForestRequest method getDataNameValueList.
public List<RequestNameValue> getDataNameValueList() {
List<RequestNameValue> nameValueList = new ArrayList<>();
for (ForestRequestBody item : body) {
if (item instanceof NameValueRequestBody) {
NameValueRequestBody nameValueRequestBody = (NameValueRequestBody) item;
String name = nameValueRequestBody.getName();
Object value = nameValueRequestBody.getValue();
RequestNameValue nameValue = new RequestNameValue(name, value, TARGET_BODY, nameValueRequestBody.getContentType());
nameValueList.add(nameValue);
}
}
return nameValueList;
}
use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class ForestBody method nameValuesMapWithObject.
/**
* 获取请求体中的键值对
* <p>包含{@link NameValueRequestBody}类型请求体项、以及{@link ObjectRequestBody}类请求体项拆解出来的键值对数据
*
* @return
*/
public Map<String, Object> nameValuesMapWithObject() {
Map<String, Object> map = new LinkedHashMap<>();
ForestJsonConverter jsonConverter = configuration.getJsonConverter();
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());
}
} else if (body instanceof ObjectRequestBody) {
ObjectRequestBody objectRequestBody = (ObjectRequestBody) body;
Map<String, Object> keyValueMap = jsonConverter.convertObjectToMap(objectRequestBody.getObject());
for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
if (!map.containsKey(name)) {
map.put(name, value);
}
}
}
}
return map;
}
use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class OkHttp3BodyBuilder method setFileBody.
@Override
protected void setFileBody(Request.Builder builder, ForestRequest request, Charset charset, String contentType, LifeCycleHandler lifeCycleHandler) {
String boundary = request.getBoundary();
MultipartBody.Builder bodyBuilder = null;
if (StringUtils.isNotEmpty(boundary)) {
bodyBuilder = new MultipartBody.Builder(boundary);
} else {
bodyBuilder = new MultipartBody.Builder();
}
ContentType objContentType = new ContentType(contentType);
MediaType mediaType = MediaType.parse(objContentType.toStringWithoutParameters());
if ("multipart".equals(mediaType.type())) {
bodyBuilder.setType(mediaType);
}
ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
List<ForestMultipart> multiparts = request.getMultiparts();
for (ForestRequestBody item : request.body()) {
if (item instanceof NameValueRequestBody) {
NameValueRequestBody nameValueItem = (NameValueRequestBody) item;
String name = nameValueItem.getName();
Object value = nameValueItem.getValue();
String partContentType = nameValueItem.getContentType();
addMultipart(bodyBuilder, name, value, partContentType, charset, jsonConverter);
} else if (item instanceof ObjectRequestBody) {
Object obj = ((ObjectRequestBody) item).getObject();
if (obj == null) {
continue;
}
Map<String, Object> attrs = jsonConverter.convertObjectToMap(obj);
for (Map.Entry<String, Object> entry : attrs.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
addMultipart(bodyBuilder, name, value, null, charset, jsonConverter);
}
}
}
for (ForestMultipart multipart : multiparts) {
RequestBody fileBody = createFileBody(request, multipart, charset, lifeCycleHandler);
bodyBuilder.addFormDataPart(multipart.getName(), multipart.getOriginalFileName(), fileBody);
}
MultipartBody body = bodyBuilder.build();
builder.method(request.getType().getName(), body);
}
use of com.dtflys.forest.http.body.NameValueRequestBody in project forest by dromara.
the class ForestJsonConverter method encodeRequestBody.
@Override
default byte[] encodeRequestBody(ForestBody body, Charset charset) {
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
List<ForestRequestBody> bodyList = new LinkedList(body);
if (!bodyList.isEmpty()) {
Object toJsonObj = bodyList;
if (bodyList.size() == 1) {
toJsonObj = bodyList.get(0);
} else {
Map<String, Object> jsonMap = null;
List jsonArray = null;
for (ForestRequestBody bodyItem : bodyList) {
if (bodyItem instanceof NameValueRequestBody) {
if (jsonMap == null) {
jsonMap = new LinkedHashMap<>(bodyList.size());
}
jsonMap.put(((NameValueRequestBody) bodyItem).getName(), ((NameValueRequestBody) bodyItem).getValue());
} else if (bodyItem instanceof StringRequestBody) {
String content = bodyItem.toString();
Map subMap = this.convertObjectToMap(content);
if (subMap != null) {
if (jsonMap == null) {
jsonMap = new LinkedHashMap<>(bodyList.size());
}
jsonMap.putAll(subMap);
} else {
if (jsonArray == null) {
jsonArray = new LinkedList<>();
}
jsonArray.add(content);
}
} else if (bodyItem instanceof ObjectRequestBody) {
Object obj = ((ObjectRequestBody) bodyItem).getObject();
if (obj == null) {
continue;
}
if (obj instanceof List) {
if (jsonArray == null) {
jsonArray = new LinkedList();
}
jsonArray.addAll((List) obj);
} else {
Map subMap = this.convertObjectToMap(obj);
if (subMap == null) {
continue;
}
if (jsonMap == null) {
jsonMap = new LinkedHashMap<>(bodyList.size());
}
jsonMap.putAll(subMap);
}
}
}
if (jsonMap != null) {
toJsonObj = jsonMap;
} else if (jsonArray != null) {
toJsonObj = jsonArray;
}
}
String text = null;
if (toJsonObj instanceof CharSequence || toJsonObj instanceof StringRequestBody) {
text = toJsonObj.toString();
return text.getBytes(charset);
} else if (toJsonObj instanceof ObjectRequestBody) {
text = this.encodeToString(((ObjectRequestBody) toJsonObj).getObject());
return text.getBytes(charset);
} else if (toJsonObj instanceof NameValueRequestBody) {
Map<String, Object> subMap = new HashMap<>(1);
subMap.put(((NameValueRequestBody) toJsonObj).getName(), ((NameValueRequestBody) toJsonObj).getValue());
text = this.encodeToString(subMap);
return text.getBytes(charset);
} else if (toJsonObj instanceof ByteArrayRequestBody) {
byte[] bytes = ((ByteArrayRequestBody) toJsonObj).getByteArray();
return bytes;
} else {
text = this.encodeToString(toJsonObj);
return text.getBytes(charset);
}
}
return new byte[0];
}
Aggregations