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"));
}
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);
}
});
});
}
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;
}
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"));
}
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());
}
Aggregations