a29afef46071d161bc0306933c38f03f5aa19830
[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.internal;
22
23 import com.google.gson.Gson;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.io.OutputStreamWriter;
29 import java.lang.annotation.Annotation;
30 import java.lang.reflect.Type;
31 import java.nio.charset.StandardCharsets;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.ext.MessageBodyReader;
38 import javax.ws.rs.ext.MessageBodyWriter;
39 import javax.ws.rs.ext.Provider;
40
41 /**
42  * Provider that serializes and de-serializes JSON via gson.
43  * 
44  * <p>Note: <i>jersey</i> will ignore this class if the maven artifact,
45  * <i>jersey-media-json-jackson</i>, is included, regardless of whether it's included
46  * directly or indirectly.
47  */
48 @Provider
49 @Consumes(MediaType.WILDCARD)
50 @Produces(MediaType.WILDCARD)
51 public class GsonMessageBodyHandler implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
52
53     /**
54      * Object to be used to serialize and de-serialize.
55      */
56     private Gson gson;
57
58     /**
59      * Constructs the object, using a plain Gson object.
60      */
61     public GsonMessageBodyHandler() {
62         this(new Gson());
63     }
64
65     /**
66      * Constructs the object.
67      * 
68      * @param gson the Gson object to be used to serialize and de-serialize
69      */
70     public GsonMessageBodyHandler(Gson gson) {
71         this.gson = gson;
72     }
73
74     @Override
75     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
76         return canHandle(mediaType);
77     }
78
79     @Override
80     public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
81         return -1;
82     }
83
84     @Override
85     public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
86                     MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
87                     throws IOException, WebApplicationException {
88
89         try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
90             Type jsonType = (type.equals(genericType) ? type : genericType);
91             gson.toJson(object, jsonType, writer);
92         }
93     }
94
95     @Override
96     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
97         return canHandle(mediaType);
98     }
99
100     /**
101      * Determines if this provider can handle the given media type.
102      * 
103      * @param mediaType the media type of interest
104      * @return {@code true} if this provider handles the given media type, {@code false}
105      *         otherwise
106      */
107     private boolean canHandle(MediaType mediaType) {
108         if (mediaType == null) {
109             return true;
110         }
111
112         String subtype = mediaType.getSubtype();
113
114         return "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json") || "javascript".equals(subtype)
115                         || "x-javascript".equals(subtype) || "x-json".equals(subtype);
116     }
117
118     @Override
119     public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
120                     MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
121                     throws IOException, WebApplicationException {
122
123         try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
124             Type jsonType = (type.equals(genericType) ? type : genericType);
125             return gson.fromJson(streamReader, jsonType);
126         }
127     }
128 }