Search in sources :

Example 1 with ResponseMeta

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

the class DefaultHttpClientFilter method afterReceiveResponse.

@Override
public Response afterReceiveResponse(Invocation invocation, HttpServletResponseEx responseEx) {
    OperationMeta operationMeta = invocation.getOperationMeta();
    ResponseMeta responseMeta = operationMeta.findResponseMeta(responseEx.getStatus());
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    ProduceProcessor produceProcessor = findProduceProcessor(swaggerRestOperation, responseEx);
    if (produceProcessor == null) {
        String msg = String.format("method %s, path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", swaggerRestOperation.getHttpMethod(), swaggerRestOperation.getAbsolutePath(), responseEx.getStatus(), responseEx.getStatusType().getReasonPhrase(), responseEx.getHeader(HttpHeaders.CONTENT_TYPE));
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        return Response.consumerFailResp(exception);
    }
    Object result = null;
    try {
        result = produceProcessor.decodeResponse(responseEx.getBodyBuffer(), responseMeta.getJavaType());
    } catch (Exception e) {
        return Response.consumerFailResp(e);
    }
    Response response = Response.create(responseEx.getStatusType(), result);
    for (String headerName : responseEx.getHeaderNames()) {
        Collection<String> headerValues = responseEx.getHeaders(headerName);
        for (String headerValue : headerValues) {
            response.getHeaders().addHeader(headerName, headerValue);
        }
    }
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) ResponseMeta(org.apache.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta)

Example 2 with ResponseMeta

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

the class TestOperationMeta method testOperationMeta.

@Test
public void testOperationMeta() {
    UnitTestMeta meta = new UnitTestMeta();
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(Impl.class);
    OperationMeta operationMeta = schemaMeta.findOperation("test");
    Assert.assertEquals("POST", operationMeta.getHttpMethod());
    Assert.assertEquals("/test", operationMeta.getOperationPath());
    Assert.assertEquals(schemaMeta, operationMeta.getSchemaMeta());
    Assert.assertEquals(Impl.class.getName() + ".test", operationMeta.getSchemaQualifiedName());
    Assert.assertEquals("app:test." + Impl.class.getName() + ".test", operationMeta.getMicroserviceQualifiedName());
    Assert.assertEquals("app:test", operationMeta.getMicroserviceName());
    Assert.assertEquals("test", operationMeta.getOperationId());
    Assert.assertEquals("x", operationMeta.getParamName(0));
    operationMeta.putExtData("ext", 1);
    Assert.assertEquals(1, (int) operationMeta.getExtData("ext"));
    ResponseMeta responseMeta = operationMeta.findResponseMeta(200);
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getJavaType().getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h1").getGenericSignature());
    Assert.assertEquals("Ljava/util/List<Ljava/lang/String;>;", responseMeta.getHeaders().get("h2").getGenericSignature());
    Assert.assertEquals(null, responseMeta.getHeaders().get("h3"));
    responseMeta = operationMeta.findResponseMeta(300);
    Assert.assertEquals("Ljava/lang/String;", responseMeta.getJavaType().getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h1").getGenericSignature());
    Assert.assertEquals("Ljava/util/List<Ljava/lang/String;>;", responseMeta.getHeaders().get("h2").getGenericSignature());
    Assert.assertEquals("Ljava/lang/Integer;", responseMeta.getHeaders().get("h3").getGenericSignature());
}
Also used : UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) ResponseMeta(org.apache.servicecomb.swagger.invocation.response.ResponseMeta) Test(org.junit.Test)

Aggregations

ResponseMeta (org.apache.servicecomb.swagger.invocation.response.ResponseMeta)2 ProduceProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)1 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 UnitTestMeta (org.apache.servicecomb.core.unittest.UnitTestMeta)1 Response (org.apache.servicecomb.swagger.invocation.Response)1 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)1 Test (org.junit.Test)1