Search in sources :

Example 11 with InvocationContext

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

the class CodeFirstSpringmvcBase method cseResponse.

public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) User(org.apache.servicecomb.demo.server.User) HttpHeaders(org.springframework.http.HttpHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 12 with InvocationContext

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

the class CodeFirstJaxrs method cseResponse.

// public Response getUserResponse() {
// 
// }
@ApiResponse(code = 200, response = User.class, message = "")
@ResponseHeaders({ @ResponseHeader(name = "h1", response = String.class), @ResponseHeader(name = "h2", response = String.class) })
@Path("/cseResponse")
@GET
public Response cseResponse(InvocationContext c1) {
    Response response = Response.createSuccess(Status.ACCEPTED, new User());
    Headers headers = response.getHeaders();
    headers.addHeader("h1", "h1v " + c1.getContext().get(Const.SRC_MICROSERVICE).toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.addHeader("h2", "h2v " + c2.getContext().get(Const.SRC_MICROSERVICE).toString());
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ApiResponse(io.swagger.annotations.ApiResponse) User(org.apache.servicecomb.demo.server.User) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ResponseHeaders(org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders) ApiResponse(io.swagger.annotations.ApiResponse)

Example 13 with InvocationContext

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

the class CodeFirstPojoClient method testCodeFirstCompletableFuture.

private void testCodeFirstCompletableFuture(CodeFirstPojoIntf codeFirst) {
    if (!CodeFirstPojoClientIntf.class.isInstance(codeFirst)) {
        return;
    }
    Vertx vertx = VertxUtils.getOrCreateVertxByName("transport", null);
    CountDownLatch latch = new CountDownLatch(1);
    // vertx.runOnContext in normal thread is not a good practice
    // here just a test, not care for this.
    vertx.runOnContext(V -> {
        InvocationContext context = new InvocationContext();
        context.addContext("k", "v");
        ContextUtils.setInvocationContext(context);
        CompletableFuture<String> future = ((CodeFirstPojoClientIntf) codeFirst).sayHiAsync("someone");
        future.thenCompose(result -> {
            TestMgr.check("someone sayhi, context k: v", result);
            TestMgr.check(true, context == ContextUtils.getInvocationContext());
            return ((CodeFirstPojoClientIntf) codeFirst).sayHiAsync("someone 1");
        }).whenComplete((r, e) -> {
            TestMgr.check("someone 1 sayhi, context k: v", r);
            latch.countDown();
        });
        ContextUtils.removeInvocationContext();
    });
    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) CodeFirstPojoIntf(org.apache.servicecomb.demo.CodeFirstPojoIntf) Vertx(io.vertx.core.Vertx) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DemoConst(org.apache.servicecomb.demo.DemoConst) ContextUtils(org.apache.servicecomb.swagger.invocation.context.ContextUtils) RpcReference(org.apache.servicecomb.provider.pojo.RpcReference) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Inject(javax.inject.Inject) CountDownLatch(java.util.concurrent.CountDownLatch) Person(org.apache.servicecomb.demo.compute.Person) List(java.util.List) User(org.apache.servicecomb.demo.server.User) VertxUtils(org.apache.servicecomb.foundation.vertx.VertxUtils) Map(java.util.Map) CseContext(org.apache.servicecomb.core.CseContext) TestMgr(org.apache.servicecomb.demo.TestMgr) Vertx(io.vertx.core.Vertx) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 14 with InvocationContext

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

the class CodeFirstRestTemplate method testTraceIdOnContextContainsTraceId.

protected void testTraceIdOnContextContainsTraceId(RestTemplate template, String cseUrlPrefix) {
    String traceIdUrl = cseUrlPrefix + "traceId";
    InvocationContext invocationContext = new InvocationContext();
    invocationContext.addContext(Const.TRACE_ID_NAME, String.valueOf(Long.MIN_VALUE));
    ContextUtils.setInvocationContext(invocationContext);
    String result = template.getForObject(traceIdUrl, String.class);
    TestMgr.check(String.valueOf(Long.MIN_VALUE), result);
    ContextUtils.removeInvocationContext();
}
Also used : InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 15 with InvocationContext

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

the class TestPojoConsumerEqualSwagger method testContext.

@Test
public void testContext() throws Exception {
    InvocationContext threadContext = new InvocationContext();
    threadContext.addContext("ta", "tvalue");
    ContextUtils.setInvocationContext(threadContext);
    InvocationContext context = new InvocationContext();
    context.addContext("a", "value");
    String result = proxy.testContext(context, "name");
    Assert.assertEquals(3, invoker.getInvocation().getContext().size());
    Assert.assertEquals("tvalue", invoker.getContext("ta"));
    Assert.assertEquals("value", invoker.getContext("a"));
    Assert.assertEquals("name", invoker.getContext("name"));
    Assert.assertEquals("name", (String) invoker.getSwaggerArgument(0));
    Assert.assertEquals("name sayhi", result);
}
Also used : InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Test(org.junit.Test)

Aggregations

InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)15 User (org.apache.servicecomb.demo.server.User)5 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)5 HttpHeaders (org.springframework.http.HttpHeaders)5 Response (org.apache.servicecomb.swagger.invocation.Response)4 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)4 Test (org.junit.Test)4 ApiResponse (io.swagger.annotations.ApiResponse)3 ResponseEntity (org.springframework.http.ResponseEntity)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 HashMap (java.util.HashMap)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Vertx (io.vertx.core.Vertx)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1