diff options
author | Jim Hahn <jrh3@att.com> | 2019-09-13 12:14:25 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-09-13 17:40:29 -0400 |
commit | af2e4018c92b0fb311f60dfe507335b12e2935a4 (patch) | |
tree | 62705712e2a88682e17bdd9fef248e866c7407e4 /policy-endpoints/src/main | |
parent | 63922e3dc9101f019cb490f6c5d02991c10fcdf5 (diff) |
Extract YamlJsonTranslator from StandardYamlCoder
Refactored StandardYamlCoder, extracting a new class, YamlJsonTranslator,
from it. This facilitates performing yaml translation when not using a
standard "gson" coder.
Added YamlJacksonHandler which supports YAML translation layered on
top of a JacksonHandler instead of a GsonMessageBodyHandler.
Also added junit tests to complete coverage of StandardCoder.
Also added public APPLICATION_YAML to YamlMessageBodyHandler.
Change-Id: Ia470fa194661fbf1aebeaf0f18b57f2a984cb64b
Issue-ID: POLICY-2081
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-endpoints/src/main')
3 files changed, 81 insertions, 26 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java index ac96cab0..7eac932a 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlExceptionMapper.java @@ -34,7 +34,7 @@ import org.yaml.snakeyaml.error.YAMLException; * error code to an HTTP 400 error code. */ @Provider -@Produces("application/yaml") +@Produces(YamlMessageBodyHandler.APPLICATION_YAML) public class YamlExceptionMapper implements ExceptionMapper<YAMLException> { private static Logger logger = LoggerFactory.getLogger(YamlExceptionMapper.class); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java new file mode 100644 index 00000000..0cab374f --- /dev/null +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlJacksonHandler.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.endpoints.http.server; + +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.ext.Provider; +import org.onap.policy.common.gson.JacksonHandler; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; + +/** + * Provider that serializes and de-serializes YAML via snakeyaml and gson using jackson + * default behaviors and annotations. + */ +@Provider +@Consumes(MediaType.WILDCARD) +@Produces(MediaType.WILDCARD) +public class YamlJacksonHandler extends YamlMessageBodyHandler { + + /** + * Translator to be used. We want a GSON object that's configured the same way as it + * is in {@link JacksonHandler}, so just get it from there. + */ + private static final YamlJsonTranslator TRANS = new YamlJsonTranslator(new JacksonHandler().getGson()); + + /** + * Constructs the object. + */ + public YamlJacksonHandler() { + super(TRANS); + } +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java index 36418e4a..89aa8ff6 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/YamlMessageBodyHandler.java @@ -26,7 +26,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.Reader; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; @@ -37,14 +36,14 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardYamlCoder; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.error.YAMLException; /** - * Provider that serializes and de-serializes JSON via gson. + * Provider that serializes and de-serializes YAML via snakeyaml and gson. */ @Provider @Consumes(MediaType.WILDCARD) @@ -53,11 +52,33 @@ public class YamlMessageBodyHandler implements MessageBodyReader<Object>, Messag public static final Logger logger = LoggerFactory.getLogger(YamlMessageBodyHandler.class); + public static final String APPLICATION_YAML = "application/yaml"; + + /** + * Translator that's used when none is specified. We want a GSON object that's + * configured the same way as it is in {@link GsonMessageBodyHandler}, so just get it + * from there. + */ + private static final YamlJsonTranslator DEFAULT_TRANSLATOR = + new YamlJsonTranslator(new GsonMessageBodyHandler().getGson()); + + private final YamlJsonTranslator translator; + /** * Constructs the object. */ public YamlMessageBodyHandler() { + this(DEFAULT_TRANSLATOR); + } + + /** + * Constructs the object. + * + * @param translator translator to use to translate to/from YAML + */ + public YamlMessageBodyHandler(YamlJsonTranslator translator) { logger.info("Accepting YAML for REST calls"); + this.translator = translator; } @Override @@ -75,10 +96,7 @@ public class YamlMessageBodyHandler implements MessageBodyReader<Object>, Messag MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) { - new MyYamlCoder().encode(writer, object); - - } catch (CoderException e) { - throw new IOException(e); + translator.toYaml(writer, object); } } @@ -104,24 +122,10 @@ public class YamlMessageBodyHandler implements MessageBodyReader<Object>, Messag try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) { Class<?> clazz = (Class<?>) genericType; - return new MyYamlCoder().decode(streamReader, clazz); - } - } + return translator.fromYaml(streamReader, clazz); - /** - * Yaml coder that yields YAMLException on input so that the http servlet can identify - * it and generate a bad-request status code. Only the {@link #decode(Reader, Class)} - * method must be overridden. - */ - private static class MyYamlCoder extends StandardYamlCoder { - @Override - public <T> T decode(Reader source, Class<T> clazz) { - try { - return fromJson(source, clazz); - - } catch (JsonSyntaxException e) { - throw new YAMLException(e); - } + } catch (JsonSyntaxException e) { + throw new YAMLException(e); } } } |