Search in sources :

Example 1 with SwaggerInvocation

use of org.apache.servicecomb.swagger.invocation.SwaggerInvocation in project incubator-servicecomb-java-chassis by apache.

the class TestConfig method testContextUtils.

@Test
public void testContextUtils() {
    ThreadLocal<InvocationContext> contextMgr = new ThreadLocal<>();
    Assert.assertEquals(contextMgr.get(), ContextUtils.getInvocationContext());
    SwaggerInvocation invocation = new SwaggerInvocation();
    invocation.addContext("test1", "testObject");
    Assert.assertEquals("testObject", invocation.getContext("test1"));
    Map<String, String> context = new HashMap<>();
    context.put("test2", new String("testObject"));
    invocation.setContext(context);
    Assert.assertEquals(context, invocation.getContext());
    invocation.setStatus(Status.OK);
    Assert.assertEquals(200, invocation.getStatus().getStatusCode());
    invocation.setStatus(204);
    Assert.assertEquals(204, invocation.getStatus().getStatusCode());
    invocation.setStatus(Status.OK);
    Assert.assertEquals((Status.OK).getStatusCode(), invocation.getStatus().getStatusCode());
    invocation.setStatus(203, "Done");
    Assert.assertEquals(203, invocation.getStatus().getStatusCode());
    ContextUtils.setInvocationContext(invocation);
    Assert.assertEquals(invocation, ContextUtils.getInvocationContext());
    ContextUtils.removeInvocationContext();
    Assert.assertEquals(null, ContextUtils.getInvocationContext());
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) HashMap(java.util.HashMap) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Test(org.junit.Test)

Example 2 with SwaggerInvocation

use of org.apache.servicecomb.swagger.invocation.SwaggerInvocation in project incubator-servicecomb-java-chassis by apache.

the class LocalProducerInvoker method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    invocation = new SwaggerInvocation();
    SwaggerConsumerOperation consumerOp = consumer.findOperation(method.getName());
    SwaggerProducerOperation producerOp = producer.findOperation(consumerOp.getSwaggerMethod().getName());
    consumerOp.getArgumentsMapper().toInvocation(args, invocation);
    CompletableFuture<Object> future = new CompletableFuture<>();
    producerOp.invoke(invocation, ar -> {
        producerResponse = ar;
        Object realResult = consumerOp.getResponseMapper().mapResponse(producerResponse);
        future.complete(realResult);
    });
    if (CompletableFuture.class.equals(method.getReturnType())) {
        return future;
    }
    return future.get();
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) CompletableFuture(java.util.concurrent.CompletableFuture) SwaggerConsumerOperation(org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation)

Example 3 with SwaggerInvocation

use of org.apache.servicecomb.swagger.invocation.SwaggerInvocation in project incubator-servicecomb-java-chassis by apache.

the class ProducerHttpRequestArgMapper method createContextArg.

@Override
public Object createContextArg(SwaggerInvocation swaggerInvocation) {
    Invocation invocation = (Invocation) swaggerInvocation;
    // 从rest transport来
    HttpServletRequest request = (HttpServletRequest) invocation.getHandlerContext().get(RestConst.REST_REQUEST);
    if (request != null) {
        return request;
    }
    // 通过args模拟request
    return new InvocationToHttpServletRequest(invocation);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation)

Aggregations

SwaggerInvocation (org.apache.servicecomb.swagger.invocation.SwaggerInvocation)3 HashMap (java.util.HashMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Invocation (org.apache.servicecomb.core.Invocation)1 SwaggerConsumerOperation (org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation)1 SwaggerProducerOperation (org.apache.servicecomb.swagger.engine.SwaggerProducerOperation)1 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)1 Test (org.junit.Test)1