Search in sources :

Example 6 with ObjectRequestBody

use of com.dtflys.forest.http.body.ObjectRequestBody 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 ObjectRequestBody

use of com.dtflys.forest.http.body.ObjectRequestBody in project forest by dromara.

the class ForestRequest method addBody.

/**
 * 添加 Map 类型 Body 数据
 * <p>将 Map 的 key 作为键值对的 key
 * <p>Map 的 value 作为键值对的 value
 * <p>批量插入到请求的请求体中
 *
 * @param bodyMap 字段名
 * @param contentType 该请求体项的Content-Type
 * @return {@link ForestRequest}类实例
 * @since 1.5.4
 */
public ForestRequest<T> addBody(Map bodyMap, String contentType) {
    if (bodyMap == null) {
        return this;
    }
    if (bodyMap.isEmpty()) {
        addBody(new ObjectRequestBody(bodyMap));
        return this;
    }
    for (Object key : bodyMap.keySet()) {
        Object value = bodyMap.get(key);
        addBody(String.valueOf(key), contentType, value);
    }
    return this;
}
Also used : ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody)

Aggregations

ObjectRequestBody (com.dtflys.forest.http.body.ObjectRequestBody)7 ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)5 NameValueRequestBody (com.dtflys.forest.http.body.NameValueRequestBody)4 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)3 Map (java.util.Map)3 StringRequestBody (com.dtflys.forest.http.body.StringRequestBody)2 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)2 LinkedHashMap (java.util.LinkedHashMap)2 ContentType (com.dtflys.forest.backend.ContentType)1 ByteArrayRequestBody (com.dtflys.forest.http.body.ByteArrayRequestBody)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