2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.endpoints.http.server.internal;
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;
42 * Provider that serializes and de-serializes JSON via gson.
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.
49 @Consumes(MediaType.WILDCARD)
50 @Produces(MediaType.WILDCARD)
51 public class GsonMessageBodyHandler implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
54 * Object to be used to serialize and de-serialize.
59 * Constructs the object, using a plain Gson object.
61 public GsonMessageBodyHandler() {
66 * Constructs the object.
68 * @param gson the Gson object to be used to serialize and de-serialize
70 public GsonMessageBodyHandler(Gson gson) {
75 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
76 return canHandle(mediaType);
80 public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
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 {
89 try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
90 Type jsonType = (type.equals(genericType) ? type : genericType);
91 gson.toJson(object, jsonType, writer);
96 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
97 return canHandle(mediaType);
101 * Determines if this provider can handle the given media type.
103 * @param mediaType the media type of interest
104 * @return {@code true} if this provider handles the given media type, {@code false}
107 private boolean canHandle(MediaType mediaType) {
108 if (mediaType == null) {
112 String subtype = mediaType.getSubtype();
114 return "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json") || "javascript".equals(subtype)
115 || "x-javascript".equals(subtype) || "x-json".equals(subtype);
119 public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
120 MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
121 throws IOException, WebApplicationException {
123 try (InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
124 Type jsonType = (type.equals(genericType) ? type : genericType);
125 return gson.fromJson(streamReader, jsonType);