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