use of com.dtflys.forest.backend.ContentType 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);
}
use of com.dtflys.forest.backend.ContentType in project forest by dromara.
the class OkHttp3ForestResponse method setupContentTypeAndCharset.
/**
* @author designer[19901753334@163.com]
* @date 2021/12/8 23:51
*/
private void setupContentTypeAndCharset() {
MediaType mediaType = body.contentType();
if (mediaType != null) {
String type = mediaType.type();
String subType = mediaType.subtype();
this.contentType = new ContentType(type, subType);
Charset charset = mediaType.charset();
if (charset != null) {
this.charset = charset.name();
return;
}
}
setupResponseCharset();
}
use of com.dtflys.forest.backend.ContentType in project forest by dromara.
the class TestUploadClient method testMixtureUploadImageWithJSONBodyParams.
@Test
public void testMixtureUploadImageWithJSONBodyParams() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
if (path.startsWith("/") && isWindows()) {
path = path.substring(1);
}
File file = new File(path);
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
ForestRequest request = uploadClient.imageUploadWithJSONBodyParams("img1.jpg", file, map);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(1, multipartList.size());
ForestMultipart multipart = multipartList.get(0);
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
assertTrue(multipart instanceof FileMultipart);
assertEquals("file", multipart.getName());
assertEquals("img1.jpg", multipart.getOriginalFileName());
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
assertEquals(1, multiparts.size());
FileItem fileItem = multiparts.get(0);
assertEquals("img1.jpg", fileItem.getName());
assertEquals("image/jpeg", fileItem.getContentType());
}).assertMultipart("params", params -> {
assertEquals(1, params.size());
FileItem item = params.get(0);
ContentType contentType = new ContentType(item.getContentType());
assertEquals("application/json", contentType.toStringWithoutParameters());
try {
assertEquals(JSON.toJSONString(map), IOUtils.toString(item.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.dtflys.forest.backend.ContentType 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);
}
}
use of com.dtflys.forest.backend.ContentType in project forest by dromara.
the class TestUploadClient method testMixtureUploadImage.
@Test
public void testMixtureUploadImage() throws InterruptedException, FileUploadException {
server.enqueue(new MockResponse().setBody(EXPECTED));
String path = Objects.requireNonNull(this.getClass().getResource("/test-img.jpg")).getPath();
if (path.startsWith("/") && isWindows()) {
path = path.substring(1);
}
File file = new File(path);
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
ForestRequest request = uploadClient.imageUploadWithMapParams("img1.jpg", file, map);
assertNotNull(request);
List<ForestMultipart> multipartList = request.getMultiparts();
assertEquals(1, multipartList.size());
ForestMultipart multipart = multipartList.get(0);
// assertTrue(Map.class.isAssignableFrom(request.getMethod().getReturnClass()));
assertTrue(multipart instanceof FileMultipart);
assertEquals("file", multipart.getName());
assertEquals("img1.jpg", multipart.getOriginalFileName());
Map result = (Map) request.execute();
assertNotNull(result);
MockServerRequest.mockRequest(server).assertMultipart("file", multiparts -> {
assertEquals(1, multiparts.size());
FileItem fileItem = multiparts.get(0);
assertEquals("img1.jpg", fileItem.getName());
assertEquals("image/jpeg", fileItem.getContentType());
}).assertMultipart("a", params -> {
assertEquals(1, params.size());
FileItem item = params.get(0);
ContentType contentType = new ContentType(item.getContentType());
assertEquals("text/plain", contentType.toStringWithoutParameters());
try {
assertEquals("1", IOUtils.toString(item.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}).assertMultipart("b", params -> {
assertEquals(1, params.size());
FileItem item = params.get(0);
ContentType contentType = new ContentType(item.getContentType());
assertEquals("text/plain", contentType.toStringWithoutParameters());
try {
assertEquals("2", IOUtils.toString(item.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
Aggregations