2013-03-23 15:52:26 +01:00
|
|
|
package com.commafeed.frontend.rest;
|
|
|
|
|
|
2013-03-26 11:36:31 +01:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
|
import java.io.BufferedOutputStream;
|
2013-03-23 15:52:26 +01:00
|
|
|
import java.io.IOException;
|
2013-03-23 23:14:14 +01:00
|
|
|
import java.io.InputStream;
|
2013-03-26 10:20:23 +01:00
|
|
|
import java.io.InputStreamReader;
|
2013-03-23 15:52:26 +01:00
|
|
|
import java.io.OutputStream;
|
2013-03-26 10:20:23 +01:00
|
|
|
import java.io.OutputStreamWriter;
|
2013-03-23 15:52:26 +01:00
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
2013-03-23 23:14:14 +01:00
|
|
|
import javax.ws.rs.Consumes;
|
2013-03-23 15:52:26 +01:00
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
|
import javax.ws.rs.core.HttpHeaders;
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
import javax.ws.rs.core.MultivaluedMap;
|
2013-03-23 23:14:14 +01:00
|
|
|
import javax.ws.rs.ext.MessageBodyReader;
|
2013-03-23 15:52:26 +01:00
|
|
|
import javax.ws.rs.ext.MessageBodyWriter;
|
|
|
|
|
import javax.ws.rs.ext.Provider;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang.time.DateUtils;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
|
import com.google.gson.JsonSerializationContext;
|
|
|
|
|
import com.google.gson.JsonSerializer;
|
|
|
|
|
|
|
|
|
|
@Provider
|
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2013-03-23 23:14:14 +01:00
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
|
|
|
|
MessageBodyReader<Object> {
|
2013-03-23 15:52:26 +01:00
|
|
|
|
2013-03-26 10:20:23 +01:00
|
|
|
private static final String UTF_8 = "UTF-8";
|
|
|
|
|
|
|
|
|
|
private Gson gson;
|
|
|
|
|
|
2013-03-23 15:52:26 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean isWriteable(Class<?> type, Type genericType,
|
|
|
|
|
Annotation[] annotations, MediaType mediaType) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 23:14:14 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean isReadable(Class<?> type, Type genericType,
|
|
|
|
|
Annotation[] annotations, MediaType mediaType) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 15:52:26 +01:00
|
|
|
@Override
|
|
|
|
|
public long getSize(Object t, Class<?> type, Type genericType,
|
|
|
|
|
Annotation[] annotations, MediaType mediaType) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void writeTo(Object t, Class<?> type, Type genericType,
|
|
|
|
|
Annotation[] annotations, MediaType mediaType,
|
|
|
|
|
MultivaluedMap<String, Object> httpHeaders,
|
|
|
|
|
OutputStream entityStream) throws IOException,
|
|
|
|
|
WebApplicationException {
|
|
|
|
|
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
|
|
|
|
|
+ ";charset=UTF-8");
|
2013-03-26 11:36:31 +01:00
|
|
|
OutputStreamWriter writer = new OutputStreamWriter(
|
|
|
|
|
new BufferedOutputStream(entityStream), UTF_8);
|
|
|
|
|
|
2013-03-26 10:20:23 +01:00
|
|
|
try {
|
|
|
|
|
Type jsonType;
|
|
|
|
|
if (type.equals(genericType)) {
|
|
|
|
|
jsonType = type;
|
|
|
|
|
} else {
|
|
|
|
|
jsonType = genericType;
|
|
|
|
|
}
|
|
|
|
|
getGson().toJson(t, jsonType, writer);
|
|
|
|
|
} finally {
|
|
|
|
|
writer.close();
|
|
|
|
|
}
|
2013-03-23 23:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object readFrom(Class<Object> type, Type genericType,
|
|
|
|
|
Annotation[] annotations, MediaType mediaType,
|
|
|
|
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
|
|
|
|
|
throws IOException, WebApplicationException {
|
2013-03-26 11:36:31 +01:00
|
|
|
InputStreamReader reader = new InputStreamReader(
|
|
|
|
|
new BufferedInputStream(entityStream), UTF_8);
|
2013-03-26 10:20:23 +01:00
|
|
|
try {
|
|
|
|
|
Type jsonType;
|
|
|
|
|
if (type.equals(genericType)) {
|
|
|
|
|
jsonType = type;
|
|
|
|
|
} else {
|
|
|
|
|
jsonType = genericType;
|
|
|
|
|
}
|
2013-03-26 11:36:31 +01:00
|
|
|
return getGson().fromJson(reader, jsonType);
|
2013-03-26 10:20:23 +01:00
|
|
|
} finally {
|
2013-03-26 11:36:31 +01:00
|
|
|
reader.close();
|
2013-03-26 10:20:23 +01:00
|
|
|
}
|
2013-03-23 23:14:14 +01:00
|
|
|
}
|
2013-03-23 15:52:26 +01:00
|
|
|
|
2013-03-23 23:14:14 +01:00
|
|
|
private Gson getGson() {
|
2013-03-26 10:20:23 +01:00
|
|
|
if (gson == null) {
|
|
|
|
|
gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
|
|
|
|
new DateSerializer()).create();
|
|
|
|
|
}
|
2013-03-23 23:14:14 +01:00
|
|
|
return gson;
|
2013-03-23 15:52:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class DateSerializer implements JsonSerializer<Date> {
|
|
|
|
|
|
|
|
|
|
private static final String DAY_FORMAT = "yyyy-MM-dd";
|
|
|
|
|
private static final String TIME_FORMAT = "HH:mm";
|
|
|
|
|
|
|
|
|
|
public JsonElement serialize(Date src, Type typeOfSrc,
|
|
|
|
|
JsonSerializationContext context) {
|
|
|
|
|
Date now = Calendar.getInstance().getTime();
|
|
|
|
|
String format = DateUtils.isSameDay(now, src) ? TIME_FORMAT
|
|
|
|
|
: DAY_FORMAT;
|
|
|
|
|
return new JsonPrimitive(new SimpleDateFormat(format).format(src));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|