Search in sources :

Example 11 with HttpServletRequestEx

use of org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx in project incubator-servicecomb-java-chassis by apache.

the class VertxHttpMethod method doMethod.

public void doMethod(HttpClientWithContext httpClientWithContext, Invocation invocation, AsyncResponse asyncResp) throws Exception {
    OperationMeta operationMeta = invocation.getOperationMeta();
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    String path = this.createRequestPath(invocation, swaggerRestOperation);
    IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
    HttpClientRequest clientRequest = this.createRequest(httpClientWithContext.getHttpClient(), invocation, ipPort, path, asyncResp);
    clientRequest.putHeader(org.apache.servicecomb.core.Const.TARGET_MICROSERVICE, invocation.getMicroserviceName());
    RestClientRequestImpl restClientRequest = new RestClientRequestImpl(clientRequest, httpClientWithContext.context().owner(), asyncResp);
    invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_REQUESTCLIENT, restClientRequest);
    Buffer requestBodyBuffer = restClientRequest.getBodyBuffer();
    HttpServletRequestEx requestEx = new VertxClientRequestToHttpServletRequest(clientRequest, requestBodyBuffer);
    for (HttpClientFilter filter : httpClientFilters) {
        filter.beforeSendRequest(invocation, requestEx);
    }
    clientRequest.exceptionHandler(e -> {
        LOGGER.error(e.toString());
        asyncResp.fail(invocation.getInvocationType(), e);
    });
    // 从业务线程转移到网络线程中去发送
    httpClientWithContext.runOnContext(httpClient -> {
        this.setCseContext(invocation, clientRequest);
        clientRequest.setTimeout(AbstractTransport.getRequestTimeoutProperty().get());
        try {
            restClientRequest.end();
        } catch (Throwable e) {
            LOGGER.error("send http request failed,", e);
            asyncResp.fail(invocation.getInvocationType(), e);
        }
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientFilter(org.apache.servicecomb.common.rest.filter.HttpClientFilter) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) 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) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)

Example 12 with HttpServletRequestEx

use of org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx in project incubator-servicecomb-java-chassis by apache.

the class TracingFilterTest method testAfterReceiveRequestOnInvocationContainsTraceId.

@Test
public void testAfterReceiveRequestOnInvocationContainsTraceId() {
    Invocation invocation = Mockito.mock(Invocation.class);
    String traceId = "traceIdTest";
    HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
    Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(traceId);
    FILTER.afterReceiveRequest(invocation, requestEx);
    Mockito.verify(requestEx, Mockito.times(0)).getHeader(Const.TRACE_ID_NAME);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) Test(org.junit.Test)

Example 13 with HttpServletRequestEx

use of org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx in project incubator-servicecomb-java-chassis by apache.

the class TracingFilterTest method testAfterReceiveRequestOnGenerateTraceId.

@Test
public void testAfterReceiveRequestOnGenerateTraceId() {
    Invocation invocation = Mockito.mock(Invocation.class);
    HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
    Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(null);
    Mockito.when(requestEx.getHeader(Const.TRACE_ID_NAME)).thenReturn(null);
    FILTER.afterReceiveRequest(invocation, requestEx);
    Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME, TestTracingFilter.TRACE_ID);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) Test(org.junit.Test)

Aggregations

HttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)13 Test (org.junit.Test)9 HashMap (java.util.HashMap)5 Expectations (mockit.Expectations)4 Invocation (org.apache.servicecomb.core.Invocation)3 HttpHeaders (org.springframework.http.HttpHeaders)3 List (java.util.List)2 HttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx)2 StandardHttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx)2 Buffer (io.vertx.core.buffer.Buffer)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 PrintWriter (java.io.PrintWriter)1 AsyncContext (javax.servlet.AsyncContext)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MockUp (mockit.MockUp)1 RestProducerInvocation (org.apache.servicecomb.common.rest.RestProducerInvocation)1 RestClientRequestImpl (org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl)1 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)1