Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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