Search in sources :

Example 1 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter 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 HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project incubator-servicecomb-java-chassis by apache.

the class AbstractRestInvocation method sendResponse.

@SuppressWarnings("deprecation")
protected void sendResponse(Response response) throws Exception {
    if (response.getHeaders().getHeaderMap() != null) {
        for (Entry<String, List<Object>> entry : response.getHeaders().getHeaderMap().entrySet()) {
            for (Object value : entry.getValue()) {
                if (!entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
                    responseEx.addHeader(entry.getKey(), String.valueOf(value));
                }
            }
        }
    }
    responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase());
    responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");
    responseEx.setAttribute(RestConst.INVOCATION_HANDLER_RESPONSE, response);
    responseEx.setAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR, produceProcessor);
    for (HttpServerFilter filter : httpServerFilters) {
        filter.beforeSendResponse(invocation, responseEx);
    }
    responseEx.flushBuffer();
}
Also used : HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) List(java.util.List)

Example 3 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project java-chassis by ServiceComb.

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 4 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method testDoSendResponseResultOKFilter.

@Test
public void testDoSendResponseResultOKFilter(@Mocked Response response) {
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.set("Content-Type", "application/json");
    new Expectations() {

        {
            response.getHeaders();
            result = headers;
            response.getStatusCode();
            result = 123;
            response.getReasonPhrase();
            result = "reason";
            response.getResult();
            result = "ok";
        }
    };
    Buffer buffer = Buffer.buffer();
    responseEx = new MockUp<HttpServletResponseEx>() {

        private Map<String, Object> attributes = new HashMap<>();

        @Mock
        public void setAttribute(String key, Object value) {
            this.attributes.put(key, value);
        }

        @Mock
        public Object getAttribute(String key) {
            return this.attributes.get(key);
        }

        @Mock
        void setBodyBuffer(Buffer bodyBuffer) {
            buffer.appendBuffer(bodyBuffer);
        }
    }.getMockInstance();
    HttpServerFilter filter = new HttpServerFilterBaseForTest() {

        @Override
        public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
            buffer.appendString("-filter");
        }
    };
    invocation.onStart(0);
    initRestInvocation();
    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.loadSortedService(HttpServerFilter.class);
    httpServerFilters.add(filter);
    restInvocation.setHttpServerFilters(httpServerFilters);
    restInvocation.sendResponse(response);
    assertEquals("\"ok\"-filter", buffer.toString());
}
Also used : Expectations(mockit.Expectations) Buffer(io.vertx.core.buffer.Buffer) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) MultiMap(io.vertx.core.MultiMap) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 5 with HttpServerFilter

use of org.apache.servicecomb.common.rest.filter.HttpServerFilter in project java-chassis by ServiceComb.

the class AbstractRestInvocation method prepareInvoke.

protected Response prepareInvoke() throws Throwable {
    this.initProduceProcessor();
    invocation.getHandlerContext().put(RestConst.REST_REQUEST, requestEx);
    invocation.getInvocationStageTrace().startServerFiltersRequest();
    for (HttpServerFilter filter : httpServerFilters) {
        if (filter.enabled()) {
            Response response = filter.afterReceiveRequest(invocation, requestEx);
            if (response != null) {
                return response;
            }
        }
    }
    return null;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter)

Aggregations

HttpServerFilter (org.apache.servicecomb.common.rest.filter.HttpServerFilter)9 MockUp (mockit.MockUp)5 Expectations (mockit.Expectations)4 Invocation (org.apache.servicecomb.core.Invocation)4 Test (org.junit.Test)4 Buffer (io.vertx.core.buffer.Buffer)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx)2 HttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx)2 StandardHttpServletRequestEx (org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx)2 StandardHttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 Before (org.junit.Before)2 MultiMap (io.vertx.core.MultiMap)1 List (java.util.List)1 Mock (mockit.Mock)1 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)1 TestPathSchema (org.apache.servicecomb.common.rest.locator.TestPathSchema)1 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)1