Search in sources :

Example 21 with Response

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

the class TestDefaultHttpClientFilter method testAfterReceiveResponseNormal.

@Test
public void testAfterReceiveResponseNormal(@Mocked Invocation invocation, @Mocked HttpServletResponseEx responseEx, @Mocked Buffer bodyBuffer, @Mocked OperationMeta operationMeta, @Mocked ResponseMeta responseMeta, @Mocked RestOperationMeta swaggerRestOperation, @Mocked ProduceProcessor produceProcessor) throws Exception {
    MultiMap responseHeader = new CaseInsensitiveHeaders();
    responseHeader.add("b", "bValue");
    Object decodedResult = new Object();
    new Expectations() {

        {
            responseEx.getHeader(HttpHeaders.CONTENT_TYPE);
            result = "json";
            responseEx.getHeaderNames();
            result = Arrays.asList("a", "b");
            responseEx.getHeaders("b");
            result = responseHeader.getAll("b");
            swaggerRestOperation.findProduceProcessor("json");
            result = produceProcessor;
            produceProcessor.decodeResponse(bodyBuffer, responseMeta.getJavaType());
            result = decodedResult;
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
            result = swaggerRestOperation;
            responseEx.getStatusType();
            result = Status.OK;
        }
    };
    Response response = filter.afterReceiveResponse(invocation, responseEx);
    Assert.assertSame(decodedResult, response.getResult());
    Assert.assertEquals(1, response.getHeaders().getHeaderMap().size());
    Assert.assertEquals(response.getHeaders().getHeader("b"), Arrays.asList("bValue"));
}
Also used : Expectations(mockit.Expectations) Response(org.apache.servicecomb.swagger.invocation.Response) MultiMap(io.vertx.core.MultiMap) CaseInsensitiveHeaders(io.vertx.core.http.CaseInsensitiveHeaders) Test(org.junit.Test)

Example 22 with Response

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

the class HighwayClient method send.

public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    HighwayClientConnectionPool tcpClientPool = clientMgr.findClientPool(invocation.isSync());
    OperationMeta operationMeta = invocation.getOperationMeta();
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    HighwayClientConnection tcpClient = tcpClientPool.findOrCreateClient(invocation.getEndpoint().getEndpoint());
    HighwayClientPackage clientPackage = new HighwayClientPackage(invocation, operationProtobuf, tcpClient);
    LOGGER.debug("Sending request by highway, qualifiedName={}, endpoint={}.", invocation.getMicroserviceQualifiedName(), invocation.getEndpoint().getEndpoint());
    tcpClient.send(clientPackage, ar -> {
        // 此时是在网络线程中,转换线程
        invocation.getResponseExecutor().execute(() -> {
            if (ar.failed()) {
                // 只会是本地异常
                asyncResp.consumerFail(ar.cause());
                return;
            }
            // 处理应答
            try {
                Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result(), tcpClient.getProtobufFeature());
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.consumerFail(e);
            }
        });
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Example 23 with Response

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

the class SpringmvcProducerResponseMapper method mapResponse.

@SuppressWarnings("unchecked")
@Override
public Response mapResponse(StatusType status, Object response) {
    ResponseEntity<Object> springmvcResponse = (ResponseEntity<Object>) response;
    StatusType responseStatus = new HttpStatus(springmvcResponse.getStatusCode().value(), springmvcResponse.getStatusCode().getReasonPhrase());
    Response cseResponse = null;
    if (HttpStatus.isSuccess(responseStatus)) {
        cseResponse = realMapper.mapResponse(responseStatus, springmvcResponse.getBody());
    } else {
        // not support fail response mapper now
        cseResponse = Response.status(responseStatus).entity(springmvcResponse.getBody());
    }
    HttpHeaders headers = springmvcResponse.getHeaders();
    Headers cseHeaders = cseResponse.getHeaders();
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        if (entry.getValue() == null || entry.getValue().isEmpty()) {
            continue;
        }
        for (String value : entry.getValue()) {
            cseHeaders.addHeader(entry.getKey(), value);
        }
    }
    return cseResponse;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.apache.servicecomb.swagger.invocation.context.HttpStatus) StatusType(javax.ws.rs.core.Response.StatusType) HttpHeaders(org.springframework.http.HttpHeaders) Headers(org.apache.servicecomb.swagger.invocation.response.Headers) List(java.util.List)

Example 24 with Response

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

the class TestSpringmvcConsumerResponseMapperFactory method asyncResponseEntity.

@Test
public void asyncResponseEntity() {
    Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "asyncResponseEntityMethod");
    Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
    ConsumerResponseMapper mapper = factorys.createResponseMapper(listMethod.getGenericReturnType(), responseEntityMethod.getGenericReturnType());
    Assert.assertThat(mapper, Matchers.instanceOf(SpringmvcConsumerResponseMapper.class));
    Response response = Response.ok(Arrays.asList("a", "b"));
    @SuppressWarnings("unchecked") ResponseEntity<String[]> responseEntity = (ResponseEntity<String[]>) mapper.mapResponse(response);
    Assert.assertThat(responseEntity.getBody(), Matchers.arrayContaining("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ResponseEntity(org.springframework.http.ResponseEntity) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 25 with Response

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

the class TestSpringmvcProducerResponseMapper method mapResponse_withoutHeader_fail.

@Test
public void mapResponse_withoutHeader_fail() {
    ResponseEntity<String[]> responseEntity = new ResponseEntity<>(arrResult, org.springframework.http.HttpStatus.BAD_REQUEST);
    Response response = mapper.mapResponse(null, responseEntity);
    Assert.assertSame(arrResult, response.getResult());
    Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus().getStatusCode());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ResponseEntity(org.springframework.http.ResponseEntity) 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