diff options
Diffstat (limited to 'participant/participant-impl/participant-impl-simulator/src/main')
2 files changed, 28 insertions, 8 deletions
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java index e84a7fc0a..16da5cf7f 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java @@ -22,7 +22,9 @@ package org.onap.policy.clamp.controlloop.participant.simulator.config; import java.util.List; import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -30,6 +32,11 @@ public class YamlConfiguration implements WebMvcConfigurer { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { - converters.add(new YamlHttpMessageConverter<>()); + converters.add(new YamlHttpMessageConverter<>("yaml")); + converters.add(new YamlHttpMessageConverter<>("json")); + + StringHttpMessageConverter converter = new StringHttpMessageConverter(); + converter.setSupportedMediaTypes(List.of(MediaType.TEXT_PLAIN)); + converters.add(converter); } } diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java index 3e1059835..d9a72ce10 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java @@ -23,20 +23,29 @@ package org.onap.policy.clamp.controlloop.participant.simulator.config; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; +import javax.ws.rs.core.Response; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardYamlCoder; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; -import org.yaml.snakeyaml.Yaml; public class YamlHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> { - public YamlHttpMessageConverter() { - super(new MediaType("application", "yaml")); + private Coder coder; + + public YamlHttpMessageConverter(String type) { + super(new MediaType("application", type, StandardCharsets.UTF_8)); + this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder(); } @Override @@ -47,16 +56,20 @@ public class YamlHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> @Override protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { - var yaml = new Yaml(); - return yaml.loadAs(inputMessage.getBody(), clazz); + try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) { + return coder.decode(is, clazz); + } catch (CoderException e) { + throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); + } } @Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { - var yaml = new Yaml(); try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) { - yaml.dump(t, writer); + coder.encode(writer, t); + } catch (CoderException e) { + throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); } } } |