diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2022-02-10 14:35:12 -0500 |
---|---|---|
committer | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2022-02-10 14:35:17 -0500 |
commit | f5fda6421dad76d369774c59e89a448a6c4e8c07 (patch) | |
tree | d0636eee32fb0fcd383ada1488ae6cba2bcefac1 /main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java | |
parent | 873803eca00830dc3ecb61e610d90710f64a8242 (diff) |
Add yaml support to pap api's
Adding yaml support to pap rest api's.
Along with related unit tests.
Issue-ID: POLICY-3864
Change-Id: I43bdbbd4151bcae5dcf4752a9385b115efa947d3
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
Diffstat (limited to 'main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java')
-rw-r--r-- | main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java | 109 |
1 files changed, 109 insertions, 0 deletions
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 new file mode 100644 index 00000000..5678b837 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/config/converter/YamlHttpMessageConverter.java @@ -0,0 +1,109 @@ +/*- + * ============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<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 { + 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 |