Skip to content

Commit

Permalink
Merge pull request #1036 from nykolaslima/avoid-flush-on-serializers
Browse files Browse the repository at this point in the history
letting web server commit the response naturally
  • Loading branch information
Turini committed Dec 11, 2015
2 parents 27afc6b + 9068875 commit fb6646a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void serialize() {
response.getWriter().append(callbackName).append("(");
super.serialize();
response.getWriter().append(")");
response.getWriter().flush();
} catch (IOException e) {
throw new ResultException("Unable to serialize data", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package br.com.caelum.vraptor.serialization.gson;

import static com.google.common.io.Flushables.flushQuietly;
import static java.util.Collections.singletonMap;
import static java.util.Objects.requireNonNull;

Expand All @@ -29,13 +28,13 @@

import javax.enterprise.inject.Vetoed;

import com.google.gson.Gson;

import br.com.caelum.vraptor.core.ReflectionProvider;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor;
import br.com.caelum.vraptor.serialization.Serializer;
import br.com.caelum.vraptor.serialization.SerializerBuilder;

import com.google.gson.Gson;

/**
* A SerializerBuilder based on Gson.
*
Expand Down Expand Up @@ -144,8 +143,6 @@ public void serialize() {
} else {
gson.toJson(singletonMap(alias, root), writer);
}

flushQuietly(writer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public PrintWriter getWriter() {
}

public String getContentAsString() {
writer.flush();
return this.content.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import javax.enterprise.inject.Vetoed;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.View;
import br.com.caelum.vraptor.core.DefaultReflectionProvider;
import br.com.caelum.vraptor.core.ReflectionProvider;
Expand All @@ -45,9 +48,6 @@
import br.com.caelum.vraptor.serialization.xstream.XStreamXMLSerialization;
import br.com.caelum.vraptor.view.EmptyResult;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

/**
*
* A mocked Result for testing your serialized objects returns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import org.junit.Before;
import org.junit.Test;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.core.DefaultReflectionProvider;
import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
Expand All @@ -50,12 +53,11 @@
import br.com.caelum.vraptor.util.test.MockInstanceImpl;
import br.com.caelum.vraptor.validator.SingletonResourceBundle;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

public class I18nMessageSerializationTest {

private I18nMessageSerialization serialization;
private ByteArrayOutputStream stream;
private PrintWriter writer;

@Before
public void setup() throws Exception {
Expand All @@ -64,7 +66,8 @@ public void setup() throws Exception {
Environment environment = mock(Environment.class);

HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(new PrintWriter(stream));
writer = new PrintWriter(stream);
when(response.getWriter()).thenReturn(writer);
DefaultTypeNameExtractor extractor = new DefaultTypeNameExtractor();
XStreamBuilder builder = XStreamBuilderImpl.cleanInstance(new MessageConverter());
XStreamXMLSerialization xmlSerialization = new XStreamXMLSerialization(response, builder, environment);
Expand All @@ -91,6 +94,8 @@ public void setup() throws Exception {
public void shouldCallJsonSerialization() {
String expectedResult = "{\"message\":{\"category\":\"success\",\"message\":\"Just another {0} in {1}\"}}";
serialization.from("success", "message.cat").as(json());
writer.flush();

assertThat(result(), is(equalTo(expectedResult)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -40,15 +42,6 @@
import org.junit.Before;
import org.junit.Test;

import br.com.caelum.vraptor.core.DefaultReflectionProvider;
import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.JSONPSerialization;
import br.com.caelum.vraptor.serialization.JSONSerialization;
import br.com.caelum.vraptor.serialization.Serializee;
import br.com.caelum.vraptor.serialization.SkipSerialization;
import br.com.caelum.vraptor.util.test.MockInstanceImpl;

import com.google.common.collect.ForwardingCollection;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
Expand All @@ -60,14 +53,22 @@
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.Since;

import br.com.caelum.vraptor.core.DefaultReflectionProvider;
import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.JSONPSerialization;
import br.com.caelum.vraptor.serialization.JSONSerialization;
import br.com.caelum.vraptor.serialization.Serializee;
import br.com.caelum.vraptor.serialization.SkipSerialization;
import br.com.caelum.vraptor.util.test.MockInstanceImpl;

public class GsonJSONSerializationTest {

private GsonJSONSerialization serialization;
private ByteArrayOutputStream stream;
private HttpServletResponse response;
private DefaultTypeNameExtractor extractor;
private Environment environment;

private GsonSerializerBuilder builder;

@Before
Expand All @@ -77,7 +78,7 @@ public void setup() throws Exception {
stream = new ByteArrayOutputStream();

response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(new PrintWriter(stream));
when(response.getWriter()).thenReturn(new AlwaysFlushWriter(stream));
extractor = new DefaultTypeNameExtractor();
environment = mock(Environment.class);

Expand All @@ -93,6 +94,20 @@ public void setup() throws Exception {
serialization = new GsonJSONSerialization(response, extractor, builder, environment, new DefaultReflectionProvider());
}

private static class AlwaysFlushWriter extends PrintWriter {

public AlwaysFlushWriter(OutputStream out) {
super(out);
}

@Override
public void write(String s) {
super.write(s);
super.flush();
}

}

@SkipSerialization
public static class UserPrivateInfo {
String cpf;
Expand Down Expand Up @@ -237,7 +252,6 @@ public void shouldSerializeGenericClass() {

GenericWrapper<Client> wrapper = new GenericWrapper<>(entityList, entityList.size());

// serialization.from(wrapper).include("entityList").include("entityList.name").serialize();
serialization.from(wrapper).include("entityList").serialize();

assertThat(result(), is(equalTo(expectedResult)));
Expand Down Expand Up @@ -491,13 +505,17 @@ public void shouldOptionallyRemoveRootIndented() {
String expected = "{\n \"price\": 15.0,\n \"comments\": \"pack it nicely, please\",\n \"items\": [\n {\n \"name\": \"any item\"\n }\n ]\n}";
Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item(
"any item", 12.99));
serialization.indented().withoutRoot().from(order).include("items").exclude("items.price")
.serialize();
serialization.indented().withoutRoot().from(order).include("items").exclude("items.price").serialize();
assertThat(result(), equalTo(expected));
}

private String result() {
return new String(stream.toByteArray());
try {
stream.flush();
return new String(stream.toByteArray());
} catch (IOException e) {
throw new RuntimeException("Error flushing stream", e);
}
}

static class MyCollection extends ForwardingCollection<Order> {
Expand Down Expand Up @@ -628,4 +646,5 @@ public void shouldNotSerializeNullFieldsByDefault(){
String expectedResult = "{\"address\":{\"street\":\"Alameda street\"}}";
assertThat(result(), is(equalTo(expectedResult)));
}

}

0 comments on commit fb6646a

Please sign in to comment.