package com.devicehive.resource.converters; /* * #%L * DeviceHive Java Server Common business logic * %% * Copyright (C) 2016 DataArt * %% * 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. * #L% */ import com.devicehive.configuration.Messages; import com.devicehive.json.GsonFactory; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; 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; import java.nio.charset.Charset; import static com.devicehive.configuration.Constants.UTF8; @Provider public class JsonRawProvider implements MessageBodyWriter<JsonObject>, MessageBodyReader<JsonObject> { @Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return MediaType.APPLICATION_JSON_TYPE.getType().equals(mediaType.getType()) && MediaType .APPLICATION_JSON_TYPE.getSubtype().equals(mediaType.getSubtype()); } @Override public JsonObject readFrom(Class<JsonObject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { JsonElement element = new JsonParser().parse(new InputStreamReader(entityStream, Charset.forName(UTF8))); if (element.isJsonObject()) { return element.getAsJsonObject(); } throw new IOException(Messages.NOT_A_JSON_OBJECT); } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return MediaType.APPLICATION_JSON_TYPE.getType().equals(mediaType.getType()) && MediaType .APPLICATION_JSON_TYPE.getSubtype().equals(mediaType.getSubtype()); } @Override public long getSize(JsonObject jsonObject, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return 0; } @Override public void writeTo(JsonObject jsonObject, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { Gson gson = GsonFactory.createGson(); Writer writer = null; try { writer = new OutputStreamWriter(entityStream, Charset.forName(UTF8)); gson.toJson(jsonObject, writer); } finally { if (writer != null) { writer.flush(); } } } }