use of com.dtflys.forest.converter.json.ForestJsonConverter 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.converter.json.ForestJsonConverter 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.converter.json.ForestJsonConverter 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();
}
use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.
the class QueryableURLBuilder method buildUrl.
@Override
public String buildUrl(ForestRequest request) {
String url = request.getUrl();
List<ForestQueryParameter> queryParameters = request.getQueryValues();
StringBuilder paramBuilder = new StringBuilder();
ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
for (int i = 0; i < queryParameters.size(); i++) {
ForestQueryParameter queryParam = queryParameters.get(i);
String name = queryParam.getName();
if (name != null) {
paramBuilder.append(queryParam.getName());
}
String value = MappingTemplate.getParameterValue(jsonConverter, queryParam.getValue());
if (value != null && request.getCharset() != null) {
if (name != null) {
paramBuilder.append('=');
}
String charset = queryParam.getCharset();
if (StringUtils.isBlank(charset)) {
charset = request.getCharset();
}
if (StringUtils.isBlank(charset)) {
charset = "UTF-8";
}
String encodedValue = null;
if (queryParam.isUrlencoded()) {
encodedValue = URLUtils.allEncode(value, charset);
} else {
encodedValue = URLUtils.queryValueEncode(value, charset);
}
if (encodedValue != null) {
paramBuilder.append(encodedValue);
}
}
if (i < queryParameters.size() - 1) {
paramBuilder.append('&');
}
}
StringBuilder urlBuilder = new StringBuilder(url);
String query = paramBuilder.toString();
if (StringUtils.isNotEmpty(query)) {
urlBuilder.append("?").append(query);
}
String ref = request.getRef();
if (StringUtils.isNotEmpty(ref)) {
urlBuilder.append("#").append(ref);
}
return urlBuilder.toString();
}
use of com.dtflys.forest.converter.json.ForestJsonConverter 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);
}
Aggregations