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"));
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations