Search in sources :

Example 1 with RECORDER_KEY

use of org.webpieces.microsvc.impl.TestCaseRecorder.RECORDER_KEY in project webpieces by deanhiller.

the class HttpsJsonClientInvokeHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    TestCaseRecorder recorder = (TestCaseRecorder) Context.get(RECORDER_KEY);
    EndpointInfo recordingInfo = null;
    if (recorder != null) {
        Map<String, Object> ctxSnapshot = Context.copyContext();
        recordingInfo = new EndpointInfo(method, args, ctxSnapshot);
        recorder.addEndpointInfo(recordingInfo);
    }
    clientAssertions.throwIfCannotGoRemote();
    Path annotation = method.getAnnotation(Path.class);
    if (annotation == null) {
        throw new IllegalArgumentException("The @Path annotation is missing from method=" + method + " clazz=" + method.getDeclaringClass());
    }
    String path = getPath(method);
    HttpMethod httpMethod = getHttpMethod(method);
    Class<?> clazz = method.getReturnType();
    if (!(CompletableFuture.class.isAssignableFrom(clazz))) {
        throw new IllegalStateException("All api methods must return a XFuture");
    }
    ParameterizedType t = (ParameterizedType) method.getGenericReturnType();
    Class retType = (Class) t.getActualTypeArguments()[0];
    Map<String, Object> context = Context.getContext();
    if (context == null) {
        throw new IllegalStateException("Please call Current.setContext with the request context or create your own context");
    }
    Object header = context.get(Context.HEADERS);
    if (header == null) {
        throw new IllegalStateException("Context.HEADERS is missing from the Context.  please set that up first");
    } else if (!(header instanceof Map)) {
        throw new IllegalStateException("Context.HEADERS is not a Map<String, String> and is setup incorrectly");
    }
    log.info("Sending http request to: " + addr.getHostName() + ":" + addr.getPort() + path);
    Object body = args[0];
    if (hasUrlParams) {
        path = transformPath(path, method, args);
        body = findBody(method, args);
    }
    Endpoint endpoint = new Endpoint(addr, httpMethod.getCode(), path);
    XFuture<Object> xFuture = clientHelper.sendHttpRequest(method, body, endpoint, retType).thenApply(retVal -> {
        // Only needed by APIs/methods that return CompletableFuture :( not XFuture
        Context.restoreContext(context);
        return retVal;
    });
    if (recorder == null) {
        return xFuture;
    }
    EndpointInfo finalInfo = recordingInfo;
    return xFuture.handle((resp, exc1) -> addTestRecordingInfo(finalInfo, resp, exc1)).thenCompose(Function.identity());
}
Also used : Logger(org.slf4j.Logger) Context(org.webpieces.util.context.Context) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) Function(java.util.function.Function) TestCaseRecorder(org.webpieces.microsvc.impl.TestCaseRecorder) Inject(javax.inject.Inject) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) javax.ws.rs(javax.ws.rs) ParameterizedType(java.lang.reflect.ParameterizedType) Current(org.checkerframework.checker.units.qual.Current) ClientAssertions(org.webpieces.util.context.ClientAssertions) Parameter(java.lang.reflect.Parameter) RECORDER_KEY(org.webpieces.microsvc.impl.TestCaseRecorder.RECORDER_KEY) Map(java.util.Map) EndpointInfo(org.webpieces.microsvc.impl.EndpointInfo) InvocationHandler(java.lang.reflect.InvocationHandler) Pattern(java.util.regex.Pattern) Method(java.lang.reflect.Method) TestCaseRecorder(org.webpieces.microsvc.impl.TestCaseRecorder) ParameterizedType(java.lang.reflect.ParameterizedType) EndpointInfo(org.webpieces.microsvc.impl.EndpointInfo) Map(java.util.Map) HttpMethod(org.webpieces.ctx.api.HttpMethod)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1 Inject (javax.inject.Inject)1 javax.ws.rs (javax.ws.rs)1 Current (org.checkerframework.checker.units.qual.Current)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 EndpointInfo (org.webpieces.microsvc.impl.EndpointInfo)1 TestCaseRecorder (org.webpieces.microsvc.impl.TestCaseRecorder)1 RECORDER_KEY (org.webpieces.microsvc.impl.TestCaseRecorder.RECORDER_KEY)1 ClientAssertions (org.webpieces.util.context.ClientAssertions)1