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

Changeable invokers for Proxy client #3544

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.glassfish.jersey.client.proxy;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.GenericType;

/**
* Default method invoker for {@link WebResourceFactory}
*/
public class JerseyResourceMethodInvoker implements ResourceMethodInvoker<Invocation.Builder> {

@Override
public <T> T method(Invocation.Builder builder, String name, GenericType<T> responseType) {
return builder.method(name, responseType);
}

@Override
public <T> T method(Invocation.Builder builder, String name, Entity<?> entity, GenericType<T> responseType) {
return builder.method(name, entity, responseType);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.glassfish.jersey.client.proxy;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.GenericType;

public interface ResourceMethodInvoker<B extends Invocation.Builder> {

/**
* Call corresponding method in specified {@link Invocation.Builder}
*
* @param <T> generic response entity type.
* @param builder invocation builder to trigger
* @param name method name
* @param responseType representation of a generic Java type the response entity will be converted to.
* @return invocation response.
*
* @see javax.ws.rs.client.SyncInvoker#method(String, GenericType)
*/
<T> T method(B builder, String name, GenericType<T> responseType);

/**
* Call corresponding method in specified {@link Invocation.Builder}
*
* @param <T> generic response entity type.
* @param builder invocation builder to trigger
* @param name method name
* @param entity request entity.
* @param responseType representation of a generic Java type the response entity will be converted to.
* @return invocation response.
*
* @see javax.ws.rs.client.SyncInvoker#method(String, Entity, GenericType)
*/
<T> T method(B builder, String name, Entity<?> entity, GenericType<T> responseType);
}


Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ public final class WebResourceFactory implements InvocationHandler {
private final MultivaluedMap<String, Object> headers;
private final List<Cookie> cookies;
private final Form form;
private final ResourceMethodInvoker methodInvoker;

private static final MultivaluedMap<String, Object> EMPTY_HEADERS = new MultivaluedHashMap<>();
private static final Form EMPTY_FORM = new Form();
private static final List<Class> PARAM_ANNOTATION_CLASSES = Arrays.<Class>asList(PathParam.class, QueryParam.class,
HeaderParam.class, CookieParam.class, MatrixParam.class, FormParam.class);
private static final ResourceMethodInvoker METHOD_INVOKER = new JerseyResourceMethodInvoker();

/**
* Creates a new client-side representation of a resource described by
Expand All @@ -116,7 +118,8 @@ public final class WebResourceFactory implements InvocationHandler {
* be used for making requests to the server.
*/
public static <C> C newResource(final Class<C> resourceInterface, final WebTarget target) {
return newResource(resourceInterface, target, false, EMPTY_HEADERS, Collections.<Cookie>emptyList(), EMPTY_FORM);
return newResource(resourceInterface, target, false, EMPTY_HEADERS, Collections.<Cookie>emptyList(), EMPTY_FORM,
METHOD_INVOKER);
}

/**
Expand All @@ -140,20 +143,23 @@ public static <C> C newResource(final Class<C> resourceInterface,
final boolean ignoreResourcePath,
final MultivaluedMap<String, Object> headers,
final List<Cookie> cookies,
final Form form) {
final Form form,
final ResourceMethodInvoker methodInvoker) {

return (C) Proxy.newProxyInstance(AccessController.doPrivileged(ReflectionHelper.getClassLoaderPA(resourceInterface)),
new Class[] {resourceInterface},
new WebResourceFactory(ignoreResourcePath ? target : addPathFromAnnotation(resourceInterface, target),
headers, cookies, form));
headers, cookies, form, methodInvoker));
}

private WebResourceFactory(final WebTarget target, final MultivaluedMap<String, Object> headers,
final List<Cookie> cookies, final Form form) {
final List<Cookie> cookies, final Form form,
final ResourceMethodInvoker methodInvoker) {
this.target = target;
this.headers = headers;
this.cookies = cookies;
this.form = form;
this.methodInvoker = methodInvoker;
}

@Override
Expand Down Expand Up @@ -292,7 +298,7 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg

if (httpMethod == null) {
// the method is a subresource locator
return WebResourceFactory.newResource(responseType, newTarget, true, headers, cookies, form);
return WebResourceFactory.newResource(responseType, newTarget, true, headers, cookies, form, methodInvoker);
}

// accepted media types
Expand Down Expand Up @@ -350,9 +356,9 @@ public Object invoke(final Object proxy, final Method method, final Object[] arg
if (entityType instanceof ParameterizedType) {
entity = new GenericEntity(entity, entityType);
}
result = builder.method(httpMethod, Entity.entity(entity, contentType), responseGenericType);
result = methodInvoker.method(builder, httpMethod, Entity.entity(entity, contentType), responseGenericType);
} else {
result = builder.method(httpMethod, responseGenericType);
result = methodInvoker.method(builder, httpMethod, responseGenericType);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void setUp() throws Exception {
final MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(1);
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
resourceWithXML = WebResourceFactory
.newResource(MyResourceIfc.class, target(), false, headers, Collections.<Cookie>emptyList(), new Form());
.newResource(MyResourceIfc.class, target(), false, headers, Collections.<Cookie>emptyList(), new Form(),
new JerseyResourceMethodInvoker());
}

@Test
Expand Down