Search in sources :

Example 26 with RestOperationMeta

use of org.apache.servicecomb.common.rest.definition.RestOperationMeta in project java-chassis by ServiceComb.

the class CseClientHttpRequest method createRequestMeta.

protected RequestMeta createRequestMeta(String httpMethod, URI uri) {
    String microserviceName = uri.getAuthority();
    MicroserviceReferenceConfig microserviceReferenceConfig = SCBEngine.getInstance().createMicroserviceReferenceConfig(microserviceName);
    MicroserviceMeta microserviceMeta = microserviceReferenceConfig.getLatestMicroserviceMeta();
    ServicePathManager servicePathManager = ServicePathManager.getServicePathManager(microserviceMeta);
    if (servicePathManager == null) {
        throw new Error(String.format("no schema defined for %s:%s", microserviceMeta.getAppId(), microserviceMeta.getMicroserviceName()));
    }
    OperationLocator locator = servicePathManager.consumerLocateOperation(path, httpMethod);
    RestOperationMeta swaggerRestOperation = locator.getOperation();
    OperationMeta operationMeta = locator.getOperation().getOperationMeta();
    ReferenceConfig referenceConfig = microserviceReferenceConfig.createReferenceConfig(operationMeta);
    Map<String, String> pathParams = locator.getPathVarMap();
    return new RequestMeta(referenceConfig, swaggerRestOperation, pathParams);
}
Also used : OperationLocator(org.apache.servicecomb.common.rest.locator.OperationLocator) MicroserviceReferenceConfig(org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) MicroserviceReferenceConfig(org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) ServicePathManager(org.apache.servicecomb.common.rest.locator.ServicePathManager)

Example 27 with RestOperationMeta

use of org.apache.servicecomb.common.rest.definition.RestOperationMeta in project java-chassis by ServiceComb.

the class RestClientTransportContextFactory method createHttpClientRequest.

protected Future<HttpClientRequest> createHttpClientRequest(Invocation invocation) {
    try {
        RestOperationMeta restOperationMeta = RestMetaUtils.getRestOperationMeta(invocation.getOperationMeta());
        HttpClientWithContext httpClientWithContext = findHttpClientPool(invocation);
        URIEndpointObject endpoint = (URIEndpointObject) invocation.getEndpoint().getAddress();
        HttpMethod method = HttpMethod.valueOf(restOperationMeta.getHttpMethod());
        RequestOptions requestOptions = new RequestOptions().setHost(endpoint.getHostOrIp()).setPort(endpoint.getPort()).setSsl(endpoint.isSslEnabled()).setMethod(method).setURI(createRequestPath(invocation, restOperationMeta));
        return httpClientRequestFactory.create(invocation, httpClientWithContext.getHttpClient(), requestOptions);
    } catch (Throwable e) {
        throw new InvocationException(BAD_REQUEST, FAILED_TO_CREATE_REST_CLIENT_TRANSPORT_CONTEXT, e.getMessage(), e);
    }
}
Also used : RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) RequestOptions(io.vertx.core.http.RequestOptions) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) HttpMethod(io.vertx.core.http.HttpMethod)

Example 28 with RestOperationMeta

use of org.apache.servicecomb.common.rest.definition.RestOperationMeta in project java-chassis by ServiceComb.

the class RestClientTransportContextFactory method doCreate.

protected RestClientTransportContext doCreate(Invocation invocation, HttpClientRequest httpClientRequest) throws Throwable {
    RestOperationMeta restOperationMeta = RestMetaUtils.getRestOperationMeta(invocation.getOperationMeta());
    HttpClientWithContext httpClientWithContext = findHttpClientPool(invocation);
    return new RestClientTransportContext(restOperationMeta, httpClientWithContext.context(), httpClientRequest, boundaryFactory);
}
Also used : RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)

Example 29 with RestOperationMeta

use of org.apache.servicecomb.common.rest.definition.RestOperationMeta in project java-chassis by ServiceComb.

the class DefaultHttpClientFilter method extractResponse.

protected Response extractResponse(Invocation invocation, HttpServletResponseEx responseEx) {
    Object result = invocation.getHandlerContext().get(RestConst.READ_STREAM_PART);
    if (result != null) {
        return Response.create(responseEx.getStatusType(), result);
    }
    OperationMeta operationMeta = invocation.getOperationMeta();
    JavaType responseType = invocation.findResponseType(responseEx.getStatus());
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    ProduceProcessor produceProcessor = findProduceProcessor(swaggerRestOperation, responseEx);
    if (produceProcessor == null) {
        // This happens outside the runtime such as Servlet filter response. Here we give a default json parser to it
        // and keep user data not get lose.
        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));
        LOGGER.warn(msg);
        produceProcessor = ProduceProcessorManager.INSTANCE.findDefaultProcessor();
    }
    try {
        result = produceProcessor.decodeResponse(responseEx.getBodyBuffer(), responseType);
        Response response = Response.create(responseEx.getStatusType(), result);
        if (response.isFailed()) {
            LOGGER.warn("invoke operation [{}] failed, status={}, msg={}", invocation.getMicroserviceQualifiedName(), responseEx.getStatusType().getStatusCode(), result == null ? "" : result.toString());
        }
        return response;
    } catch (Exception e) {
        LOGGER.error("failed to decode response body, exception is [{}]", e.getMessage());
        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));
        if (HttpStatus.isSuccess(responseEx.getStatus())) {
            return Response.createConsumerFail(new InvocationException(400, responseEx.getStatusType().getReasonPhrase(), new CommonExceptionData(msg), e));
        }
        return Response.createConsumerFail(new InvocationException(responseEx.getStatus(), responseEx.getStatusType().getReasonPhrase(), new CommonExceptionData(msg), e));
    }
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) JavaType(com.fasterxml.jackson.databind.JavaType) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException)

Example 30 with RestOperationMeta

use of org.apache.servicecomb.common.rest.definition.RestOperationMeta in project incubator-servicecomb-java-chassis by apache.

the class VertxHttpMethod method getMethod.

private HttpMethod getMethod(Invocation invocation) {
    OperationMeta operationMeta = invocation.getOperationMeta();
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    String method = swaggerRestOperation.getHttpMethod();
    return HttpMethod.valueOf(method);
}
Also used : RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta)

Aggregations

RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)55 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)24 Test (org.junit.Test)20 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)10 Response (org.apache.servicecomb.swagger.invocation.Response)9 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)9 InvocationStageTrace (org.apache.servicecomb.core.invocation.InvocationStageTrace)8 HttpClientWithContext (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)7 Invocation (org.apache.servicecomb.core.Invocation)6 URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)6 HttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx)6 HttpClientRequest (io.vertx.core.http.HttpClientRequest)5 HttpMethod (io.vertx.core.http.HttpMethod)5 HashMap (java.util.HashMap)5 RestClientRequestImpl (org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl)5 ProduceProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)5 HttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)5 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)5 HttpClientResponse (io.vertx.core.http.HttpClientResponse)4 RequestOptions (io.vertx.core.http.RequestOptions)4