d9a72ce107fdc1806c2271b0e5e78c7f0668a669
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.participant.simulator.config;
24
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.nio.charset.StandardCharsets;
29 import javax.ws.rs.core.Response;
30 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.coder.StandardYamlCoder;
35 import org.springframework.http.HttpInputMessage;
36 import org.springframework.http.HttpOutputMessage;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.converter.AbstractHttpMessageConverter;
39 import org.springframework.http.converter.HttpMessageNotReadableException;
40 import org.springframework.http.converter.HttpMessageNotWritableException;
41
42 public class YamlHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> {
43
44     private Coder coder;
45
46     public YamlHttpMessageConverter(String type) {
47         super(new MediaType("application", type, StandardCharsets.UTF_8));
48         this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder();
49     }
50
51     @Override
52     protected boolean supports(Class<?> clazz) {
53         return true;
54     }
55
56     @Override
57     protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
58             throws IOException, HttpMessageNotReadableException {
59         try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) {
60             return coder.decode(is, clazz);
61         } catch (CoderException e) {
62             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
63         }
64     }
65
66     @Override
67     protected void writeInternal(T t, HttpOutputMessage outputMessage)
68             throws IOException, HttpMessageNotWritableException {
69         try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) {
70             coder.encode(writer, t);
71         } catch (CoderException e) {
72             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
73         }
74     }
75 }