Skip to content

Commit

Permalink
Upgrade of MP Rest client to 1.3.3.
Browse files Browse the repository at this point in the history
First refactoring steps to fix generics declarations.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer committed Aug 15, 2019
1 parent 14e8f18 commit d3b2d63
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 91 deletions.
2 changes: 1 addition & 1 deletion ext/microprofile/mp-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<version>1.2.1</version>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Contains information about method parameter or class field which is annotated by {@link BeanParam}.
*
* @author David Kral
* @author Tomas Langer
*/
class BeanParamModel extends ParamModel<Object> {

Expand Down Expand Up @@ -68,7 +69,7 @@ class BeanParamModel extends ParamModel<Object> {
}

@Override
public Object handleParameter(Object requestPart, Class<?> annotationClass, Object instance) {
public Object handleParameter(Object requestPart, Class<? extends Annotation> annotationClass, Object instance) {
ParamHandler handler = PARAM_HANDLERS.get(annotationClass);

if (null == handler) {
Expand All @@ -79,7 +80,7 @@ public Object handleParameter(Object requestPart, Class<?> annotationClass, Obje
}

@Override
public boolean handles(Class<Annotation> annotation) {
public boolean handles(Class<? extends Annotation> annotation) {
return PARAM_HANDLERS.containsKey(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Contains information about method parameter or class field which is annotated by {@link CookieParam}.
*
* @author David Kral
* @author Tomas Langer
*/
class CookieParamModel extends ParamModel<Map<String, String>> {

Expand All @@ -36,14 +37,16 @@ class CookieParamModel extends ParamModel<Map<String, String>> {
}

@Override
Map<String, String> handleParameter(Map<String, String> requestPart, Class<?> annotationClass, Object instance) {
Map<String, String> handleParameter(Map<String, String> requestPart,
Class<? extends Annotation> annotationClass,
Object instance) {
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
requestPart.put(cookieParamName, (String) resolvedValue);
return requestPart;
}

@Override
boolean handles(Class<Annotation> annotation) {
boolean handles(Class<? extends Annotation> annotation) {
return CookieParam.class.equals(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Contains information about method parameter or class field which is annotated by {@link FormParam}.
*
* @author David Kral
* @author Tomas Langer
*/
class FormParamModel extends ParamModel<Form> {

Expand All @@ -37,7 +38,7 @@ class FormParamModel extends ParamModel<Form> {
}

@Override
Form handleParameter(Form form, Class<?> annotationClass, Object instance) {
Form handleParameter(Form form, Class<? extends Annotation> annotationClass, Object instance) {
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
if (resolvedValue instanceof Collection) {
for (final Object v : ((Collection) resolvedValue)) {
Expand All @@ -50,7 +51,7 @@ Form handleParameter(Form form, Class<?> annotationClass, Object instance) {
}

@Override
boolean handles(Class<Annotation> annotation) {
boolean handles(Class<? extends Annotation> annotation) {
return FormParam.class.equals(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Contains information about method parameter or class field which is annotated by {@link HeaderParam}.
*
* @author David Kral
* @author Tomas Langer
*/
class HeaderParamModel extends ParamModel<MultivaluedMap<String, Object>> {

Expand All @@ -38,14 +39,14 @@ class HeaderParamModel extends ParamModel<MultivaluedMap<String, Object>> {

@Override
MultivaluedMap<String, Object> handleParameter(MultivaluedMap<String, Object> requestPart,
Class<?> annotationClass, Object instance) {
Class<? extends Annotation> annotationClass, Object instance) {
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
requestPart.put(headerParamName, Collections.singletonList(resolvedValue));
return requestPart;
}

@Override
boolean handles(Class<Annotation> annotation) {
boolean handles(Class<? extends Annotation> annotation) {
return HeaderParam.class.equals(annotation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
*
* @author David Kral
* @author Patrik Dudits
* @author Tomas Langer
*/
class InterfaceModel {

Expand Down Expand Up @@ -298,25 +299,25 @@ Builder pathValue(Path path) {

/**
* Extracts MediaTypes from {@link Produces} annotation.
* If annotation is null, new String array with {@link MediaType#WILDCARD} is set.
* If annotation is null, new String array with {@link MediaType#APPLICATION_JSON} is set.
*
* @param produces {@link Produces} annotation
* @return updated Builder instance
*/
Builder produces(Produces produces) {
this.produces = produces != null ? produces.value() : new String[] {MediaType.WILDCARD};
this.produces = produces != null ? produces.value() : new String[] {MediaType.APPLICATION_JSON};
return this;
}

/**
* Extracts MediaTypes from {@link Consumes} annotation.
* If annotation is null, new String array with {@link MediaType#WILDCARD} is set.
* If annotation is null, new String array with {@link MediaType#APPLICATION_JSON} is set.
*
* @param consumes {@link Consumes} annotation
* @return updated Builder instance
*/
Builder consumes(Consumes consumes) {
this.consumes = consumes != null ? consumes.value() : new String[] {MediaType.WILDCARD};
this.consumes = consumes != null ? consumes.value() : new String[] {MediaType.APPLICATION_JSON};
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Contains information to method parameter which is annotated by {@link MatrixParam}.
*
* @author David Kral
* @author Tomas Langer
*/
class MatrixParamModel extends ParamModel<WebTarget> {

Expand All @@ -43,7 +44,7 @@ class MatrixParamModel extends ParamModel<WebTarget> {
}

@Override
public WebTarget handleParameter(WebTarget requestPart, Class<?> annotationClass, Object instance) {
public WebTarget handleParameter(WebTarget requestPart, Class<? extends Annotation> annotationClass, Object instance) {
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
if (resolvedValue instanceof Collection) {
return requestPart.matrixParam(matrixParamName, ((Collection) resolvedValue).toArray());
Expand All @@ -53,7 +54,7 @@ public WebTarget handleParameter(WebTarget requestPart, Class<?> annotationClass
}

@Override
public boolean handles(Class<Annotation> annotation) {
public boolean handles(Class<? extends Annotation> annotation) {
return MatrixParam.class.equals(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
*
* @author David Kral
* @author Patrik Dudits
* @author Tomas Langer
*/
class MethodModel {

Expand Down Expand Up @@ -445,15 +446,16 @@ private static String parseHttpMethod(InterfaceModel classModel, Method method)
throw new RestClientDefinitionException("Method can't have more then one annotation of @HttpMethod type. "
+ "See " + classModel.getRestClientClass().getName()
+ "::" + method.getName());
} else if (httpAnnotations.isEmpty()) {
}
if (httpAnnotations.isEmpty()) {
//Sub resource method
return "";
}
return httpAnnotations.get(0).getSimpleName();
}

private static List<ParamModel> parameterModels(InterfaceModel classModel, Method method) {
ArrayList<ParamModel> parameterModels = new ArrayList<>();
List<ParamModel> parameterModels = new ArrayList<>();
final List<org.glassfish.jersey.model.Parameter> jerseyParameters = org.glassfish.jersey.model.Parameter
.create(classModel.getRestClientClass(), classModel.getRestClientClass(),
method, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Abstract model for all elements with parameter annotation.
*
* @author David Kral
* @author Tomas Langer
*/
abstract class ParamModel<T> {

Expand Down Expand Up @@ -129,15 +130,15 @@ boolean isEntity() {
* @param instance actual method parameter value
* @return updated request part
*/
abstract T handleParameter(T requestPart, Class<?> annotationClass, Object instance);
abstract T handleParameter(T requestPart, Class<? extends Annotation> annotationClass, Object instance);

/**
* Evaluates if the annotation passed in parameter is supported by this parameter.
*
* @param annotation checked annotation
* @return if annotation is supported
*/
abstract boolean handles(Class<Annotation> annotation);
abstract boolean handles(Class<? extends Annotation> annotation);

protected static class Builder {

Expand Down Expand Up @@ -174,14 +175,14 @@ ParamModel build() {
}

entity = true;
return new ParamModel(this) {
return new ParamModel<Object>(this) {
@Override
public Object handleParameter(Object requestPart, Class annotationClass, Object instance) {
public Object handleParameter(Object requestPart, Class<? extends Annotation> annotationClass, Object instance) {
return requestPart;
}

@Override
public boolean handles(Class annotation) {
public boolean handles(Class<? extends Annotation> annotation) {
return false;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Contains information about method parameter or class field which is annotated by {@link PathParam}.
*
* @author David Kral
* @author Tomas Langer
*/
class PathParamModel extends ParamModel<WebTarget> {

Expand All @@ -40,13 +41,13 @@ public String getPathParamName() {
}

@Override
public WebTarget handleParameter(WebTarget requestPart, Class<?> annotationClass, Object instance) {
public WebTarget handleParameter(WebTarget requestPart, Class<? extends Annotation> annotationClass, Object instance) {
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
return requestPart.resolveTemplate(pathParamName, resolvedValue);
}

@Override
public boolean handles(Class<Annotation> annotation) {
public boolean handles(Class<? extends Annotation> annotation) {
return PathParam.class.equals(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,55 @@

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;

/**
* Invocation handler for interface proxy.
*
* @author David Kral
* @author Tomas Langer
*/
class ProxyInvocationHandler implements InvocationHandler {

private final Client client;
private final WebTarget target;
private final RestClientModel restClientModel;
private final AtomicBoolean closed = new AtomicBoolean(false);

ProxyInvocationHandler(WebTarget target,
// top level
ProxyInvocationHandler(Client client,
WebTarget target,
RestClientModel restClientModel) {

this.client = client;
this.target = target;
this.restClientModel = restClientModel;
}

// used for sub-resources
ProxyInvocationHandler(WebTarget target,
RestClientModel restClientModel) {
this(null, target, restClientModel);
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) {
if (method.getName().contains("toString") && (args == null || args.length == 0)) {
if (method.getName().equals("toString") && (args == null || args.length == 0)) {
return restClientModel.toString();
}
if (method.getName().equals("close") && (args == null || args.length == 0)) {
closed.set(true);
if (null != client) {
client.close();
}
return null;
}

if (closed.get()) {
throw new IllegalStateException("Attempting to invoke a method on a closed client.");
}
return restClientModel.invokeMethod(target, method, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Model which contains information about query parameter
*
* @author David Kral
* @author Tomas Langer
*/
class QueryParamModel extends ParamModel<Map<String, Object[]>> {

Expand All @@ -37,7 +38,7 @@ class QueryParamModel extends ParamModel<Map<String, Object[]>> {

@Override
public Map<String, Object[]> handleParameter(Map<String, Object[]> requestPart,
Class<?> annotationClass,
Class<? extends Annotation> annotationClass,
Object instance) {
Object resolvedValue = interfaceModel.resolveParamValue(instance, parameter);
if (resolvedValue instanceof Object[]) {
Expand All @@ -49,7 +50,7 @@ public Map<String, Object[]> handleParameter(Map<String, Object[]> requestPart,
}

@Override
public boolean handles(Class<Annotation> annotation) {
public boolean handles(Class<? extends Annotation> annotation) {
return QueryParam.class.equals(annotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.glassfish.jersey.internal.util.ReflectionHelper;

/**
* Created by David Kral.
* @author David Kral
* @author Tomas Langer
*/
class ReflectionUtil {
final class ReflectionUtil {

private ReflectionUtil() {
}

static <T> T createInstance(Class<T> tClass) {
return AccessController.doPrivileged((PrivilegedAction<T>) () -> {
try {
return tClass.newInstance();
return tClass.getConstructor().newInstance();
} catch (Throwable t) {
throw new RuntimeException("No default constructor in class " + tClass + " present. Class cannot be created!", t);
}
Expand Down
Loading

0 comments on commit d3b2d63

Please sign in to comment.