forked from loafle/openapi-generator-original
[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:
@@ -57,6 +57,7 @@ import java.io.File;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -707,14 +708,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
/**
|
/**
|
||||||
* Parse content type object from header value
|
* Parse content type object from header value
|
||||||
*/
|
*/
|
||||||
private ContentType getContentType(String headerValue) {
|
private ContentType getContentType(String headerValue) throws ApiException {
|
||||||
return ContentType.getByMimeType(headerValue);
|
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
|
* 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");
|
Header contentTypeHeader = response.getFirstHeader("Content-Type");
|
||||||
if (contentTypeHeader != null) {
|
if (contentTypeHeader != null) {
|
||||||
return getContentType(contentTypeHeader.getValue()).getMimeType();
|
return getContentType(contentTypeHeader.getValue()).getMimeType();
|
||||||
@@ -748,7 +753,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
} else if (value instanceof byte[]) {
|
} else if (value instanceof byte[]) {
|
||||||
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
|
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
|
||||||
} else {
|
} 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();
|
return multiPartBuilder.build();
|
||||||
@@ -757,11 +769,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
for (Entry<String, Object> paramEntry : formParams.entrySet()) {
|
for (Entry<String, Object> paramEntry : formParams.entrySet()) {
|
||||||
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
|
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
|
||||||
}
|
}
|
||||||
try {
|
return new UrlEncodedFormEntity(formValues, contentType.getCharset());
|
||||||
return new UrlEncodedFormEntity(formValues);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Handle files with unknown content type
|
// Handle files with unknown content type
|
||||||
if (obj instanceof File) {
|
if (obj instanceof File) {
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ import java.io.File;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -662,14 +663,18 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
/**
|
/**
|
||||||
* Parse content type object from header value
|
* Parse content type object from header value
|
||||||
*/
|
*/
|
||||||
private ContentType getContentType(String headerValue) {
|
private ContentType getContentType(String headerValue) throws ApiException {
|
||||||
return ContentType.getByMimeType(headerValue);
|
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
|
* 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");
|
Header contentTypeHeader = response.getFirstHeader("Content-Type");
|
||||||
if (contentTypeHeader != null) {
|
if (contentTypeHeader != null) {
|
||||||
return getContentType(contentTypeHeader.getValue()).getMimeType();
|
return getContentType(contentTypeHeader.getValue()).getMimeType();
|
||||||
@@ -703,7 +708,14 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
} else if (value instanceof byte[]) {
|
} else if (value instanceof byte[]) {
|
||||||
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
|
multiPartBuilder.addBinaryBody(paramEntry.getKey(), (byte[]) value);
|
||||||
} else {
|
} 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();
|
return multiPartBuilder.build();
|
||||||
@@ -712,11 +724,7 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
for (Entry<String, Object> paramEntry : formParams.entrySet()) {
|
for (Entry<String, Object> paramEntry : formParams.entrySet()) {
|
||||||
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
|
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
|
||||||
}
|
}
|
||||||
try {
|
return new UrlEncodedFormEntity(formValues, contentType.getCharset());
|
||||||
return new UrlEncodedFormEntity(formValues);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Handle files with unknown content type
|
// Handle files with unknown content type
|
||||||
if (obj instanceof File) {
|
if (obj instanceof File) {
|
||||||
|
|||||||
Reference in New Issue
Block a user