Skip to content

Commit

Permalink
making body content requirements explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Kerwin committed May 25, 2021
1 parent a1d0df8 commit 5287472
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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 @@ -28,31 +27,25 @@
import com.google.gson.Gson;

public class BodyContentConverter {
public static final ContentType DEFAULT = ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8);
public static final ContentType OCTET_STREAM_UTF_8 = ContentType.APPLICATION_OCTET_STREAM.withCharset(StandardCharsets.UTF_8);
public static final ContentType TEXT_PLAIN_UTF_8 = ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8);

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

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

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

this.bodyEncoding = bodyEncoding;
}

public HttpEntity fromHttpEntity(HttpEntity httpEntity) {
return httpEntity;
}

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

public HttpEntity fromMap(Map<String, String> bodyContentStringMap) {
public HttpEntity fromMap(Map<String, String> bodyContentStringMap, Charset bodyEncoding) {
List<NameValuePair> parameters = new ArrayList<>();
for (Map.Entry<String, String> entry : bodyContentStringMap.entrySet()) {
NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue());
Expand All @@ -74,13 +67,13 @@ public HttpEntity fromMultipart(Map<String, File> bodyContentFileMap, Map<String
return builder.build();
}

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

public HttpEntity fromObject(Object bodyContentObject) {
public HttpEntity fromObject(Object bodyContentObject, ContentType contentType) {
String bodyContentString = gson.toJson(bodyContentObject);
return fromString(bodyContentString);
return fromString(bodyContentString, contentType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

public class FileBodyContent implements BodyContent {
private final File bodyContentFile;
private final ContentType contentType;

public FileBodyContent(File bodyContentFile) {
public FileBodyContent(File bodyContentFile, ContentType contentType) {
this.bodyContentFile = bodyContentFile;
this.contentType = contentType;
}

@Override
public HttpEntity createEntity(BodyContentConverter bodyContentConverter) {
return bodyContentConverter.fromFile(bodyContentFile);
return bodyContentConverter.fromFile(bodyContentFile, contentType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
*/
package com.synopsys.integration.rest.body;

import java.nio.charset.Charset;
import java.util.Map;

import org.apache.http.HttpEntity;

public class MapBodyContent implements BodyContent {
private final Map<String, String> bodyContentStringMap;
private final Charset encoding;

public MapBodyContent(Map<String, String> bodyContentStringMap) {
public MapBodyContent(Map<String, String> bodyContentStringMap, Charset encoding) {
this.bodyContentStringMap = bodyContentStringMap;
this.encoding = encoding;
}

@Override
public HttpEntity createEntity(BodyContentConverter bodyContentConverter) {
return bodyContentConverter.fromMap(bodyContentStringMap);
return bodyContentConverter.fromMap(bodyContentStringMap, encoding);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
package com.synopsys.integration.rest.body;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

public class ObjectBodyContent implements BodyContent {
private final Object bodyContentObject;
private final ContentType contentType;

public ObjectBodyContent(Object bodyContentObject) {
public ObjectBodyContent(Object bodyContentObject, ContentType contentType) {
this.bodyContentObject = bodyContentObject;
this.contentType = contentType;
}

@Override
public HttpEntity createEntity(BodyContentConverter bodyContentConverter) {
return bodyContentConverter.fromObject(bodyContentObject);
return bodyContentConverter.fromObject(bodyContentObject, contentType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
package com.synopsys.integration.rest.body;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

public class StringBodyContent implements BodyContent {
private final String bodyContentString;
private final ContentType contentType;

public StringBodyContent(String bodyContentString) {
public StringBodyContent(String bodyContentString, ContentType contentType) {
this.bodyContentString = bodyContentString;
this.contentType = contentType;
}

@Override
public HttpEntity createEntity(BodyContentConverter bodyContentConverter) {
return bodyContentConverter.fromString(bodyContentString);
return bodyContentConverter.fromString(bodyContentString, contentType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public HttpUriRequest createHttpUriRequest(Request integrationRequest) throws In

BodyContent bodyContent = integrationRequest.getBodyContent();
if (null != bodyContent) {
BodyContentConverter bodyContentConverter = new BodyContentConverter(gson, integrationRequest.getAcceptMimeType(), integrationRequest.getBodyEncoding());
BodyContentConverter bodyContentConverter = new BodyContentConverter(gson);
HttpEntity httpEntity = bodyContent.createEntity(bodyContentConverter);
requestBuilder.setEntity(httpEntity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class IntHttpClientTest {
assert uriRequest.getURI().toString().contains('offset=0')
assert uriRequest.getURI().toString().contains('limit=100')

request = new Request.Builder(url).queryParameters([q: ['q'] as Set, test: ['one'] as Set, query: ['two'] as Set, offset: ['0'] as Set, limit: ['100'] as Set]).acceptMimeType('mime').headers([header: 'one', thing: 'two']).
request = new Request.Builder(url).queryParameters([q: ['q'] as Set, test: ['one'] as Set, query: ['two'] as Set, offset: ['0'] as Set, limit: ['100'] as Set]).headers([header: 'one', thing: 'two']).
build()
uriRequest = restConnection.createHttpUriRequest(request)
assert HttpMethod.GET.name() == uriRequest.method
Expand All @@ -166,7 +166,7 @@ class IntHttpClientTest {

Map headersMap = [header: 'one', thing: 'two']
headersMap.put(HttpHeaders.ACCEPT, ContentType.APPLICATION_XML.getMimeType())
request = new Request.Builder(url).queryParameters([q: ['q'] as Set, test: ['one'] as Set, query: ['two'] as Set, offset: ['0'] as Set, limit: ['100'] as Set]).acceptMimeType('mime').bodyEncoding(bodyEncoding).
request = new Request.Builder(url).queryParameters([q: ['q'] as Set, test: ['one'] as Set, query: ['two'] as Set, offset: ['0'] as Set, limit: ['100'] as Set]).bodyEncoding(bodyEncoding).
headers(headersMap).build()
uriRequest = restConnection.createHttpUriRequest(request)
assert HttpMethod.GET.name() == uriRequest.method
Expand All @@ -176,7 +176,7 @@ class IntHttpClientTest {
assert uriRequest.getURI().toString().contains('offset=0')
assert uriRequest.getURI().toString().contains('limit=100')

Request deleteRequest = new Request.Builder(url).method(HttpMethod.DELETE).acceptMimeType('mime').bodyEncoding(bodyEncoding).headers([header: 'one', thing: 'two']).build()
Request deleteRequest = new Request.Builder(url).method(HttpMethod.DELETE).bodyEncoding(bodyEncoding).headers([header: 'one', thing: 'two']).build()
uriRequest = restConnection.createHttpUriRequest(deleteRequest)
assert HttpMethod.DELETE.name() == uriRequest.method
assert 'one' == uriRequest.getFirstHeader('header').getValue()
Expand Down

0 comments on commit 5287472

Please sign in to comment.