Skip to content

Commit

Permalink
[Java] apache-httpclient serialize support custom contentType (#13058)
Browse files Browse the repository at this point in the history
* java http-client multiPartBuilder support custom contentType for textBody

* java apache-httpclient serialize support custom contentType

* modify getContentType method
  • Loading branch information
JoeCqupt authored Aug 3, 2022
1 parent 1e3a39b commit 72991e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -748,7 +753,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
} else if (value instanceof byte[]) {
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
} else {
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()));
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();
Expand All @@ -757,11 +769,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
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;
Expand Down Expand Up @@ -662,14 +663,18 @@ protected Map<String, List<String>> transformResponseHeaders(Header[] headers) {
/**
* 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();
Expand Down Expand Up @@ -703,7 +708,14 @@ public HttpEntity serialize(Object obj, Map<String, Object> formParams, ContentT
} else if (value instanceof byte[]) {
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
} else {
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()));
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();
Expand All @@ -712,11 +724,7 @@ public HttpEntity serialize(Object obj, Map<String, Object> formParams, ContentT
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) {
Expand Down

0 comments on commit 72991e6

Please sign in to comment.