Search in sources :

Example 21 with InvocationContext

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

the class CodeFirstSpringmvcBase method responseEntity.

public ResponseEntity<Date> responseEntity(InvocationContext c1, Date date) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("h1", "h1v " + c1.getContext().toString());
    InvocationContext c2 = ContextUtils.getInvocationContext();
    headers.add("h2", "h2v " + c2.getContext().toString());
    return new ResponseEntity<Date>(date, headers, HttpStatus.ACCEPTED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext)

Example 22 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 23 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 24 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 25 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)

Aggregations

InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)35 User (org.apache.servicecomb.demo.server.User)11 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)10 Response (org.apache.servicecomb.swagger.invocation.Response)9 Test (org.junit.Test)8 ApiResponse (io.swagger.annotations.ApiResponse)7 HttpHeaders (org.springframework.http.HttpHeaders)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 HashMap (java.util.HashMap)5 ResponseEntity (org.springframework.http.ResponseEntity)5 Date (java.util.Date)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)4 SwaggerInvocation (org.apache.servicecomb.swagger.invocation.SwaggerInvocation)3 Vertx (io.vertx.core.Vertx)2 LocalDate (java.time.LocalDate)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2