Search in sources :

Example 11 with ForestJsonConverter

use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.

the class OkHttp3Executor method prepareHeaders.

protected void prepareHeaders(Request.Builder builder) {
    ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
    List<RequestNameValue> headerList = request.getHeaderNameValueList();
    String contentType = request.getContentType();
    String contentEncoding = request.getContentEncoding();
    String contentTypeHeaderName = ForestHeader.CONTENT_TYPE;
    String contentEncodingHeaderName = ForestHeader.CONTENT_ENCODING;
    if (headerList != null && !headerList.isEmpty()) {
        for (RequestNameValue nameValue : headerList) {
            String name = nameValue.getName();
            if (ForestHeader.CONTENT_TYPE.equalsIgnoreCase(name)) {
                contentTypeHeaderName = name;
            } else if (ForestHeader.CONTENT_ENCODING.equalsIgnoreCase(name)) {
                contentEncodingHeaderName = name;
            } else {
                builder.addHeader(name, MappingTemplate.getParameterValue(jsonConverter, nameValue.getValue()));
            }
        }
    }
    if (StringUtils.isNotEmpty(contentType)) {
        builder.addHeader(contentTypeHeaderName, contentType);
    }
    if (StringUtils.isNotEmpty(contentEncoding)) {
        builder.addHeader(contentEncodingHeaderName, contentEncoding);
    }
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue)

Example 12 with ForestJsonConverter

use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.

the class DefaultFormConvertor method encodeToString.

@Override
public String encodeToString(Object obj) {
    ForestJsonConverter jsonConverter = configuration.getJsonConverter();
    Map<String, Object> map = jsonConverter.convertObjectToMap(obj);
    List<RequestNameValue> nameValueList = new LinkedList<>();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        RequestNameValue nameValue = new RequestNameValue(entry.getKey(), MappingParameter.TARGET_BODY);
        nameValue.setValue(entry.getValue());
        nameValueList.add(nameValue);
    }
    nameValueList = processFromNameValueList(nameValueList, configuration);
    return formUrlEncodedString(nameValueList, StandardCharsets.UTF_8);
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 13 with ForestJsonConverter

use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.

the class DefaultFormConvertor method formUrlEncodedString.

private String formUrlEncodedString(List<RequestNameValue> nameValueList, Charset charset) {
    ForestJsonConverter jsonConverter = configuration.getJsonConverter();
    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < nameValueList.size(); i++) {
        RequestNameValue nameValue = nameValueList.get(i);
        if (!nameValue.isInBody()) {
            continue;
        }
        String name = nameValue.getName();
        Object value = nameValue.getValue();
        strBuilder.append(name);
        if (value != null) {
            value = MappingTemplate.getFormValueString(jsonConverter, value);
            strBuilder.append("=").append(URLEncoder.FORM_VALUE.encode(String.valueOf(value), charset));
        }
        if (i < nameValueList.size() - 1) {
            strBuilder.append("&");
        }
    }
    return strBuilder.toString();
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue)

Example 14 with ForestJsonConverter

use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.

the class QueryableURLBuilder method buildUrl.

@Override
public String buildUrl(ForestRequest request) {
    String url = request.getUrl();
    List<ForestQueryParameter> queryParameters = request.getQueryValues();
    StringBuilder paramBuilder = new StringBuilder();
    ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
    for (int i = 0; i < queryParameters.size(); i++) {
        ForestQueryParameter queryParam = queryParameters.get(i);
        String name = queryParam.getName();
        if (name != null) {
            paramBuilder.append(queryParam.getName());
        }
        String value = MappingTemplate.getParameterValue(jsonConverter, queryParam.getValue());
        if (value != null && request.getCharset() != null) {
            if (name != null) {
                paramBuilder.append('=');
            }
            String charset = queryParam.getCharset();
            if (StringUtils.isBlank(charset)) {
                charset = request.getCharset();
            }
            if (StringUtils.isBlank(charset)) {
                charset = "UTF-8";
            }
            String encodedValue = null;
            if (queryParam.isUrlencoded()) {
                encodedValue = URLUtils.allEncode(value, charset);
            } else {
                encodedValue = URLUtils.queryValueEncode(value, charset);
            }
            if (encodedValue != null) {
                paramBuilder.append(encodedValue);
            }
        }
        if (i < queryParameters.size() - 1) {
            paramBuilder.append('&');
        }
    }
    StringBuilder urlBuilder = new StringBuilder(url);
    String query = paramBuilder.toString();
    if (StringUtils.isNotEmpty(query)) {
        urlBuilder.append("?").append(query);
    }
    String ref = request.getRef();
    if (StringUtils.isNotEmpty(ref)) {
        urlBuilder.append("#").append(ref);
    }
    return urlBuilder.toString();
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ForestQueryParameter(com.dtflys.forest.http.ForestQueryParameter)

Example 15 with ForestJsonConverter

use of com.dtflys.forest.converter.json.ForestJsonConverter 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

ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)20 RequestNameValue (com.dtflys.forest.utils.RequestNameValue)6 Map (java.util.Map)4 Test (org.junit.Test)4 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)3 ForestQueryParameter (com.dtflys.forest.http.ForestQueryParameter)3 ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)3 NameValueRequestBody (com.dtflys.forest.http.body.NameValueRequestBody)3 ObjectRequestBody (com.dtflys.forest.http.body.ObjectRequestBody)3 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 ForestVariableUndefinedException (com.dtflys.forest.exceptions.ForestVariableUndefinedException)2 ForestURL (com.dtflys.forest.http.ForestURL)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 ContentType (com.dtflys.forest.backend.ContentType)1 AddressSource (com.dtflys.forest.callback.AddressSource)1 OnError (com.dtflys.forest.callback.OnError)1