diff --git a/src/main/java/com/synopsys/integration/rest/body/BodyContentConverter.java b/src/main/java/com/synopsys/integration/rest/body/BodyContentConverter.java index 87e97c2..83a691c 100644 --- a/src/main/java/com/synopsys/integration/rest/body/BodyContentConverter.java +++ b/src/main/java/com/synopsys/integration/rest/body/BodyContentConverter.java @@ -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; @@ -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 bodyContentStringMap) { + public HttpEntity fromMap(Map bodyContentStringMap, Charset bodyEncoding) { List parameters = new ArrayList<>(); for (Map.Entry entry : bodyContentStringMap.entrySet()) { NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue()); @@ -74,13 +67,13 @@ public HttpEntity fromMultipart(Map bodyContentFileMap, Map bodyContentStringMap; + private final Charset encoding; - public MapBodyContent(Map bodyContentStringMap) { + public MapBodyContent(Map bodyContentStringMap, Charset encoding) { this.bodyContentStringMap = bodyContentStringMap; + this.encoding = encoding; } @Override public HttpEntity createEntity(BodyContentConverter bodyContentConverter) { - return bodyContentConverter.fromMap(bodyContentStringMap); + return bodyContentConverter.fromMap(bodyContentStringMap, encoding); } } diff --git a/src/main/java/com/synopsys/integration/rest/body/ObjectBodyContent.java b/src/main/java/com/synopsys/integration/rest/body/ObjectBodyContent.java index 3fda6ff..89b95d0 100644 --- a/src/main/java/com/synopsys/integration/rest/body/ObjectBodyContent.java +++ b/src/main/java/com/synopsys/integration/rest/body/ObjectBodyContent.java @@ -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); } } diff --git a/src/main/java/com/synopsys/integration/rest/body/StringBodyContent.java b/src/main/java/com/synopsys/integration/rest/body/StringBodyContent.java index 1c53006..c6e4e99 100644 --- a/src/main/java/com/synopsys/integration/rest/body/StringBodyContent.java +++ b/src/main/java/com/synopsys/integration/rest/body/StringBodyContent.java @@ -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); } } diff --git a/src/main/java/com/synopsys/integration/rest/client/IntHttpClient.java b/src/main/java/com/synopsys/integration/rest/client/IntHttpClient.java index 3aca2fe..018cb16 100644 --- a/src/main/java/com/synopsys/integration/rest/client/IntHttpClient.java +++ b/src/main/java/com/synopsys/integration/rest/client/IntHttpClient.java @@ -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); } diff --git a/src/test/groovy/com/synopsys/integration/rest/IntHttpClientTest.groovy b/src/test/groovy/com/synopsys/integration/rest/IntHttpClientTest.groovy index a952100..74963e9 100644 --- a/src/test/groovy/com/synopsys/integration/rest/IntHttpClientTest.groovy +++ b/src/test/groovy/com/synopsys/integration/rest/IntHttpClientTest.groovy @@ -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 @@ -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 @@ -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()