ab09c1a6ff90d91826fdb9aac4a6313c7245b2ab
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.http.server;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.lang.annotation.Annotation;
29 import java.lang.reflect.Type;
30 import java.nio.charset.StandardCharsets;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.ext.MessageBodyReader;
36 import javax.ws.rs.ext.MessageBodyWriter;
37 import javax.ws.rs.ext.Provider;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardYamlCoder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Provider that serializes and de-serializes JSON via gson.
45  */
46 @Provider
47 @Consumes(MediaType.WILDCARD)
48 @Produces(MediaType.WILDCARD)
49 public class YamlMessageBodyHandler implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
50
51     public static final Logger logger = LoggerFactory.getLogger(YamlMessageBodyHandler.class);
52
53     /**
54      * Constructs the object.
55      */
56     public YamlMessageBodyHandler() {
57         logger.info("Accepting YAML for REST calls");
58     }
59
60     @Override
61     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
62         return canHandle(mediaType);
63     }
64
65     @Override
66     public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
67         return -1;
68     }
69
70     @Override
71     public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
72                     MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
73
74         try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
75             new StandardYamlCoder().encode(writer, object);
76
77         } catch (CoderException e) {
78             throw new IOException(e);
79         }
80     }
81
82     @Override
83     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
84         return canHandle(mediaType);
85     }
86
87     /**
88      * Determines if this provider can handle the given media type.
89      *
90      * @param mediaType the media type of interest
91      * @return {@code true} if this provider handles the given media type, {@code false}
92      *         otherwise
93      */
94     private boolean canHandle(MediaType mediaType) {
95         return (mediaType != null && "yaml".equalsIgnoreCase(mediaType.getSubtype()));
96     }
97
98     @Override
99     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
100                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
101
102         try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
103             Class<?> clazz = (Class<?>) genericType;
104             return new StandardYamlCoder().decode(streamReader, clazz);
105
106         } catch (CoderException e) {
107             throw new IOException(e);
108         }
109     }
110 }