Skip to content

Commit

Permalink
follow up to pr karatelabs#1299 edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 5, 2020
1 parent 0d8b888 commit fa36967
Showing 1 changed file with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,51 @@
* @author pthomas3
*/
public class ApacheHttpUtils {

private ApacheHttpUtils() {
// only static methods
}

public static HttpBody toBody(HttpEntity entity) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
return HttpBody.bytes(baos.toByteArray(), entity.getContentType().getValue());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

// all this complexity is to be able to support "bad" values such as an empty string
private static ContentType getContentType(String mediaType, Charset charset) {
if (!HttpUtils.isPrintable(mediaType)) {
try {
return ContentType.create(mediaType);
} catch (Exception e) {
} catch (Exception e) {
return null;
}
}

// if charset is null that means mediaType does not contains any charset
Charset existingCharset = HttpUtil.getCharset(mediaType, null);

// appending charset if not present
if(Objects.isNull(existingCharset)) {
mediaType = new StringBuilder(mediaType).append("; ").append(HttpUtils.CHARSET)
.append("=").append(charset.name()).toString();
if (Objects.isNull(existingCharset)) {
StringBuilder sb = new StringBuilder(mediaType).append("; ").append(HttpUtils.CHARSET);
if (charset != null) { // if user set configure charset = null (to omit that piece of the header)
sb.append("=").append(charset.name());
}
mediaType = sb.toString();
}

return ContentType.parse(mediaType);
}

public static HttpEntity getEntity(InputStream is, String mediaType, Charset charset) {
try {
return new InputStreamEntity(is, is.available(), getContentType(mediaType, charset));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}

public static HttpEntity getEntity(String value, String mediaType, Charset charset) {
try {
ContentType ct = getContentType(mediaType, charset);
Expand All @@ -112,12 +112,12 @@ public static HttpEntity getEntity(String value, String mediaType, Charset chars
return entity;
} else {
return new StringEntity(value, ct);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

public static HttpEntity getEntity(MultiValuedMap fields, String mediaType, Charset charset) {
List<NameValuePair> list = new ArrayList<>(fields.size());
for (Map.Entry<String, List> entry : fields.entrySet()) {
Expand All @@ -143,7 +143,7 @@ public static HttpEntity getEntity(MultiValuedMap fields, String mediaType, Char
Charset cs = HttpUtils.parseContentTypeCharset(mediaType);
if (cs == null) {
cs = charset;
}
}
String raw = URLEncodedUtils.format(list, cs);
int pos = mediaType.indexOf(';');
if (pos != -1) { // strip out charset param from content-type
Expand Down Expand Up @@ -174,13 +174,13 @@ public static HttpEntity getEntity(List<MultiPartItem> items, String mediaType,
if (item.getValue() == null || item.getValue().isNull()) {
continue;
}
String name = item.getName();
String name = item.getName();
if (name == null) {
// will never happen because we divert this flow to the home-made multi-part builder above
// builder.addPart(bodyPart);
} else {
ScriptValue sv = item.getValue();
String ct = item.getContentType();
String ct = item.getContentType();
if (ct == null) {
ct = HttpUtils.getContentType(sv);
}
Expand All @@ -190,12 +190,12 @@ public static HttpEntity getEntity(List<MultiPartItem> items, String mediaType,
if (cs == null) {
cs = charset;
}
contentType = contentType.withCharset(cs);
}
contentType = contentType.withCharset(cs);
}
FormBodyPartBuilder formBuilder = FormBodyPartBuilder.create().setName(name);
ContentBody contentBody;
String filename = item.getFilename();
if (filename != null) {
if (filename != null) {
contentBody = new ByteArrayBody(sv.getAsByteArray(), contentType, filename);
} else if (sv.isStream()) {
contentBody = new InputStreamBody(sv.getAsStream(), contentType);
Expand All @@ -208,6 +208,6 @@ public static HttpEntity getEntity(List<MultiPartItem> items, String mediaType,
}
return builder.build();
}
}
}

}

0 comments on commit fa36967

Please sign in to comment.