forked from Archives/Athou_commafeed
stream response, bypassing the string step
This commit is contained in:
@@ -2,7 +2,9 @@ package com.commafeed.frontend.rest;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@@ -19,7 +21,6 @@ import javax.ws.rs.ext.MessageBodyReader;
|
|||||||
import javax.ws.rs.ext.MessageBodyWriter;
|
import javax.ws.rs.ext.MessageBodyWriter;
|
||||||
import javax.ws.rs.ext.Provider;
|
import javax.ws.rs.ext.Provider;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.commons.lang.time.DateUtils;
|
import org.apache.commons.lang.time.DateUtils;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
@@ -35,6 +36,10 @@ import com.google.gson.JsonSerializer;
|
|||||||
public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
||||||
MessageBodyReader<Object> {
|
MessageBodyReader<Object> {
|
||||||
|
|
||||||
|
private static final String UTF_8 = "UTF-8";
|
||||||
|
|
||||||
|
private Gson gson;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isWriteable(Class<?> type, Type genericType,
|
public boolean isWriteable(Class<?> type, Type genericType,
|
||||||
Annotation[] annotations, MediaType mediaType) {
|
Annotation[] annotations, MediaType mediaType) {
|
||||||
@@ -61,8 +66,18 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
|||||||
WebApplicationException {
|
WebApplicationException {
|
||||||
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
|
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
|
||||||
+ ";charset=UTF-8");
|
+ ";charset=UTF-8");
|
||||||
IOUtils.write(getGson().toJson(t), entityStream, "UTF-8");
|
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
|
||||||
|
try {
|
||||||
|
Type jsonType;
|
||||||
|
if (type.equals(genericType)) {
|
||||||
|
jsonType = type;
|
||||||
|
} else {
|
||||||
|
jsonType = genericType;
|
||||||
|
}
|
||||||
|
getGson().toJson(t, jsonType, writer);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -70,13 +85,26 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
|
|||||||
Annotation[] annotations, MediaType mediaType,
|
Annotation[] annotations, MediaType mediaType,
|
||||||
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
|
||||||
throws IOException, WebApplicationException {
|
throws IOException, WebApplicationException {
|
||||||
String json = IOUtils.toString(entityStream, "UTF-8");
|
InputStreamReader streamReader = new InputStreamReader(entityStream,
|
||||||
return getGson().fromJson(json, type);
|
UTF_8);
|
||||||
|
try {
|
||||||
|
Type jsonType;
|
||||||
|
if (type.equals(genericType)) {
|
||||||
|
jsonType = type;
|
||||||
|
} else {
|
||||||
|
jsonType = genericType;
|
||||||
|
}
|
||||||
|
return getGson().fromJson(streamReader, jsonType);
|
||||||
|
} finally {
|
||||||
|
streamReader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Gson getGson() {
|
private Gson getGson() {
|
||||||
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
if (gson == null) {
|
||||||
new DateSerializer()).create();
|
gson = new GsonBuilder().registerTypeAdapter(Date.class,
|
||||||
|
new DateSerializer()).create();
|
||||||
|
}
|
||||||
return gson;
|
return gson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user