Skip to content

Commit

Permalink
can't have a null mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Kerwin committed May 25, 2021
1 parent 0e7c37d commit babf958
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand All @@ -31,12 +32,15 @@ public class BodyContentConverter {
public static final ContentType TEXT_PLAIN_UTF_8 = ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8);

private final Gson gson;
private final String acceptMimeType;
private Charset bodyEncoding;
private final String mimeType;
private final Charset bodyEncoding;

public BodyContentConverter(Gson gson, String acceptMimeType, Charset bodyEncoding) {
public BodyContentConverter(Gson gson, String mimeType, Charset bodyEncoding) {
this.gson = gson;
this.acceptMimeType = acceptMimeType;

//ContentType requires a non-null mime type
this.mimeType = StringUtils.isNotBlank(mimeType) ? mimeType : ContentType.APPLICATION_JSON.getMimeType();

this.bodyEncoding = bodyEncoding;
}

Expand All @@ -45,13 +49,13 @@ public HttpEntity fromHttpEntity(HttpEntity httpEntity) {
}

public HttpEntity fromFile(File bodyContentFile) {
return new FileEntity(bodyContentFile, ContentType.create(acceptMimeType, bodyEncoding));
return new FileEntity(bodyContentFile, ContentType.create(mimeType, bodyEncoding));
}

public HttpEntity fromMap(Map<String, String> bodyContentStringMap) {
final List<NameValuePair> parameters = new ArrayList<>();
for (final Map.Entry<String, String> entry : bodyContentStringMap.entrySet()) {
final NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue());
List<NameValuePair> parameters = new ArrayList<>();
for (Map.Entry<String, String> entry : bodyContentStringMap.entrySet()) {
NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue());
parameters.add(nameValuePair);
}
return new UrlEncodedFormEntity(parameters, bodyEncoding);
Expand All @@ -71,7 +75,7 @@ public HttpEntity fromMultipart(Map<String, File> bodyContentFileMap, Map<String
}

public HttpEntity fromString(String bodyContentString) {
return new StringEntity(bodyContentString, ContentType.create(acceptMimeType, bodyEncoding));
return new StringEntity(bodyContentString, ContentType.create(mimeType, bodyEncoding));
}

public HttpEntity fromObject(Object bodyContentObject) {
Expand Down

0 comments on commit babf958

Please sign in to comment.