Skip to content

Commit

Permalink
[RESTEASY-1499] @gzip annotation on client proxy doesn't set content-…
Browse files Browse the repository at this point in the history
…type gzip to the request header (#933)

Issue: https://issues.jboss.org/browse/RESTEASY-1499
  • Loading branch information
iweiss authored and asoldano committed Sep 21, 2016
1 parent 5cf87d1 commit c7222ec
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
@@ -1,9 +1,9 @@
package org.jboss.resteasy.client.jaxrs.internal;

import org.jboss.resteasy.annotations.ContentEncoding;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext;
import org.jboss.resteasy.core.interception.ClientWriterInterceptorContext;
import org.jboss.resteasy.spi.ResteasyConfiguration;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.util.DelegatingOutputStream;
import org.jboss.resteasy.util.Types;
Expand Down Expand Up @@ -32,7 +32,6 @@
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Providers;
import javax.ws.rs.ext.WriterInterceptor;
import java.io.IOException;
Expand All @@ -44,10 +43,7 @@
import java.net.URI;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
Expand Down Expand Up @@ -327,7 +323,17 @@ public void setEntity(Entity entity)
Variant v = entity.getVariant();
headers.setMediaType(v.getMediaType());
headers.setLanguage(v.getLanguage());
headers.header("Content-Encoding", v.getEncoding());

headers.header("Content-Encoding", null);

if(v.getEncoding() == null) {
for(Annotation annotation : this.entityAnnotations) {
ContentEncoding contentEncoding = annotation.annotationType().getAnnotation(ContentEncoding.class);
if(contentEncoding != null) {
headers.header("Content-Encoding", contentEncoding.value());
}
}
}
}

}
Expand Down
Expand Up @@ -5,31 +5,30 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jboss.resteasy.annotations.ContentEncoding;
import org.jboss.resteasy.annotations.GZIP;
import org.jboss.resteasy.client.jaxrs.ProxyBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget;
import org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor;
import org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor;
import org.jboss.resteasy.test.BaseResourceTest;
import org.jboss.resteasy.test.TestPortProvider;
import org.jboss.resteasy.test.nextgen.interceptors.resource.GzipProxy;
import org.jboss.resteasy.test.nextgen.interceptors.resource.Pair;
import org.jboss.resteasy.util.HttpResponseCodes;
import org.jboss.resteasy.util.ReadFromStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -141,6 +140,17 @@ public void putText(String text) throws Exception
{
Assert.assertEquals("hello world", text);
}

@POST
@Path("/gzippost")
@Consumes(MediaType.TEXT_PLAIN)
public Response postGZipped(@GZIP String value, @Context HttpHeaders headers) {
MultivaluedMap<String, String> requestHeaders = headers.getRequestHeaders();
Assert.assertEquals(true, requestHeaders.containsKey("accept-encoding"));
Assert.assertEquals(true, requestHeaders.get("accept-encoding").toString().contains("gzip"));

return Response.ok().type(MediaType.TEXT_PLAIN).encoding("gzip").build();
}
}

@Before
Expand Down Expand Up @@ -342,5 +352,21 @@ public void testWithoutAcceptEncoding() throws Exception

}

/**
* Send POST request with gzip encoded data using @GZIP annotation and client proxy framework
*/
@Test
public void testGzipPost() {
Client client = new ResteasyClientBuilder().build();
WebTarget target = client.target(TestPortProvider.generateURL(""));
GzipProxy gzipProxy = ProxyBuilder.builder(GzipProxy.class, target).build();
Pair data = new Pair();
data.setP1("first");
data.setP2("second");

Response response = gzipProxy.post(data);
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("gzip", response.getHeaderString("Content-Encoding"));
}

}
@@ -0,0 +1,18 @@
package org.jboss.resteasy.test.nextgen.interceptors.resource;

import org.jboss.resteasy.annotations.GZIP;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/gzippost")
public interface GzipProxy {

@Consumes(MediaType.TEXT_PLAIN)
@POST
@GZIP
public Response post(@GZIP Pair pair);
}
@@ -0,0 +1,26 @@
package org.jboss.resteasy.test.nextgen.interceptors.resource;

import java.io.Serializable;

public class Pair implements Serializable {

private static final long serialVersionUID = 1L;
private String P1;
private String P2;

public String getP1() {
return this.P1;
}

public String getP2() {
return this.P2;
}

public void setP1(String p1) {
this.P1 = p1;
}

public void setP2(String p2) {
this.P2 = p2;
}
}

0 comments on commit c7222ec

Please sign in to comment.