com.tteky.xenonext.jaxrs.client.ProxyHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xenon-ext-jaxrs Show documentation
Show all versions of xenon-ext-jaxrs Show documentation
JAX-RS annotation processing capability for Xenon Stateless services and Service client.
The newest version!
package com.tteky.xenonext.jaxrs.client;
import com.tteky.xenonext.jaxrs.reflect.MethodInfo;
import com.tteky.xenonext.jaxrs.reflect.MethodInfoBuilder;
import com.tteky.xenonext.jaxrs.reflect.ParamMetadata;
import com.vmware.xenon.common.Operation;
import com.vmware.xenon.common.ServiceRequestSender;
import com.vmware.xenon.common.Utils;
import org.apache.commons.lang3.tuple.Pair;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.net.URI;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import static com.vmware.xenon.common.UriUtils.extendUri;
import static com.vmware.xenon.common.UriUtils.extendUriWithQuery;
/**
* Handles the method invocation on contract interfaces.
* Capable of handling only methods annotated with JAX-RS annotations.
* Convert jax-rs method invocation to operation and post back results
*/
public class ProxyHandler implements InvocationHandler {
private Class> resourceInterface;
private String referrer = "/jaxrs/xenon/client";
private List httpMethods = Collections.emptyList();
private Map> typeResolution = Collections.emptyMap();
private Supplier baseUriSupplier;
private ServiceRequestSender client;
private BiFunction opBuilder = this::buildOperation;
private BiConsumer, CompletableFuture> errorHandler = this::defaultErrorHandler;
private BiFunction responseDecoder = this::operationDecoder;
private OperationInterceptor interceptor = new OperationInterceptor() {
};
void init() {
this.httpMethods = MethodInfoBuilder.parseInterfaceForJaxRsInfo(resourceInterface, typeResolution);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.isDefault()) {
Class> declaringClass = method.getDeclaringClass();
Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
field.setAccessible(true);
MethodHandles.Lookup lookup = (MethodHandles.Lookup) field.get(null);
return lookup.unreflectSpecial(method, declaringClass)
.bindTo(proxy)
.invokeWithArguments(args);
}
// get the interface describing the resource
Class> proxyIfc = proxy.getClass().getInterfaces()[0];
if (proxyIfc.equals(resourceInterface)) {
Optional first = this.httpMethods.stream()
.filter(httpMethod -> httpMethod.getMethod().equals(method))
.findFirst();
MethodInfo methodInfo = first.orElseGet(() ->
MethodInfoBuilder.generateMethodInfo(new Method[]{method}, typeResolution).get(0)
);
Operation op = opBuilder.apply(methodInfo, args);
op = interceptor.interceptBeforeComplete(op);
CompletableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy