@@ -65,6 +65,8 @@ public class MessageBirdServiceImpl implements MessageBirdService {
65
65
private final String userAgentString ;
66
66
private Proxy proxy = null ;
67
67
68
+ private final ObjectMapper mapper ;
69
+
68
70
public MessageBirdServiceImpl (final String accessKey , final String serviceUrl ) {
69
71
if (accessKey == null ) {
70
72
throw new IllegalArgumentException (ACCESS_KEY_MUST_BE_SPECIFIED );
@@ -75,6 +77,17 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
75
77
this .accessKey = accessKey ;
76
78
this .serviceUrl = serviceUrl ;
77
79
this .userAgentString = determineUserAgentString ();
80
+
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 ());
78
91
}
79
92
80
93
private String determineUserAgentString () {
@@ -217,14 +230,6 @@ public <T, P> T getJsonData(final String request, final P payload, final String
217
230
final int status = apiResponse .getStatus ();
218
231
219
232
if (status == HttpURLConnection .HTTP_OK || status == HttpURLConnection .HTTP_CREATED ) {
220
- final ObjectMapper mapper = new ObjectMapper ();
221
-
222
- // If we as new properties, we don't want the system to fail, we rather want to ignore them
223
- mapper .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
224
- // Enable case insensitivity to avoid parsing errors if parameters' case in api response doesn't match sdk's
225
- mapper .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES );
226
- mapper .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_ENUMS );
227
-
228
233
try {
229
234
return mapper .readValue (body , clazz );
230
235
} catch (IOException ioe ) {
@@ -477,14 +482,6 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P body
477
482
connection .setRequestMethod (requestType );
478
483
connection .setDoOutput (true );
479
484
connection .setRequestProperty ("Content-Type" , "application/json" );
480
- ObjectMapper mapper = new ObjectMapper ();
481
- mapper .setSerializationInclusion (Include .NON_NULL );
482
-
483
- // Specifically set the date format for POST requests so scheduled
484
- // messages and other things relying on specific date formats don't
485
- // fail when sending.
486
- DateFormat df = getDateFormat ();
487
- mapper .setDateFormat (df );
488
485
489
486
final String json = mapper .writeValueAsString (body );
490
487
connection .getOutputStream ().write (json .getBytes (String .valueOf (StandardCharsets .UTF_8 )));
@@ -538,15 +535,13 @@ private double getVersion() throws GeneralException {
538
535
* @return Error report, or null if the body can not be deserialized.
539
536
*/
540
537
private List <ErrorReport > getErrorReportOrNull (final String body ) {
541
- ObjectMapper objectMapper = new ObjectMapper ();
542
-
543
538
try {
544
- JsonNode jsonNode = objectMapper .readValue (body , JsonNode .class );
539
+ JsonNode jsonNode = mapper .readValue (body , JsonNode .class );
545
540
if (!jsonNode .has ("errors" )) {
546
541
return null ;
547
542
}
548
543
549
- ErrorReport [] errors = objectMapper .readValue (jsonNode .get ("errors" ).toString (), ErrorReport [].class );
544
+ ErrorReport [] errors = mapper .readValue (jsonNode .get ("errors" ).toString (), ErrorReport [].class );
550
545
551
546
List <ErrorReport > result = Arrays .asList (errors );
552
547
0 commit comments