Skip to content

Commit 821eb37

Browse files
authored
Merge pull request #120 from messagebird/Issue_119
reverted reuse of objectmapper
2 parents d1e91e7 + df1a778 commit 821eb37

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

api/src/main/java/com/messagebird/MessageBirdServiceImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public class MessageBirdServiceImpl implements MessageBirdService {
6565
private final String userAgentString;
6666
private Proxy proxy = null;
6767

68-
private final ObjectMapper mapper;
69-
7068
public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
7169
if (accessKey == null) {
7270
throw new IllegalArgumentException(ACCESS_KEY_MUST_BE_SPECIFIED);
@@ -78,16 +76,6 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
7876
this.serviceUrl = serviceUrl;
7977
this.userAgentString = determineUserAgentString();
8078

81-
this.mapper = new ObjectMapper()
82-
// If we as new properties, we don't want the system to fail, we rather want to ignore them
83-
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
84-
// Enable case insensitivity to avoid parsing errors if parameters' case in api response doesn't match sdk's
85-
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
86-
.setSerializationInclusion(Include.NON_NULL)
87-
// Specifically set the date format for POST requests so scheduled
88-
// messages and other things relying on specific date formats don't
89-
// fail when sending.
90-
.setDateFormat(getDateFormat());
9179
}
9280

9381
private String determineUserAgentString() {
@@ -231,6 +219,13 @@ public <T, P> T getJsonData(final String request, final P payload, final String
231219

232220
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) {
233221
try {
222+
final ObjectMapper mapper = new ObjectMapper();
223+
// If we as new properties, we don't want the system to fail, we rather want to ignore them
224+
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
225+
// Enable case insensitivity to avoid parsing errors if parameters' case in api response doesn't match sdk's
226+
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
227+
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
228+
234229
return mapper.readValue(body, clazz);
235230
} catch (IOException ioe) {
236231
throw new GeneralException(ioe);
@@ -482,6 +477,13 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P body
482477
connection.setRequestMethod(requestType);
483478
connection.setDoOutput(true);
484479
connection.setRequestProperty("Content-Type", "application/json");
480+
ObjectMapper mapper = new ObjectMapper();
481+
mapper.setSerializationInclusion(Include.NON_NULL);
482+
// Specifically set the date format for POST requests so scheduled
483+
// messages and other things relying on specific date formats don't
484+
// fail when sending.
485+
DateFormat df = getDateFormat();
486+
mapper.setDateFormat(df);
485487

486488
final String json = mapper.writeValueAsString(body);
487489
connection.getOutputStream().write(json.getBytes(String.valueOf(StandardCharsets.UTF_8)));
@@ -535,13 +537,14 @@ private double getVersion() throws GeneralException {
535537
* @return Error report, or null if the body can not be deserialized.
536538
*/
537539
private List<ErrorReport> getErrorReportOrNull(final String body) {
540+
ObjectMapper objectMapper = new ObjectMapper();
538541
try {
539-
JsonNode jsonNode = mapper.readValue(body, JsonNode.class);
542+
JsonNode jsonNode = objectMapper.readValue(body, JsonNode.class);
540543
if(!jsonNode.has("errors")) {
541544
return null;
542545
}
543546

544-
ErrorReport[] errors = mapper.readValue(jsonNode.get("errors").toString(), ErrorReport[].class);
547+
ErrorReport[] errors = objectMapper.readValue(jsonNode.get("errors").toString(), ErrorReport[].class);
545548

546549
List<ErrorReport> result = Arrays.asList(errors);
547550

0 commit comments

Comments
 (0)