summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRam Krishna Verma <ram_krishna.verma@bell.ca>2022-02-16 12:04:23 -0500
committerRam Krishna Verma <ram_krishna.verma@bell.ca>2022-02-16 12:04:28 -0500
commitcb67ccf8fa4e3b2663daf2907d2069d9b662f4e7 (patch)
tree08acc300b7170faf501986ac7d48083b02ae28d3
parent5f919673b13d8fd40a6a90b97f4e662ade068304 (diff)
Use classes from policy/common
Use YamlHttpMessageConverter provided by policy/common. Fix few sonar issues. Issue-ID: POLICY-3931 Change-Id: I3c093df059f4ff70e3f99669c2bdea7d84cefcc6 Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
-rw-r--r--main/pom.xml5
-rw-r--r--main/src/main/java/org/onap/policy/api/main/config/WebConfig.java9
-rw-r--r--main/src/main/java/org/onap/policy/api/main/config/converter/YamlHttpMessageConverter.java109
3 files changed, 9 insertions, 114 deletions
diff --git a/main/pom.xml b/main/pom.xml
index c89eafd1..fffaa03f 100644
--- a/main/pom.xml
+++ b/main/pom.xml
@@ -42,6 +42,11 @@
<version>${policy.models.version}</version>
</dependency>
<dependency>
+ <groupId>org.onap.policy.common</groupId>
+ <artifactId>spring-utils</artifactId>
+ <version>${policy.common.version}</version>
+ </dependency>
+ <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
diff --git a/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java b/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java
index a50dd187..88871f55 100644
--- a/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java
+++ b/main/src/main/java/org/onap/policy/api/main/config/WebConfig.java
@@ -20,14 +20,13 @@
package org.onap.policy.api.main.config;
-import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.google.gson.JsonSerializer;
import java.util.Arrays;
import java.util.List;
import org.onap.policy.api.main.config.converter.StringToEnumConverter;
-import org.onap.policy.api.main.config.converter.YamlHttpMessageConverter;
+import org.onap.policy.common.spring.utils.YamlHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
@@ -48,12 +47,12 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
- YamlHttpMessageConverter yamlConverter = new YamlHttpMessageConverter();
+ var yamlConverter = new YamlHttpMessageConverter();
yamlConverter.setSupportedMediaTypes(Arrays.asList(MediaType.parseMediaType("application/yaml")));
converters.add(yamlConverter);
GsonHttpMessageConverter converter = buildGsonConverter();
- converters.removeIf(c -> c instanceof GsonHttpMessageConverter);
+ converters.removeIf(GsonHttpMessageConverter.class::isInstance);
converters.add(0, converter);
}
@@ -66,7 +65,7 @@ public class WebConfig implements WebMvcConfigurer {
private GsonHttpMessageConverter buildGsonConverter() {
JsonSerializer<Json> serializer = (json, type, jsonSerializationContext) ->
JsonParser.parseString(json.value());
- Gson gson = new GsonBuilder().registerTypeAdapter(Json.class, serializer).create();
+ var gson = new GsonBuilder().registerTypeAdapter(Json.class, serializer).create();
return new GsonHttpMessageConverter(gson);
}
} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/config/converter/YamlHttpMessageConverter.java b/main/src/main/java/org/onap/policy/api/main/config/converter/YamlHttpMessageConverter.java
deleted file mode 100644
index 9fe9b031..00000000
--- a/main/src/main/java/org/onap/policy/api/main/config/converter/YamlHttpMessageConverter.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2022 Bell Canada. 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.api.main.config.converter;
-
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.io.Writer;
-import java.lang.reflect.Type;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import org.onap.policy.common.utils.coder.YamlJsonTranslator;
-import org.springframework.core.GenericTypeResolver;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpInputMessage;
-import org.springframework.http.HttpOutputMessage;
-import org.springframework.http.MediaType;
-import org.springframework.http.converter.AbstractGenericHttpMessageConverter;
-import org.springframework.http.converter.HttpMessageNotReadableException;
-import org.springframework.http.converter.HttpMessageNotWritableException;
-import org.springframework.lang.Nullable;
-
-/**
- * Custom converter to marshal/unmarshall data structured with YAML media type.
- */
-public class YamlHttpMessageConverter extends AbstractGenericHttpMessageConverter<Object> {
-
- public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
-
- private static final YamlJsonTranslator TRANSLATOR = new YamlJsonTranslator();
-
- public YamlHttpMessageConverter() {
- super(new MediaType("application", "yaml"));
- setDefaultCharset(DEFAULT_CHARSET);
- }
-
- @Override
- public final Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
- throws IOException {
- return readResolved(GenericTypeResolver.resolveType(type, contextClass), inputMessage);
- }
-
- @Override
- protected final Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException {
- return readResolved(clazz, inputMessage);
- }
-
- private Object readInternal(Type resolvedType, Reader reader) {
- Class<?> clazz = (Class<?>) resolvedType;
- return TRANSLATOR.fromYaml(reader, clazz);
- }
-
- @Override
- protected final void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
- throws IOException {
- Writer writer = getWriter(outputMessage);
- try {
- writeInternal(object, type, writer);
- } catch (Exception ex) {
- throw new HttpMessageNotWritableException("Could not write YAML: " + ex.getMessage(), ex);
- }
- writer.flush();
- }
-
- private void writeInternal(Object object, @Nullable Type type, Writer writer) {
- TRANSLATOR.toYaml(writer, object);
- }
-
- private Object readResolved(Type resolvedType, HttpInputMessage inputMessage) throws IOException {
- Reader reader = getReader(inputMessage);
- try {
- return readInternal(resolvedType, reader);
- } catch (Exception ex) {
- throw new HttpMessageNotReadableException("Could not read YAML: " + ex.getMessage(), ex, inputMessage);
- }
- }
-
- private static Reader getReader(HttpInputMessage inputMessage) throws IOException {
- return new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));
- }
-
- private static Writer getWriter(HttpOutputMessage outputMessage) throws IOException {
- return new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders()));
- }
-
- private static Charset getCharset(HttpHeaders headers) {
- Charset charset = (headers.getContentType() == null ? null : headers.getContentType().getCharset());
- return (charset != null ? charset : DEFAULT_CHARSET);
- }
-} \ No newline at end of file