Java 17 Upgrade
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / rest / TextMessageBodyHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.dmaap.rest;
23
24 import jakarta.ws.rs.Consumes;
25 import jakarta.ws.rs.core.MediaType;
26 import jakarta.ws.rs.core.MultivaluedMap;
27 import jakarta.ws.rs.ext.MessageBodyReader;
28 import jakarta.ws.rs.ext.Provider;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.lang.annotation.Annotation;
34 import java.lang.reflect.Type;
35 import java.nio.charset.StandardCharsets;
36 import java.util.LinkedList;
37 import java.util.List;
38
39 /**
40  * Provider that decodes "text/plain" messages.
41  */
42 @Provider
43 @Consumes(TextMessageBodyHandler.MEDIA_TYPE_TEXT_PLAIN)
44 public class TextMessageBodyHandler implements MessageBodyReader<Object> {
45     public static final String MEDIA_TYPE_TEXT_PLAIN = "text/plain";
46
47     @Override
48     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
49         return (mediaType != null && MEDIA_TYPE_TEXT_PLAIN.equals(mediaType.toString()));
50     }
51
52     @Override
53     public List<Object> readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
54                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
55
56         try (var bufferedReader = new BufferedReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8))) {
57             List<Object> messages = new LinkedList<>();
58             String msg;
59             while ((msg = bufferedReader.readLine()) != null) {
60                 messages.add(msg);
61             }
62
63             return messages;
64         }
65     }
66 }