Search in sources :

Example 1 with HttpServletRequestEx

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

the class TestRestServletProducerInvocation method findRestOperationCacheTrue.

@Test
public void findRestOperationCacheTrue(@Mocked HttpServletRequest request, @Mocked HttpServerFilter f1) {
    HttpServletRequestEx requestEx = new StandardHttpServletRequestEx(request);
    Deencapsulation.setField(restInvocation, "requestEx", requestEx);
    new MockUp<RestProducerInvocation>() {

        @Mock
        void findRestOperation() {
            Deencapsulation.setField(getMockInstance(), "restOperationMeta", restOperationMeta);
        }
    };
    List<HttpServerFilter> httpServerFilters = Arrays.asList(f1);
    new Expectations() {

        {
            f1.needCacheRequest(operationMeta);
            result = true;
        }
    };
    restInvocation.setHttpServerFilters(httpServerFilters);
    restInvocation.findRestOperation();
    Assert.assertTrue(Deencapsulation.getField(requestEx, "cacheRequest"));
}
Also used : Expectations(mockit.Expectations) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) MockUp(mockit.MockUp) StandardHttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) StandardHttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx) Test(org.junit.Test)

Example 2 with HttpServletRequestEx

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

the class VertxRestDispatcher method onRequest.

private void onRequest(RoutingContext context) {
    if (transport == null) {
        transport = CseContext.getInstance().getTransportManager().findTransport(Const.RESTFUL);
    }
    HttpServletRequestEx requestEx = new VertxServerRequestToHttpServletRequest(context);
    HttpServletResponseEx responseEx = new VertxServerResponseToHttpServletResponse(context.response());
    RestProducerInvocation restProducerInvocation = new RestProducerInvocation();
    context.put(RestConst.REST_PRODUCER_INVOCATION, restProducerInvocation);
    restProducerInvocation.invoke(transport, requestEx, responseEx, httpServerFilters);
}
Also used : VertxServerResponseToHttpServletResponse(org.apache.servicecomb.foundation.vertx.http.VertxServerResponseToHttpServletResponse) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) RestProducerInvocation(org.apache.servicecomb.common.rest.RestProducerInvocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) VertxServerRequestToHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.VertxServerRequestToHttpServletRequest)

Example 3 with HttpServletRequestEx

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

the class TracingFilterTest method testAfterReceiveRequestOnHeaderContainsTraceId.

@Test
public void testAfterReceiveRequestOnHeaderContainsTraceId() {
    Invocation invocation = Mockito.mock(Invocation.class);
    String traceId = "traceIdTest";
    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(traceId);
    FILTER.afterReceiveRequest(invocation, requestEx);
    Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME, traceId);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) Test(org.junit.Test)

Example 4 with HttpServletRequestEx

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

the class RestAsyncListener method onTimeout.

@Override
public void onTimeout(AsyncEvent event) throws IOException {
    // in this time, maybe:
    // 1.invocation in executor's queue
    // 2.already executing in executor
    // 3.already send response
    // to avoid concurrent, must lock request
    ServletRequest request = event.getSuppliedRequest();
    HttpServletRequestEx requestEx = (HttpServletRequestEx) request.getAttribute(RestConst.REST_REQUEST);
    synchronized (requestEx) {
        ServletResponse response = event.getAsyncContext().getResponse();
        if (!response.isCommitted()) {
            LOGGER.error("Rest request timeout, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());
            // invocation in executor's queue
            response.setContentType(MediaType.APPLICATION_JSON);
            // we don't know if developers declared one statusCode in contract
            // so we use cse inner statusCode here
            ((HttpServletResponse) response).setStatus(ExceptionFactory.PRODUCER_INNER_STATUS_CODE);
            PrintWriter out = response.getWriter();
            out.write(TIMEOUT_MESSAGE);
            response.flushBuffer();
        }
        request.removeAttribute(RestConst.REST_REQUEST);
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) PrintWriter(java.io.PrintWriter)

Example 5 with HttpServletRequestEx

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

the class ServletRestDispatcher method service.

public void service(HttpServletRequest request, HttpServletResponse response) {
    if (transport == null) {
        transport = CseContext.getInstance().getTransportManager().findTransport(Const.RESTFUL);
    }
    // 异步场景
    final AsyncContext asyncCtx = request.startAsync();
    asyncCtx.addListener(restAsyncListener);
    asyncCtx.setTimeout(ServletConfig.getServerTimeout());
    HttpServletRequestEx requestEx = new StandardHttpServletRequestEx(request);
    HttpServletResponseEx responseEx = new StandardHttpServletResponseEx(response);
    RestServletProducerInvocation restProducerInvocation = new RestServletProducerInvocation();
    restProducerInvocation.invoke(transport, requestEx, responseEx, httpServerFilters);
}
Also used : HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) AsyncContext(javax.servlet.AsyncContext) StandardHttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) StandardHttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx)

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