From 2609cc76f0565466667fff8ae4d0707b94993877 Mon Sep 17 00:00:00 2001 From: Eylon Malin Date: Thu, 12 Sep 2019 14:25:32 +0300 Subject: create JoshworksJacksonObjectMapper and use it everywhere needed Create class that implement joshworks object mapper that used jackson object mapper that support kotlin. Use it instead local anonymous classes where needed Issue-ID: VID-611 Signed-off-by: Eylon Malin Change-Id: Ie00dce0ec9b366515c5e40d9f37b9e64a2ceb357 --- .../java/org/onap/vid/client/SyncRestClient.java | 25 +++---------------- .../java/org/onap/vid/controller/WebConfig.java | 28 +++------------------- .../main/java/org/onap/vid/utils/KotlinUtils.kt | 14 +++++++++++ 3 files changed, 20 insertions(+), 47 deletions(-) (limited to 'vid-app-common/src/main/java') diff --git a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java index 18f87223c..398d81dfb 100644 --- a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java @@ -3,6 +3,7 @@ * VID * ================================================================================ * Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * Modifications Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +21,8 @@ package org.onap.vid.client; -import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.onap.vid.client.UnirestPatchKt.patched; +import static org.onap.vid.utils.KotlinUtilsKt.JOSHWORKS_JACKSON_OBJECT_MAPPER; import com.att.eelf.configuration.EELFLogger; import io.joshworks.restclient.http.HttpResponse; @@ -183,27 +184,7 @@ public class SyncRestClient implements SyncRestClientInterface { } private ObjectMapper defaultObjectMapper() { - com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper(); - - return new ObjectMapper() { - @Override - public T readValue(String value, Class aClass) { - try { - return isEmpty(value) ? null : objectMapper.readValue(value, aClass); - } catch (IOException e) { - throw new SyncRestClientException("IOException while reading value", e); - } - } - - @Override - public String writeValue(Object value) { - try { - return objectMapper.writeValueAsString(value); - } catch (IOException e) { - throw new SyncRestClientException("IOException while writing value", e); - } - } - }; + return JOSHWORKS_JACKSON_OBJECT_MAPPER; } private CloseableHttpClient defaultHttpClient() { diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java index 99845f06d..9faa7ade5 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java @@ -22,13 +22,10 @@ package org.onap.vid.controller; import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; -import static org.apache.commons.lang3.StringUtils.isEmpty; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.module.kotlin.KotlinModule; import io.joshworks.restclient.http.mapper.ObjectMapper; import java.io.File; -import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.servlet.ServletContext; @@ -66,6 +63,7 @@ import org.onap.vid.services.AaiServiceImpl; import org.onap.vid.services.ChangeManagementService; import org.onap.vid.services.PombaService; import org.onap.vid.services.PombaServiceImpl; +import org.onap.vid.utils.JoshworksJacksonObjectMapper; import org.onap.vid.utils.Logging; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; @@ -198,28 +196,8 @@ public class WebConfig { } @Bean - public ObjectMapper unirestFasterxmlObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) { - return new ObjectMapper() { - - @Override - public T readValue(String s, Class aClass) { - try { - return isEmpty(s) ? null : objectMapper.readValue(s, aClass); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public String writeValue(Object o) { - try { - return objectMapper.writeValueAsString(o); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - } - }; - + public ObjectMapper unirestFasterxmlObjectMapper() { + return new JoshworksJacksonObjectMapper(); } @Bean diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt b/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt index cf532624c..81afe29e0 100644 --- a/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt +++ b/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt @@ -22,9 +22,23 @@ package org.onap.vid.utils import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.apache.commons.lang3.StringUtils.isEmpty inline fun > getEnumFromMapOfStrings(map: Map, key:String, defaultValue:E): E { return java.lang.Enum.valueOf(E::class.java, (map.getOrDefault(key, defaultValue.name) as String)) } @JvmField val JACKSON_OBJECT_MAPPER: ObjectMapper = jacksonObjectMapper() + +class JoshworksJacksonObjectMapper: io.joshworks.restclient.http.mapper.ObjectMapper { + override fun writeValue(value: Any?): String? { + return JACKSON_OBJECT_MAPPER.writeValueAsString(value) + } + + override fun readValue(value: String?, valueType: Class?): T? { + return if (isEmpty(value)) null else JACKSON_OBJECT_MAPPER.readValue(value, valueType) + } +} + +@JvmField val JOSHWORKS_JACKSON_OBJECT_MAPPER: + io.joshworks.restclient.http.mapper.ObjectMapper = JoshworksJacksonObjectMapper() -- cgit 1.2.3-korg