Search in sources :

Example 1 with ByteArrayRequestBody

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

Aggregations

ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)1 ByteArrayRequestBody (com.dtflys.forest.http.body.ByteArrayRequestBody)1 NameValueRequestBody (com.dtflys.forest.http.body.NameValueRequestBody)1 ObjectRequestBody (com.dtflys.forest.http.body.ObjectRequestBody)1 StringRequestBody (com.dtflys.forest.http.body.StringRequestBody)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1