package com.genuitec.qfconf.backend.encoding;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class GsonMessageBodyHandler implements MessageBodyWriter<Object>,
		MessageBodyReader<Object> {

	private static final String UTF_8 = "UTF-8";

	private Gson gson;

	private Gson getGson() {
		if (gson == null) {
			final GsonBuilder gsonBuilder = new GsonBuilder();
			gson = gsonBuilder.create();
		}
		return gson;
	}

	@Override
	public boolean isReadable(Class<?> type, Type genericType,
			java.lang.annotation.Annotation[] annotations, MediaType mediaType) {
		return true;
	}

	@Override
	public Object readFrom(Class<Object> type, Type genericType,
			Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
			throws IOException, WebApplicationException {
		InputStreamReader streamReader = new InputStreamReader(entityStream,
				UTF_8);
		try {
			Type jsonType;
			if (type.equals(genericType)) {
				jsonType = type;
			} else {
				jsonType = genericType;
			}
			return getGson().fromJson(streamReader, jsonType);
		} finally {
			streamReader.close();
		}
	}

	@Override
	public boolean isWriteable(Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType) {
		return true;
	}

	@Override
	public long getSize(Object object, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType) {
		return -1;
	}

	@Override
	public void writeTo(Object object, Class<?> type, Type genericType,
			Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			OutputStream entityStream) throws IOException,
			WebApplicationException {
		OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
		try {
			Type jsonType;
			if (type.equals(genericType)) {
				jsonType = type;
			} else {
				jsonType = genericType;
			}
			getGson().toJson(object, jsonType, writer);
		} finally {
			writer.close();
		}
	}
}
@@ -856,17 +856,16 @@
 ring UTF
-_
 8 = %22UTF
@@ -869,16 +869,16 @@
 UTF-8%22;%0A
+
 %0A%09privat
@@ -1052,171 +1052,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public boolean isReadable(Class%3C?%3E type, Type genericType,%0A%09%09%09java.lang.annotation.Annotation%5B%5D annotations, MediaType mediaType) %7B%0A%09%09return true;%0A%09%7D%0A%0A
 %09@Ov
@@ -1371,17 +1371,16 @@
 %0A%09%09%09%09UTF
-_
 8);%0A%09%09tr
@@ -1607,300 +1607,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public boolean isWriteable(Class%3C?%3E type, Type genericType,%0A%09%09%09Annotation%5B%5D annotations, MediaType mediaType) %7B%0A%09%09return true;%0A%09%7D%0A%0A%09@Override%0A%09public long getSize(Object object, Class%3C?%3E type, Type genericType,%0A%09%09%09Annotation%5B%5D annotations, MediaType mediaType) %7B%0A%09%09return -1;%0A%09%7D%0A%0A
 %09@Ov
@@ -1929,17 +1929,16 @@
 eam, UTF
-_
 8);%0A%09%09tr
@@ -2140,16 +2140,471 @@
 lose();%0A
-
 %09%09%7D%0A%09%7D
+%0A%0A%09@Override%0A%09public boolean isReadable(Class%3C?%3E type, Type genericType,%0A%09%09%09java.lang.annotation.Annotation%5B%5D annotations, MediaType mediaType) %7B%0A%09%09return true;%0A%09%7D%0A%0A%09@Override%0A%09public boolean isWriteable(Class%3C?%3E type, Type genericType,%0A%09%09%09Annotation%5B%5D annotations, MediaType mediaType) %7B%0A%09%09return true;%0A%09%7D%0A%0A%09@Override%0A%09public long getSize(Object object, Class%3C?%3E type, Type genericType,%0A%09%09%09Annotation%5B%5D annotations, MediaType mediaType) %7B%0A%09%09return -1;%0A%09%7D
 %0A%7D

Clean up Gson encoding