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