2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.http.server;
23 import com.google.gson.JsonSyntaxException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.io.OutputStreamWriter;
29 import java.io.Reader;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Type;
32 import java.nio.charset.StandardCharsets;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.ext.MessageBodyReader;
38 import javax.ws.rs.ext.MessageBodyWriter;
39 import javax.ws.rs.ext.Provider;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardYamlCoder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.yaml.snakeyaml.error.YAMLException;
47 * Provider that serializes and de-serializes JSON via gson.
50 @Consumes(MediaType.WILDCARD)
51 @Produces(MediaType.WILDCARD)
52 public class YamlMessageBodyHandler implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
54 public static final Logger logger = LoggerFactory.getLogger(YamlMessageBodyHandler.class);
57 * Constructs the object.
59 public YamlMessageBodyHandler() {
60 logger.info("Accepting YAML for REST calls");
64 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
65 return canHandle(mediaType);
69 public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
74 public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
75 MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
77 try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
78 new MyYamlCoder().encode(writer, object);
80 } catch (CoderException e) {
81 throw new IOException(e);
86 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
87 return canHandle(mediaType);
91 * Determines if this provider can handle the given media type.
93 * @param mediaType the media type of interest
94 * @return {@code true} if this provider handles the given media type, {@code false}
97 private boolean canHandle(MediaType mediaType) {
98 return (mediaType != null && "yaml".equalsIgnoreCase(mediaType.getSubtype()));
102 public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
103 MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
105 try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
106 Class<?> clazz = (Class<?>) genericType;
107 return new MyYamlCoder().decode(streamReader, clazz);
112 * Yaml coder that yields YAMLException on input so that the http servlet can identify
113 * it and generate a bad-request status code. Only the {@link #decode(Reader, Class)}
114 * method must be overridden.
116 private static class MyYamlCoder extends StandardYamlCoder {
118 public <T> T decode(Reader source, Class<T> clazz) {
120 return fromJson(source, clazz);
122 } catch (JsonSyntaxException e) {
123 throw new YAMLException(e);