[Java] apache-httpclient serialize support custom contentType (#13058)

* java http-client multiPartBuilder support custom contentType for textBody

* java apache-httpclient serialize support custom contentType

* modify getContentType method
This commit is contained in:
jiangyuan
2022-08-04 00:41:18 +08:00
committed by GitHub
parent 1e3a39b460
commit 72991e6be3
2 changed files with 34 additions and 18 deletions

View File

@@ -57,6 +57,7 @@ import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.Paths;
@@ -707,14 +708,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
/**
* Parse content type object from header value
*/
private ContentType getContentType(String headerValue) {
return ContentType.getByMimeType(headerValue);
private ContentType getContentType(String headerValue) throws ApiException {
try {
return ContentType.parse(headerValue);
} catch (ParseException e) {
throw new ApiException("Could not parse content type " + headerValue);
}
}
/**
* Get content type of a response or null if one was not provided
*/
private String getResponseMimeType(HttpResponse response) {
private String getResponseMimeType(HttpResponse response) throws ApiException {
Header contentTypeHeader = response.getFirstHeader("Content-Type");
if (contentTypeHeader != null) {
return getContentType(contentTypeHeader.getValue()).getMimeType();
@@ -747,21 +752,24 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (File) value);
} else if (value instanceof byte[]) {
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
} else {
Charset charset = contentType.getCharset();
if (charset != null) {
ContentType customContentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset);
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()),
customContentType);
} else {
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()));
}
}
}
return multiPartBuilder.build();
} else if (mimeType.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
List<NameValuePair> formValues = new ArrayList<>();
for (Entry<String, Object> paramEntry : formParams.entrySet()) {
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
}
try {
return new UrlEncodedFormEntity(formValues);
} catch (UnsupportedEncodingException e) {
throw new ApiException(e);
}
return new UrlEncodedFormEntity(formValues, contentType.getCharset());
} else {
// Handle files with unknown content type
if (obj instanceof File) {

View File

@@ -65,6 +65,7 @@ import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.Paths;
@@ -662,14 +663,18 @@ public class ApiClient extends JavaTimeFormatter {
/**
* Parse content type object from header value
*/
private ContentType getContentType(String headerValue) {
return ContentType.getByMimeType(headerValue);
private ContentType getContentType(String headerValue) throws ApiException {
try {
return ContentType.parse(headerValue);
} catch (ParseException e) {
throw new ApiException("Could not parse content type " + headerValue);
}
}
/**
* Get content type of a response or null if one was not provided
*/
private String getResponseMimeType(HttpResponse response) {
private String getResponseMimeType(HttpResponse response) throws ApiException {
Header contentTypeHeader = response.getFirstHeader("Content-Type");
if (contentTypeHeader != null) {
return getContentType(contentTypeHeader.getValue()).getMimeType();
@@ -702,21 +707,24 @@ public class ApiClient extends JavaTimeFormatter {
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (File) value);
} else if (value instanceof byte[]) {
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
} else {
Charset charset = contentType.getCharset();
if (charset != null) {
ContentType customContentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset);
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()),
customContentType);
} else {
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()));
}
}
}
return multiPartBuilder.build();
} else if (mimeType.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
List<NameValuePair> formValues = new ArrayList<>();
for (Entry<String, Object> paramEntry : formParams.entrySet()) {
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
}
try {
return new UrlEncodedFormEntity(formValues);
} catch (UnsupportedEncodingException e) {
throw new ApiException(e);
}
return new UrlEncodedFormEntity(formValues, contentType.getCharset());
} else {
// Handle files with unknown content type
if (obj instanceof File) {