]> git.basschouten.com Git - openhab-addons.git/blob
99d905416d64ac4a5e66e5c6ea348ef173066c8f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lametrictime.internal;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.io.OutputStream;
19 import java.lang.annotation.Annotation;
20 import java.lang.reflect.Type;
21 import java.nio.charset.StandardCharsets;
22
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.WebApplicationException;
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.MessageBodyWriter;
30 import javax.ws.rs.ext.Provider;
31
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.openhab.binding.lametrictime.internal.api.common.impl.GsonGenerator;
34 import org.osgi.service.component.annotations.Component;
35
36 import com.google.gson.Gson;
37
38 /**
39  * JSON reader/writer for Jersey using GSON.
40  *
41  * @author Simon Kaufmann - Initial contribution
42  *
43  * @param <T>
44  */
45 @Provider
46 @Produces(MediaType.APPLICATION_JSON)
47 @Consumes(MediaType.APPLICATION_JSON)
48 @Component
49 public class GsonProvider<T> implements MessageBodyReader<T>, MessageBodyWriter<T> {
50
51     private final Gson gson;
52
53     public GsonProvider() {
54         gson = GsonGenerator.create();
55     }
56
57     @Override
58     public long getSize(T t, @Nullable Class<?> type, @Nullable Type genericType, Annotation @Nullable [] annotations,
59             @Nullable MediaType mediaType) {
60         return -1;
61     }
62
63     @Override
64     public boolean isWriteable(@Nullable Class<?> type, @Nullable Type genericType, Annotation @Nullable [] annotations,
65             @Nullable MediaType mediaType) {
66         return true;
67     }
68
69     @Override
70     public void writeTo(T object, @Nullable Class<?> type, @Nullable Type genericType,
71             Annotation @Nullable [] annotations, @Nullable MediaType mediaType,
72             @Nullable MultivaluedMap<String, Object> httpHeaders, @Nullable OutputStream entityStream)
73             throws IOException, WebApplicationException {
74         try (OutputStream stream = entityStream) {
75             entityStream.write(gson.toJson(object).getBytes(StandardCharsets.UTF_8));
76             entityStream.flush();
77         }
78     }
79
80     @Override
81     public boolean isReadable(@Nullable Class<?> type, @Nullable Type genericType, Annotation @Nullable [] annotations,
82             @Nullable MediaType mediaType) {
83         return true;
84     }
85
86     @Override
87     public T readFrom(@Nullable Class<T> type, @Nullable Type genericType, Annotation @Nullable [] annotations,
88             @Nullable MediaType mediaType, @Nullable MultivaluedMap<String, String> httpHeaders,
89             @Nullable InputStream entityStream) throws IOException, WebApplicationException {
90         try (InputStreamReader reader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
91             return gson.fromJson(reader, type);
92         }
93     }
94 }