From: Ram Krishna Verma Date: Wed, 16 Feb 2022 17:12:20 +0000 (-0500) Subject: Use classes from policy/common X-Git-Tag: 2.6.1~6^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F53%2F127153%2F2;p=policy%2Fpap.git Use classes from policy/common Use YamlHttpMessageConverter from policy/common. Use CustomImplicitNamingStrategy from policy/common. Fix few sonar issues. Issue-ID: POLICY-3931 Change-Id: I4720761cbf8da464ec57541f9b19f8d124503e17 Signed-off-by: Ram Krishna Verma --- diff --git a/main/pom.xml b/main/pom.xml index 621fb45e..06c03345 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -90,6 +90,11 @@ policy-models-pdp ${policy.models.version} + + org.onap.policy.common + spring-utils + ${policy.common.version} + org.onap.policy.common utils-test diff --git a/main/src/main/java/org/onap/policy/pap/main/CustomImplicitNamingStrategy.java b/main/src/main/java/org/onap/policy/pap/main/CustomImplicitNamingStrategy.java deleted file mode 100644 index 7505039b..00000000 --- a/main/src/main/java/org/onap/policy/pap/main/CustomImplicitNamingStrategy.java +++ /dev/null @@ -1,36 +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.pap.main; - -import org.hibernate.boot.model.naming.Identifier; -import org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource; -import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; - -public class CustomImplicitNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl { - - private static final long serialVersionUID = 8666774028328486896L; - - @Override - public Identifier determineJoinColumnName(ImplicitJoinColumnNameSource source) { - String name = source.getReferencedColumnName().getText(); - return toIdentifier(name, source.getBuildingContext()); - } -} diff --git a/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java b/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java index 751ca611..d8aee3a7 100644 --- a/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java +++ b/main/src/main/java/org/onap/policy/pap/main/config/WebConfig.java @@ -22,7 +22,7 @@ package org.onap.policy.pap.main.config; import java.util.Arrays; import java.util.List; -import org.onap.policy.pap.main.config.converter.YamlHttpMessageConverter; +import org.onap.policy.common.spring.utils.YamlHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; @@ -36,7 +36,7 @@ public class WebConfig implements WebMvcConfigurer { @Override public void extendMessageConverters(List> converters) { - YamlHttpMessageConverter yamlConverter = new YamlHttpMessageConverter(); + var yamlConverter = new YamlHttpMessageConverter(); yamlConverter.setSupportedMediaTypes(Arrays.asList(MediaType.parseMediaType("application/yaml"))); converters.add(yamlConverter); } diff --git a/main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java b/main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java deleted file mode 100644 index 5678b837..00000000 --- a/main/src/main/java/org/onap/policy/pap/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.pap.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 { - - 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 { - var 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 { - var 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 diff --git a/main/src/main/resources/application.yaml b/main/src/main/resources/application.yaml index d6111aa1..2b6d8fc8 100644 --- a/main/src/main/resources/application.yaml +++ b/main/src/main/resources/application.yaml @@ -19,7 +19,7 @@ spring: ddl-auto: none naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - implicit-strategy: org.onap.policy.pap.main.CustomImplicitNamingStrategy + implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy server: port: 6969 diff --git a/main/src/test/resources/config/application.yaml b/main/src/test/resources/config/application.yaml index b9579db1..b6a497ac 100644 --- a/main/src/test/resources/config/application.yaml +++ b/main/src/test/resources/config/application.yaml @@ -17,7 +17,7 @@ spring: ddl-auto: update naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - implicit-strategy: org.onap.policy.pap.main.CustomImplicitNamingStrategy + implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy server: port: 6969 diff --git a/packages/policy-pap-tarball/src/main/resources/etc/papParameters.yaml b/packages/policy-pap-tarball/src/main/resources/etc/papParameters.yaml index 9bfb5c09..0c37b468 100644 --- a/packages/policy-pap-tarball/src/main/resources/etc/papParameters.yaml +++ b/packages/policy-pap-tarball/src/main/resources/etc/papParameters.yaml @@ -19,7 +19,7 @@ spring: ddl-auto: none naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - implicit-strategy: org.onap.policy.pap.main.CustomImplicitNamingStrategy + implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy server: port: 6969