Search in sources :

Example 6 with RestClientRequestImpl

use of org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl in project java-chassis by ServiceComb.

the class ClientRestArgsFilter method beforeSendRequest.

@Override
@SuppressWarnings("unchecked")
public void beforeSendRequest(Invocation invocation, HttpServletRequestEx requestEx) {
    RestClientRequestImpl restClientRequest = (RestClientRequestImpl) invocation.getHandlerContext().get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT);
    OperationMeta operationMeta = invocation.getOperationMeta();
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    try {
        RestCodec.argsToRest(invocation.getSwaggerArguments(), swaggerRestOperation, restClientRequest);
        requestEx.setBodyBuffer(restClientRequest.getBodyBuffer());
    } catch (Throwable e) {
        throw ExceptionFactory.convertConsumerException(e);
    }
}
Also used : RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) RestClientRequestImpl(org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta)

Example 7 with RestClientRequestImpl

use of org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl in project java-chassis by ServiceComb.

the class RestClientInvocation method invoke.

public void invoke(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    this.invocation = invocation;
    this.asyncResp = asyncResp;
    OperationMeta operationMeta = invocation.getOperationMeta();
    restOperationMeta = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    String path = this.createRequestPath(restOperationMeta);
    IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
    Future<HttpClientRequest> requestFuture = createRequest(ipPort, path);
    invocation.getInvocationStageTrace().startGetConnection();
    requestFuture.compose(clientRequest -> {
        invocation.getInvocationStageTrace().finishGetConnection();
        this.clientRequest = clientRequest;
        clientRequest.putHeader(org.apache.servicecomb.core.Const.TARGET_MICROSERVICE, invocation.getMicroserviceName());
        RestClientRequestImpl restClientRequest = new RestClientRequestImpl(clientRequest, httpClientWithContext.context(), asyncResp, throwableHandler);
        invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_REQUESTCLIENT, restClientRequest);
        Buffer requestBodyBuffer;
        try {
            requestBodyBuffer = restClientRequest.getBodyBuffer();
        } catch (Exception e) {
            return Future.failedFuture(e);
        }
        HttpServletRequestEx requestEx = new VertxClientRequestToHttpServletRequest(clientRequest, requestBodyBuffer);
        invocation.getInvocationStageTrace().startClientFiltersRequest();
        for (HttpClientFilter filter : httpClientFilters) {
            if (filter.enabled()) {
                filter.beforeSendRequest(invocation, requestEx);
            }
        }
        // 从业务线程转移到网络线程中去发送
        invocation.onStartSendRequest();
        httpClientWithContext.runOnContext(httpClient -> {
            clientRequest.setTimeout(operationMeta.getConfig().getMsRequestTimeout());
            clientRequest.response().onComplete(asyncResult -> {
                if (asyncResult.failed()) {
                    fail(asyncResult.cause());
                    return;
                }
                handleResponse(asyncResult.result());
            });
            processServiceCombHeaders(invocation, operationMeta);
            restClientRequest.end().onComplete((t) -> invocation.getInvocationStageTrace().finishWriteToBuffer(System.nanoTime()));
        });
        return Future.succeededFuture();
    }).onFailure(failure -> {
        invocation.getTraceIdLogger().error(LOGGER, "Failed to send request, alreadyFailed:{}, local:{}, remote:{}, message={}.", alreadyFailed, getLocalAddress(), ipPort.getSocketAddress(), ExceptionUtils.getExceptionMessageWithoutTrace(failure));
        throwableHandler.handle(failure);
    });
}
Also used : CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) RestClientRequestImpl(org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl) OperationConfig(org.apache.servicecomb.core.definition.OperationConfig) VertxClientRequestToHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.VertxClientRequestToHttpServletRequest) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) RequestOptions(io.vertx.core.http.RequestOptions) DefinitionConst(org.apache.servicecomb.registry.definition.DefinitionConst) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) StringUtils(org.apache.commons.lang3.StringUtils) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ExceptionUtils(org.apache.servicecomb.foundation.common.utils.ExceptionUtils) JsonUtils(org.apache.servicecomb.foundation.common.utils.JsonUtils) ReadStreamPart(org.apache.servicecomb.foundation.vertx.http.ReadStreamPart) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) RestConst(org.apache.servicecomb.common.rest.RestConst) HttpClientFilter(org.apache.servicecomb.common.rest.filter.HttpClientFilter) Status(javax.ws.rs.core.Response.Status) Response(org.apache.servicecomb.swagger.invocation.Response) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) InvocationStageTrace(org.apache.servicecomb.core.invocation.InvocationStageTrace) HttpStatus(org.apache.servicecomb.foundation.common.http.HttpStatus) Logger(org.slf4j.Logger) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) VertxClientResponseToHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.VertxClientResponseToHttpServletResponse) Const(org.apache.servicecomb.core.Const) Future(io.vertx.core.Future) Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) Handler(io.vertx.core.Handler) Buffer(io.vertx.core.buffer.Buffer) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientFilter(org.apache.servicecomb.common.rest.filter.HttpClientFilter) RestClientRequestImpl(org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl) VertxClientRequestToHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.VertxClientRequestToHttpServletRequest) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) TimeoutException(java.util.concurrent.TimeoutException) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)

Example 8 with RestClientRequestImpl

use of org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl in project java-chassis by ServiceComb.

the class HttpMethodAccessItem method appendClientFormattedItem.

@Override
public void appendClientFormattedItem(InvocationFinishEvent finishEvent, StringBuilder builder) {
    OperationMeta operationMeta = finishEvent.getInvocation().getOperationMeta();
    if (operationMeta != null && !StringUtils.isEmpty(operationMeta.getHttpMethod())) {
        builder.append(operationMeta.getHttpMethod());
        return;
    }
    RestClientRequestImpl restRequestImpl = (RestClientRequestImpl) finishEvent.getInvocation().getHandlerContext().get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT);
    if (null == restRequestImpl || null == restRequestImpl.getRequest() || null == restRequestImpl.getRequest().getMethod()) {
        builder.append(EMPTY_RESULT);
        return;
    }
    builder.append(restRequestImpl.getRequest().getMethod().toString());
}
Also used : RestClientRequestImpl(org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Example 9 with RestClientRequestImpl

use of org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl in project java-chassis by ServiceComb.

the class LocalPortAccessItem method appendClientFormattedItem.

@Override
public void appendClientFormattedItem(InvocationFinishEvent finishEvent, StringBuilder builder) {
    RestClientRequestImpl restRequestImpl = (RestClientRequestImpl) finishEvent.getInvocation().getHandlerContext().get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT);
    if (null == restRequestImpl || null == restRequestImpl.getRequest() || null == restRequestImpl.getRequest().connection() || null == restRequestImpl.getRequest().connection().localAddress()) {
        builder.append(EMPTY_RESULT);
        return;
    }
    builder.append(restRequestImpl.getRequest().connection().localAddress().port());
}
Also used : RestClientRequestImpl(org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl)

Example 10 with RestClientRequestImpl

use of org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl in project java-chassis by ServiceComb.

the class QueryStringAccessItem method appendClientFormattedItem.

@Override
public void appendClientFormattedItem(InvocationFinishEvent finishEvent, StringBuilder builder) {
    RestClientRequestImpl restRequestImpl = (RestClientRequestImpl) finishEvent.getInvocation().getHandlerContext().get(RestConst.INVOCATION_HANDLER_REQUESTCLIENT);
    if (null == restRequestImpl || null == restRequestImpl.getRequest() || StringUtils.isEmpty(restRequestImpl.getRequest().query())) {
        builder.append(EMPTY_RESULT);
        return;
    }
    builder.append(restRequestImpl.getRequest().query());
}
Also used : RestClientRequestImpl(org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl)

Aggregations

RestClientRequestImpl (org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl)11 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)6 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4 Buffer (io.vertx.core.buffer.Buffer)2 HttpClientRequest (io.vertx.core.http.HttpClientRequest)2 HttpClientFilter (org.apache.servicecomb.common.rest.filter.HttpClientFilter)2 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)2 HttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)2 VertxClientRequestToHttpServletRequest (org.apache.servicecomb.foundation.vertx.http.VertxClientRequestToHttpServletRequest)2 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 HttpMethod (io.vertx.core.http.HttpMethod)1 RequestOptions (io.vertx.core.http.RequestOptions)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 Status (javax.ws.rs.core.Response.Status)1 StringUtils (org.apache.commons.lang3.StringUtils)1 RestConst (org.apache.servicecomb.common.rest.RestConst)1 Const (org.apache.servicecomb.core.Const)1