Search in sources :

Example 6 with ForestRequestBody

use of com.dtflys.forest.http.ForestRequestBody 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);
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ContentType(com.dtflys.forest.backend.ContentType) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody) Map(java.util.Map) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody)

Example 7 with ForestRequestBody

use of com.dtflys.forest.http.ForestRequestBody 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];
}
Also used : ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) StringRequestBody(com.dtflys.forest.http.body.StringRequestBody) ByteArrayRequestBody(com.dtflys.forest.http.body.ByteArrayRequestBody) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with ForestRequestBody

use of com.dtflys.forest.http.ForestRequestBody in project forest by dromara.

the class ForestJaxbConverter method encodeRequestBody.

@Override
public byte[] encodeRequestBody(ForestBody body, Charset charset) {
    StringBuilder builder = new StringBuilder();
    for (ForestRequestBody item : body) {
        if (item instanceof ObjectRequestBody) {
            Object obj = ((ObjectRequestBody) item).getObject();
            String text = encodeToString(obj);
            builder.append(text);
        } else if (item instanceof StringRequestBody) {
            builder.append(((StringRequestBody) item).getContent());
        }
    }
    return builder.toString().getBytes(charset);
}
Also used : ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) StringRequestBody(com.dtflys.forest.http.body.StringRequestBody)

Example 9 with ForestRequestBody

use of com.dtflys.forest.http.ForestRequestBody 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)

Aggregations

ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)9 ObjectRequestBody (com.dtflys.forest.http.body.ObjectRequestBody)5 NameValueRequestBody (com.dtflys.forest.http.body.NameValueRequestBody)4 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)4 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)3 StringRequestBody (com.dtflys.forest.http.body.StringRequestBody)3 LinkedList (java.util.LinkedList)3 RequestNameValue (com.dtflys.forest.utils.RequestNameValue)2 Map (java.util.Map)2 ContentType (com.dtflys.forest.backend.ContentType)1 AddressSource (com.dtflys.forest.callback.AddressSource)1 OnError (com.dtflys.forest.callback.OnError)1 OnLoadCookie (com.dtflys.forest.callback.OnLoadCookie)1 OnProgress (com.dtflys.forest.callback.OnProgress)1 OnRedirection (com.dtflys.forest.callback.OnRedirection)1 OnSaveCookie (com.dtflys.forest.callback.OnSaveCookie)1 OnSuccess (com.dtflys.forest.callback.OnSuccess)1 VariableScope (com.dtflys.forest.config.VariableScope)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 ForestAddress (com.dtflys.forest.http.ForestAddress)1