Search in sources :

Example 51 with Response

use of org.apache.servicecomb.swagger.invocation.Response 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 52 with Response

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

the class ServerRestArgsFilter method beforeSendResponse.

@Override
public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
    Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
    ProduceProcessor produceProcessor = (ProduceProcessor) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }
    try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
        produceProcessor.encodeResponse(output, body);
        responseEx.setBodyBuffer(output.getBuffer());
    } catch (Throwable e) {
        throw ExceptionFactory.convertProducerException(e);
    }
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) BufferOutputStream(org.apache.servicecomb.foundation.vertx.stream.BufferOutputStream)

Example 53 with Response

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

the class TestInvoker method syncInvoke_normal.

@Test
public void syncInvoke_normal(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Response response = Response.ok("1");
    new MockUp<InvokerUtils>() {

        @Mock
        Response innerSyncInvoke(Invocation invocation) {
            return response;
        }
    };
    new Expectations() {

        {
            consumerOperation.getResponseMapper();
            result = mapper;
            mapper.mapResponse(response);
            result = 1;
        }
    };
    Invoker invoker = new Invoker("test", null, IPerson.class);
    Object result = invoker.syncInvoke(invocation, consumerOperation);
    Assert.assertEquals(1, result);
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 54 with Response

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

the class TestInvoker method syncInvoke_failed.

@Test
public void syncInvoke_failed(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Throwable error = new Error("failed");
    Response response = Response.createConsumerFail(error);
    new MockUp<InvokerUtils>() {

        @Mock
        Response innerSyncInvoke(Invocation invocation) {
            return response;
        }
    };
    expectedException.expect(InvocationException.class);
    expectedException.expectCause(Matchers.sameInstance(error));
    Invoker invoker = new Invoker("test", null, IPerson.class);
    invoker.syncInvoke(invocation, consumerOperation);
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 55 with Response

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

the class TestInvoker method completableFutureInvoke_normal.

@Test
public void completableFutureInvoke_normal(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Response response = Response.ok("1");
    new MockUp<InvokerUtils>() {

        @Mock
        void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
            asyncResp.handle(response);
            ;
        }
    };
    new Expectations() {

        {
            consumerOperation.getResponseMapper();
            result = mapper;
            mapper.mapResponse(response);
            result = 1;
        }
    };
    Invoker invoker = new Invoker("test", null, IPerson.class);
    CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
    future.whenComplete((result, ex) -> {
        Assert.assertEquals(1, result);
        Assert.assertEquals(null, ex);
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Aggregations

Response (org.apache.servicecomb.swagger.invocation.Response)65 Test (org.junit.Test)44 Invocation (org.apache.servicecomb.core.Invocation)17 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)14 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)11 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)8 Expectations (mockit.Expectations)7 ResponseEntity (org.springframework.http.ResponseEntity)7 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)6 MockUp (mockit.MockUp)6 Method (java.lang.reflect.Method)5 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)5 Holder (javax.xml.ws.Holder)4 User (org.apache.servicecomb.demo.server.User)4 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)4 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ApiResponse (io.swagger.annotations.ApiResponse)3 List (java.util.List)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)3