Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit fcf841b

Browse files
committed
JERSEY-2696 fix: client proxy incorrectly resets "Accept" header defined by @produces annotation
1 parent fdc3d39 commit fcf841b

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

ext/proxy-client/src/main/java/org/glassfish/jersey/client/proxy/WebResourceFactory.java

100644100755
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
*/
8888
public final class WebResourceFactory implements InvocationHandler {
8989

90+
static private final String[] EMPTY = {};
91+
9092
private final WebTarget target;
9193
private final MultivaluedMap<String, Object> headers;
9294
private final List<Cookie> cookies;
@@ -279,7 +281,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
279281
if (produces == null) {
280282
produces = proxyIfc.getAnnotation(Produces.class);
281283
}
282-
final String[] accepts = produces == null ? null : produces.value();
284+
final String[] accepts = (produces == null) ? EMPTY : produces.value();
283285

284286
// determine content type
285287
String contentType = null;
@@ -294,15 +296,9 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
294296
}
295297
}
296298

297-
Invocation.Builder builder;
298-
if (accepts != null) {
299-
builder = newTarget.request(accepts);
300-
} else {
301-
builder = newTarget.request();
302-
}
303-
304-
// apply header params and cookies
305-
builder.headers(headers);
299+
Invocation.Builder builder = newTarget.request()
300+
.headers(headers) // this resets all headers so do this first
301+
.accept(accepts); // if @Produces is defined, propagate values into Accept header; empty array is NO-OP
306302

307303
for (final Cookie c : cookies) {
308304
builder = builder.cookie(c);

ext/proxy-client/src/test/java/org/glassfish/jersey/client/proxy/MyResource.java

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
package org.glassfish.jersey.client.proxy;
4141

42+
import javax.ws.rs.core.HttpHeaders;
43+
import javax.ws.rs.core.MediaType;
4244
import java.util.List;
4345
import java.util.Set;
4446
import java.util.SortedSet;
@@ -164,4 +166,10 @@ public String postByNameFormSortedSet(SortedSet<String> name) {
164166
public MySubResourceIfc getSubResource() {
165167
return new MySubResource();
166168
}
169+
170+
@Override
171+
public boolean isAcceptHeaderValid(HttpHeaders headers) {
172+
List<MediaType> accepts = headers.getAcceptableMediaTypes();
173+
return accepts.contains(MediaType.TEXT_PLAIN_TYPE) && accepts.contains(MediaType.TEXT_XML_TYPE);
174+
}
167175
}

ext/proxy-client/src/test/java/org/glassfish/jersey/client/proxy/MyResourceIfc.java

100644100755
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import javax.ws.rs.PathParam;
5555
import javax.ws.rs.Produces;
5656
import javax.ws.rs.QueryParam;
57+
import javax.ws.rs.core.Context;
58+
import javax.ws.rs.core.HttpHeaders;
5759
import javax.ws.rs.core.MediaType;
5860

5961
@Path("myresource")
@@ -175,4 +177,9 @@ public interface MyResourceIfc {
175177

176178
@Path("subresource")
177179
MySubResourceIfc getSubResource();
180+
181+
@Path("isAcceptHeaderValid")
182+
@GET
183+
@Produces({MediaType.TEXT_PLAIN, MediaType.TEXT_XML})
184+
boolean isAcceptHeaderValid(@Context HttpHeaders headers);
178185
}

0 commit comments

Comments
 (0)