Search in sources :

Example 1 with ForestBody

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

the class DefaultBinaryConverter method encodeRequestBody.

@Override
public byte[] encodeRequestBody(ForestRequest request, Charset charset) {
    ForestBody reqBody = request.body();
    List<ForestMultipart> multiparts = request.getMultiparts();
    List<byte[]> byteList = new LinkedList<>();
    int size = 0;
    for (ForestMultipart multipart : multiparts) {
        byte[] byteArray = multipart.getBytes();
        byteList.add(byteArray);
        size += byteArray.length;
    }
    for (ForestRequestBody body : reqBody) {
        byte[] byteArray = body.getByteArray();
        byteList.add(byteArray);
        size += byteArray.length;
    }
    byte[] bytes = new byte[size];
    int pos = 0;
    for (byte[] bytesItem : byteList) {
        for (int i = 0; i < bytesItem.length; i++) {
            bytes[pos + i] = bytesItem[i];
        }
        pos += bytesItem.length;
    }
    return bytes;
}
Also used : ForestBody(com.dtflys.forest.http.ForestBody) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) LinkedList(java.util.LinkedList)

Example 2 with ForestBody

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

the class AbstractBodyBuilder method buildBody.

/**
 * 构建请求体
 * @param httpRequest 后端http请求对象
 * @param request Forest请求对象
 * @param lifeCycleHandler 生命周期处理器
 */
@Override
public void buildBody(T httpRequest, ForestRequest request, LifeCycleHandler lifeCycleHandler) {
    String contentType = request.getContentType();
    if (StringUtils.isEmpty(contentType)) {
        contentType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
    }
    String[] typeGroup = contentType.split(";[ ]*charset=");
    String mineType = typeGroup[0];
    String strCharset = request.getCharset();
    Charset charset = null;
    boolean mergeCharset = typeGroup.length > 1;
    if (StringUtils.isEmpty(strCharset)) {
        if (typeGroup.length > 1) {
            strCharset = typeGroup[1];
            charset = Charset.forName(strCharset);
        } else {
            charset = StandardCharsets.UTF_8;
        }
    } else {
        charset = Charset.forName(strCharset);
    }
    if (StringUtils.isEmpty(mineType)) {
        mineType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
    }
    ContentType mineContentType = new ContentType(mineType);
    String ctypeWithoutParams = mineContentType.toStringWithoutParameters();
    ForestEncoder encoder = request.getEncoder();
    if (encoder != null) {
        byte[] bodyBytes = encoder.encodeRequestBody(request, charset);
        setBinaryBody(httpRequest, request, charset, ctypeWithoutParams, bodyBytes, mergeCharset);
        return;
    }
    ForestBody reqBody = request.getBody();
    boolean needRequestBody = request.getType().isNeedBody() || !reqBody.isEmpty() || !request.getMultiparts().isEmpty();
    if (needRequestBody) {
        ForestDataType bodyType = request.bodyType();
        if (bodyType == null || bodyType == ForestDataType.AUTO) {
            bodyType = mineContentType.bodyType();
        }
        if (bodyType == ForestDataType.MULTIPART) {
            setFileBody(httpRequest, request, charset, ctypeWithoutParams, lifeCycleHandler);
            return;
        }
        ForestEncoder bodyEncoder = (ForestEncoder) request.getConfiguration().getConverterMap().get(bodyType);
        if (bodyEncoder == null) {
            bodyEncoder = (ForestEncoder) request.getConfiguration().getConverterMap().get(ForestDataType.TEXT);
        }
        byte[] bodyBytes = bodyEncoder.encodeRequestBody(request, charset);
        setBinaryBody(httpRequest, request, charset, ctypeWithoutParams, bodyBytes, mergeCharset);
    }
}
Also used : ForestEncoder(com.dtflys.forest.converter.ForestEncoder) ForestBody(com.dtflys.forest.http.ForestBody) ContentType(com.dtflys.forest.backend.ContentType) Charset(java.nio.charset.Charset) ForestDataType(com.dtflys.forest.utils.ForestDataType)

Aggregations

ForestBody (com.dtflys.forest.http.ForestBody)2 ContentType (com.dtflys.forest.backend.ContentType)1 ForestEncoder (com.dtflys.forest.converter.ForestEncoder)1 ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)1 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)1 ForestDataType (com.dtflys.forest.utils.ForestDataType)1 Charset (java.nio.charset.Charset)1 LinkedList (java.util.LinkedList)1