Java tutorial
/* * Copyright (C) 2016 R&D Solutions Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.hawkcd.http; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.hawkcd.utilities.deserializers.MaterialDefinitionAdapter; import io.hawkcd.utilities.deserializers.TaskDefinitionAdapter; import io.hawkcd.utilities.deserializers.WsContractDeserializer; import io.hawkcd.model.MaterialDefinition; import io.hawkcd.model.TaskDefinition; import io.hawkcd.model.dto.WsContractDto; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; import javax.ws.rs.ext.MessageBodyReader; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import java.io.*; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @Provider @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class GsonProvider<T> implements MessageBodyReader<T>, MessageBodyWriter<T> { private static final String PRETTY_PRINT = "pretty-print"; private final Gson gson; private final Gson prettyGson; @Context private UriInfo ui; public GsonProvider() { GsonBuilder builder = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization(); this.gson = builder.registerTypeAdapter(WsContractDto.class, new WsContractDeserializer()) .registerTypeAdapter(TaskDefinition.class, new TaskDefinitionAdapter()) .registerTypeAdapter(MaterialDefinition.class, new MaterialDefinitionAdapter()).create(); this.prettyGson = builder.setPrettyPrinting().create(); } @Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return true; } @Override public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8"); try { return gson.fromJson(reader, type); } finally { reader.close(); } } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return true; } @Override public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { PrintWriter printWriter = new PrintWriter(entityStream); try { String json; if (ui.getQueryParameters().containsKey(PRETTY_PRINT)) { json = prettyGson.toJson(t); } else { json = gson.toJson(t); } printWriter.write(json); printWriter.flush(); } finally { printWriter.close(); } } }