Search in sources :

Example 6 with NameValueRequestBody

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);
}
Also used : AbstractContentBody(org.apache.http.entity.mime.content.AbstractContentBody) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ContentType(org.apache.http.entity.ContentType) HttpEntity(org.apache.http.HttpEntity) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) Charset(java.nio.charset.Charset) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody)

Example 7 with NameValueRequestBody

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;
}
Also used : NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody)

Example 8 with NameValueRequestBody

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;
}
Also used : NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

NameValueRequestBody (com.dtflys.forest.http.body.NameValueRequestBody)8 ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)4 ObjectRequestBody (com.dtflys.forest.http.body.ObjectRequestBody)4 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)2 ContentType (com.dtflys.forest.backend.ContentType)1 ByteArrayRequestBody (com.dtflys.forest.http.body.ByteArrayRequestBody)1 StringRequestBody (com.dtflys.forest.http.body.StringRequestBody)1 RequestNameValue (com.dtflys.forest.utils.RequestNameValue)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 HttpEntity (org.apache.http.HttpEntity)1 ContentType (org.apache.http.entity.ContentType)1 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)1 AbstractContentBody (org.apache.http.entity.mime.content.AbstractContentBody)1