From 01851b39add9712b2ce66ab23ccb7dd839c1ce0c Mon Sep 17 00:00:00 2001 From: Ram Krishna Verma Date: Mon, 14 Feb 2022 14:17:14 -0500 Subject: Move common classes to policy/common Moving YamlHttpMessageConverter & CustomImplicitNamingStrategy classes to policy/common (spring-utils sub-module). So that the same can be used by multiple components without duplicating it. These classes are already covered by tests at each component. Issue-ID: POLICY-3931 Change-Id: I8fc6978b585bfcd930e0916f67961d05946d207c Signed-off-by: Ram Krishna Verma --- pom.xml | 1 + spring-utils/pom.xml | 54 +++++++++++ .../spring/utils/CustomImplicitNamingStrategy.java | 36 +++++++ .../spring/utils/YamlHttpMessageConverter.java | 107 +++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 spring-utils/pom.xml create mode 100644 spring-utils/src/main/java/org/onap/policy/common/spring/utils/CustomImplicitNamingStrategy.java create mode 100644 spring-utils/src/main/java/org/onap/policy/common/spring/utils/YamlHttpMessageConverter.java diff --git a/pom.xml b/pom.xml index 2e544bcf..af783c80 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,7 @@ integrity-audit integrity-monitor policy-endpoints + spring-utils diff --git a/spring-utils/pom.xml b/spring-utils/pom.xml new file mode 100644 index 00000000..a770b14e --- /dev/null +++ b/spring-utils/pom.xml @@ -0,0 +1,54 @@ + + + + 4.0.0 + + + org.onap.policy.common + common-modules + 1.10.1-SNAPSHOT + + + spring-utils + spring-utils + This artifact contains the common spring related utilitiy classes to be used by multiple policy framework components. + + + + org.onap.policy.common + utils + ${project.version} + + + + org.springframework + spring-web + + + + org.hibernate + hibernate-core + + + + \ No newline at end of file diff --git a/spring-utils/src/main/java/org/onap/policy/common/spring/utils/CustomImplicitNamingStrategy.java b/spring-utils/src/main/java/org/onap/policy/common/spring/utils/CustomImplicitNamingStrategy.java new file mode 100644 index 00000000..1a747a3c --- /dev/null +++ b/spring-utils/src/main/java/org/onap/policy/common/spring/utils/CustomImplicitNamingStrategy.java @@ -0,0 +1,36 @@ +/*- + * ============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.common.spring.utils; + +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/spring-utils/src/main/java/org/onap/policy/common/spring/utils/YamlHttpMessageConverter.java b/spring-utils/src/main/java/org/onap/policy/common/spring/utils/YamlHttpMessageConverter.java new file mode 100644 index 00000000..d7dc96a9 --- /dev/null +++ b/spring-utils/src/main/java/org/onap/policy/common/spring/utils/YamlHttpMessageConverter.java @@ -0,0 +1,107 @@ +/*- + * ============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.common.spring.utils; + +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 { + try (var writer = getWriter(outputMessage)) { + writeInternal(object, type, writer); + writer.flush(); + } catch (Exception ex) { + throw new HttpMessageNotWritableException("Could not write YAML: " + ex.getMessage(), ex); + } + } + + private void writeInternal(Object object, @Nullable Type type, Writer writer) { + TRANSLATOR.toYaml(writer, object); + } + + private Object readResolved(Type resolvedType, HttpInputMessage inputMessage) throws IOException { + try (var reader = getReader(inputMessage)) { + 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 -- cgit 1.2.3-korg