cdd6ea73b298eb72e163e79eae482132b4072f89
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.config;
22
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.OutputStreamWriter;
26 import java.nio.charset.StandardCharsets;
27 import javax.ws.rs.core.Response;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.common.utils.coder.Coder;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.coder.StandardYamlCoder;
33 import org.springframework.http.HttpInputMessage;
34 import org.springframework.http.HttpOutputMessage;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.converter.AbstractHttpMessageConverter;
37 import org.springframework.http.converter.HttpMessageNotReadableException;
38 import org.springframework.http.converter.HttpMessageNotWritableException;
39
40 public class CoderHttpMesageConverter<T> extends AbstractHttpMessageConverter<T> {
41
42     private Coder coder;
43
44     public CoderHttpMesageConverter(String type) {
45         super(new MediaType("application", type, StandardCharsets.UTF_8));
46         this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder();
47     }
48
49     @Override
50     protected boolean supports(Class<?> clazz) {
51         return true;
52     }
53
54     @Override
55     protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
56             throws IOException, HttpMessageNotReadableException {
57         try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) {
58             return coder.decode(is, clazz);
59         } catch (CoderException e) {
60             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
61         }
62     }
63
64     @Override
65     protected void writeInternal(T t, HttpOutputMessage outputMessage)
66             throws IOException, HttpMessageNotWritableException {
67         try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) {
68             coder.encode(writer, t);
69         } catch (CoderException e) {
70             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
71         }
72     }
73
74 }